Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 60 | jimmy | 1 | /** |
| 2 | * Timebase software module. Utilizes timer0 to provide a 32bit timebase with |
||
| 3 | * millisecond-granularity to the application. |
||
| 4 | * |
||
| 5 | * @author Martin Thomas |
||
| 6 | * @author Jimmy Myhrman |
||
| 7 | * |
||
| 127 | jimmy | 8 | * @date 2006-11-19 |
| 60 | jimmy | 9 | */ |
| 10 | |||
| 11 | /*----------------------------------------------------------------------------- |
||
| 12 | * Includes |
||
| 13 | *---------------------------------------------------------------------------*/ |
||
| 14 | #include <inttypes.h> |
||
| 15 | #include <avr/io.h> |
||
| 16 | #include <avr/interrupt.h> |
||
| 17 | |||
| 18 | #include <timebase.h> |
||
| 19 | |||
| 20 | |||
| 21 | /*----------------------------------------------------------------------------- |
||
| 22 | * Globals |
||
| 23 | *---------------------------------------------------------------------------*/ |
||
| 24 | volatile uint32_t gMilliSecTick; |
||
| 25 | |||
| 26 | |||
| 27 | /*----------------------------------------------------------------------------- |
||
| 28 | * Interrupt Service Routines |
||
| 29 | *---------------------------------------------------------------------------*/ |
||
| 30 | ISR(SIG_OVERFLOW0) { |
||
| 31 | TCNT0 = TIMEBASE_RELOAD; |
||
| 32 | gMilliSecTick++; |
||
| 33 | } |
||
| 34 | |||
| 35 | |||
| 36 | /*----------------------------------------------------------------------------- |
||
| 37 | * Public Functions |
||
| 38 | *---------------------------------------------------------------------------*/ |
||
| 149 | jimmy | 39 | |
| 60 | jimmy | 40 | /** |
| 41 | * Initializes the timebase. The hardware timer interrupt source will be |
||
| 42 | * enabled, but global interrupts need to be enabled by the application. |
||
| 43 | */ |
||
| 127 | jimmy | 44 | void Timebase_Init() { |
| 149 | jimmy | 45 | |
| 46 | #if defined(__AVR_ATmega8__) |
||
| 60 | jimmy | 47 | TCCR0 = (1<<CS01) | (1<<CS00); // prescaler: 64 |
| 48 | TCNT0 = TIMEBASE_RELOAD; // set initial reload-value |
||
| 49 | TIFR |= (1<<TOV0); // clear overflow int. |
||
| 50 | TIMSK |= (1<<TOIE0); // enable overflow-interrupt |
||
| 149 | jimmy | 51 | #endif |
| 52 | |||
| 53 | #if defined(__AVR_ATmega88__) |
||
| 150 | arune | 54 | TCCR0B = (1<<CS01) | (1<<CS00); // prescaler: 64 |
| 149 | jimmy | 55 | TCNT0 = TIMEBASE_RELOAD; // set initial reload-value |
| 56 | TIFR0 |= (1<<TOV0); // clear overflow int. |
||
| 57 | TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt |
||
| 58 | #endif |
||
| 60 | jimmy | 59 | } |
| 60 | |||
| 149 | jimmy | 61 | |
| 60 | jimmy | 62 | /** |
| 63 | * Reads the current time. |
||
| 64 | * |
||
| 65 | * @return |
||
| 66 | * The current time in milliseconds (32bit value). |
||
| 67 | */ |
||
| 127 | jimmy | 68 | uint32_t Timebase_CurrentTime(void) { |
| 60 | jimmy | 69 | uint8_t sreg; |
| 70 | uint16_t res; |
||
| 71 | sreg=SREG; |
||
| 72 | cli(); |
||
| 73 | res = gMilliSecTick; |
||
| 74 | SREG=sreg; |
||
| 75 | return res; |
||
| 76 | } |
||
| 77 | |||
| 149 | jimmy | 78 | |
| 60 | jimmy | 79 | /** |
| 80 | * Checks how much time has passed since a given timestamp. |
||
| 81 | * |
||
| 82 | * @param t0 |
||
| 83 | * The timestamp. |
||
| 84 | * @return |
||
| 85 | * Number of milliseconds that have passed since t0. |
||
| 86 | */ |
||
| 127 | jimmy | 87 | uint32_t Timebase_PassedTimeMillis(uint32_t t0) { |
| 60 | jimmy | 88 | uint8_t sreg; |
| 89 | uint16_t res; |
||
| 90 | sreg=SREG; |
||
| 91 | cli(); |
||
| 92 | res = (uint16_t)(gMilliSecTick-t0); |
||
| 93 | SREG=sreg; |
||
| 94 | return res; |
||
| 95 | } |