timer_xmega.c
Go to the documentation of this file.00001
00047 #include <drv/timer_xmega.h>
00048 #include <cfg/macros.h>
00049
00050 #include <cpu/types.h>
00051 #include <cpu/irq.h>
00052
00053 #include <avr/io.h>
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #if TC0_CLKSEL_gm != TC1_CLKSEL_gm || TC0_WGMODE_gm != TC1_WGMODE_gm || TC0_OVFINTLVL_gm != TC1_OVFINTLVL_gm
00064 #error TC0 and TC1 Timer/Counters cannot be configured with the same bitvalues
00065 #endif
00066
00067 #define TIMER_CONFIG_CLOCK_SOURCE(_clkSel)\
00068 ((TIMERCOUNTER).CTRLA = ((TIMERCOUNTER).CTRLA & ~TC0_CLKSEL_gm) | _clkSel)
00069
00070 #define TIMER_CLEAR_FLAGS() ((TIMERCOUNTER).INTFLAGS = 0xFF)
00071
00072 #define TIMER_SET_PERIOD( _period ) ( (TIMERCOUNTER).PER = (_period) )
00073
00074 #define TIMER_SET_OVERFLOW_INTERRUPT_LEVEL( _interruptLevel )\
00075 ((TIMERCOUNTER).INTCTRLA = ( (TIMERCOUNTER).INTCTRLA & ~TC0_OVFINTLVL_gm ) | _interruptLevel)
00076
00077 #define TIMER_CONFIG_WGM(_wgm)\
00078 ((TIMERCOUNTER).CTRLB = ( (TIMERCOUNTER).CTRLB & ~TC0_WGMODE_gm ) | _wgm)
00079
00080 #define TIMER_RESET() ( (TIMERCOUNTER).CTRLFSET = TC_CMD_RESET_gc )
00081
00082
00083
00084 #if TIMER_PRESCALER == 0
00085 #define TIMER_CLKSEL_gc TC_CLKSEL_OFF_gc
00086 #elif TIMER_PRESCALER == 1
00087 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV1_gc
00088 #elif TIMER_PRESCALER == 2
00089 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV2_gc
00090 #elif TIMER_PRESCALER == 4
00091 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV4_gc
00092 #elif TIMER_PRESCALER == 16
00093 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV16_gc
00094 #elif TIMER_PRESCALER == 64
00095 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV64_gc
00096 #elif TIMER_PRESCALER == 256
00097 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV256_gc
00098 #elif TIMER_PRESCALER == 1024
00099 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV1024_gc
00100 #else
00101 #error Invalid value for TIMER_PRESCALER has been defined! Using default of 1
00102 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV1_gc
00103 #endif
00104
00105 void timer_hw_init(void)
00106 {
00107
00108 cpu_flags_t flags;
00109 IRQ_SAVE_DISABLE(flags);
00110
00111 TIMER_CONFIG_CLOCK_SOURCE(TC_CLKSEL_OFF_gc);
00112
00113 TIMER_CLEAR_FLAGS();
00114
00115
00116 TIMER_SET_PERIOD(TIMER_PERIOD_VALUE);
00117
00118 TIMER_CONFIG_WGM(TC_WGMODE_NORMAL_gc);
00119
00120
00121 TIMER_SET_OVERFLOW_INTERRUPT_LEVEL(TC_OVFINTLVL_HI_gc);
00122
00123 TIMER_CONFIG_CLOCK_SOURCE(TIMER_CLKSEL_gc);
00124
00125 IRQ_RESTORE(flags);
00126 }