Skip to content

Commit

Permalink
Merge pull request #303 from TG9541/bits
Browse files Browse the repository at this point in the history
Bits
  • Loading branch information
TG9541 committed May 10, 2020
2 parents d17684d + 7eaa2c2 commit e9736c2
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 121 deletions.
2 changes: 1 addition & 1 deletion DCDC/globconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
HAS_ADC = 1 ; Analog input words

HAS_BACKGROUND = 1 ; Background Forth task (TIM2 ticker)
BG_TIM2_REL = 0x08FC ; 1ms ticker for 7S-LED MPX
BG_TIM_REL = 0x08FC ; 1ms ticker for 7S-LED MPX
BG_RUNMASK = 0 ; BG task runs if "(BG_RUNMASK AND TICKCNT) equals 0"
HAS_CPNVM = 1 ; Can compile to Flash, always interpret to RAM
HAS_DOES = 1 ; CREATE-DOES> extension
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
E4THCOM=e4thcom-0.6.3
E4THCOM=e4thcom
TERM_PORT=ttyUSB0
TERM_BAUD=9600
TERM_FLAGS=
TERM_FLAGS="-p mcu:target:lib"

ifeq ($(BOARD),)

Expand Down Expand Up @@ -50,6 +50,9 @@ defaults:
defaults105:
stm8flash -c stlinkv2 -p stm8s105k4 -s opt -w tools/stm8s105FactoryDefaults.bin

term:
$(E4THCOM) -t stm8ef -p .:lib $(TERM_FLAGS) -d $(TERM_PORT) -b B$(TERM_BAUD)

