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
 *---------------------------------------------------------------------------*/
247 eqlazer 30
#if defined(TIMER2)
31
ISR(SIG_OVERFLOW2) {
32
    TCNT2 = TIMEBASE_RELOAD;
33
    gMilliSecTick++;
34
}
35
#else
60 jimmy 36
ISR(SIG_OVERFLOW0) {
37
    TCNT0 = TIMEBASE_RELOAD;
38
    gMilliSecTick++;
39
}
247 eqlazer 40
#endif
60 jimmy 41
 
42
 
43
/*-----------------------------------------------------------------------------
44
 * Public Functions
45
 *---------------------------------------------------------------------------*/
149 jimmy 46
 
60 jimmy 47
/**
48
 * Initializes the timebase. The hardware timer interrupt source will be
49
 * enabled, but global interrupts need to be enabled by the application.
50
 */
127 jimmy 51
void Timebase_Init() {
247 eqlazer 52
#if defined(TIMER2)
53
    #if defined(__AVR_ATmega8__)
54
    #if TIMEBASE_PRESCALE == 64
272 jimmy 55
    TCCR2 = (1<<CS22);  // prescaler: 64
247 eqlazer 56
    #elif TIMEBASE_PRESCALE == 256
272 jimmy 57
    TCCR2 = (1<<CS22) | (1<<CS21); // prescaler: 256
247 eqlazer 58
    #endif
59
    TCNT2 = TIMEBASE_RELOAD; // set initial reload-value
60
    TIFR  |= (1<<TOV2);  // clear overflow int.
61
    TIMSK |= (1<<TOIE2); // enable overflow-interrupt
62
    #endif
149 jimmy 63
 
381 eqlazer 64
    #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
247 eqlazer 65
    #if TIMEBASE_PRESCALE == 64
255 eqlazer 66
    TCCR2B = (1<<CS22); // prescaler: 64
247 eqlazer 67
    #elif TIMEBASE_PRESCALE == 256
255 eqlazer 68
    TCCR2B = (1<<CS22)|(1<<CS21);               // prescaler: 256
247 eqlazer 69
    #endif
70
    TCNT2 = TIMEBASE_RELOAD; // set initial reload-value
71
    TIFR2  |= (1<<TOV2);  // clear overflow int.
72
    TIMSK2 |= (1<<TOIE2); // enable overflow-interrupt
73
    #endif
74
#else /* Use timer0 */
149 jimmy 75
    #if defined(__AVR_ATmega8__)
224 arune 76
    #if TIMEBASE_PRESCALE == 64
219 arune 77
    TCCR0 = (1<<CS01) | (1<<CS00);  // prescaler: 64
224 arune 78
    #elif TIMEBASE_PRESCALE == 256
219 arune 79
    TCCR0 = (1<<CS02);              // prescaler: 256
80
    #endif
60 jimmy 81
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
82
    TIFR  |= (1<<TOV0);  // clear overflow int.
83
    TIMSK |= (1<<TOIE0); // enable overflow-interrupt
149 jimmy 84
    #endif
85
 
381 eqlazer 86
    #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
219 arune 87
    #if TIMEBASE_PRESCALE == 64
88
    TCCR0B = (1<<CS01) | (1<<CS00); // prescaler: 64
89
    #elif TIMEBASE_PRESCALE == 256
90
    TCCR0B = (1<<CS02);             // prescaler: 256
91
    #endif
149 jimmy 92
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
93
    TIFR0  |= (1<<TOV0);  // clear overflow int.
94
    TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt
247 eqlazer 95
    #endif
96
#endif
60 jimmy 97
}
98
 
149 jimmy 99
 
60 jimmy 100
/**
101
 * Reads the current time.
102
 *
103
 * @return
104
 *      The current time in milliseconds (32bit value).
105
 */
127 jimmy 106
uint32_t Timebase_CurrentTime(void) {
60 jimmy 107
    uint8_t sreg;
108
    uint16_t res;
109
    sreg=SREG;
110
    cli();
111
    res = gMilliSecTick;
112
    SREG=sreg;
113
    return res;
114
}
115
 
149 jimmy 116
 
60 jimmy 117
/**
118
 * Checks how much time has passed since a given timestamp.
119
 *
120
 * @param t0
121
 *      The timestamp.
122
 * @return
123
 *      Number of milliseconds that have passed since t0.
124
 */
127 jimmy 125
uint32_t Timebase_PassedTimeMillis(uint32_t t0) {
60 jimmy 126
    uint8_t sreg;
127
    uint16_t res;
128
    sreg=SREG;
129
    cli();
130
    res = (uint16_t)(gMilliSecTick-t0);
131
    SREG=sreg;
132
    return res;
133
}