]>
Commit | Line | Data |
---|---|---|
5985ce1c | 1 | ; Timer module |
9c15f366 | 2 | ; |
9c15f366 L |
3 | ; Copyright (C) 2010 Leo C. |
4 | ; | |
5 | ; This file is part of avrcpm. | |
6 | ; | |
7 | ; avrcpm is free software: you can redistribute it and/or modify it | |
8 | ; under the terms of the GNU General Public License as published by | |
9 | ; the Free Software Foundation, either version 3 of the License, or | |
10 | ; (at your option) any later version. | |
11 | ; | |
12 | ; avrcpm is distributed in the hope that it will be useful, | |
13 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ; GNU General Public License for more details. | |
16 | ; | |
17 | ; You should have received a copy of the GNU General Public License | |
18 | ; along with avrcpm. If not, see <http://www.gnu.org/licenses/>. | |
19 | ; | |
b741422e | 20 | ; $Id$ |
9c15f366 L |
21 | ; |
22 | ||
9c15f366 L |
23 | .dseg |
24 | ||
25 | delay_timer1: | |
26 | .byte 1 | |
27 | delay_timer2: | |
28 | .byte 1 | |
29 | timer_base: | |
30 | timer_ms: | |
31 | .byte 2 | |
32 | timer_s: | |
33 | .byte 4 | |
34 | ; don't change order here, clock put/get depends on it. | |
35 | cntms_out: ; register for ms | |
36 | .byte 2 | |
37 | utime_io: ; register for uptime. | |
38 | .byte 4 | |
39 | cnt_1ms: | |
40 | .byte 2 | |
41 | uptime: | |
42 | .byte 4 | |
43 | timer_top: | |
44 | .equ timer_size = timer_top - timer_base | |
45 | ||
46 | .equ clkofs = cnt_1ms-cntms_out | |
47 | .equ timerofs = cnt_1ms-timer_ms | |
48 | ||
9c15f366 | 49 | .cseg |
9c15f366 | 50 | |
5985ce1c L |
51 | ; ------------- system timer 1ms --------------- |
52 | ||
53 | ||
4675c141 L |
54 | ; Timer/Counter1 Compare Match B interrupt |
55 | ||
56 | INTERRUPT OC1Baddr | |
57 | ||
9c15f366 L |
58 | push zl |
59 | in zl,SREG | |
60 | push zl | |
61 | push zh | |
62 | inm8 zl,OCR1BL | |
63 | inm8 zh,OCR1BH | |
64 | addiw z,F_CPU/1000 | |
65 | outm8 OCR1BH,zh | |
66 | outm8 OCR1BL,zl | |
67 | ||
68 | #if DRAM_8BIT /* Implies software uart */ | |
69 | lds zl,srx_char_to | |
70 | subi zl,1 | |
71 | brcs syscl0 | |
72 | sts srx_char_to,zl | |
73 | brne syscl0 | |
74 | rcall srx_to | |
75 | syscl0: | |
76 | #endif | |
77 | lds zl,delay_timer1 | |
78 | subi zl,1 | |
79 | brcs syscl_t1n | |
80 | sts delay_timer1,zl | |
81 | syscl_t1n: | |
82 | lds zl,delay_timer2 | |
83 | subi zl,1 | |
84 | brcs syscl_t2n | |
85 | sts delay_timer2,zl | |
86 | syscl_t2n: | |
87 | lds zl,cnt_1ms | |
88 | lds zh,cnt_1ms+1 | |
89 | adiw z,1 | |
90 | ||
91 | sts cnt_1ms,zl | |
92 | sts cnt_1ms+1,zh | |
93 | cpi zl,low(1000) | |
94 | ldi zl,high(1000) ;doesn't change flags | |
95 | cpc zh,zl | |
96 | brlo syscl_end | |
97 | ||
98 | sts cnt_1ms,_0 | |
99 | sts cnt_1ms+1,_0 | |
100 | ||
101 | lds zl,uptime+0 | |
102 | inc zl | |
103 | sts uptime+0,zl | |
104 | brne syscl_end | |
105 | lds zl,uptime+1 | |
106 | inc zl | |
107 | sts uptime+1,zl | |
108 | brne syscl_end | |
109 | lds zl,uptime+2 | |
110 | inc zl | |
111 | sts uptime+2,zl | |
112 | brne syscl_end | |
113 | lds zl,uptime+3 | |
114 | inc zl | |
115 | sts uptime+3,zl | |
116 | ||
117 | syscl_end: | |
118 | pop zh | |
119 | pop zl | |
120 | out SREG,zl | |
121 | pop zl | |
122 | reti | |
123 | ||
5985ce1c L |
124 | ; ---------------------------------------------- |
125 | ; delay | |
126 | ; | |
9c15f366 | 127 | ; wait for temp ms |
5985ce1c | 128 | ; |
9c15f366 L |
129 | |
130 | delay_ms: | |
131 | sts delay_timer1,temp | |
132 | dly_loop: | |
133 | lds temp,delay_timer1 | |
134 | cpi temp,0 | |
135 | brne dly_loop | |
136 | ret | |
137 | ||
5985ce1c | 138 | ; ---------------------------------------------- |
9c15f366 L |
139 | ; |
140 | ||
141 | clockget: | |
142 | ldi temp,0xFF | |
143 | subi temp2,TIMER_MSECS | |
144 | brcs clkget_end ;Port number in range? | |
145 | ldiw z,cntms_out | |
146 | breq clkget_copy ;lowest byte requestet, latch clock | |
147 | cpi temp2,6 | |
148 | brsh clkget_end ;Port number to high? | |
149 | ||
150 | add zl,temp2 | |
151 | brcc PC+2 | |
152 | inc zh | |
153 | ld temp,z | |
154 | clkget_end: | |
155 | ret | |
156 | ||
157 | ||
158 | ||
159 | clkget_copy: | |
160 | ldi temp2,6 | |
161 | cli | |
162 | clkget_l: | |
163 | ldd temp,z+clkofs | |
164 | st z+,temp | |
165 | dec temp2 | |
166 | brne clkget_l | |
167 | sei | |
168 | lds temp,cntms_out | |
169 | ;req. byte in temp | |
170 | ret | |
171 | ||
172 | clockput: | |
173 | subi temp2,TIMERPORT | |
174 | brcs clkput_end ;Port number in range? | |
175 | brne clkput_1 | |
176 | ||
177 | ; clock control | |
178 | ||
179 | cpi temp,starttimercmd | |
180 | breq timer_start | |
181 | cpi temp,quitTimerCmd | |
182 | breq timer_quit | |
183 | cpi temp,printTimerCmd | |
184 | breq timer_print | |
185 | cpi temp,uptimeCmd | |
186 | brne cp_ex | |
187 | rjmp uptime_print | |
188 | cp_ex: | |
189 | ret | |
190 | ||
191 | timer_quit: | |
192 | rcall timer_print | |
193 | rjmp timer_start | |
194 | ||
195 | clkput_1: | |
196 | dec temp2 | |
197 | ldiw z,cntms_out | |
198 | breq clkput_copy ;lowest byte requestet, latch clock | |
199 | cpi temp2,6 | |
200 | brsh clkput_end ;Port number to high? | |
201 | ||
202 | add zl,temp2 | |
203 | brcc PC+2 | |
204 | inc zh | |
205 | st z,temp | |
206 | clkput_end: | |
207 | ret | |
208 | ||
209 | clkput_copy: | |
210 | st z,temp | |
211 | adiw z,5 | |
212 | ldi temp2,6 | |
213 | cli | |
214 | clkput_l: | |
215 | ldd temp,z+clkofs | |
216 | st z+,temp | |
217 | dec temp2 | |
218 | brne clkput_l | |
219 | sei | |
220 | ret | |
221 | ||
222 | ; start/reset timer | |
223 | ; | |
224 | timer_start: | |
225 | ldiw z,timer_ms | |
226 | ldi temp2,6 | |
227 | cli | |
228 | ts_loop: | |
229 | ldd temp,z+timerofs | |
230 | st z+,temp | |
231 | dec temp2 | |
232 | brne ts_loop | |
233 | sei | |
234 | ret | |
235 | ||
236 | ||
237 | ; print timer | |
238 | ; | |
239 | ||
240 | timer_print: | |
241 | push yh | |
242 | push yl | |
243 | ldiw z,timer_ms | |
244 | ||
245 | ; put ms on stack (16 bit) | |
246 | ||
247 | cli | |
248 | ldd yl,z+timerofs | |
249 | ld temp2,z+ | |
250 | sub yl,temp2 | |
251 | ldd yh,z+timerofs | |
252 | ld temp2,z+ | |
253 | sbc yh,temp2 | |
254 | brsh tp_s | |
255 | ||
256 | addiw y,1000 | |
257 | sec | |
258 | tp_s: | |
259 | push yh | |
260 | push yl | |
261 | ||
262 | ldd temp,z+timerofs | |
263 | ld yl,z+ | |
264 | sbc temp,yl | |
265 | ||
266 | ldd temp2,z+timerofs | |
267 | ld yh,z+ | |
268 | sbc temp2,yh | |
269 | ||
270 | ldd temp3,z+timerofs | |
271 | ld yl,z+ | |
272 | sbc temp3,yl | |
273 | ||
274 | sei | |
275 | ldd temp4,z+timerofs | |
276 | ld yh,z+ | |
277 | sbc temp4,yh | |
278 | ||
279 | printnewline | |
280 | printstring "Timer running. Elapsed: " | |
281 | rcall print_ultoa | |
282 | ||
283 | printstring "." | |
284 | pop temp | |
285 | pop temp2 | |
286 | ldi temp3,0 | |
287 | ldi temp4,0 | |
288 | rcall print_ultoa | |
289 | printstring "s." | |
290 | ||
291 | pop yl | |
292 | pop yh | |
293 | ret | |
294 | ||
295 | uptime_print: | |
296 | ||
297 | ldiw z,cnt_1ms | |
298 | ||
299 | cli | |
300 | ld temp,z+ | |
301 | push temp | |
302 | ld temp,z+ | |
303 | push temp | |
304 | ||
305 | ld temp,z+ | |
306 | ld temp2,z+ | |
307 | ld temp3,z+ | |
308 | sei | |
309 | ld temp4,z+ | |
310 | ||
311 | printnewline | |
312 | printstring "Uptime: " | |
313 | ||
314 | rcall print_ultoa | |
315 | printstring "," | |
316 | ||
317 | ldi temp3,0 | |
318 | ldi temp4,0 | |
319 | pop temp2 | |
320 | pop temp | |
321 | rcall print_ultoa | |
322 | printstring "s." | |
323 | ||
324 | ret | |
325 | ||
5985ce1c | 326 | ; vim:set ts=8 noet nowrap |
9c15f366 | 327 |