-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.asm
320 lines (305 loc) · 7.28 KB
/
uart.asm
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
;;
; Copyright Jacques Deschênes 2019
; This file is part of STM8_NUCLEO
;
; STM8_NUCLEO is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; STM8_NUCLEO is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with STM8_NUCLEO. If not, see <http://www.gnu.org/licenses/>.
;;
;--------------------------------------
; UART peripherals module
; DATE: 2019-11-29
; NOTE: global routines interface designed
; to be callable from C with the
; prototype given. SDCC for stm8
; pass functions arguments on stack
; pushed from right to left.
;--------------------------------------
.module UART
.nlist
.include "../../inc/nucleo_8s208.inc"
.include "../../inc/stm8s208.inc"
.include "../../inc/ascii.inc"
.list
.area DATA
.area CODE
.asciz "UART"
; contain uarts registers base address
uart_regs: .word UART1_BASE,UART3_BASE
; baud rate constants for BRR1 and BRR2
; Fcy=8Mhz , HSI
baud_fcy8:
.byte 0xd0,0x05 ; 2400
.byte 0x68,0x03 ; 4800
.byte 0x34,0x01 ; 9600
.byte 0x1a,0x01 ; 19200
.byte 0x0d,0x00 ; 38400
.byte 0x08,0x0b ; 57600
.byte 0x04,0x05 ; 115200
.byte 0x02,0x03 ; 230400
.byte 0x01,0x01 ; 460800
; Fcy=16Mhz, HSE maximum frequency
baud_fcy16:
.byte 0xa0,0x1b ; 2400
.byte 0xd0,0x05 ; 4800
.byte 0x68,0x03 ; 9600
.byte 0x34,0x01 ; 19200
.byte 0x1a,0x01 ; 38400
.byte 0x0d,0x00 ; 57600
.byte 0x08,0x0b ; 115200
.byte 0x04,0x05 ; 230400
.byte 0x02,0x03 ; 460800
.byte 0x01,0x01 ; 921600
;--------------------------------------
; select uart base register
; private function
; input:
; A UART_ID
; output:
; X uart register base address
;-------------------------------------
ACC16=1
LOCAL_SIZE=2
select_uart:
sub sp,#LOCAL_SIZE
sll a
ld (ACC16+1,sp),a
clr (ACC16,sp)
ldw x,#uart_regs
addw x,(ACC16,sp)
ldw x,(x)
addw sp,#LOCAL_SIZE
ret
;----------------------------------------------
; initialize UART peripheral
; initilialize config: 8N1 no flow control
; C prototype: void uart_init(uint8_t baud,uint8_t uart_id)
; input:
; baud uint8_t baud
; uart_id uin8t_t uart selector {UART1,UART3}
; output:
; none
;---------------------------------------------
ARG_OFS=4
BAUD=ARG_OFS+1
UART=ARG_OFS+2
; local var
ACC16=1
LOCAL_SIZE=2
_uart_init::
uart_init::
sub sp,#LOCAL_SIZE
clr (ACC16,sp)
;configure port
ld a,(UART,sp)
jrne uart3
uart1:
bset PA_DDR,#UART1_TX_PIN ; tx pin
bset PA_CR1,#UART1_TX_PIN ; push-pull output
bset PA_CR2,#UART1_TX_PIN ; fast output
jra config_baud
uart3:
bset PD_DDR,#UART3_TX_PIN ; tx pin
bset PD_CR1,#UART3_TX_PIN ; push-pull output
bset PD_CR2,#UART3_TX_PIN ; fast output
config_baud:
call select_uart
; check whick clock is active
ld a,#CLK_SWR_HSE ; HSE clock 8Mhz
cp a,CLK_CMSR
jrne 4$
ldw y,#baud_fcy8
jra 5$
4$: ldw y,#baud_fcy16
5$: ld a,(BAUD,sp)
sll a
ld (ACC16+1,sp),a
addw y,(ACC16,sp)
; now Y point to baud rate values for BRR1
; ***** BRR2 must be loaded before BRR1 *****
ld a,(1,y)
ld (UART_BRR2,x),a
ld a,(y)
ld (UART_BRR1,x),a
; enable TX and RX but no interrupts
ld a,#((1<<UART_CR2_TEN)|(1<<UART_CR2_REN));|(1<<UART_CR2_RIEN))
ld (UART_CR2,x),a
ld a,(UART,sp)
addw sp,#LOCAL_SIZE
ret
;------------------------------------
; serial port communication routines
;------------------------------------
;------------------------------------
; transmit character in a via uart
; C prototype: char uart_putc(char c, unit8_t uart)
; input:
; CHAR stack argument
; UART_ID stack argument
; output:
; none
;------------------------------------
ARG_OFS=2
CHAR=ARG_OFS+1
UART_ID=ARG_OFS+2
_uart_putc::
uart_putc::
ld a,(UART_ID,sp)
call select_uart
1$: ld a,#(1<<UART_SR_TXE)
and a,(UART_SR,x)
jreq 1$
ld a,(CHAR,sp)
ld (UART_DR,x),a
ret
;------------------------------------
; print n spaces
; C prototype: void uart_spaces(uint8_t n,uint8_t uart_id)
; input:
; n number of space to print
; uart_id uart selector
; output:
; none
;------------------------------------
ARG_OFS=2
N=ARG_OFS+1
UART=ARG_OFS+2
_uart_spaces::
uart_spaces::
tnz (N,sp)
jreq 3$
ld a,(UART,sp)
push a
ld a,#SPACE
push a
call uart_putc
addw sp,#2
dec (N,sp)
jra uart_spaces
3$: ret
;------------------------------------
; put string to uart channel
; C prototype: int uart_puts(char *str,uint8_t uart)
; input:
; str char *string to print
; uart uart identifier
; output:
; x string length
;------------------------------------
ARG_OFS=8
STR=ARG_OFS+1
UART=ARG_OFS+3
; uart_putc arguments
PUTC_CHAR=1
PUTC_UART=2
LENGTH=3
LOCAL_SIZE=4
_uart_puts::
uart_puts::
pushw y
sub sp,#LOCAL_SIZE
clrw x
ldw (LENGTH,sp),x
ldw y,(STR,sp)
; check for null pointer
cpw y,#0
jreq 1$
ld a,(UART,sp)
ld (PUTC_UART,sp),a
0$: ld a,(y)
jreq 1$
ld (PUTC_CHAR,sp),a
call uart_putc
incw y
inc (LENGTH+1,sp)
jrne 0$
inc (LENGTH,sp)
jra 0$
1$: ldw x,(LENGTH,sp)
addw sp,#LOCAL_SIZE
popw y
ret
;------------------------------------
; check if char available
; return char if available else 0
; C prototype: char uint8_t uart_query(uint8_t uart_id)
; input:
; uart_id uart identifier
; output:
; A 0 = not char | char
;------------------------------------
ARG_OFS=4
UART=ARG_OFS+1
_uart_query::
uart_query::
pushw x
ld a,(UART,sp)
call select_uart
1$: ld a,#(1<<UART_SR_RXNE)
and a,(UART_SR,x)
jreq 2$
ld a,(UART_DR,x)
2$: popw x
ret
;------------------------------------
; wait for character from uart
; C prototype: int uart_getc(uint8_t uart_id)
; input:
; uart_id uart_identifier
; output:
; X character received extended to 16 bits
;------------------------------------
ARG_OFS=2
UART=ARG_OFS+1
_uart_getc::
uart_getc::
ld a,(UART,sp)
call select_uart
1$: ld a,#(1<<UART_SR_RXNE)
and a,(UART_SR,x)
jreq 1$
ld a, (UART_DR,x)
clrw x
2$: ld xl,a
ret
;------------------------------------
; delete n character from input line
; C prototype: void uart_delete(uint8_t n,uint8_t uart_id)
; input:
; n number of characters to delete
; uart_id uart identifier
; output:
; none
;------------------------------------
ARG_OFS=2
N=ARG_OFS+1
UART=ARG_OFS+2
_uart_delete:: ; pour appel à partir de 'C'.
uart_delete::
del_loop:
tnz (N,sp)
jreq 1$
ld a,(UART,sp)
push a
ld a,#BSP
push a
call uart_putc
ld a,#SPACE
ld (1,sp),a
call uart_putc
ld a,#BSP
ld (1,sp),a
call uart_putc
addw sp,#2
dec (N,sp)
jra del_loop
1$: ret