timer_avr.h

Go to the documentation of this file.
00001 
00043 #ifndef DRV_TIMER_AVR_H
00044 #define DRV_TIMER_AVR_H
00045 
00046 #include <hw/hw_cpufreq.h>   /* CPU_FREQ */
00047 
00048 #include "cfg/cfg_timer.h"   /* CONFIG_TIMER */
00049 #include <cfg/compiler.h>    /* uint8_t */
00050 #include <cfg/macros.h>      /* DIV_ROUND */
00051 
00052 #include <avr/io.h>
00053 #include <avr/interrupt.h>
00054 
00062 #define TIMER_ON_OUTPUT_COMPARE0  1
00063 #define TIMER_ON_OVERFLOW1        2
00064 #define TIMER_ON_OUTPUT_COMPARE2  3
00065 #define TIMER_ON_OVERFLOW3        4
00066 
00067 #define TIMER_DEFAULT TIMER_ON_OUTPUT_COMPARE0 
00068 
00069 /*
00070  * Hardware dependent timer initialization.
00071  */
00072 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
00073 
00074     #define TIMER_PRESCALER      64
00075     #define TIMER_HW_BITS        8
00076     #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
00077         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE0A)
00078     #else
00079         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE0)
00080     #endif
00081     #define TIMER_TICKS_PER_SEC  1000
00082     #define TIMER_HW_CNT         OCR_DIVISOR
00083 
00085     typedef uint8_t hptime_t;
00086 
00087     INLINE hptime_t timer_hw_hpread(void)
00088     {
00089         return TCNT0;
00090     }
00091 
00092 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
00093 
00094     #define TIMER_PRESCALER      1
00095     #define TIMER_HW_BITS        8
00096 
00097     #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
00098     #define DEFINE_TIMER_ISR     SIGNAL(SIG_OVERFLOW1)
00099     #define TIMER_TICKS_PER_SEC  DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
00100 
00102     typedef uint16_t hptime_t;
00103 
00104     INLINE hptime_t timer_hw_hpread(void)
00105     {
00106         return TCNT1;
00107     }
00108 
00109 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
00110 
00111     #define TIMER_PRESCALER      64
00112     #define TIMER_HW_BITS        8
00113     #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
00114         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE2A)
00115     #else
00116         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE2)
00117     #endif
00118     #define TIMER_TICKS_PER_SEC  1000
00119 
00120     #define TIMER_HW_CNT         OCR_DIVISOR
00121 
00123     typedef uint8_t hptime_t;
00124 
00125     INLINE hptime_t timer_hw_hpread(void)
00126     {
00127         return TCNT2;
00128     }
00129 
00130 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
00131 
00132     #define TIMER_PRESCALER      1
00133     #define TIMER_HW_BITS        8
00134 
00135     #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
00136     #define DEFINE_TIMER_ISR     SIGNAL(SIG_OVERFLOW3)
00137     #define TIMER_TICKS_PER_SEC  DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
00138 
00140     typedef uint16_t hptime_t;
00141 
00142     INLINE hptime_t timer_hw_hpread(void)
00143     {
00144         return TCNT3;
00145     }
00146 
00147 #else
00148 
00149     #error Unimplemented value for CONFIG_TIMER
00150 #endif /* CONFIG_TIMER */
00151 
00152 
00154 #define TIMER_HW_HPTICKS_PER_SEC  DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
00155 
00160 #define OCR_DIVISOR  (DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC) - 1)
00161 
00163 #define timer_hw_irq() do {} while (0)
00164 
00166 #define timer_hw_triggered() (true)
00167 
00168 void timer_hw_init(void);
00169 
00170 #endif /* DRV_TIMER_AVR_H */