-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_funcs.asm
165 lines (140 loc) · 4.61 KB
/
uart_funcs.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
;------------------------------------------------------------------------------
; Ôóíêöèè äëÿ ðàáîòû ñ UART
;
; Ôóíêöèÿ Buff_Pop äëÿ ðàáîòû ñ êîëüöåâûì áóôåðîì UART
;
;
; (C) 2017-2020 Vitaliy Zinoviev
; https://github.com/nf-zvv/IVC_Tracer
;------------------------------------------------------------------------------
#ifndef _UART_FUNCS_ASM_
#define _UART_FUNCS_ASM_
.ifndef __zero_reg__
.def __zero_reg__ = r2
.endif
;------------------------------------------------------------------------------
; Read from loop Buffer
; USED: r16*,r18,r19*,XL,XH
; IN: NONE
; OUT: R16 - Data,
; R19
; = 1 - ïóñòîé áóôåð (áîëüøå íå÷åãî ÷èòàòü),
; = 0 - â áóôåðå åñòü äàííûå
;------------------------------------------------------------------------------
Buff_Pop:
push XL
push XH
push r18
LDI XL,low(IN_buff) ; Áåðåì àäðåñ íà÷àëà áóôôåðà
LDI XH,high(IN_buff)
LDS R16,IN_PTR_E ; Áåðåì ñìåùåíèå òî÷êè çàïèñè
LDS R18,IN_PTR_S ; Áåðåì ñìåùåíèå òî÷êè ÷òåíèÿ
; Áåðì ôëàã ïåðåïîëíåíèÿ
; Åñëè áóôôåð ïåðåïîëíåí, òî óêàçàòåëü íà÷àëà
; ðàâåí óêàçàòåëþ êîíöà. Ýòî íàäî ó÷åñòü.
BRFS UART_Flags,UART_IN_FULL,NeedPop
CP R18,R16 ; Óêàçàòåëü ÷òåíèÿ äîñòèã óêàçàòåëÿ çàïèñè?
BRNE NeedPop ; Íåò! Áóôôåð íå ïóñò. Ðàáîòàåì äàëüøå
LDI R19,1 ; Êîä îøèáêè - ïóñòîé áóôôåð!
RJMP Buff_Pop_END ; Âûõîäèìs
NeedPop:
; Ñáðàñûâàåì ôëàã ïåðåïîëíåíèÿ
CLFL UART_Flags,UART_IN_FULL
ADD XL,R18 ; Ñëîæåíèåì àäðåñà ñî ñìåùåíèåì
ADC XH,__zero_reg__ ; ïîëó÷àåì àäðåñ òî÷êè ÷òåíèÿ
LD R16,X ; Áåðåì áàéò èç áóôôåðà
CLR R19 ; Ñáðîñ êîäà îøèáêè
INC R18 ; Óâåëè÷èâàåì ñìåùåíèå óêàçàòåëÿ ÷òåíèÿ
CPI R18,MAXBUFF_IN ; Äîñòèãëè êîíöà êîëüöà?
BRNE Buff_Pop_OUT ; Íåò?
CLR R18 ; Äà? Ñáðàñûâàåì, ïåðåñòàâëÿÿ íà 0
Buff_Pop_OUT:
STS IN_PTR_S,R18 ; Ñîõðàíÿåì óêàçàòåëü
Buff_Pop_END:
pop r18
pop XH
pop XL
ret
;-----------------------------------------------
; UART SENT
; îòïðàâêà ñèìâîëà â UART
; èñïîëüçóåìûå ðåãèñòðû - r16 (íå èçì.), r15*
; âõîäíîé ðåãèñòð - r16
; âûõîäíîé ðåãèñòð - íåò
;-----------------------------------------------
uart_snt:
#if defined(__ATmega168__) || defined(__ATmega328P__) || defined(__ATmega1284P__)
LDS r15,UCSR0A
SBRS r15,UDRE0
;SBIS UCSR0A,UDRE0 ; <-- error: Operand 1 out of range: 0xc0
#elif defined(__ATmega8__) || defined(__ATmega16A__) || defined(__ATmega16__)
SBIS UCSRA,UDRE ; Ïðîïóñê åñëè íåò ôëàãà ãîòîâíîñòè
#else
#error "Unsupported part:" __PART_NAME__
#endif // part specific code
RJMP uart_snt ; æäåì ãîòîâíîñòè - ôëàãà UDRE
#if defined(__ATmega168__) || defined(__ATmega328P__) || defined(__ATmega1284P__)
OutReg UDR0,r16 ; øëåì áàéò!
#elif defined(__ATmega8__) || defined(__ATmega16A__) || defined(__ATmega16__)
OutReg UDR,r16 ; øëåì áàéò!
#else
#error "Unsupported part:" __PART_NAME__
#endif // part specific code
RET
;------------------------------------------------------------------------------
; Send null-terminated RAM string to UART
;
; USED: r16*, X*
; CALL: uart_snt
; IN: X - pointer to null-terminated string
; OUT: -
;------------------------------------------------------------------------------
STRING_TO_UART:
ld r16,X+
tst r16
breq STRING_TO_UART_END ; end of string
cpi r16,' ' ; ïðîáåëû íå âûâîäèì
breq STRING_TO_UART
; Send data
rcall uart_snt
rjmp STRING_TO_UART
STRING_TO_UART_END:
ret
;------------------------------------------------------------------------------
; Send null-terminated Flash string to UART
;
; USED: r16*, Z*
; CALL: uart_snt
; IN: Z - pointer to null-terminated string
; OUT: -
;------------------------------------------------------------------------------
FLASH_CONST_TO_UART:
lpm r16,Z+
tst r16
breq STRING_TO_UART_END ; end of string
; Send data
rcall uart_snt
rjmp FLASH_CONST_TO_UART
FLASH_CONST_TO_UART_END:
ret
;------------------------------------------------------------------------------
; Âûâîä ñîîáùåíèÿ "OK" â òåðìèíàë
;------------------------------------------------------------------------------
UART_OK:
ldi r16,'O'
rcall uart_snt
ldi r16,'K'
rcall uart_snt
;------------------------------------------------------------------------------
; Íîâàÿ ñòðîêà LF+CR
;------------------------------------------------------------------------------
UART_LF_CR:
ldi r16,10
rcall uart_snt
ldi r16,13
rcall uart_snt
ret
#endif /* _UART_FUNCS_ASM_ */
;------------------------------------------------------------------------------
; End of file
;------------------------------------------------------------------------------