Subversion Repositories HomeAutomation

Rev

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