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 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
34 * \brief Low-level timer module for AVR (interface).
38 * \author Bernardo Innocenti <bernie@develer.com>
39 * \author Francesco Sacchi <batt@develer.com>
43 #ifndef DRV_TIMER_AVR_H
44 #define DRV_TIMER_AVR_H
46 #include "hw/hw_cpu.h" /* CLOCK_FREQ */
48 #include "cfg/cfg_timer.h" /* CONFIG_TIMER */
49 #include <cfg/compiler.h> /* uint8_t */
50 #include <cfg/macros.h> /* DIV_ROUND */
54 * \name Values for CONFIG_TIMER.
56 * Select which hardware timer interrupt to use for system clock and softtimers.
57 * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
61 #define TIMER_ON_OUTPUT_COMPARE0 1
62 #define TIMER_ON_OVERFLOW1 2
63 #define TIMER_ON_OUTPUT_COMPARE2 3
64 #define TIMER_ON_OVERFLOW3 4
66 #define TIMER_DEFAULT TIMER_ON_OUTPUT_COMPARE0 ///< Default system timer
70 * Hardware dependent timer initialization.
72 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
74 #define TIMER_PRESCALER 64
75 #define TIMER_HW_BITS 8
76 #define DEFINE_TIMER_ISR SIGNAL(SIG_OUTPUT_COMPARE0)
77 #define TIMER_TICKS_PER_SEC 1000
78 #define TIMER_HW_CNT OCR_DIVISOR
80 /// Type of time expressed in ticks of the hardware high-precision timer
81 typedef uint8_t hptime_t;
83 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
85 #define TIMER_PRESCALER 1
86 #define TIMER_HW_BITS 8
87 /** This value is the maximum in overflow based timers. */
88 #define TIMER_HW_CNT (1 << TIMER_HW_BITS)
89 #define DEFINE_TIMER_ISR SIGNAL(SIG_OVERFLOW1)
90 #define TIMER_TICKS_PER_SEC DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
92 /// Type of time expressed in ticks of the hardware high precision timer
93 typedef uint16_t hptime_t;
95 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
97 #define TIMER_PRESCALER 64
98 #define TIMER_HW_BITS 8
99 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
100 #define DEFINE_TIMER_ISR SIGNAL(SIG_OUTPUT_COMPARE2A)
102 #define DEFINE_TIMER_ISR SIGNAL(SIG_OUTPUT_COMPARE2)
104 #define TIMER_TICKS_PER_SEC 1000
105 /** Value for OCR register in output-compare based timers. */
106 #define TIMER_HW_CNT OCR_DIVISOR
109 /// Type of time expressed in ticks of the hardware high precision timer
110 typedef uint8_t hptime_t;
112 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
114 #define TIMER_PRESCALER 1
115 #define TIMER_HW_BITS 8
116 /** This value is the maximum in overflow based timers. */
117 #define TIMER_HW_CNT (1 << TIMER_HW_BITS)
118 #define DEFINE_TIMER_ISR SIGNAL(SIG_OVERFLOW3)
119 #define TIMER_TICKS_PER_SEC DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
121 /// Type of time expressed in ticks of the hardware high precision timer
122 typedef uint16_t hptime_t;
125 #error Unimplemented value for CONFIG_TIMER
126 #endif /* CONFIG_TIMER */
129 /** Frequency of the hardware high precision timer. */
130 #define TIMER_HW_HPTICKS_PER_SEC DIV_ROUND(CLOCK_FREQ, TIMER_PRESCALER)
133 * System timer: additional division after the prescaler
134 * 12288000 / 64 / 192 (0..191) = 1 ms
136 #define OCR_DIVISOR (DIV_ROUND(DIV_ROUND(CLOCK_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC) - 1)
138 /** Not needed, IRQ timer flag cleared automatically */
139 #define timer_hw_irq() do {} while (0)
141 /** Not needed, timer IRQ handler called only for timer source */
142 #define timer_hw_triggered() (true)
145 #endif /* DRV_TIMER_AVR_H */