4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2005, 2010 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2011 Onno <developer@gorgoz.org>
34 * \author Onno <developer@gorgoz.org>
36 * \brief Low-level timer module for AVR XMEGA (implementation).
38 * This file is heavily inspired by the AVR implementation for BeRTOS,
39 * but uses a different approach for implementing the different debug
40 * ports, by using the timer structs.
42 * This module is automatically included so no need to include
47 #include <drv/timer_xmega.h>
48 #include <cfg/macros.h> // BV()
50 #include <cpu/types.h>
55 /* Helper MACROS taken from the Atmel examples and altered
57 * The AVR XMEGA has different structures for TC0 and TC1,
58 * however these only differ in the amount of compare/capture
59 * channels, so for this purpose they can be used exactly the same
62 //check if the bitvalues of the TC0 and TC1 Timer/Counters are really the same
63 #if TC0_CLKSEL_gm != TC1_CLKSEL_gm || TC0_WGMODE_gm != TC1_WGMODE_gm || TC0_OVFINTLVL_gm != TC1_OVFINTLVL_gm
64 #error TC0 and TC1 Timer/Counters cannot be configured with the same bitvalues
67 #define TIMER_CONFIG_CLOCK_SOURCE(_clkSel)\
68 ((TIMERCOUNTER).CTRLA = ((TIMERCOUNTER).CTRLA & ~TC0_CLKSEL_gm) | _clkSel)
70 #define TIMER_CLEAR_FLAGS() ((TIMERCOUNTER).INTFLAGS = 0xFF)
72 #define TIMER_SET_PERIOD( _period ) ( (TIMERCOUNTER).PER = (_period) )
74 #define TIMER_SET_OVERFLOW_INTERRUPT_LEVEL( _interruptLevel )\
75 ((TIMERCOUNTER).INTCTRLA = ( (TIMERCOUNTER).INTCTRLA & ~TC0_OVFINTLVL_gm ) | _interruptLevel)
77 #define TIMER_CONFIG_WGM(_wgm)\
78 ((TIMERCOUNTER).CTRLB = ( (TIMERCOUNTER).CTRLB & ~TC0_WGMODE_gm ) | _wgm)
80 #define TIMER_RESET() ( (TIMERCOUNTER).CTRLFSET = TC_CMD_RESET_gc )
82 //Define TIMER_PRESCALE_REG_VALUE bases on the provided
83 //TIMER_PRESCALER value
84 #if TIMER_PRESCALER == 0
85 #define TIMER_CLKSEL_gc TC_CLKSEL_OFF_gc
86 #elif TIMER_PRESCALER == 1
87 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV1_gc
88 #elif TIMER_PRESCALER == 2
89 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV2_gc
90 #elif TIMER_PRESCALER == 4
91 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV4_gc
92 #elif TIMER_PRESCALER == 16
93 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV16_gc
94 #elif TIMER_PRESCALER == 64
95 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV64_gc
96 #elif TIMER_PRESCALER == 256
97 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV256_gc
98 #elif TIMER_PRESCALER == 1024
99 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV1024_gc
101 #error Invalid value for TIMER_PRESCALER has been defined! Using default of 1
102 #define TIMER_CLKSEL_gc TC_CLKSEL_DIV1_gc
105 void timer_hw_init(void)
107 //Save and disable IRQ
109 IRQ_SAVE_DISABLE(flags);
111 TIMER_CONFIG_CLOCK_SOURCE(TC_CLKSEL_OFF_gc);
114 //setup the Periode register value
115 //CNT register will be reset to 0 when CNT == PER
116 TIMER_SET_PERIOD(TIMER_PERIOD_VALUE);
117 //set the Waveform Generation Mode to Normal
118 TIMER_CONFIG_WGM(TC_WGMODE_NORMAL_gc);
119 //enable the overflow interrupt
120 //use the highest priority
121 TIMER_SET_OVERFLOW_INTERRUPT_LEVEL(TC_OVFINTLVL_HI_gc);
122 //enable timer by setting the correct prescaler/clock
123 TIMER_CONFIG_CLOCK_SOURCE(TIMER_CLKSEL_gc);