Subversion Repositories HomeAutomation

Rev

Rev 381 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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.  *
  8.  * @date    2006-11-19
  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. #if defined(TIMER2)
  31. ISR(SIG_OVERFLOW2) {
  32.     TCNT2 = TIMEBASE_RELOAD;
  33.     gMilliSecTick++;
  34. }
  35. #else
  36. ISR(SIG_OVERFLOW0) {
  37.     TCNT0 = TIMEBASE_RELOAD;
  38.     gMilliSecTick++;
  39. }
  40. #endif
  41.  
  42.  
  43. /*-----------------------------------------------------------------------------
  44.  * Public Functions
  45.  *---------------------------------------------------------------------------*/
  46.  
  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.  */
  51. void Timebase_Init() {
  52. #if defined(TIMER2)
  53.     #if defined(__AVR_ATmega8__)
  54.     #if TIMEBASE_PRESCALE == 64
  55.     TCCR2 = (1<<CS22);  // prescaler: 64
  56.     #elif TIMEBASE_PRESCALE == 256
  57.     TCCR2 = (1<<CS22) | (1<<CS21); // prescaler: 256
  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
  63.    
  64.     #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
  65.     #if TIMEBASE_PRESCALE == 64
  66.     TCCR2B = (1<<CS22); // prescaler: 64
  67.     #elif TIMEBASE_PRESCALE == 256
  68.     TCCR2B = (1<<CS22)|(1<<CS21);               // prescaler: 256
  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 */
  75.     #if defined(__AVR_ATmega8__)
  76.     #if TIMEBASE_PRESCALE == 64
  77.     TCCR0 = (1<<CS01) | (1<<CS00);  // prescaler: 64
  78.     #elif TIMEBASE_PRESCALE == 256
  79.     TCCR0 = (1<<CS02);              // prescaler: 256
  80.     #endif
  81.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  82.     TIFR  |= (1<<TOV0);  // clear overflow int.
  83.     TIMSK |= (1<<TOIE0); // enable overflow-interrupt
  84.     #endif
  85.    
  86.     #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
  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
  92.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  93.     TIFR0  |= (1<<TOV0);  // clear overflow int.
  94.     TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt
  95.     #endif
  96. #endif
  97. }
  98.  
  99.  
  100. /**
  101.  * Reads the current time.
  102.  *
  103.  * @return
  104.  *      The current time in milliseconds (32bit value).
  105.  */
  106. uint32_t Timebase_CurrentTime(void) {
  107.     uint8_t sreg;
  108.     uint16_t res;
  109.     sreg=SREG;
  110.     cli();
  111.     res = gMilliSecTick;
  112.     SREG=sreg;
  113.     return res;
  114. }
  115.  
  116.  
  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.  */
  125. uint32_t Timebase_PassedTimeMillis(uint32_t t0) {
  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. }
  134.