else
include forth.mk
endif
Expand Down
53 changes: 24 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Various STM8 Discovery boards and [breakout boards](https://github.com/TG9541/st

TG9541/STM8EF provides board support for several common "Chinese gadgets" like the following:

* [MINDEV](https://github.com/TG9541/stm8ef/wiki/Breakout-Boards) for the STM8S103F3P6 $0.65 "minimum development board"
* [C0135](https://github.com/TG9541/stm8ef/wiki/Board-C0135) "Relay-4 Board" - it can be used as a *Nano PLC*
* [W1209](https://github.com/TG9541/stm8ef/wiki/Board-W1209) $1.50 thermostat board w/ 3 digit 7S-LED display, full- or half-duplex RS232
* [W1219](https://github.com/TG9541/stm8ef/wiki/Board-W1219) low cost thermostat with 2x3 digit 7S-LED display, half-duplex RS232 through PD1/SWIM
* [W1401](https://github.com/TG9541/stm8ef/wiki/Board-W1401) (also XH-W1401) thermostat with 3x2 digit 7S-LED display, half-duplex RS232 through SWIM
* [MINDEV](https://github.com/TG9541/stm8ef/wiki/Breakout-Boards) for the STM8S103F3P6 $0.80 "minimum development board"
* [C0135](https://github.com/TG9541/stm8ef/wiki/Board-C0135) the "Relay-4 Board" can be used as a *Nano PLC* (Forth MODBUS support is available)
* [W1209](https://github.com/TG9541/stm8ef/wiki/Board-W1209) $1.50 thermostat board w/ 3 digit 7S-LED display, full- or half-duplex RS232 (some board variants, e.g. with CA LED displays, are supported)
* [W1219](https://github.com/TG9541/stm8ef/wiki/Board-W1219) low cost thermostat with 2x3 digit 7S-LED display with half-duplex RS232 through PD1/SWIM
* [W1401](https://github.com/TG9541/stm8ef/wiki/Board-W1401) (also XH-W1401) thermostat with 3x2 digit 7S-LED display with half-duplex RS232 through shared PD1/SWIM
* [DCDC](https://github.com/TG9541/stm8ef/wiki/Board-CN2596) hacked DCDC converter with voltmeter
* [XH-M194](https://github.com/TG9541/stm8ef/wiki/Board-XH-M194) Timer board with 6 relays, RTC with clock display, and 6 keys
* [XH-M194](https://github.com/TG9541/stm8ef/wiki/Board-XH-M194) Timer board with STM8S105K4T6C, 6 relays, RTC with clock display, 6 keys with half-duplex RS232 through PD1/SWIM
* [XY-PWM](https://github.com/TG9541/stm8ef/wiki/XY-PWM) PWM board w/ 3 digit 7S-LED display, 3 keys, dual PWM and full-duplex RS232
* [XY-LPWM](https://github.com/TG9541/stm8ef/wiki/Board-XY-LPWM) PWM board w/ 2x4 digit 7S-LCD display, 4 keys, PWM and full-duplex RS232

Expand All @@ -62,9 +62,9 @@ From STM8 eForth 2.2.24 on, the binary release contains all the files that are n

In addition to the initial code STM8 eForth offers many features:

* Subroutine Threaded Code (STC) with improved code density
* Subroutine Threaded Code (STC) with improved code density that rivals DTC
* native `BRANCH` (JP), and `EXIT` (RET)
* relative CALL where possible (2 instead of 3 bytes)
* relative CALL when possible (2 instead of 3 bytes)
* TRAP as pseudo-opcode for literals (3 instead of 5 bytes)
* [ALIAS words](https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Alias-Words) for indirect dictionary entries ([even in EEPROM!](https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Alias-Words#dictionary-with-alias-words-in-the-eeprom))
* Forth - machine-code interface using STM8 registers
Expand All @@ -81,51 +81,46 @@ In addition to the initial code STM8 eForth offers many features:
* [on supported boards](https://github.com/TG9541/stm8ef/wiki/eForth-Background-Task) `?KEY` reads board keys, `EMIT` uses board display
* robust context switch with "clean stack" approach
* cooperative multitasking with `'IDLE`
* [idle task](https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Idle-Task) execution while there is no console input
* very fast idle loop (< 10µs)
* [idle task](https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Idle-Task) execution while there is no console input with < 10µs cycle time
* code in the `'IDLE` task can use the interpreter with `EVALUATE`
* configuration options for serial console or dual serial interface
* UART: `?RX` and `TX!`
* any GPIO or pair of GPIOs from ports PA through PD can be used as a simulated COM port
* UART: `?RX` and `TX!` full-duplex w/ half-duplex option on STM8 Low Density devices
* GPIO w/ Port edge & Timer4 interrupts: `?RXP .. TXP!`
* half-duplex "bus style" communication using simulated COM port or UART
* any GPIO or pair of GPIOs from ports PA through PD can be used as a simulated COM port
* option for `TX! .. ?RX` on simulated COM port, and `?RXP .. TXP!` on UART
* configurable vocabulary subsets for binary size optimization
* configuration possible down to the level of single words
* export of `ALIAS` definitions for unlinked words
* board dependent configuration possible down to the level of single words
* export of `ALIAS` definitions for any unlinked words
* Extended vocabulary:
* `CREATE ... DOES>` for defining *defining words*
* Vectored I/O: `'KEY?` and `'EMIT`
* Loop structure words: `DO .. LEAVE .. LOOP`, `+LOOP`
* `CONSTANT` (yes, that was missing in the original code)
* `'KEY?` and `'EMIT` for I/O redirection (that was missing, too)
* `CREATE ... DOES>` for *defining words* (few eForth variants have it)
* `DO .. LEAVE .. LOOP`, `+LOOP` for better compatibility with generic Forth
* STM8S ADC control: `ADC!`, `ADC@`
* board keys, outputs, LEDs: `BKEY`, `KEYB?`, `EMIT7S`, `OUT`, `OUT!`
* EEPROM, FLASH lock/unlock: `LOCK`, `ULOCK`, `LOCKF`, `ULOCKF`
* native bit set/reset: `B!` (b a u -- ), `[ .. ]B!` (and more)
* native 16bit STM8 timer register access: `2C@`, `2C!`
* compile to Flash memory: `NVR`, `RAM`, `WIPE`, `RESET`
* compile to Flash memory: `NVR`, `RAM`, `WIPE`, `RESET` and `PERSIST`
* autostart applications: `'BOOT`
* `EVALUATE` for interpreting text strings (even in the idle task!)
* many words that were missing in eForth compared to Forth systems popular in the 1980s
* board support for [STM8S based very-low-cost boards][WG1]:
* STM8S103F3P6 "$0.80" breakout board
* Termostats, e.g. W1209, W1219, or W1401
* Low cost power supply boards, e.g. XH-M188, DCDC w/ voltmeter
* C0135 Relay Board
* configuration folders for easy application to new boards
* `EVALUATE` can run the Forth interpreter on text strings (even compilation is possible!)
* many words from Forth systems that were popular in the 1980s are provided in the library

## Other changes to the original STM8EF code:

* "ASxxxx V2.0" syntax (the free [SDCC tool chain](http://sdcc.sourceforge.net/) allows mixing Forth, assembly, and C)
* hard STM8S105C6 dependencies removed (e.g. initialization, clock, RAM layout, UART2)
* hard STM8S105C6 dependencies were removed (e.g. initialization, clock, RAM layout, UART2)
* flexible RAM layout, basic RAM memory management, meaningful symbols for RAM locations
* conditional code for different target boards with a subdirectory based configuration framework
* original code bugs fixed (e.g. `COMPILE`, `DEPTH`, `R!`, `PICK`)
* significant binary size reduction
* many more

# Disclaimer, copyright

This is a hobby project! Don't use the code if support or correctness are required.

There is a policy to keep the code free from 3rd party licenses but it can't be guaranteed that no additional licenses apply which, in the worst case, may require derived work to be made public! Please refer to LICENSE.md for details.
The license is MIT. Please refer to LICENSE.md for details.

[WG1]: https://github.com/TG9541/stm8ef/wiki/STM8S-Value-Line-Gadgets

1 change: 1 addition & 0 deletions STM8S207RB/globconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
; Clock: HSI (no crystal)

HALF_DUPLEX = 0 ; Use UART in half duplex mode
USE_UART2 = 0 ; Use the 2nd UART for the console (STM8S207: optional)
HAS_TXUART = 1 ; No UART TXD, word TX!
HAS_RXUART = 1 ; No UART RXD, word ?RX
HAS_TXSIM = 0 ; Enable TxD via GPIO/TIM4, word TXGP!
Expand Down
2 changes: 1 addition & 1 deletion W1219/globconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
HAS_ADC = 1 ; Analog input words

HAS_BACKGROUND = 1 ; Background Forth task (TIM2 ticker)
BG_TIM2_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_TIM_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_RUNMASK = 3 ; BG task runs "in one out of four" cycles

HAS_CPNVM = 1 ; Can compile to Flash, always interpret to RAM
Expand Down
2 changes: 1 addition & 1 deletion W1401/globconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
HAS_ADC = 1 ; Analog input words

HAS_BACKGROUND = 1 ; Background Forth task (TIM2 ticker)
BG_TIM2_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_TIM_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_RUNMASK = 3 ; BG task runs "in one out of four" cycles

HAS_CPNVM = 1 ; Can compile to Flash, always interpret to RAM
Expand Down
2 changes: 1 addition & 1 deletion XH-M194/globconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
HAS_ADC = 0 ; Analog input words

HAS_BACKGROUND = 1 ; Background Forth task (TIM2 ticker)
BG_TIM2_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_TIM_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_RUNMASK = 3 ; BG task runs "in one out of four" cycles

HAS_CPNVM = 1 ; Can compile to Flash, always interpret to RAM
Expand Down
2 changes: 1 addition & 1 deletion XY-PWM/globconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
HAS_ADC = 0 ; no, Analog input words

HAS_BACKGROUND = 1 ; Background Forth task (TIM2 ticker)
BG_TIM2_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_TIM_REL = 0x09BA ; 5ms/4 = 1.25ms ticker for 7S-LED MPX
BG_RUNMASK = 3 ; BG task runs "in one out of four" cycles

HAS_CPNVM = 1 ; Can compile to Flash, always interpret to RAM
Expand Down
26 changes: 16 additions & 10 deletions forth.asm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
.globl _EXTI2_IRQHandler
.globl _EXTI3_IRQHandler
.globl _EXTI4_IRQHandler
.globl _TIM1_UO_IRQHandler
.globl _TIM2_UO_IRQHandler
.globl _TIM4_IRQHandler
.globl _forth
Expand Down Expand Up @@ -374,10 +375,11 @@ _TIM4_IRQHandler:

; ==============================================

; TIM2 interrupt handler for background task
; TIM1 or TIM2 interrupt handler for background task
_TIM1_UO_IRQHandler:
_TIM2_UO_IRQHandler:
.ifne (HAS_LED7SEG + HAS_BACKGROUND)
BRES TIM2_SR1,#0 ; clear TIM2 UIF
BRES BG_TIM_SR1,#0 ; clear TIM1 or TIM2 UIF

.ifne HAS_LED7SEG
CALL LED_MPX ; board dependent code for 7Seg-LED-Displays
Expand Down Expand Up @@ -524,12 +526,17 @@ COLD:

.ifne HAS_BACKGROUND
; init BG timer interrupt
.ifne BG_USE_TIM1
MOV ITC_SPR3,#0x3F ; Interrupt prio. low for TIM1 (Int11)
MOV TIM1_PSCRL,#7 ; prescaler 1/(7+1) = 1/8
.else
MOV ITC_SPR4,#0xF7 ; Interrupt prio. low for TIM2 (Int13)
MOV TIM2_PSCR,#0x03 ; prescaler 1/8
MOV TIM2_ARRH,#(BG_TIM2_REL/256) ; reload H
MOV TIM2_ARRL,#(BG_TIM2_REL%256) ; L
MOV TIM2_CR1,#0x01 ; enable TIM2
MOV TIM2_IER,#0x01 ; enable TIM2 interrupt
MOV TIM2_PSCR,#0x03 ; prescaler 1/(2^3) = 1/8
.endif
MOV BG_TIM_ARRH,#(BG_TIM_REL/256) ; reload H
MOV BG_TIM_ARRL,#(BG_TIM_REL%256) ; L
MOV BG_TIM_CR1,#0x01 ; enable background timer
MOV BG_TIM_IER,#0x01 ; enable background timer interrupt
.endif

.ifne HAS_RXUART+HAS_TXUART
Expand Down Expand Up @@ -1051,9 +1058,8 @@ DROP:

HEADER DDROP "2DROP"
DDROP:
INCW X
INCW X
JRA DROP
ADDW X,#4
RET

; DUP ( w -- w w ) ( TOS STM8: -- Y,Z,N )
; Duplicate top stack item.
Expand Down
13 changes: 8 additions & 5 deletions inc/defconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

TERM_LINUX = 1 ; LF terminates line

HALF_DUPLEX = 0 ; Use UART in half duplex mode (1: PD5, 2: PA3)
HALF_DUPLEX = 0 ; Use the STM8S Low Density UART in half duplex mode (1: PD5, 2: PA3)
USE_UART2 = 0 ; Use the 2nd UART for the console (STM8S207: optional, STM8S105: default)
HAS_RXUART = 1 ; Enable UART RXD, word ?RX
HAS_TXUART = 1 ; Enable UART TXD, word TX!
HAS_RXSIM = 0 ; Enable RxD via GPIO/TIM4, word ?RXGP
Expand All @@ -17,8 +18,8 @@
PNTX = 1 ; Port GPIO# for HAS_TXDSIM
SERPRIOTIM = 0 ; RXSIM priority in BG ticks

EMIT_BG = DROP ; TODO: vectored NUL background EMIT vector
QKEY_BG = ZERO ; TODO: NUL background QKEY vector
EMIT_BG = DROP ; vectored NUL background EMIT vector
QKEY_BG = ZERO ; NUL background QKEY vector

HAS_LED7SEG = 0 ; 7-seg LED display, number of groups (0: none)
LEN_7SGROUP = 3 ; default: 3 dig. 7-seg LED
Expand All @@ -28,9 +29,11 @@
HAS_INPUTS = 0 ; Board digital inputs
HAS_ADC = 0 ; Board analog inputs

HAS_BACKGROUND = 0 ; Background Forth task (TIM2 ticker)
BG_TIM2_REL = 0x26DE ; Reload value for TIM2 background ticker (default 0x26DE @ 5ms HSE)
HAS_BACKGROUND = 0 ; Background Forth task (TIM1 or TIM2 ticker)
BG_USE_TIM1 = 0 ; Use TIM1 for the Background Task (instead of TIM2)
BG_TIM_REL = 0x26DE ; Reload value for background ticker timer (default 0x26DE @ 5ms HSE)
BG_RUNMASK = 0 ; BG task runs if "(BG_RUNMASK AND TICKCNT) equals 0"

HAS_CPNVM = 0 ; Can compile to Flash, always interpret to RAM
HAS_DOES = 0 ; DOES> extension
HAS_DOLOOP = 0 ; DO .. LOOP extension: DO LEAVE LOOP +LOOP
Expand Down
35 changes: 19 additions & 16 deletions inc/stm8device.inc
Original file line number Diff line number Diff line change
Expand Up @@ -249,24 +249,13 @@
UART2_GTR = 0x524A ; UART2 guard time register (0x00)
UART2_PSCR = 0x524B ; UART2 prescaler register (0x00)

UART3_SR = 0x5240 ; UART3 status register (0xC0)
UART3_DR = 0x5241 ; UART3 data register (0xXX)
UART3_BRR1 = 0x5242 ; UART3 baud rate register 1 (0x00)
UART3_BRR2 = 0x5243 ; UART3 baud rate register 2 (0x00)
UART3_CR1 = 0x5244 ; UART3 control register 1 (0x00)
UART3_CR2 = 0x5245 ; UART3 control register 2 (0x00)
UART3_CR3 = 0x5246 ; UART3 control register 3 (0x00)
UART3_CR4 = 0x5247 ; UART3 control register 4 (0x00)
; 0x5248
UART3_CR6 = 0x5249 ; UART3 control register 6 (0x00)

.ifeq (TARGET - STM8S105K4)
UART_DR = UART2_DR
.ifeq (TARGET - STM8S105K4) * (USE_UART2 - 1)
UART_DR = UART2_DR ; STM8S105 or STM8S207
UART_SR = UART2_SR
UART_BRR1 = UART2_BRR1
UART_CR2 = UART2_CR2
.else
UART_DR = UART1_DR ; STM8S103 and STM8S207
UART_DR = UART1_DR ; STM8S103 or STM8S207
UART_SR = UART1_SR
UART_BRR1 = UART1_BRR1
UART_CR2 = UART1_CR2
Expand Down Expand Up @@ -309,7 +298,6 @@
TIM1_OISR = 0x526F ; TIM1 output idle state register (0x00)

; ***** TIM2
;.ifeq HAS_BACKGROUND

TIM2_CR1 = 0x5300 ; TIM2 control register 1 (0x00)
.ifne (TARGET - STM8S103F3)
Expand Down Expand Up @@ -355,7 +343,22 @@
TIM2_CCR3H = 0x5315 ; TIM2 capture/compare register 3 high (0x00)
TIM2_CCR3L = 0x5316 ; TIM2 capture/compare register 3 low (0x00)
.endif
;.endif

; define symbols for the background task timer
.ifne BG_USE_TIM1
BG_TIM_CR1 = TIM1_CR1
BG_TIM_IER = TIM1_IER
BG_TIM_SR1 = TIM1_SR1
BG_TIM_ARRH = TIM1_ARRH
BG_TIM_ARRL = TIM1_ARRL
.else
BG_TIM_CR1 = TIM2_CR1
BG_TIM_IER = TIM2_IER
BG_TIM_SR1 = TIM2_SR1
BG_TIM_ARRH = TIM2_ARRH
BG_TIM_ARRL = TIM2_ARRL
.endif


; ***** TIM3 (High Density)

Expand Down
12 changes: 12 additions & 0 deletions inc/stm8ldevice.inc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,19 @@
TIM2_CCR2L = 0x5264 ; TIM2 capture/compare register 2 low 0x00
TIM2_BKR = 0x5265 ; TIM2 break register 0x00
TIM2_OISR = 0x5266 ; TIM2 output idle state register 0x00

.ifne BG_USE_TIM1
error: BG_USE_TIM1 not supported
.else
BG_TIM_CR1 = TIM2_CR1
BG_TIM_IER = TIM2_IER
BG_TIM_SR1 = TIM2_SR1
BG_TIM_ARRH = TIM2_ARRH
BG_TIM_ARRL = TIM2_ARRL
.endif

; 0x00 5267 to 0x00 527F Reserved area (25 bytes)

; TIM3
TIM3_CR1 = 0x5280 ; TIM3 control register 1 0x00
TIM3_CR2 = 0x5281 ; TIM3 control register 2 0x00
Expand Down
9 changes: 8 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// The following declarations are just to provide a placeholder
// so that mixing-in C code with own memory gets easier

volatile __at(0x40) uint8_t forthData[0x03FF-0x40];
// When creating mixed C/Forth applications with Medium Density or
// High Density devices don't forget to adjust this address range!
volatile __at(0x30) uint8_t forthData[0x03FF-0x30];

// declare trap handler
void TRAP_Handler() __trap;
Expand All @@ -25,6 +27,11 @@ void EXTI3_IRQHandler() __interrupt (INTVEC_EXTI3);
// declare interrupt handler for Port E external interrupts
void EXTI4_IRQHandler() __interrupt (INTVEC_EXTI4);

#ifdef INTVEC_TIM1_UPDATE
// declare interrupt handler for TIM1 update overflow
void TIM1_UO_IRQHandler() __interrupt (INTVEC_TIM1_UPDATE);
#endif

// declare interrupt handler for TIM2 update overflow
void TIM2_UO_IRQHandler() __interrupt (INTVEC_TIM2_UPDATE);

Expand Down
Loading

0 comments on commit e9736c2

Please sign in to comment.