Commit | Line | Data |
---|---|---|
93ac315e MPM |
1 | /* $Id: time.c,v 1.2 2009/03/16 20:32:23 simimeie Exp $ |
2 | * USB interface for ds1820 | |
3 | * This file provides time functions. | |
4 | * (C) Michael "Fox" Meier 2009 | |
5 | */ | |
6 | ||
7 | #include <avr/interrupt.h> | |
8 | #include "time.h" | |
9 | ||
10 | static volatile uint32_t curts[2] = { 0, 0 }; | |
11 | static volatile uint8_t actts = 0; | |
12 | ||
13 | void time_init(void) { | |
14 | /* Set timer0 to run with CLK/1024. That gives 57.2 Overflow interrupts | |
15 | * per second at 15 MHz. */ | |
16 | TCCR0B |= _BV(2) | _BV(0); | |
17 | /* Enable timer interrupts */ | |
18 | TIMSK |= _BV(1); | |
19 | } | |
20 | ||
21 | /* ISR(TIM1_OVF_vect, ISR_NOBLOCK) doesn't work in the goddamn stoneage ubuntu version */ | |
22 | ISR(TIM0_OVF_vect) | |
23 | { | |
24 | sei(); /* Workaround, should use ISR_NOBLOCK instead */ | |
25 | { | |
26 | uint8_t nextts = (~actts) & 0x01; | |
27 | curts[nextts]++; | |
28 | actts = nextts; | |
29 | } | |
30 | } | |
31 | ||
32 | uint32_t gettime(void) | |
33 | { | |
34 | return curts[actts]; | |
35 | } |