BeRTOS
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 
00057 /*
00058  * \name Values for CONFIG_TIMER.
00059  *
00060  * Select which hardware timer interrupt to use for system clock and softtimers.
00061  * $WIZ$ timer_select = "TIMER_USE_TCC0", "TIMER_USE_TCC1", "TIMER_USE_TCD0", "TIMER_USE_TCE0", "TIMER_USE_TCD1", "TIMER_DEFAULT"
00062  */
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 // The XMEGA A Family has one extra timer
00068 #ifdef CPU_AVR_XMEGA_A
00069     #define TIMER_USE_TCD1    5
00070 #endif
00071 
00072 #define TIMER_DEFAULT TIMER_USE_TCC1 ///< Default system timer
00073 
00074 /*
00075  * Hardware dependent timer initialization.
00076  */
00077 #if (CONFIG_TIMER == TIMER_USE_TCC0)
00078     #define TIMER_OVF_VECT  TCC0_OVF_vect
00079     #define TIMERCOUNTER    TCC0
00080 #elif (CONFIG_TIMER == TIMER_USE_TCC1)
00081     #define TIMER_OVF_VECT  TCC1_OVF_vect
00082     #define TIMERCOUNTER    TCC1
00083 #elif (CONFIG_TIMER == TIMER_USE_TCD0)
00084     #define TIMER_OVF_VECT  TCD0_OVF_vect
00085     #define TIMERCOUNTER    TCD0
00086 #elif (CONFIG_TIMER == TIMER_USE_TCE0)
00087     #define TIMER_OVF_VECT  TCE0_OVF_vect
00088     #define TIMERCOUNTER    TCE0
00089 #elif (CONFIG_TIMER == TIMER_USE_TCD1)
00090     #define TIMER_OVF_VECT  TCD1_OVF_vect
00091     #define TIMERCOUNTER    TCD1
00092 #else
00093     #error Unimplemented value for CONFIG_TIMER
00094 #endif /* CONFIG_TIMER */
00095 
00096 //define the Interrupt Service Routine for this Timer Mode
00097 #define DEFINE_TIMER_ISR            DECLARE_ISR_CONTEXT_SWITCH(TIMER_OVF_VECT)
00098 //define the Ticks per second we want
00099 #define TIMER_TICKS_PER_SEC         1000
00100 //define the Prescaler to use, which is dependend on the amount
00101 //of ticks per second, the maximum value for the TOP value of the
00102 //timer (0xFFFF) and the clock frequency.
00103 //The maximum clock frequency is 32Mhz, so as long as the TIMER_TICKS_PER_SEC
00104 //is larger then (about) 500 no prescaler is required.
00105 #define TIMER_PRESCALER             1
00106 //define the TOP/PERIOD value
00107 #define TIMER_PERIOD_VALUE          DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC)
00108 //check if the TIMER_PRESCALER is large enough to accomate for the TIMER_TICKS_PER_SEC
00109 #if TIMER_PERIOD_VALUE > 0xFFFF
00110     #error Timer cannot generate the required Ticks per second, please adjust TIMER_PRESCALER
00111 #endif
00112 //define TIMER_HW_CNT it is used by the timer.c module to determine the 'edge' of the hardware counter
00113 #define TIMER_HW_CNT                TIMER_PERIOD_VALUE
00114 
00115 #define TIMER_HW_HPTICKS_PER_SEC    DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
00116 
00117 // Type of time expressed in ticks of the hardware high-precision timer
00118 typedef uint16_t hptime_t;
00119 #define SIZEOF_HPTIME_T 2
00120 
00121 INLINE hptime_t timer_hw_hpread(void)
00122 {
00123     return (TIMERCOUNTER).CNT;
00124 }
00125 
00126 /* Not needed, IRQ timer flag cleared automatically */
00127 #define timer_hw_irq() do {} while (0)
00128 
00129 /* Not needed, timer IRQ handler called only for timer source */
00130 #define timer_hw_triggered() (true)
00131 
00132 void timer_hw_init(void);
00133 
00134 #endif /* DRV_TIMER_XMEGA_H */