#include #include #include "timer.h" /* On AVR, two 16-bit timers are used: * timer1 for emitting a low frequency IRQ to update the display * timer3 freerunning quickly for timestamping purposes * * XXX note: most of the functions in this module must be called with * interrupts already disabled in order to be safe */ static void timer_init(void) { /* timer1 has /1024 prescaler, 1 Hz comparator val, enable IRQ */ TCCR1B |= (1 << CS10) | (1 << CS12) | (1 << WGM12); TIMSK1 |= (1 << OCIE1A); OCR1A = F_CPU / 1024; /* timer3 has /256 prescaler, no comparartor (free-running with overflow) * and no IRQ enabled. /256 chosen so that the counter doesn't overflow * within a 1 second window */ TCCR3B |= (1 << CS32); } static uint16_t timer_get_value(void) { return TCNT3; } void get_system_timer(struct timer *timer) { timer->init = timer_init; timer->get_time = timer_get_value; }