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     #define SIZEOF_HPTIME_T 1
00087 
00088     INLINE hptime_t timer_hw_hpread(void)
00089     {
00090         return TCNT0;
00091     }
00092 
00093 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
00094 
00095     #define TIMER_PRESCALER      1
00096     #define TIMER_HW_BITS        8
00097 
00098     #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
00099     #define DEFINE_TIMER_ISR     SIGNAL(SIG_OVERFLOW1)
00100     #define TIMER_TICKS_PER_SEC  DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
00101 
00103     typedef uint16_t hptime_t;
00104     #define SIZEOF_HPTIME_T 2
00105 
00106     INLINE hptime_t timer_hw_hpread(void)
00107     {
00108         return TCNT1;
00109     }
00110 
00111 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
00112 
00113     #define TIMER_PRESCALER      64
00114     #define TIMER_HW_BITS        8
00115     #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
00116         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE2A)
00117     #else
00118         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE2)
00119     #endif
00120     #define TIMER_TICKS_PER_SEC  1000
00121 
00122     #define TIMER_HW_CNT         OCR_DIVISOR
00123 
00125     typedef uint8_t hptime_t;
00126     #define SIZEOF_HPTIME_T 1
00127 
00128     INLINE hptime_t timer_hw_hpread(void)
00129     {
00130         return TCNT2;
00131     }
00132 
00133 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
00134 
00135     #define TIMER_PRESCALER      1
00136     #define TIMER_HW_BITS        8
00137 
00138     #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
00139     #define DEFINE_TIMER_ISR     SIGNAL(SIG_OVERFLOW3)
00140     #define TIMER_TICKS_PER_SEC  DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
00141 
00143     typedef uint16_t hptime_t;
00144     #define SIZEOF_HPTIME_T 2
00145 
00146     INLINE hptime_t timer_hw_hpread(void)
00147     {
00148         return TCNT3;
00149     }
00150 
00151 #else
00152 
00153     #error Unimplemented value for CONFIG_TIMER
00154 #endif /* CONFIG_TIMER */
00155 
00156 
00158 #define TIMER_HW_HPTICKS_PER_SEC  DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
00159 
00164 #define OCR_DIVISOR  (DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC) - 1)
00165 
00167 #define timer_hw_irq() do {} while (0)
00168 
00170 #define timer_hw_triggered() (true)
00171 
00172 void timer_hw_init(void);
00173 
00174 #endif /* DRV_TIMER_AVR_H */