Merge contributed patch to extend support of atxmega.
[bertos.git] / bertos / cpu / avr / drv / timer_xmega.h
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 2003, 2004, 2005, 2010 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
31  * Copyright 2011 Onno <developer@gorgoz.org>
32  *
33  * -->
34  *
35  * \brief Low-level timer module for AVR XMEGA (interface).
36  *
37  * This file is heavily inspired by the AVR implementation for BeRTOS,
38  * but uses a different approach for implementing the different debug
39  * ports, by using the timer structs.
40  *
41  * \author Onno <developer@gorgoz.org>
42  *
43  */
44
45 #ifndef DRV_TIMER_XMEGA_H
46 #define DRV_TIMER_XMEGA_H
47
48 #include <hw/hw_cpufreq.h>   /* CPU_FREQ */
49
50 #include "cfg/cfg_timer.h"   /* CONFIG_TIMER */
51 #include <cfg/compiler.h>    /* uint8_t */
52 #include <cfg/macros.h>      /* DIV_ROUND */
53
54 #include <avr/io.h>
55 #include <avr/interrupt.h>
56
57 /**
58  * \name Values for CONFIG_TIMER.
59  *
60  * Select which hardware timer interrupt to use for system clock and softtimers.
61  * $WIZ$ timer_select = "TIMER_USE_TCC0", "TIMER_USE_TCC1", "TIMER_USE_TCD0", "TIMER_USE_TCE0", "TIMER_USE_TCD1", "TIMER_USE_TCF0", "TIMER_USE_TCE1", "TIMER_USE_TCF1", "TIMER_DEFAULT"
62  */
63 #define TIMER_USE_TCC0    1
64 #define TIMER_USE_TCC1    2
65 #define TIMER_USE_TCD0    3
66 #define TIMER_USE_TCE0    4
67 #if CPU_AVR_XMEGA_A4 || CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
68         #define TIMER_USE_TCD1  5
69 #else
70         #define TIMER_USE_TCD1  0
71 #endif
72 #if CPU_AVR_XMEGA_D3 || CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
73         #define TIMER_USE_TCF0  6
74 #else
75         #define TIMER_USE_TCF0  0
76 #endif
77 #if CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
78         #define TIMER_USE_TCE1  7
79 #else
80         #define TIMER_USE_TCE1  0
81 #endif
82 #if CPU_AVR_XMEGA_A1
83         #define TIMER_USE_TCF1  8
84 #else
85         #define TIMER_USE_TCF1  0
86 #endif
87
88 #define TIMER_DEFAULT TIMER_USE_TCC1 ///< Default system timer
89
90 /*
91  * Hardware dependent timer initialization.
92  */
93 #if (CONFIG_TIMER == TIMER_USE_TCC0)
94         #define TIMER_OVF_VECT  TCC0_OVF_vect
95         #define TIMERCOUNTER    TCC0
96 #elif (CONFIG_TIMER == TIMER_USE_TCC1)
97         #define TIMER_OVF_VECT  TCC1_OVF_vect
98         #define TIMERCOUNTER    TCC1
99 #elif (CONFIG_TIMER == TIMER_USE_TCD0)
100         #define TIMER_OVF_VECT  TCD0_OVF_vect
101         #define TIMERCOUNTER    TCD0
102 #elif (CONFIG_TIMER == TIMER_USE_TCD1)
103         #define TIMER_OVF_VECT  TCD1_OVF_vect
104         #define TIMERCOUNTER    TCD1
105 #elif (CONFIG_TIMER == TIMER_USE_TCE0)
106         #define TIMER_OVF_VECT  TCE0_OVF_vect
107         #define TIMERCOUNTER    TCE0
108 #elif (CONFIG_TIMER == TIMER_USE_TCE1)
109         #define TIMER_OVF_VECT  TCE1_OVF_vect
110         #define TIMERCOUNTER    TCE1
111 #elif (CONFIG_TIMER == TIMER_USE_TCF0)
112         #define TIMER_OVF_VECT  TCF0_OVF_vect
113         #define TIMERCOUNTER    TCF0
114 #elif (CONFIG_TIMER == TIMER_USE_TCF1)
115         #define TIMER_OVF_VECT  TCF1_OVF_vect
116         #define TIMERCOUNTER    TCF1
117 #else
118         #error Unimplemented value for CONFIG_TIMER
119 #endif /* CONFIG_TIMER */
120
121 //define the Interrupt Service Routine for this Timer Mode
122 #define DEFINE_TIMER_ISR                        DECLARE_ISR_CONTEXT_SWITCH(TIMER_OVF_VECT)
123 //define the Ticks per second we want
124 #define TIMER_TICKS_PER_SEC                     1000
125 //define the Prescaler to use, which is dependend on the amount
126 //of ticks per second, the maximum value for the TOP value of the
127 //timer (0xFFFF) and the clock frequency.
128 //The maximum clock frequency is 32Mhz, so as long as the TIMER_TICKS_PER_SEC
129 //is larger then (about) 500 no prescaler is required.
130 #define TIMER_PRESCALER                         1
131 //define the TOP/PERIOD value
132 #define TIMER_PERIOD_VALUE                      DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC)
133 //check if the TIMER_PRESCALER is large enough to accomate for the TIMER_TICKS_PER_SEC
134 #if TIMER_PERIOD_VALUE > 0xFFFF
135         #error Timer cannot generate the required Ticks per second, please adjust TIMER_PRESCALER
136 #endif
137 //define TIMER_HW_CNT it is used by the timer.c module to determine the 'edge' of the hardware counter
138 #define TIMER_HW_CNT                            TIMER_PERIOD_VALUE
139 /** Frequency of the hardware high precision timer. */
140 #define TIMER_HW_HPTICKS_PER_SEC        DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
141
142 // Type of time expressed in ticks of the hardware high-precision timer
143 typedef uint16_t hptime_t;
144 #define SIZEOF_HPTIME_T 2
145
146 INLINE hptime_t timer_hw_hpread(void)
147 {
148         return (TIMERCOUNTER).CNT;
149 }
150
151 /* Not needed, IRQ timer flag cleared automatically */
152 #define timer_hw_irq() do {} while (0)
153
154 /* Not needed, timer IRQ handler called only for timer source */
155 #define timer_hw_triggered() (true)
156
157 void timer_hw_init(void);
158
159 #endif /* DRV_TIMER_XMEGA_H */