Implement AVR XMega timer driver.
[bertos.git] / bertos / cpu / avr / drv / timer_xmega.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2005, 2010 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2011 Onno <developer@gorgoz.org>
31  *
32  * -->
33  *
34  * \author Onno <developer@gorgoz.org>
35  *
36  * \brief Low-level timer module for AVR XMEGA (implementation).
37  *
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.
41  *
42  * This module is automatically included so no need to include
43  * in test list.
44  * notest: xmega
45  */
46
47 #include <drv/timer_xmega.h>
48 #include <cfg/macros.h> // BV()
49
50 #include <cpu/types.h>
51 #include <cpu/irq.h>
52
53 #include <avr/io.h>
54
55 /* Helper MACROS taken from the Atmel examples and altered
56  *
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
60   */
61
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
65 #endif
66
67 #define TIMER_CONFIG_CLOCK_SOURCE(_clkSel)\
68         ((TIMERCOUNTER).CTRLA = ((TIMERCOUNTER).CTRLA & ~TC0_CLKSEL_gm) | _clkSel)
69
70 #define TIMER_CLEAR_FLAGS() ((TIMERCOUNTER).INTFLAGS = 0xFF)
71
72 #define TIMER_SET_PERIOD( _period ) ( (TIMERCOUNTER).PER = (_period) )
73
74 #define TIMER_SET_OVERFLOW_INTERRUPT_LEVEL( _interruptLevel )\
75         ((TIMERCOUNTER).INTCTRLA = ( (TIMERCOUNTER).INTCTRLA & ~TC0_OVFINTLVL_gm ) | _interruptLevel)
76
77 #define TIMER_CONFIG_WGM(_wgm)\
78         ((TIMERCOUNTER).CTRLB = ( (TIMERCOUNTER).CTRLB & ~TC0_WGMODE_gm ) | _wgm)
79
80 #define TIMER_RESET() ( (TIMERCOUNTER).CTRLFSET = TC_CMD_RESET_gc )
81
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
100 #else
101         #error Invalid value for TIMER_PRESCALER has been defined! Using default of 1
102         #define TIMER_CLKSEL_gc                         TC_CLKSEL_DIV1_gc
103 #endif
104
105 void timer_hw_init(void)
106 {
107         //Save and disable IRQ
108         cpu_flags_t flags;
109         IRQ_SAVE_DISABLE(flags);
110         //disable the timer
111         TIMER_CONFIG_CLOCK_SOURCE(TC_CLKSEL_OFF_gc);
112         //clear all flags
113         TIMER_CLEAR_FLAGS();
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);
124         //Restore IRQ
125         IRQ_RESTORE(flags);
126 }