Subversion Repositories HomeAutomation

Rev

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__)
219 arune 47
    #if TIMEBASE_PRESCALE = 64
48
    TCCR0 = (1<<CS01) | (1<<CS00);  // prescaler: 64
49
    #elif TIMEBASE_PRESCALE = 256
50
    TCCR0 = (1<<CS02);              // prescaler: 256
51
    #endif
60 jimmy 52
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
53
    TIFR  |= (1<<TOV0);  // clear overflow int.
54
    TIMSK |= (1<<TOIE0); // enable overflow-interrupt
149 jimmy 55
    #endif
56
 
57
    #if defined(__AVR_ATmega88__)
219 arune 58
    #if TIMEBASE_PRESCALE == 64
59
    TCCR0B = (1<<CS01) | (1<<CS00); // prescaler: 64
60
    #elif TIMEBASE_PRESCALE == 256
61
    TCCR0B = (1<<CS02);             // prescaler: 256
62
    #endif
149 jimmy 63
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
64
    TIFR0  |= (1<<TOV0);  // clear overflow int.
65
    TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt
66
    #endif
60 jimmy 67
}
68
 
149 jimmy 69
 
60 jimmy 70
/**
71
 * Reads the current time.
72
 *
73
 * @return
74
 *      The current time in milliseconds (32bit value).
75
 */
127 jimmy 76
uint32_t Timebase_CurrentTime(void) {
60 jimmy 77
    uint8_t sreg;
78
    uint16_t res;
79
    sreg=SREG;
80
    cli();
81
    res = gMilliSecTick;
82
    SREG=sreg;
83
    return res;
84
}
85
 
149 jimmy 86
 
60 jimmy 87
/**
88
 * Checks how much time has passed since a given timestamp.
89
 *
90
 * @param t0
91
 *      The timestamp.
92
 * @return
93
 *      Number of milliseconds that have passed since t0.
94
 */
127 jimmy 95
uint32_t Timebase_PassedTimeMillis(uint32_t t0) {
60 jimmy 96
    uint8_t sreg;
97
    uint16_t res;
98
    sreg=SREG;
99
    cli();
100
    res = (uint16_t)(gMilliSecTick-t0);
101
    SREG=sreg;
102
    return res;
103
}