IOs: Use new IRQ alloc function
[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 static avr_cycle_count_t avr_timer_tov(struct avr_t * avr, avr_cycle_count_t when, void * param)
98 {
99         avr_timer_t * p = (avr_timer_t *)param;
100         int start = p->tov_base == 0;
101
102         if (!start)
103                 avr_raise_interrupt(avr, &p->overflow);
104         p->tov_base = when;
105
106         static const avr_cycle_timer_t dispatch[AVR_TIMER_COMP_COUNT] =
107                 { avr_timer_compa, avr_timer_compb, avr_timer_compc };
108
109         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
110                 if (p->comp[compi].comp_cycles) {
111                         if (p->comp[compi].comp_cycles < p->tov_cycles)
112                                 avr_cycle_timer_register(avr,
113                                         p->comp[compi].comp_cycles,
114                                         dispatch[compi], p);
115                         else if (p->tov_cycles == p->comp[compi].comp_cycles && !start)
116                                 dispatch[compi](avr, when, param);
117                 }
118         }
119
120         return when + p->tov_cycles;
121 }
122
123 static uint16_t _avr_timer_get_current_tcnt(avr_timer_t * p)
124 {
125         avr_t * avr = p->io.avr;
126         if (p->tov_cycles) {
127                 uint64_t when = avr->cycle - p->tov_base;
128
129                 return (when * p->tov_top) / p->tov_cycles;
130         }
131         return 0;
132 }
133
134 static uint8_t avr_timer_tcnt_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
135 {
136         avr_timer_t * p = (avr_timer_t *)param;
137         // made to trigger potential watchpoints
138
139         uint16_t tcnt = _avr_timer_get_current_tcnt(p);
140
141         avr->data[p->r_tcnt] = tcnt;
142         if (p->r_tcnth)
143                 avr->data[p->r_tcnth] = tcnt >> 8;
144         
145         return avr_core_watch_read(avr, addr);
146 }
147
148 static void avr_timer_tcnt_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
149 {
150         avr_timer_t * p = (avr_timer_t *)param;
151         avr_core_watch_write(avr, addr, v);
152         uint16_t tcnt = _timer_get_tcnt(p);
153
154         if (!p->tov_top)
155                 return;
156                 
157         if (tcnt >= p->tov_top)
158                 tcnt = 0;
159         
160         // this involves some magicking
161         // cancel the current timers, recalculate the "base" we should be at, reset the
162         // timer base as it should, and re-shedule the timers using that base.
163         
164         avr_cycle_timer_cancel(avr, avr_timer_tov, p);
165         avr_cycle_timer_cancel(avr, avr_timer_compa, p);
166         avr_cycle_timer_cancel(avr, avr_timer_compb, p);
167         avr_cycle_timer_cancel(avr, avr_timer_compc, p);
168
169         uint64_t cycles = (tcnt * p->tov_cycles) / p->tov_top;
170
171 //      printf("%s-%c %d/%d -- cycles %d/%d\n", __FUNCTION__, p->name, tcnt, p->tov_top, (uint32_t)cycles, (uint32_t)p->tov_cycles);
172
173         // this reset the timers bases to the new base
174         p->tov_base = 0;
175         avr_cycle_timer_register(avr, p->tov_cycles - cycles, avr_timer_tov, p);
176         avr_timer_tov(avr, avr->cycle - cycles, p);
177
178 //      tcnt = ((avr->cycle - p->tov_base) * p->tov_top) / p->tov_cycles;
179 //      printf("%s-%c new tnt derive to %d\n", __FUNCTION__, p->name, tcnt);    
180 }
181
182 static void avr_timer_configure(avr_timer_t * p, uint32_t clock, uint32_t top)
183 {
184         float t = clock / (float)(top+1);
185         float frequency = p->io.avr->frequency;
186
187         p->tov_cycles = 0;
188         p->tov_top = top;
189
190         p->tov_cycles = frequency / t; // avr_hz_to_cycles(frequency, t);
191         printf("%s-%c TOP %.2fHz = %d cycles\n", __FUNCTION__, p->name, t, (int)p->tov_cycles);
192
193         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
194                 uint32_t ocr = _timer_get_ocr(p, compi);
195                 float fc = clock / (float)(ocr+1);
196
197                 p->comp[compi].comp_cycles = 0;
198 //              printf("%s-%c clock %d top %d OCR%c %d\n", __FUNCTION__, p->name, clock, top, 'A'+compi, ocr);
199
200                 if (ocr && ocr <= top) {
201                         p->comp[compi].comp_cycles = frequency / fc; // avr_hz_to_cycles(p->io.avr, fa);
202                         printf("%s-%c %c %.2fHz = %d cycles\n", __FUNCTION__, p->name,
203                                         'A'+compi, fc, (int)p->comp[compi].comp_cycles);
204                 }
205         }
206
207         if (p->tov_cycles > 1) {
208                 avr_cycle_timer_register(p->io.avr, p->tov_cycles, avr_timer_tov, p);
209                 // calling it once, with when == 0 tells it to arm the A/B/C timers if needed
210                 p->tov_base = 0;
211                 avr_timer_tov(p->io.avr, p->io.avr->cycle, p);
212         }
213 }
214
215 static void avr_timer_reconfigure(avr_timer_t * p)
216 {
217         avr_t * avr = p->io.avr;
218
219         avr_timer_wgm_t zero={0};
220         p->mode = zero;
221         // cancel everything
222         p->comp[AVR_TIMER_COMPA].comp_cycles = 0;
223         p->comp[AVR_TIMER_COMPB].comp_cycles = 0;
224         p->comp[AVR_TIMER_COMPC].comp_cycles = 0;
225         p->tov_cycles = 0;
226         
227         avr_cycle_timer_cancel(avr, avr_timer_tov, p);
228         avr_cycle_timer_cancel(avr, avr_timer_compa, p);
229         avr_cycle_timer_cancel(avr, avr_timer_compb, p);
230         avr_cycle_timer_cancel(avr, avr_timer_compc, p);
231
232         long clock = avr->frequency;
233
234         // only can exists on "asynchronous" 8 bits timers
235         if (avr_regbit_get(avr, p->as2))
236                 clock = 32768;
237
238         uint8_t cs = avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs));
239         if (cs == 0) {
240                 printf("%s-%c clock turned off\n", __FUNCTION__, p->name);              
241                 return;
242         }
243
244         uint8_t mode = avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm));
245         uint8_t cs_div = p->cs_div[cs];
246         uint32_t f = clock >> cs_div;
247
248         p->mode = p->wgm_op[mode];
249         //printf("%s-%c clock %d, div %d(/%d) = %d ; mode %d\n", __FUNCTION__, p->name, clock, cs, 1 << cs_div, f, mode);
250         switch (p->mode.kind) {
251                 case avr_timer_wgm_normal:
252                         avr_timer_configure(p, f, (1 << p->mode.size) - 1);
253                         break;
254                 case avr_timer_wgm_ctc: {
255                         avr_timer_configure(p, f, _timer_get_ocr(p, AVR_TIMER_COMPA));
256                 }       break;
257                 case avr_timer_wgm_pwm: {
258                         uint16_t top = p->mode.top == avr_timer_wgm_reg_ocra ? _timer_get_ocr(p, AVR_TIMER_COMPA) : _timer_get_icr(p);
259                         avr_timer_configure(p, f, top);
260                 }       break;
261                 case avr_timer_wgm_fast_pwm:
262                 //      avr_timer_configure(p, f, (1 << p->mode.size) - 1);
263                         break;
264                 default:
265                         printf("%s-%c unsupported timer mode wgm=%d (%d)\n", __FUNCTION__, p->name, mode, p->mode.kind);
266         }       
267 }
268
269 static void avr_timer_write_ocr(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
270 {
271         avr_timer_t * p = (avr_timer_t *)param;
272         avr_core_watch_write(avr, addr, v);
273
274         switch (p->mode.kind) {
275                 case avr_timer_wgm_pwm:
276                         if (p->mode.top != avr_timer_wgm_reg_ocra) {
277                                 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM0, _timer_get_ocr(p, AVR_TIMER_COMPA));
278                                 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM1, _timer_get_ocr(p, AVR_TIMER_COMPB));
279                         }
280                         break;
281                 case avr_timer_wgm_fast_pwm:
282                         avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM0, _timer_get_ocr(p, AVR_TIMER_COMPA));
283                         avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM1, _timer_get_ocr(p, AVR_TIMER_COMPB));
284                         break;
285                 default:
286                         printf("%s-%c mode %d\n", __FUNCTION__, p->name, p->mode.kind);
287                         avr_timer_reconfigure(p);
288                         break;
289         }
290 }
291
292 static void avr_timer_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
293 {
294         avr_timer_t * p = (avr_timer_t *)param;
295         avr_core_watch_write(avr, addr, v);
296         avr_timer_reconfigure(p);
297 }
298
299 /*
300  * write to the TIFR register. Watch for code that writes "1" to clear
301  * pending interrupts.
302  */
303 static void avr_timer_write_pending(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
304 {
305         avr_timer_t * p = (avr_timer_t *)param;
306         // save old bits values
307         uint8_t ov = avr_regbit_get(avr, p->overflow.raised);
308         uint8_t ic = avr_regbit_get(avr, p->icr.raised);
309         uint8_t cp[AVR_TIMER_COMP_COUNT];
310
311         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++)
312                 cp[compi] = avr_regbit_get(avr, p->comp[compi].interrupt.raised);
313
314         // write the value
315         avr_core_watch_write(avr, addr, v);
316
317         // clear any interrupts & flags
318         avr_clear_interupt_if(avr, &p->overflow, ov);
319         avr_clear_interupt_if(avr, &p->icr, ic);
320
321         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++)
322                 avr_clear_interupt_if(avr, &p->comp[compi].interrupt, cp[compi]);
323 }
324
325 static void avr_timer_irq_icp(struct avr_irq_t * irq, uint32_t value, void * param)
326 {
327         avr_timer_t * p = (avr_timer_t *)param;
328         avr_t * avr = p->io.avr;
329
330         // input capture disabled when ICR is used as top
331         if (p->mode.top == avr_timer_wgm_reg_icr)
332                 return;
333         int bing = 0;
334         if (avr_regbit_get(avr, p->ices)) { // rising edge
335                 if (!irq->value && value)
336                         bing++;
337         } else {        // default, falling edge
338                 if (irq->value && !value)
339                         bing++;
340         }
341         if (!bing)
342                 return;
343         // get current TCNT, copy it to ICR, and raise interrupt
344         uint16_t tcnt = _avr_timer_get_current_tcnt(p);
345         avr->data[p->r_icr] = tcnt;
346         if (p->r_icrh)
347                 avr->data[p->r_icrh] = tcnt >> 8;
348         avr_raise_interrupt(avr, &p->icr);
349 }
350
351 static void avr_timer_reset(avr_io_t * port)
352 {
353         avr_timer_t * p = (avr_timer_t *)port;
354         avr_cycle_timer_cancel(p->io.avr, avr_timer_tov, p);
355         avr_cycle_timer_cancel(p->io.avr, avr_timer_compa, p);
356         avr_cycle_timer_cancel(p->io.avr, avr_timer_compb, p);
357         avr_cycle_timer_cancel(p->io.avr, avr_timer_compc, p);
358
359         // check to see if the comparators have a pin output. If they do,
360         // (try) to get the ioport corresponding IRQ and connect them
361         // they will automagically be triggered when the comparator raises
362         // it's own IRQ
363         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
364                 p->comp[compi].comp_cycles = 0;
365
366                 avr_ioport_getirq_t req = {
367                         .bit = p->comp[compi].com_pin
368                 };
369                 if (avr_ioctl(port->avr, AVR_IOCTL_IOPORT_GETIRQ_REGBIT, &req) > 0) {
370                         // cool, got an IRQ
371 //                      printf("%s-%c COMP%c Connecting PIN IRQ %d\n", __FUNCTION__, p->name, 'A'+compi, req.irq[0]->irq);
372                         avr_connect_irq(&port->irq[TIMER_IRQ_OUT_COMP + compi], req.irq[0]);
373                 }
374         }
375         avr_ioport_getirq_t req = {
376                 .bit = p->icp
377         };
378         if (avr_ioctl(port->avr, AVR_IOCTL_IOPORT_GETIRQ_REGBIT, &req) > 0) {
379                 // cool, got an IRQ for the input capture pin
380 //              printf("%s-%c ICP Connecting PIN IRQ %d\n", __FUNCTION__, p->name, req.irq[0]->irq);
381                 avr_irq_register_notify(req.irq[0], avr_timer_irq_icp, p);
382         }
383
384 }
385
386 static  avr_io_t        _io = {
387         .kind = "timer",
388         .reset = avr_timer_reset,
389 };
390
391 void avr_timer_init(avr_t * avr, avr_timer_t * p)
392 {
393         p->io = _io;
394
395         avr_register_io(avr, &p->io);
396         avr_register_vector(avr, &p->overflow);
397         avr_register_vector(avr, &p->icr);
398
399         // allocate this module's IRQ
400         avr_io_setirqs(&p->io, AVR_IOCTL_TIMER_GETIRQ(p->name), TIMER_IRQ_COUNT, NULL);
401
402         // marking IRQs as "filtered" means they don't propagate if the
403         // new value raised is the same as the last one.. in the case of the
404         // pwm value it makes sense not to bother.
405         p->io.irq[TIMER_IRQ_OUT_PWM0].flags |= IRQ_FLAG_FILTERED;
406         p->io.irq[TIMER_IRQ_OUT_PWM1].flags |= IRQ_FLAG_FILTERED;
407
408         if (p->wgm[0].reg) // these are not present on older AVRs
409                 avr_register_io_write(avr, p->wgm[0].reg, avr_timer_write, p);
410         avr_register_io_write(avr, p->cs[0].reg, avr_timer_write, p);
411
412         // this assumes all the "pending" interrupt bits are in the same
413         // register. Might not be true on all devices ?
414         avr_register_io_write(avr, p->overflow.raised.reg, avr_timer_write_pending, p);
415
416         /*
417          * Even if the timer is 16 bits, we don't care to have watches on the
418          * high bytes because the datasheet says that the low address is always
419          * the trigger.
420          */
421         for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
422                 avr_register_vector(avr, &p->comp[compi].interrupt);
423
424                 if (p->comp[compi].r_ocr) // not all timers have all comparators
425                         avr_register_io_write(avr, p->comp[compi].r_ocr, avr_timer_write_ocr, p);
426         }
427         avr_register_io_write(avr, p->r_tcnt, avr_timer_tcnt_write, p);
428         avr_register_io_read(avr, p->r_tcnt, avr_timer_tcnt_read, p);
429 }