Fix interupt -> interrupt typo, including in some function names.
[simavr] / simavr / sim / avr_timer.c
1 /*
2         avr_timer.c
3
4         Handles the 8 bits and 16 bits AVR timer.
5         Handles
6         + CDC
7         + Fast PWM
8
9         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
10
11         This file is part of simavr.
12
13         simavr is free software: you can redistribute it and/or modify
14         it under the terms of the GNU General Public License as published by
15         the Free Software Foundation, either version 3 of the License, or
16         (at your option) any later version.
17
18         simavr is distributed in the hope that it will be useful,
19         but WITHOUT ANY WARRANTY; without even the implied warranty of
20         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21         GNU General Public License for more details.
22
23         You should have received a copy of the GNU General Public License
24         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27 #include <stdio.h>
28 #include "avr_timer.h"
29 #include "avr_ioport.h"
30
31 /*
32  * The timers are /always/ 16 bits here, if the higher byte register
33  * is specified it's just added.
34  */
35 static uint16_t _timer_get_ocr(avr_timer_t * p, int compi)
36 {
37         return p->io.avr->data[p->comp[compi].r_ocr] |
38                       (p->comp[compi].r_ocrh ? (p->io.avr->data[p->comp[compi].r_ocrh] << 8) : 0);
39 }
40 static uint16_t _timer_get_tcnt(avr_timer_t * p)
41 {
42         return p->io.avr->data[p->r_tcnt] |
43                                 (p->r_tcnth ? (p->io.avr->data[p->r_tcnth] << 8) : 0);
44 }
45 static uint16_t _timer_get_icr(avr_timer_t * p)
46 {
47         return p->io.avr->data[p->r_icr] |
48                                 (p->r_tcnth ? (p->io.avr->data[p->r_icrh] << 8) : 0);
49 }
50 static avr_cycle_count_t avr_timer_comp(avr_timer_t *p, avr_cycle_count_t when, uint8_t comp)
51 {
52         avr_t * avr = p->io.avr;
53         avr_raise_interrupt(avr, &p->comp[comp].interrupt);
54
55         // check output compare mode and set/clear pins
56         uint8_t mode = avr_regbit_get(avr, p->comp[comp].com);
57         avr_irq_t * irq = &p->io.irq[TIMER_IRQ_OUT_COMP + comp];
58
59         switch (mode) {
60                 case avr_timer_com_normal: // Normal mode OCnA disconnected
61                         break;
62                 case avr_timer_com_toggle: // Toggle OCnA on compare match
63                         if (p->comp[comp].com_pin.reg)  // we got a physical pin
64                                 avr_raise_irq(irq,
65                                                 0x100 + (avr_regbit_get(avr, p->comp[comp].com_pin) ? 0 : 1));
66                         else // no pin, toggle the IRQ anyway
67                                 avr_raise_irq(irq,
68                                                 p->io.irq[TIMER_IRQ_OUT_COMP + comp].value ? 0 : 1);
69                         break;
70                 case avr_timer_com_clear:
71                         avr_raise_irq(irq, 0);
72                         break;
73                 case avr_timer_com_set:
74                         avr_raise_irq(irq, 1);
75                         break;
76         }
77
78         return p->tov_cycles ? 0 : p->comp[comp].comp_cycles ? when
79                 + p->comp[comp].comp_cycles : 0;
80 }
81
82 static avr_cycle_count_t avr_timer_compa(struct avr_t * avr, avr_cycle_count_t when, void * param)
83 {
84         return avr_timer_comp((avr_timer_t*)param, when, AVR_TIMER_COMPA);
85 }
86
87 static avr_cycle_count_t avr_timer_compb(struct avr_t * avr, avr_cycle_count_t when, void * param)
88 {
89         return avr_timer_comp((avr_timer_t*)param, when, AVR_TIMER_COMPB);
90 }
91
92 static avr_cycle_count_t avr_timer_compc(struct avr_t * avr, avr_cycle_count_t when, void * param)
93 {
94         return avr_timer_comp((avr_timer_t*)param, when, AVR_TIMER_COMPC);
95 }
96
97 // timer overflow
98 static avr_cycle_count_t avr_timer_tov(struct avr_t * avr, avr_cycle_count_t when, void * param)
99 {
100         avr_timer_t * p = (avr_timer_t *)param;
101         int start = p->tov_base == 0;
102
103         if (!start)
104                 avr_raise_interrupt(avr, &p->overflow);
105         p->tov_base = when;
106
107         static const avr_cycle_timer_t dispatch[AVR_TIMER_COMP_COUNT] =
108                 { avr_timer_compa, avr_timer_compb, avr_timer_compc };
109
110         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
111                 if (p->comp[compi].comp_cycles) {
112                         if (p->comp[compi].comp_cycles < p->tov_cycles)
113                                 avr_cycle_timer_register(avr,
114                                         p->comp[compi].comp_cycles,
115                                         dispatch[compi], p);
116                         else if (p->tov_cycles == p->comp[compi].comp_cycles && !start)
117                                 dispatch[compi](avr, when, param);
118                 }
119         }
120
121         return when + p->tov_cycles;
122 }
123
124 static uint16_t _avr_timer_get_current_tcnt(avr_timer_t * p)
125 {
126         avr_t * avr = p->io.avr;
127         if (p->tov_cycles) {
128                 uint64_t when = avr->cycle - p->tov_base;
129
130                 return (when * (((uint32_t)p->tov_top)+1)) / p->tov_cycles;
131         }
132         return 0;
133 }
134
135 static uint8_t avr_timer_tcnt_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
136 {
137         avr_timer_t * p = (avr_timer_t *)param;
138         // made to trigger potential watchpoints
139
140         uint16_t tcnt = _avr_timer_get_current_tcnt(p);
141
142         avr->data[p->r_tcnt] = tcnt;
143         if (p->r_tcnth)
144                 avr->data[p->r_tcnth] = tcnt >> 8;
145         
146         return avr_core_watch_read(avr, addr);
147 }
148
149 static void avr_timer_tcnt_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
150 {
151         avr_timer_t * p = (avr_timer_t *)param;
152         avr_core_watch_write(avr, addr, v);
153         uint16_t tcnt = _timer_get_tcnt(p);
154
155         if (!p->tov_top)
156                 return;
157                 
158         if (tcnt >= p->tov_top)
159                 tcnt = 0;
160         
161         // this involves some magicking
162         // cancel the current timers, recalculate the "base" we should be at, reset the
163         // timer base as it should, and re-shedule the timers using that base.
164         
165         avr_cycle_timer_cancel(avr, avr_timer_tov, p);
166         avr_cycle_timer_cancel(avr, avr_timer_compa, p);
167         avr_cycle_timer_cancel(avr, avr_timer_compb, p);
168         avr_cycle_timer_cancel(avr, avr_timer_compc, p);
169
170         uint64_t cycles = (tcnt * p->tov_cycles) / p->tov_top;
171
172 //      printf("%s-%c %d/%d -- cycles %d/%d\n", __FUNCTION__, p->name, tcnt, p->tov_top, (uint32_t)cycles, (uint32_t)p->tov_cycles);
173
174         // this reset the timers bases to the new base
175         p->tov_base = 0;
176         avr_cycle_timer_register(avr, p->tov_cycles - cycles, avr_timer_tov, p);
177         avr_timer_tov(avr, avr->cycle - cycles, p);
178
179 //      tcnt = ((avr->cycle - p->tov_base) * p->tov_top) / p->tov_cycles;
180 //      printf("%s-%c new tnt derive to %d\n", __FUNCTION__, p->name, tcnt);    
181 }
182
183 static void avr_timer_configure(avr_timer_t * p, uint32_t clock, uint32_t top)
184 {
185         float t = clock / (float)(top+1);
186         float frequency = p->io.avr->frequency;
187
188         p->tov_cycles = 0;
189         p->tov_top = top;
190
191         p->tov_cycles = frequency / t; // avr_hz_to_cycles(frequency, t);
192         printf("%s-%c TOP %.2fHz = %d cycles\n", __FUNCTION__, p->name, t, (int)p->tov_cycles);
193
194         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
195                 uint32_t ocr = _timer_get_ocr(p, compi);
196                 float fc = clock / (float)(ocr+1);
197
198                 p->comp[compi].comp_cycles = 0;
199 //              printf("%s-%c clock %d top %d OCR%c %d\n", __FUNCTION__, p->name, clock, top, 'A'+compi, ocr);
200
201                 if (ocr && ocr <= top) {
202                         p->comp[compi].comp_cycles = frequency / fc; // avr_hz_to_cycles(p->io.avr, fa);
203                         printf("%s-%c %c %.2fHz = %d cycles\n", __FUNCTION__, p->name,
204                                         'A'+compi, fc, (int)p->comp[compi].comp_cycles);
205                 }
206         }
207
208         if (p->tov_cycles > 1) {
209                 avr_cycle_timer_register(p->io.avr, p->tov_cycles, avr_timer_tov, p);
210                 // calling it once, with when == 0 tells it to arm the A/B/C timers if needed
211                 p->tov_base = 0;
212                 avr_timer_tov(p->io.avr, p->io.avr->cycle, p);
213         }
214 }
215
216 static void avr_timer_reconfigure(avr_timer_t * p)
217 {
218         avr_t * avr = p->io.avr;
219
220         avr_timer_wgm_t zero={0};
221         p->mode = zero;
222         // cancel everything
223         p->comp[AVR_TIMER_COMPA].comp_cycles = 0;
224         p->comp[AVR_TIMER_COMPB].comp_cycles = 0;
225         p->comp[AVR_TIMER_COMPC].comp_cycles = 0;
226         p->tov_cycles = 0;
227         
228         avr_cycle_timer_cancel(avr, avr_timer_tov, p);
229         avr_cycle_timer_cancel(avr, avr_timer_compa, p);
230         avr_cycle_timer_cancel(avr, avr_timer_compb, p);
231         avr_cycle_timer_cancel(avr, avr_timer_compc, p);
232
233         long clock = avr->frequency;
234
235         // only can exists on "asynchronous" 8 bits timers
236         if (avr_regbit_get(avr, p->as2))
237                 clock = 32768;
238
239         uint8_t cs = avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs));
240         if (cs == 0) {
241                 printf("%s-%c clock turned off\n", __FUNCTION__, p->name);              
242                 return;
243         }
244
245         uint8_t mode = avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm));
246         uint8_t cs_div = p->cs_div[cs];
247         uint32_t f = clock >> cs_div;
248
249         p->mode = p->wgm_op[mode];
250         //printf("%s-%c clock %d, div %d(/%d) = %d ; mode %d\n", __FUNCTION__, p->name, clock, cs, 1 << cs_div, f, mode);
251         switch (p->mode.kind) {
252                 case avr_timer_wgm_normal:
253                         avr_timer_configure(p, f, (1 << p->mode.size) - 1);
254                         break;
255                 case avr_timer_wgm_ctc: {
256                         avr_timer_configure(p, f, _timer_get_ocr(p, AVR_TIMER_COMPA));
257                 }       break;
258                 case avr_timer_wgm_pwm: {
259                         uint16_t top = p->mode.top == avr_timer_wgm_reg_ocra ? _timer_get_ocr(p, AVR_TIMER_COMPA) : _timer_get_icr(p);
260                         avr_timer_configure(p, f, top);
261                 }       break;
262                 case avr_timer_wgm_fast_pwm:
263                         avr_timer_configure(p, f, (1 << p->mode.size) - 1);
264                         break;
265                 default:
266                         printf("%s-%c unsupported timer mode wgm=%d (%d)\n", __FUNCTION__, p->name, mode, p->mode.kind);
267         }       
268 }
269
270 static void avr_timer_write_ocr(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
271 {
272         avr_timer_t * p = (avr_timer_t *)param;
273         avr_core_watch_write(avr, addr, v);
274
275         switch (p->mode.kind) {
276                 case avr_timer_wgm_normal:
277                         avr_timer_reconfigure(p);
278                         break;
279                 case avr_timer_wgm_pwm:
280                         if (p->mode.top != avr_timer_wgm_reg_ocra) {
281                                 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM0, _timer_get_ocr(p, AVR_TIMER_COMPA));
282                                 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM1, _timer_get_ocr(p, AVR_TIMER_COMPB));
283                         }
284                         break;
285                 case avr_timer_wgm_fast_pwm:
286                         avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM0, _timer_get_ocr(p, AVR_TIMER_COMPA));
287                         avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM1, _timer_get_ocr(p, AVR_TIMER_COMPB));
288                         break;
289                 default:
290                         printf("%s-%c mode %d UNSUPORTED\n", __FUNCTION__, p->name, p->mode.kind);
291                         avr_timer_reconfigure(p);
292                         break;
293         }
294 }
295
296 static void avr_timer_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
297 {
298         avr_timer_t * p = (avr_timer_t *)param;
299
300         uint8_t as2 = avr_regbit_get(avr, p->as2);
301         uint8_t cs = avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs));
302         uint8_t mode = avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm));
303
304         avr_core_watch_write(avr, addr, v);
305
306         // only reconfigure the timer if "relevant" bits have changed
307         // this prevent the timer reset when changing the edge detector
308         // or other minor bits
309         if (avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs)) != cs ||
310                         avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm)) != mode ||
311                                         avr_regbit_get(avr, p->as2) != as2) {
312                 avr_timer_reconfigure(p);
313         }
314 }
315
316 /*
317  * write to the TIFR register. Watch for code that writes "1" to clear
318  * pending interrupts.
319  */
320 static void avr_timer_write_pending(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
321 {
322         avr_timer_t * p = (avr_timer_t *)param;
323         // save old bits values
324         uint8_t ov = avr_regbit_get(avr, p->overflow.raised);
325         uint8_t ic = avr_regbit_get(avr, p->icr.raised);
326         uint8_t cp[AVR_TIMER_COMP_COUNT];
327
328         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++)
329                 cp[compi] = avr_regbit_get(avr, p->comp[compi].interrupt.raised);
330
331         // write the value
332         avr_core_watch_write(avr, addr, v);
333
334         // clear any interrupts & flags
335         avr_clear_interrupt_if(avr, &p->overflow, ov);
336         avr_clear_interrupt_if(avr, &p->icr, ic);
337
338         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++)
339                 avr_clear_interrupt_if(avr, &p->comp[compi].interrupt, cp[compi]);
340 }
341
342 static void avr_timer_irq_icp(struct avr_irq_t * irq, uint32_t value, void * param)
343 {
344         avr_timer_t * p = (avr_timer_t *)param;
345         avr_t * avr = p->io.avr;
346
347         // input capture disabled when ICR is used as top
348         if (p->mode.top == avr_timer_wgm_reg_icr)
349                 return;
350         int bing = 0;
351         if (avr_regbit_get(avr, p->ices)) { // rising edge
352                 if (!irq->value && value)
353                         bing++;
354         } else {        // default, falling edge
355                 if (irq->value && !value)
356                         bing++;
357         }
358         if (!bing)
359                 return;
360         // get current TCNT, copy it to ICR, and raise interrupt
361         uint16_t tcnt = _avr_timer_get_current_tcnt(p);
362         avr->data[p->r_icr] = tcnt;
363         if (p->r_icrh)
364                 avr->data[p->r_icrh] = tcnt >> 8;
365         avr_raise_interrupt(avr, &p->icr);
366 }
367
368 static void avr_timer_reset(avr_io_t * port)
369 {
370         avr_timer_t * p = (avr_timer_t *)port;
371         avr_cycle_timer_cancel(p->io.avr, avr_timer_tov, p);
372         avr_cycle_timer_cancel(p->io.avr, avr_timer_compa, p);
373         avr_cycle_timer_cancel(p->io.avr, avr_timer_compb, p);
374         avr_cycle_timer_cancel(p->io.avr, avr_timer_compc, p);
375
376         // check to see if the comparators have a pin output. If they do,
377         // (try) to get the ioport corresponding IRQ and connect them
378         // they will automagically be triggered when the comparator raises
379         // it's own IRQ
380         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
381                 p->comp[compi].comp_cycles = 0;
382
383                 avr_ioport_getirq_t req = {
384                         .bit = p->comp[compi].com_pin
385                 };
386                 if (avr_ioctl(port->avr, AVR_IOCTL_IOPORT_GETIRQ_REGBIT, &req) > 0) {
387                         // cool, got an IRQ
388 //                      printf("%s-%c COMP%c Connecting PIN IRQ %d\n", __FUNCTION__, p->name, 'A'+compi, req.irq[0]->irq);
389                         avr_connect_irq(&port->irq[TIMER_IRQ_OUT_COMP + compi], req.irq[0]);
390                 }
391         }
392         avr_ioport_getirq_t req = {
393                 .bit = p->icp
394         };
395         if (avr_ioctl(port->avr, AVR_IOCTL_IOPORT_GETIRQ_REGBIT, &req) > 0) {
396                 // cool, got an IRQ for the input capture pin
397 //              printf("%s-%c ICP Connecting PIN IRQ %d\n", __FUNCTION__, p->name, req.irq[0]->irq);
398                 avr_irq_register_notify(req.irq[0], avr_timer_irq_icp, p);
399         }
400
401 }
402
403 static const char * irq_names[TIMER_IRQ_COUNT] = {
404         [TIMER_IRQ_OUT_PWM0] = "8>pwm0",
405         [TIMER_IRQ_OUT_PWM1] = "8>pwm1",
406         [TIMER_IRQ_OUT_COMP + 0] = ">compa",
407         [TIMER_IRQ_OUT_COMP + 1] = ">compb",
408         [TIMER_IRQ_OUT_COMP + 2] = ">compc",
409 };
410
411 static  avr_io_t        _io = {
412         .kind = "timer",
413         .reset = avr_timer_reset,
414         .irq_names = irq_names,
415 };
416
417 void avr_timer_init(avr_t * avr, avr_timer_t * p)
418 {
419         p->io = _io;
420
421         avr_register_io(avr, &p->io);
422         avr_register_vector(avr, &p->overflow);
423         avr_register_vector(avr, &p->icr);
424
425         // allocate this module's IRQ
426         avr_io_setirqs(&p->io, AVR_IOCTL_TIMER_GETIRQ(p->name), TIMER_IRQ_COUNT, NULL);
427
428         // marking IRQs as "filtered" means they don't propagate if the
429         // new value raised is the same as the last one.. in the case of the
430         // pwm value it makes sense not to bother.
431         p->io.irq[TIMER_IRQ_OUT_PWM0].flags |= IRQ_FLAG_FILTERED;
432         p->io.irq[TIMER_IRQ_OUT_PWM1].flags |= IRQ_FLAG_FILTERED;
433
434         if (p->wgm[0].reg) // these are not present on older AVRs
435                 avr_register_io_write(avr, p->wgm[0].reg, avr_timer_write, p);
436         avr_register_io_write(avr, p->cs[0].reg, avr_timer_write, p);
437
438         // this assumes all the "pending" interrupt bits are in the same
439         // register. Might not be true on all devices ?
440         avr_register_io_write(avr, p->overflow.raised.reg, avr_timer_write_pending, p);
441
442         /*
443          * Even if the timer is 16 bits, we don't care to have watches on the
444          * high bytes because the datasheet says that the low address is always
445          * the trigger.
446          */
447         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
448                 avr_register_vector(avr, &p->comp[compi].interrupt);
449
450                 if (p->comp[compi].r_ocr) // not all timers have all comparators
451                         avr_register_io_write(avr, p->comp[compi].r_ocr, avr_timer_write_ocr, p);
452         }
453         avr_register_io_write(avr, p->r_tcnt, avr_timer_tcnt_write, p);
454         avr_register_io_read(avr, p->r_tcnt, avr_timer_tcnt_read, p);
455 }