timer_xmega.h
Go to the documentation of this file.
00001
00045 #ifndef DRV_TIMER_XMEGA_H
00046 #define DRV_TIMER_XMEGA_H
00047 
00048 #include <hw/hw_cpufreq.h>   /* CPU_FREQ */
00049
00050 #include "cfg/cfg_timer.h"   /* CONFIG_TIMER */
00051 #include <cfg/compiler.h>    /* uint8_t */
00052 #include <cfg/macros.h>      /* DIV_ROUND */
00053
00054 #include <avr/io.h>
00055 #include <avr/interrupt.h>
00056
00063 #define TIMER_USE_TCC0    1
00064 #define TIMER_USE_TCC1    2
00065 #define TIMER_USE_TCD0    3
00066 #define TIMER_USE_TCE0    4
00067 #if CPU_AVR_XMEGA_A4 || CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
00068     #define TIMER_USE_TCD1  5
00069 #else
00070     #define TIMER_USE_TCD1  0
00071 #endif
00072 #if CPU_AVR_XMEGA_D3 || CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
00073     #define TIMER_USE_TCF0  6
00074 #else
00075     #define TIMER_USE_TCF0  0
00076 #endif
00077 #if CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
00078     #define TIMER_USE_TCE1  7
00079 #else
00080     #define TIMER_USE_TCE1  0
00081 #endif
00082 #if CPU_AVR_XMEGA_A1
00083     #define TIMER_USE_TCF1  8
00084 #else
00085     #define TIMER_USE_TCF1  0
00086 #endif
00087 
00088 #define TIMER_DEFAULT TIMER_USE_TCC1 ///< Default system timer
00089 
00090 /*
00091  * Hardware dependent timer initialization.
00092  */
00093 #if (CONFIG_TIMER == TIMER_USE_TCC0)
00094     #define TIMER_OVF_VECT  TCC0_OVF_vect
00095     #define TIMERCOUNTER    TCC0
00096 #elif (CONFIG_TIMER == TIMER_USE_TCC1)
00097     #define TIMER_OVF_VECT  TCC1_OVF_vect
00098     #define TIMERCOUNTER    TCC1
00099 #elif (CONFIG_TIMER == TIMER_USE_TCD0)
00100     #define TIMER_OVF_VECT  TCD0_OVF_vect
00101     #define TIMERCOUNTER    TCD0
00102 #elif (CONFIG_TIMER == TIMER_USE_TCD1)
00103     #define TIMER_OVF_VECT  TCD1_OVF_vect
00104     #define TIMERCOUNTER    TCD1
00105 #elif (CONFIG_TIMER == TIMER_USE_TCE0)
00106     #define TIMER_OVF_VECT  TCE0_OVF_vect
00107     #define TIMERCOUNTER    TCE0
00108 #elif (CONFIG_TIMER == TIMER_USE_TCE1)
00109     #define TIMER_OVF_VECT  TCE1_OVF_vect
00110     #define TIMERCOUNTER    TCE1
00111 #elif (CONFIG_TIMER == TIMER_USE_TCF0)
00112     #define TIMER_OVF_VECT  TCF0_OVF_vect
00113     #define TIMERCOUNTER    TCF0
00114 #elif (CONFIG_TIMER == TIMER_USE_TCF1)
00115     #define TIMER_OVF_VECT  TCF1_OVF_vect
00116     #define TIMERCOUNTER    TCF1
00117 #else
00118     #error Unimplemented value for CONFIG_TIMER
00119 #endif /* CONFIG_TIMER */
00120
00121 //define the Interrupt Service Routine for this Timer Mode
00122 #define DEFINE_TIMER_ISR            DECLARE_ISR_CONTEXT_SWITCH(TIMER_OVF_VECT)
00123 //define the Ticks per second we want
00124 #define TIMER_TICKS_PER_SEC         1000
00125 //define the Prescaler to use, which is dependend on the amount
00126 //of ticks per second, the maximum value for the TOP value of the
00127 //timer (0xFFFF) and the clock frequency.
00128 //The maximum clock frequency is 32Mhz, so as long as the TIMER_TICKS_PER_SEC
00129 //is larger then (about) 500 no prescaler is required.
00130 #define TIMER_PRESCALER             1
00131 //define the TOP/PERIOD value
00132 #define TIMER_PERIOD_VALUE          DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC)
00133 //check if the TIMER_PRESCALER is large enough to accomate for the TIMER_TICKS_PER_SEC
00134 #if TIMER_PERIOD_VALUE > 0xFFFF
00135     #error Timer cannot generate the required Ticks per second, please adjust TIMER_PRESCALER
00136 #endif
00137 //define TIMER_HW_CNT it is used by the timer.c module to determine the 'edge' of the hardware counter
00138 #define TIMER_HW_CNT                TIMER_PERIOD_VALUE
00139 
00140 #define TIMER_HW_HPTICKS_PER_SEC    DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
00141 
00142 // Type of time expressed in ticks of the hardware high-precision timer
00143 typedef uint16_t hptime_t;
00144 #define SIZEOF_HPTIME_T 2
00145 
00146 INLINE hptime_t timer_hw_hpread(void)
00147 {
00148     return (TIMERCOUNTER).CNT;
00149 }
00150
00151 /* Not needed, IRQ timer flag cleared automatically */
00152 #define timer_hw_irq() do {} while (0)
00153 
00154 /* Not needed, timer IRQ handler called only for timer source */
00155 #define timer_hw_triggered() (true)
00156 
00157 void timer_hw_init(void);
00158
00159 #endif /* DRV_TIMER_XMEGA_H */