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 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>
35 * \brief Low-level timer module for AVR XMEGA (interface).
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.
41 * \author Onno <developer@gorgoz.org>
45 #ifndef DRV_TIMER_XMEGA_H
46 #define DRV_TIMER_XMEGA_H
48 #include <hw/hw_cpufreq.h> /* CPU_FREQ */
50 #include "cfg/cfg_timer.h" /* CONFIG_TIMER */
51 #include <cfg/compiler.h> /* uint8_t */
52 #include <cfg/macros.h> /* DIV_ROUND */
55 #include <avr/interrupt.h>
58 * \name Values for CONFIG_TIMER.
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_DEFAULT"
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 // The XMEGA A Family has one extra timer
68 #ifdef CPU_AVR_XMEGA_A
69 #define TIMER_USE_TCD1 5
72 #define TIMER_DEFAULT TIMER_USE_TCC1 ///< Default system timer
75 * Hardware dependent timer initialization.
77 #if (CONFIG_TIMER == TIMER_USE_TCC0)
78 #define TIMER_OVF_VECT TCC0_OVF_vect
79 #define TIMERCOUNTER TCC0
80 #elif (CONFIG_TIMER == TIMER_USE_TCC1)
81 #define TIMER_OVF_VECT TCC1_OVF_vect
82 #define TIMERCOUNTER TCC1
83 #elif (CONFIG_TIMER == TIMER_USE_TCD0)
84 #define TIMER_OVF_VECT TCD0_OVF_vect
85 #define TIMERCOUNTER TCD0
86 #elif (CONFIG_TIMER == TIMER_USE_TCE0)
87 #define TIMER_OVF_VECT TCE0_OVF_vect
88 #define TIMERCOUNTER TCE0
89 #elif (CONFIG_TIMER == TIMER_USE_TCD1)
90 #define TIMER_OVF_VECT TCD1_OVF_vect
91 #define TIMERCOUNTER TCD1
93 #error Unimplemented value for CONFIG_TIMER
94 #endif /* CONFIG_TIMER */
96 //define the Interrupt Service Routine for this Timer Mode
97 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER_OVF_VECT)
98 //define the Ticks per second we want
99 #define TIMER_TICKS_PER_SEC 1000
100 //define the Prescaler to use, which is dependend on the amount
101 //of ticks per second, the maximum value for the TOP value of the
102 //timer (0xFFFF) and the clock frequency.
103 //The maximum clock frequency is 32Mhz, so as long as the TIMER_TICKS_PER_SEC
104 //is larger then (about) 500 no prescaler is required.
105 #define TIMER_PRESCALER 1
106 //define the TOP/PERIOD value
107 #define TIMER_PERIOD_VALUE DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC)
108 //check if the TIMER_PRESCALER is large enough to accomate for the TIMER_TICKS_PER_SEC
109 #if TIMER_PERIOD_VALUE > 0xFFFF
110 #error Timer cannot generate the required Ticks per second, please adjust TIMER_PRESCALER
112 //define TIMER_HW_CNT it is used by the timer.c module to determine the 'edge' of the hardware counter
113 #define TIMER_HW_CNT TIMER_PERIOD_VALUE
114 /** Frequency of the hardware high precision timer. */
115 #define TIMER_HW_HPTICKS_PER_SEC DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
117 // Type of time expressed in ticks of the hardware high-precision timer
118 typedef uint16_t hptime_t;
119 #define SIZEOF_HPTIME_T 2
121 INLINE hptime_t timer_hw_hpread(void)
123 return (TIMERCOUNTER).CNT;
126 /* Not needed, IRQ timer flag cleared automatically */
127 #define timer_hw_irq() do {} while (0)
129 /* Not needed, timer IRQ handler called only for timer source */
130 #define timer_hw_triggered() (true)
132 void timer_hw_init(void);
134 #endif /* DRV_TIMER_XMEGA_H */