aboutsummaryrefslogtreecommitdiff
path: root/timer.c
blob: 498e621cf4d0de5abf0a05edd07c3148efa96756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <avr/io.h>
#include <stdint.h>

#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;
}