4 Handles the 8 bits and 16 bits AVR timer.
9 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
11 This file is part of simavr.
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.
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.
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/>.
28 #include "avr_timer.h"
29 #include "avr_ioport.h"
32 * The timers are /always/ 16 bits here, if the higher byte register
33 * is specified it's just added.
35 static uint16_t _timer_get_ocr(avr_timer_t * p, int compi)
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);
40 static uint16_t _timer_get_tcnt(avr_timer_t * p)
42 return p->io.avr->data[p->r_tcnt] |
43 (p->r_tcnth ? (p->io.avr->data[p->r_tcnth] << 8) : 0);
45 static uint16_t _timer_get_icr(avr_timer_t * p)
47 return p->io.avr->data[p->r_icr] |
48 (p->r_tcnth ? (p->io.avr->data[p->r_icrh] << 8) : 0);
50 static avr_cycle_count_t avr_timer_comp(avr_timer_t *p, avr_cycle_count_t when, uint8_t comp)
52 avr_t * avr = p->io.avr;
53 avr_raise_interrupt(avr, &p->comp[comp].interrupt);
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];
60 case avr_timer_com_normal: // Normal mode OCnA disconnected
62 case avr_timer_com_toggle: // Toggle OCnA on compare match
63 if (p->comp[comp].com_pin.reg) // we got a physical pin
65 AVR_IOPORT_OUTPUT | (avr_regbit_get(avr, p->comp[comp].com_pin) ? 0 : 1));
66 else // no pin, toggle the IRQ anyway
68 p->io.irq[TIMER_IRQ_OUT_COMP + comp].value ? 0 : 1);
70 case avr_timer_com_clear:
71 avr_raise_irq(irq, 0);
73 case avr_timer_com_set:
74 avr_raise_irq(irq, 1);
78 return p->tov_cycles ? 0 :
79 p->comp[comp].comp_cycles ?
80 when + p->comp[comp].comp_cycles : 0;
83 static avr_cycle_count_t avr_timer_compa(struct avr_t * avr, avr_cycle_count_t when, void * param)
85 return avr_timer_comp((avr_timer_t*)param, when, AVR_TIMER_COMPA);
88 static avr_cycle_count_t avr_timer_compb(struct avr_t * avr, avr_cycle_count_t when, void * param)
90 return avr_timer_comp((avr_timer_t*)param, when, AVR_TIMER_COMPB);
93 static avr_cycle_count_t avr_timer_compc(struct avr_t * avr, avr_cycle_count_t when, void * param)
95 return avr_timer_comp((avr_timer_t*)param, when, AVR_TIMER_COMPC);
99 static avr_cycle_count_t avr_timer_tov(struct avr_t * avr, avr_cycle_count_t when, void * param)
101 avr_timer_t * p = (avr_timer_t *)param;
102 int start = p->tov_base == 0;
105 avr_raise_interrupt(avr, &p->overflow);
108 static const avr_cycle_timer_t dispatch[AVR_TIMER_COMP_COUNT] =
109 { avr_timer_compa, avr_timer_compb, avr_timer_compc };
111 for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
112 if (p->comp[compi].comp_cycles) {
113 if (p->comp[compi].comp_cycles < p->tov_cycles)
114 avr_cycle_timer_register(avr,
115 p->comp[compi].comp_cycles,
117 else if (p->tov_cycles == p->comp[compi].comp_cycles && !start)
118 dispatch[compi](avr, when, param);
122 return when + p->tov_cycles;
125 static uint16_t _avr_timer_get_current_tcnt(avr_timer_t * p)
127 avr_t * avr = p->io.avr;
129 uint64_t when = avr->cycle - p->tov_base;
131 return (when * (((uint32_t)p->tov_top)+1)) / p->tov_cycles;
136 static uint8_t avr_timer_tcnt_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
138 avr_timer_t * p = (avr_timer_t *)param;
139 // made to trigger potential watchpoints
141 uint16_t tcnt = _avr_timer_get_current_tcnt(p);
143 avr->data[p->r_tcnt] = tcnt;
145 avr->data[p->r_tcnth] = tcnt >> 8;
147 return avr_core_watch_read(avr, addr);
150 static void avr_timer_tcnt_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
152 avr_timer_t * p = (avr_timer_t *)param;
153 avr_core_watch_write(avr, addr, v);
154 uint16_t tcnt = _timer_get_tcnt(p);
159 if (tcnt >= p->tov_top)
162 // this involves some magicking
163 // cancel the current timers, recalculate the "base" we should be at, reset the
164 // timer base as it should, and re-schedule the timers using that base.
166 avr_cycle_timer_cancel(avr, avr_timer_tov, p);
167 avr_cycle_timer_cancel(avr, avr_timer_compa, p);
168 avr_cycle_timer_cancel(avr, avr_timer_compb, p);
169 avr_cycle_timer_cancel(avr, avr_timer_compc, p);
171 uint64_t cycles = (tcnt * p->tov_cycles) / p->tov_top;
173 // printf("%s-%c %d/%d -- cycles %d/%d\n", __FUNCTION__, p->name, tcnt, p->tov_top, (uint32_t)cycles, (uint32_t)p->tov_cycles);
175 // this reset the timers bases to the new base
176 if (p->tov_cycles > 1) {
177 avr_cycle_timer_register(avr, p->tov_cycles - cycles, avr_timer_tov, p);
179 avr_timer_tov(avr, avr->cycle - cycles, p);
182 // tcnt = ((avr->cycle - p->tov_base) * p->tov_top) / p->tov_cycles;
183 // printf("%s-%c new tnt derive to %d\n", __FUNCTION__, p->name, tcnt);
186 static void avr_timer_configure(avr_timer_t * p, uint32_t clock, uint32_t top)
188 float t = clock / (float)(top+1);
189 float frequency = p->io.avr->frequency;
194 p->tov_cycles = frequency / t; // avr_hz_to_cycles(frequency, t);
196 AVR_LOG(p->io.avr, LOG_TRACE, "TIMER: %s-%c TOP %.2fHz = %d cycles\n",
197 __FUNCTION__, p->name, t, (int)p->tov_cycles);
199 for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
200 if (!p->comp[compi].r_ocr)
202 uint32_t ocr = _timer_get_ocr(p, compi);
203 float fc = clock / (float)(ocr+1);
205 p->comp[compi].comp_cycles = 0;
206 // printf("%s-%c clock %d top %d OCR%c %d\n", __FUNCTION__, p->name, clock, top, 'A'+compi, ocr);
208 if (ocr && ocr <= top) {
209 p->comp[compi].comp_cycles = frequency / fc; // avr_hz_to_cycles(p->io.avr, fa);
210 AVR_LOG(p->io.avr, LOG_TRACE, "TIMER: %s-%c %c %.2fHz = %d cycles\n", __FUNCTION__, p->name,
211 'A'+compi, fc, (int)p->comp[compi].comp_cycles);
215 if (p->tov_cycles > 1) {
216 avr_cycle_timer_register(p->io.avr, p->tov_cycles, avr_timer_tov, p);
217 // calling it once, with when == 0 tells it to arm the A/B/C timers if needed
219 avr_timer_tov(p->io.avr, p->io.avr->cycle, p);
223 static void avr_timer_reconfigure(avr_timer_t * p)
225 avr_t * avr = p->io.avr;
227 avr_timer_wgm_t zero={0};
230 p->comp[AVR_TIMER_COMPA].comp_cycles = 0;
231 p->comp[AVR_TIMER_COMPB].comp_cycles = 0;
232 p->comp[AVR_TIMER_COMPC].comp_cycles = 0;
235 avr_cycle_timer_cancel(avr, avr_timer_tov, p);
236 avr_cycle_timer_cancel(avr, avr_timer_compa, p);
237 avr_cycle_timer_cancel(avr, avr_timer_compb, p);
238 avr_cycle_timer_cancel(avr, avr_timer_compc, p);
240 long clock = avr->frequency;
242 // only can exists on "asynchronous" 8 bits timers
243 if (avr_regbit_get(avr, p->as2))
246 uint8_t cs = avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs));
248 AVR_LOG(avr, LOG_TRACE, "TIMER: %s-%c clock turned off\n", __FUNCTION__, p->name);
252 uint8_t mode = avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm));
253 uint8_t cs_div = p->cs_div[cs];
254 uint32_t f = clock >> cs_div;
256 p->mode = p->wgm_op[mode];
257 //printf("%s-%c clock %d, div %d(/%d) = %d ; mode %d\n", __FUNCTION__, p->name, clock, cs, 1 << cs_div, f, mode);
258 switch (p->mode.kind) {
259 case avr_timer_wgm_normal:
260 avr_timer_configure(p, f, (1 << p->mode.size) - 1);
262 case avr_timer_wgm_fc_pwm:
263 avr_timer_configure(p, f, (1 << p->mode.size) - 1);
265 case avr_timer_wgm_ctc: {
266 avr_timer_configure(p, f, _timer_get_ocr(p, AVR_TIMER_COMPA));
268 case avr_timer_wgm_pwm: {
269 uint16_t top = p->mode.top == avr_timer_wgm_reg_ocra ? _timer_get_ocr(p, AVR_TIMER_COMPA) : _timer_get_icr(p);
270 avr_timer_configure(p, f, top);
272 case avr_timer_wgm_fast_pwm:
273 avr_timer_configure(p, f, (1 << p->mode.size) - 1);
276 AVR_LOG(avr, LOG_WARNING, "TIMER: %s-%c unsupported timer mode wgm=%d (%d)\n",
277 __FUNCTION__, p->name, mode, p->mode.kind);
281 static void avr_timer_write_ocr(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
283 avr_timer_t * p = (avr_timer_t *)param;
284 uint16_t oldv[AVR_TIMER_COMP_COUNT];
287 /* check to see if the OCR values actually changed */
288 for (int oi = 0; oi < AVR_TIMER_COMP_COUNT; oi++)
289 oldv[oi] = _timer_get_ocr(p, oi);
290 avr_core_watch_write(avr, addr, v);
291 for (int oi = 0; oi < AVR_TIMER_COMP_COUNT; oi++)
292 if (oldv[oi] != _timer_get_ocr(p, oi)) {
297 switch (p->mode.kind) {
298 case avr_timer_wgm_normal:
299 avr_timer_reconfigure(p);
301 case avr_timer_wgm_fc_pwm: // OCR is not used here
302 avr_timer_reconfigure(p);
304 case avr_timer_wgm_ctc:
305 avr_timer_reconfigure(p);
307 case avr_timer_wgm_pwm:
308 if (p->mode.top != avr_timer_wgm_reg_ocra) {
309 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM0, _timer_get_ocr(p, AVR_TIMER_COMPA));
310 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM1, _timer_get_ocr(p, AVR_TIMER_COMPB));
313 case avr_timer_wgm_fast_pwm:
315 avr_timer_reconfigure(p);
316 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM0, _timer_get_ocr(p, AVR_TIMER_COMPA));
317 avr_raise_irq(p->io.irq + TIMER_IRQ_OUT_PWM1, _timer_get_ocr(p, AVR_TIMER_COMPB));
320 AVR_LOG(avr, LOG_WARNING, "TIMER: %s-%c mode %d UNSUPPORTED\n", __FUNCTION__, p->name, p->mode.kind);
321 avr_timer_reconfigure(p);
326 static void avr_timer_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
328 avr_timer_t * p = (avr_timer_t *)param;
330 uint8_t as2 = avr_regbit_get(avr, p->as2);
331 uint8_t cs = avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs));
332 uint8_t mode = avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm));
334 avr_core_watch_write(avr, addr, v);
336 // only reconfigure the timer if "relevant" bits have changed
337 // this prevent the timer reset when changing the edge detector
338 // or other minor bits
339 if (avr_regbit_get_array(avr, p->cs, ARRAY_SIZE(p->cs)) != cs ||
340 avr_regbit_get_array(avr, p->wgm, ARRAY_SIZE(p->wgm)) != mode ||
341 avr_regbit_get(avr, p->as2) != as2) {
342 avr_timer_reconfigure(p);
347 * write to the TIFR register. Watch for code that writes "1" to clear
348 * pending interrupts.
350 static void avr_timer_write_pending(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
352 avr_timer_t * p = (avr_timer_t *)param;
353 // save old bits values
354 uint8_t ov = avr_regbit_get(avr, p->overflow.raised);
355 uint8_t ic = avr_regbit_get(avr, p->icr.raised);
356 uint8_t cp[AVR_TIMER_COMP_COUNT];
358 for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++)
359 cp[compi] = avr_regbit_get(avr, p->comp[compi].interrupt.raised);
362 avr_core_watch_write(avr, addr, v);
364 // clear any interrupts & flags
365 avr_clear_interrupt_if(avr, &p->overflow, ov);
366 avr_clear_interrupt_if(avr, &p->icr, ic);
368 for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++)
369 avr_clear_interrupt_if(avr, &p->comp[compi].interrupt, cp[compi]);
372 static void avr_timer_irq_icp(struct avr_irq_t * irq, uint32_t value, void * param)
374 avr_timer_t * p = (avr_timer_t *)param;
375 avr_t * avr = p->io.avr;
377 // input capture disabled when ICR is used as top
378 if (p->mode.top == avr_timer_wgm_reg_icr)
381 if (avr_regbit_get(avr, p->ices)) { // rising edge
382 if (!irq->value && value)
384 } else { // default, falling edge
385 if (irq->value && !value)
390 // get current TCNT, copy it to ICR, and raise interrupt
391 uint16_t tcnt = _avr_timer_get_current_tcnt(p);
392 avr->data[p->r_icr] = tcnt;
394 avr->data[p->r_icrh] = tcnt >> 8;
395 avr_raise_interrupt(avr, &p->icr);
398 static void avr_timer_reset(avr_io_t * port)
400 avr_timer_t * p = (avr_timer_t *)port;
401 avr_cycle_timer_cancel(p->io.avr, avr_timer_tov, p);
402 avr_cycle_timer_cancel(p->io.avr, avr_timer_compa, p);
403 avr_cycle_timer_cancel(p->io.avr, avr_timer_compb, p);
404 avr_cycle_timer_cancel(p->io.avr, avr_timer_compc, p);
406 // check to see if the comparators have a pin output. If they do,
407 // (try) to get the ioport corresponding IRQ and connect them
408 // they will automagically be triggered when the comparator raises
410 for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
411 p->comp[compi].comp_cycles = 0;
413 avr_ioport_getirq_t req = {
414 .bit = p->comp[compi].com_pin
416 if (avr_ioctl(port->avr, AVR_IOCTL_IOPORT_GETIRQ_REGBIT, &req) > 0) {
418 // printf("%s-%c COMP%c Connecting PIN IRQ %d\n", __FUNCTION__, p->name, 'A'+compi, req.irq[0]->irq);
419 avr_connect_irq(&port->irq[TIMER_IRQ_OUT_COMP + compi], req.irq[0]);
422 avr_ioport_getirq_t req = {
425 if (avr_ioctl(port->avr, AVR_IOCTL_IOPORT_GETIRQ_REGBIT, &req) > 0) {
426 // cool, got an IRQ for the input capture pin
427 // printf("%s-%c ICP Connecting PIN IRQ %d\n", __FUNCTION__, p->name, req.irq[0]->irq);
428 avr_irq_register_notify(req.irq[0], avr_timer_irq_icp, p);
433 static const char * irq_names[TIMER_IRQ_COUNT] = {
434 [TIMER_IRQ_OUT_PWM0] = "8>pwm0",
435 [TIMER_IRQ_OUT_PWM1] = "8>pwm1",
436 [TIMER_IRQ_OUT_COMP + 0] = ">compa",
437 [TIMER_IRQ_OUT_COMP + 1] = ">compb",
438 [TIMER_IRQ_OUT_COMP + 2] = ">compc",
441 static avr_io_t _io = {
443 .reset = avr_timer_reset,
444 .irq_names = irq_names,
447 void avr_timer_init(avr_t * avr, avr_timer_t * p)
451 avr_register_io(avr, &p->io);
452 avr_register_vector(avr, &p->overflow);
453 avr_register_vector(avr, &p->icr);
455 // allocate this module's IRQ
456 avr_io_setirqs(&p->io, AVR_IOCTL_TIMER_GETIRQ(p->name), TIMER_IRQ_COUNT, NULL);
458 // marking IRQs as "filtered" means they don't propagate if the
459 // new value raised is the same as the last one.. in the case of the
460 // pwm value it makes sense not to bother.
461 p->io.irq[TIMER_IRQ_OUT_PWM0].flags |= IRQ_FLAG_FILTERED;
462 p->io.irq[TIMER_IRQ_OUT_PWM1].flags |= IRQ_FLAG_FILTERED;
464 if (p->wgm[0].reg) // these are not present on older AVRs
465 avr_register_io_write(avr, p->wgm[0].reg, avr_timer_write, p);
466 avr_register_io_write(avr, p->cs[0].reg, avr_timer_write, p);
468 // this assumes all the "pending" interrupt bits are in the same
469 // register. Might not be true on all devices ?
470 avr_register_io_write(avr, p->overflow.raised.reg, avr_timer_write_pending, p);
473 * Even if the timer is 16 bits, we don't care to have watches on the
474 * high bytes because the datasheet says that the low address is always
477 for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
478 avr_register_vector(avr, &p->comp[compi].interrupt);
480 if (p->comp[compi].r_ocr) // not all timers have all comparators
481 avr_register_io_write(avr, p->comp[compi].r_ocr, avr_timer_write_ocr, p);
483 avr_register_io_write(avr, p->r_tcnt, avr_timer_tcnt_write, p);
484 avr_register_io_read(avr, p->r_tcnt, avr_timer_tcnt_read, p);