-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathINTRX
92 lines (72 loc) · 1.78 KB
/
INTRX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
\ STM8 eForth buffered UART receive
\ refer to github.com/TG9541/stm8ef/blob/master/LICENSE.md
\ The code assumes target CPU resource definitions and that
\ RX buffer length are defined prior to loading!
\ More in the example at the end of the file
#require WIPE
#require :NVM
#require ALIAS
#require ]B!
#require '?KEY
5 CONSTANT #RIEN
NVM
VARIABLE rxbuf RBLEN 2- ALLOT
VARIABLE rxp \ UART RX ISR buffer write pointer
VARIABLE rrp \ ?RXB buffer read pointer
\ increment buffer pointer w/ wrap around
:NVM ( a -- )
DUP @ 1+ ( a ab1 ) [ rxbuf RBLEN + 1- ] LITERAL OVER < IF
( a n1 ) DROP rxbuf ( a n0 )
THEN
( a n ) SWAP !
;RAM ALIAS rpi
\ RX ISR handler
:NVM
SAVEC
UART1_DR C@ rxp @ C! rxp rpi
IRET
[ OVERT INT_UARTRX !
\ like ?RX only buffered
: ?RXB ( -- c T | F )
rrp @ rxp @ = IF
0
ELSE
rrp @ C@ -1 rrp rpi
THEN
;
\ Interrupt RX UART handler
: INTRX ( -- )
[ ' ?RXB ] LITERAL '?KEY !
rxbuf DUP rxp ! rrp !
[ 1 UART1_CR2 #RIEN ]B!
;
WIPE RAM
\\ Example
\ select the generic STM8S controller first
\ alternatively specify STM8S103, STM8S105 or STM8S207
\res MCU: STM8S103
\ load (or define) device independent constants
\res export INT_UARTRX UART_DR UART_CR2
\ define the UART buffer length
8 CONSTANT RBLEN
\ then load the controller independent code
#require INTRX
\ put it to work
#require WIPE
#require :NVM
#require OSCFREQ
#require UART_DIV
#require UARTBRR
NVM
'BOOT ( xt )
:NVM
INTRX
( xt ) LITERAL EXECUTE
;NVM 'BOOT !
\ calculate UART_DIV settings for 3840*10 = 38400 baud @ CPU clock rate
3840 OSCFREQ UART_DIV UARTBRR !
WIPE RAM
\ make it survive a RESET command
#require PERSIST
PERSIST
\ remember to set your terminal program (e.g. e4thcom) to 38400 baud!