2146620c943674e1dd939051b1467d3f3bfb15b8
[bertos.git] / bertos / cpu / avr / drv / timer_mega.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  *
32  * -->
33  *
34  * \brief Low-level timer module for AVR (interface).
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Francesco Sacchi <batt@develer.com>
38  * \author Luca Ottaviano <lottaviano@develer.com>
39  *
40  */
41
42 #ifndef DRV_TIMER_AVR_H
43 #define DRV_TIMER_AVR_H
44
45 #include <hw/hw_cpufreq.h>   /* CPU_FREQ */
46
47 #include "cfg/cfg_timer.h"   /* CONFIG_TIMER */
48 #include <cfg/compiler.h>    /* uint8_t */
49 #include <cfg/macros.h>      /* DIV_ROUND */
50
51 #include <avr/io.h>
52 #include <avr/interrupt.h>
53
54 /**
55  * \name Values for CONFIG_TIMER.
56  *
57  * Select which hardware timer interrupt to use for system clock and softtimers.
58  * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
59  * $WIZ$ timer_select = "TIMER_ON_OUTPUT_COMPARE0", "TIMER_ON_OVERFLOW1", "TIMER_ON_OUTPUT_COMPARE2", "TIMER_ON_OVERFLOW3", "TIMER_DEFAULT"
60  */
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
65
66 #define TIMER_DEFAULT TIMER_ON_OUTPUT_COMPARE0 ///< Default system timer
67
68 /*
69  * Hardware dependent timer initialization.
70  */
71 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
72
73         #define TIMER_PRESCALER      64
74         #define TIMER_HW_BITS        8
75         #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P || CPU_AVR_ATMEGA2560
76                 #define DEFINE_TIMER_ISR     DECLARE_ISR_CONTEXT_SWITCH(TIMER0_COMPA_vect)
77         #else
78                 #define DEFINE_TIMER_ISR     DECLARE_ISR_CONTEXT_SWITCH(TIMER0_COMP_vect)
79         #endif
80         #define TIMER_TICKS_PER_SEC  1000
81         #define TIMER_HW_CNT         OCR_DIVISOR
82
83         /// Type of time expressed in ticks of the hardware high-precision timer
84         typedef uint8_t hptime_t;
85         #define SIZEOF_HPTIME_T 1
86
87         INLINE hptime_t timer_hw_hpread(void)
88         {
89                 return TCNT0;
90         }
91
92 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
93
94         #define TIMER_PRESCALER      1
95         #define TIMER_HW_BITS        8
96         /** This value is the maximum in overflow based timers. */
97         #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
98         #define DEFINE_TIMER_ISR     DECLARE_ISR_CONTEXT_SWITCH(TIMER1_OVF_vect)
99         #define TIMER_TICKS_PER_SEC  DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
100
101         /// Type of time expressed in ticks of the hardware high precision timer
102         typedef uint16_t hptime_t;
103         #define SIZEOF_HPTIME_T 2
104
105         INLINE hptime_t timer_hw_hpread(void)
106         {
107                 return TCNT1;
108         }
109
110 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
111
112         #define TIMER_PRESCALER      64
113         #define TIMER_HW_BITS        8
114         #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P || CPU_AVR_ATMEGA2560
115                 #define DEFINE_TIMER_ISR     DECLARE_ISR_CONTEXT_SWITCH(TIMER2_COMPA_vect)
116         #else
117                 #define DEFINE_TIMER_ISR     DECLARE_ISR_CONTEXT_SWITCH(TIMER2_COMP_vect)
118         #endif
119         #define TIMER_TICKS_PER_SEC  1000
120         /** Value for OCR register in output-compare based timers. */
121         #define TIMER_HW_CNT         OCR_DIVISOR
122
123         /// Type of time expressed in ticks of the hardware high precision timer
124         typedef uint8_t hptime_t;
125         #define SIZEOF_HPTIME_T 1
126
127         INLINE hptime_t timer_hw_hpread(void)
128         {
129                 return TCNT2;
130         }
131
132 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
133
134         #define TIMER_PRESCALER      1
135         #define TIMER_HW_BITS        8
136         /** This value is the maximum in overflow based timers. */
137         #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
138         #define DEFINE_TIMER_ISR     DECLARE_ISR_CONTEXT_SWITCH(TIMER3_OVF_vect)
139         #define TIMER_TICKS_PER_SEC  DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, TIMER_HW_CNT)
140
141         /// Type of time expressed in ticks of the hardware high precision timer
142         typedef uint16_t hptime_t;
143         #define SIZEOF_HPTIME_T 2
144
145         INLINE hptime_t timer_hw_hpread(void)
146         {
147                 return TCNT3;
148         }
149
150 #else
151
152         #error Unimplemented value for CONFIG_TIMER
153 #endif /* CONFIG_TIMER */
154
155
156 /** Frequency of the hardware high precision timer. */
157 #define TIMER_HW_HPTICKS_PER_SEC  DIV_ROUND(CPU_FREQ, TIMER_PRESCALER)
158
159 /**
160  * System timer: additional division after the prescaler
161  * 12288000 / 64 / 192 (0..191) = 1 ms
162  */
163 #define OCR_DIVISOR  (DIV_ROUND(DIV_ROUND(CPU_FREQ, TIMER_PRESCALER), TIMER_TICKS_PER_SEC) - 1)
164
165 /** Not needed, IRQ timer flag cleared automatically */
166 #define timer_hw_irq() do {} while (0)
167
168 /** Not needed, timer IRQ handler called only for timer source */
169 #define timer_hw_triggered() (true)
170
171 void timer_hw_init(void);
172
173 #endif /* DRV_TIMER_AVR_H */