1 /* $Id: timers.c,v 1.1 2010/06/27 22:18:26 simimeie Exp $
2 * Functions for timing.
3 * This mainly allows you to get a timestamp value
7 #include <avr/interrupt.h>
11 static volatile uint16_t ticks = 0;
13 /* FIXME TCNT1H TCNT1L */
15 /* Executes every 65536 cpu cycles when the timer overflows */
21 uint16_t getticks(void)
30 uint16_t getticks_noirq(void)
35 struct timestamp gettimestamp(void)
37 uint16_t cou1, cou2, tick1;
39 /* Low byte must be read first for 16bit registers */
41 cou1 |= (uint16_t)(TCNT1H) << 8;
42 tick1 = getticks(); /* enables interrupts! */
44 cou2 |= (uint16_t)(TCNT1H) << 8;
45 if (cou1 > cou2) { /* An overflow occured in between */
46 /* get new ticks value - we could already have it, but we do not know
47 * for sure. If we get another one now, we do - because there is no
48 * way we're going to need another 65k cpu cycles. */
49 res.ticks = getticks();
50 } else { /* no overflow. */
57 struct timestamp gettimestamp_noirq(void)
59 uint16_t cou1, cou2; uint8_t ovf;
61 /* Low byte must be read first for 16bit registers */
63 cou1 |= (uint16_t)(TCNT1H) << 8;
64 /* Check if there is an overflow pending */
65 ovf = TIFR1 & _BV(TOV1);
67 /* Overflow pending, reread the counter to be sure */
69 cou2 |= (uint16_t)(TCNT1H) << 8;
70 res.ticks = ticks + 1;
79 void timers_init(void)
81 /* Enable counter1, no prescaling (1 increment per clock cycle!) */
83 /* Enable interrupt when timer overflows (at 65536) */