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>
34 * \brief Low-level timer module for AVR (interface).
38 * \author Bernie Innocenti <bernie@codewiz.org>
39 * \author Francesco Sacchi <batt@develer.com>
40 * \author Luca Ottaviano <lottaviano@develer.com>
44 #ifndef DRV_TIMER_AVR_H
45 #define DRV_TIMER_AVR_H
47 #include <hw/hw_cpufreq.h> /* CPU_FREQ */
49 #include "cfg/cfg_timer.h" /* CONFIG_TIMER */
50 #include <cfg/compiler.h> /* uint8_t */
51 #include <cfg/macros.h> /* DIV_ROUND */
54 #include <avr/interrupt.h>
57 * \name Values for CONFIG_TIMER.
59 * Select which hardware timer interrupt to use for system clock and softtimers.
60 * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
61 * $WIZ$ timer_select = "TIMER_ON_OUTPUT_COMPARE0", "TIMER_ON_OVERFLOW1", "TIMER_ON_OUTPUT_COMPARE2", "TIMER_ON_OVERFLOW3", "TIMER_DEFAULT"
63 #define TIMER_ON_OUTPUT_COMPARE0 1
64 #define TIMER_ON_OVERFLOW1 2
65 #define TIMER_ON_OUTPUT_COMPARE2 3
66 #define TIMER_ON_OVERFLOW3 4
68 #define TIMER_DEFAULT TIMER_ON_OUTPUT_COMPARE0 ///< Default system timer
71 * Hardware dependent timer initialization.
73 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
75 #define TIMER_PRESCALER 64
76 #define TIMER_HW_BITS 8
77 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
78 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER0_COMPA_vect)
80 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER0_COMP_vect)
82 #define TIMER_TICKS_PER_SEC 1000
83 #define TIMER_HW_CNT OCR_DIVISOR
85 /// Type of time expressed in ticks of the hardware high-precision timer
86 typedef uint8_t hptime_t;
87 #define SIZEOF_HPTIME_T 1
89 INLINE hptime_t timer_hw_hpread(void)
94 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
96 #define TIMER_PRESCALER 1
97 #define TIMER_HW_BITS 8
98 /** This value is the maximum in overflow based timers. */
99 #define TIMER_HW_CNT (1 << TIMER_HW_BITS)
100 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER1_OVF_vect)
101 #define TIMER_TICKS_PER_SEC DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
103 /// Type of time expressed in ticks of the hardware high precision timer
104 typedef uint16_t hptime_t;
105 #define SIZEOF_HPTIME_T 2
107 INLINE hptime_t timer_hw_hpread(void)
112 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
114 #define TIMER_PRESCALER 64
115 #define TIMER_HW_BITS 8
116 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
117 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER2_COMPA_vect)
119 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER2_COMP_vect)
121 #define TIMER_TICKS_PER_SEC 1000
122 /** Value for OCR register in output-compare based timers. */
123 #define TIMER_HW_CNT OCR_DIVISOR
125 /// Type of time expressed in ticks of the hardware high precision timer
126 typedef uint8_t hptime_t;
127 #define SIZEOF_HPTIME_T 1
129 INLINE hptime_t timer_hw_hpread(void)
134 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
136 #define TIMER_PRESCALER 1
137 #define TIMER_HW_BITS 8
138 /** This value is the maximum in overflow based timers. */
139 #define TIMER_HW_CNT (1 << TIMER_HW_BITS)
140 #define DEFINE_TIMER_ISR DECLARE_ISR_CONTEXT_SWITCH(TIMER3_OVF_vect)
141 #define TIMER_TICKS_PER_SEC DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
143 /// Type of time expressed in ticks of the hardware high precision timer
144 typedef uint16_t hptime_t;
145 #define SIZEOF_HPTIME_T 2
147 INLINE hptime_t timer_hw_hpread(void)
154 #error Unimplemented value for CONFIG_TIMER
155 #endif /* CONFIG_TIMER */
158 /** Frequency of the hardware high precision timer. */
159 #define TIMER_HW_HPTICKS_PER_SEC DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
162 * System timer: additional division after the prescaler
163 * 12288000 / 64 / 192 (0..191) = 1 ms
165 #define OCR_DIVISOR (DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC) - 1)
167 /** Not needed, IRQ timer flag cleared automatically */
168 #define timer_hw_irq() do {} while (0)
170 /** Not needed, timer IRQ handler called only for timer source */
171 #define timer_hw_triggered() (true)
174 INLINE hptime_t timer_hw_hpticks(ticks_t clock)
176 return timer_hw_hpread() + clock * TIMER_HW_CNT;
179 void timer_hw_init(void);
181 #endif /* DRV_TIMER_AVR_H */