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
 * @target  ATmega8
6
 *
7
 * @author  Martin Thomas
8
 * @author  Jimmy Myhrman
9
 *
127 jimmy 10
 * @date    2006-11-19
60 jimmy 11
 */
12
 
13
/*-----------------------------------------------------------------------------
14
 * Includes
15
 *---------------------------------------------------------------------------*/
16
#include <inttypes.h>
17
#include <avr/io.h>
18
#include <avr/interrupt.h>
19
 
20
#include <timebase.h>
21
 
22
 
23
/*-----------------------------------------------------------------------------
24
 * Globals
25
 *---------------------------------------------------------------------------*/
26
volatile uint32_t gMilliSecTick;
27
 
28
 
29
/*-----------------------------------------------------------------------------
30
 * Interrupt Service Routines
31
 *---------------------------------------------------------------------------*/
32
ISR(SIG_OVERFLOW0) {
33
    TCNT0 = TIMEBASE_RELOAD;
34
    gMilliSecTick++;
35
}
36
 
37
 
38
/*-----------------------------------------------------------------------------
39
 * Public Functions
40
 *---------------------------------------------------------------------------*/
41
/**
42
 * Initializes the timebase. The hardware timer interrupt source will be
43
 * enabled, but global interrupts need to be enabled by the application.
44
 */
127 jimmy 45
void Timebase_Init() {
60 jimmy 46
    TCCR0 = (1<<CS01) | (1<<CS00); // prescaler: 64
47
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
48
    TIFR  |= (1<<TOV0);  // clear overflow int.
49
    TIMSK |= (1<<TOIE0); // enable overflow-interrupt
50
}
51
 
52
/**
53
 * Reads the current time.
54
 *
55
 * @return
56
 *      The current time in milliseconds (32bit value).
57
 */
127 jimmy 58
uint32_t Timebase_CurrentTime(void) {
60 jimmy 59
    uint8_t sreg;
60
    uint16_t res;
61
    sreg=SREG;
62
    cli();
63
    res = gMilliSecTick;
64
    SREG=sreg;
65
    return res;
66
}
67
 
68
/**
69
 * Checks how much time has passed since a given timestamp.
70
 *
71
 * @param t0
72
 *      The timestamp.
73
 * @return
74
 *      Number of milliseconds that have passed since t0.
75
 */
127 jimmy 76
uint32_t Timebase_PassedTimeMillis(uint32_t t0) {
60 jimmy 77
    uint8_t sreg;
78
    uint16_t res;
79
    sreg=SREG;
80
    cli();
81
    res = (uint16_t)(gMilliSecTick-t0);
82
    SREG=sreg;
83
    return res;
84
}