291f205d9115b27ad72e9152ab3de713c725cb46
[bertos.git] / bertos / cpu / avr / drv / timer_avr.c
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 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  * \brief Low-level timer module for AVR (implementation).
39  *
40  * This module is automatically included so no need to include
41  * in test list.
42  * notest: avr
43  */
44
45 #include <drv/timer_avr.h>
46 #include <cfg/macros.h> // BV()
47
48 #include <cpu/types.h>
49 #include <cpu/irq.h>
50
51 #include <avr/interrupt.h>
52 #include <avr/io.h>
53
54 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
55         #define REG_TIFR0 TIFR0
56         #define REG_TIFR2 TIFR2
57
58         #define REG_TIMSK0 TIMSK0
59         #define REG_TIMSK2 TIMSK2
60
61         #define REG_TCCR0A TCCR0A
62         #define REG_TCCR0B TCCR0B
63
64         #define REG_TCCR2A TCCR2A
65         #define REG_TCCR2B TCCR2B
66
67         #define REG_OCR0A  OCR0A
68         #define REG_OCR2A  OCR2A
69
70         #define BIT_OCF0A  OCF0A
71         #define BIT_OCF2A  OCF2A
72
73         #define BIT_OCIE0A OCIE0A
74         #define BIT_OCIE2A OCIE2A
75 #else
76         #define REG_TIFR0 TIFR
77         #define REG_TIFR2 TIFR
78
79         #define REG_TIMSK0 TIMSK
80         #define REG_TIMSK2 TIMSK
81
82         #define REG_TCCR0A TCCR0
83         #define REG_TCCR0B TCCR0
84
85         #define REG_TCCR2A TCCR2
86         #define REG_TCCR2B TCCR2
87
88         #define REG_OCR0A  OCR0
89         #define REG_OCR2A  OCR2
90
91         #define BIT_OCF0A  OCF0
92         #define BIT_OCF2A  OCF2
93
94         #define BIT_OCIE0A OCIE0
95         #define BIT_OCIE2A OCIE2
96 #endif
97
98 #if CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA103
99     /* These ATMega have different prescaler options. */
100     #define TIMER0_PRESCALER_64 BV(CS02)
101     #define TIMER2_PRESCALER_64 (BV(CS21) | BV(CS20))
102 #else
103     #define TIMER0_PRESCALER_64 (BV(CS01) | BV(CS00))
104     #define TIMER2_PRESCALER_64 BV(CS22)
105 #endif
106
107 /** HW dependent timer initialization  */
108 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
109
110         static void timer_hw_init(void)
111         {
112                 cpu_flags_t flags;
113                 IRQ_SAVE_DISABLE(flags);
114
115                 /* Reset Timer flags */
116                 REG_TIFR0 = BV(BIT_OCF0A) | BV(TOV0);
117
118                 /* Setup Timer/Counter interrupt */
119                 ASSR = 0x00;                  /* Internal system clock */
120
121                 REG_TCCR0A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
122                 REG_TCCR0B = 0;
123
124                 REG_TCCR0A = BV(WGM01);             /* Clear on Compare match */
125                         #if TIMER_PRESCALER == 64
126                         REG_TCCR0B |= TIMER0_PRESCALER_64;
127                         #else
128                                 #error Unsupported value of TIMER_PRESCALER
129                         #endif
130                 ;
131                 TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
132                 REG_OCR0A = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
133
134                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
135                 REG_TIMSK0 &= ~BV(TOIE0);
136                 REG_TIMSK0 |= BV(BIT_OCIE0A);
137
138                 IRQ_RESTORE(flags);
139         }
140
141         INLINE hptime_t timer_hw_hpread(void)
142         {
143                 return TCNT0;
144         }
145
146 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
147
148         static void timer_hw_init(void)
149         {
150                 cpu_flags_t flags;
151                 IRQ_SAVE_DISABLE(flags);
152
153                 /* Reset Timer overflow flag */
154                 REG_TIFR0 |= BV(TOV1);
155
156                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
157                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
158                         TCCR1A |= BV(WGM11);
159                         TCCR1A &= ~BV(WGM10);
160                         TCCR1B |= BV(WGM12) | BV(CS10);
161                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
162                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
163                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
164                         TCCR1A |= BV(WGM10);
165                         TCCR1A &= ~BV(WGM11);
166                         TCCR1B |= BV(WGM12) | BV(CS10);
167                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
168                 #else
169                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
170                 #endif
171
172                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
173
174                 /* Enable timer interrupt: Timer/Counter1 Overflow */
175                 REG_TIMSK0 |= BV(TOIE1);
176
177                 IRQ_RESTORE(flags);
178         }
179
180         INLINE hptime_t timer_hw_hpread(void)
181         {
182                 return TCNT1;
183         }
184
185 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
186         static void timer_hw_init(void)
187         {
188                 cpu_flags_t flags;
189                 IRQ_SAVE_DISABLE(flags);
190
191                 /* Reset Timer flags */
192                 REG_TIFR2 = BV(BIT_OCF2A) | BV(TOV2);
193
194                 /* Setup Timer/Counter interrupt */
195                 REG_TCCR2A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
196                 REG_TCCR2B = 0; // ensure correct initialization.
197
198                 REG_TCCR2A = BV(WGM21);
199                 #if TIMER_PRESCALER == 64
200                         REG_TCCR2B |= TIMER2_PRESCALER_64;
201                 #else
202                         #error Unsupported value of TIMER_PRESCALER
203                 #endif
204
205                 /* Clear on Compare match & prescaler = 64, internal sys clock.
206                    When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
207                 TCNT2 = 0x00;         /* initialization of Timer/Counter */
208                 REG_OCR2A = OCR_DIVISOR;   /* Timer/Counter Output Compare Register */
209
210                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
211                 REG_TIMSK2 &= ~BV(TOIE2);
212                 REG_TIMSK2 |= BV(BIT_OCIE2A);
213
214                 IRQ_RESTORE(flags);
215         }
216
217         INLINE hptime_t timer_hw_hpread(void)
218         {
219                 return TCNT2;
220         }
221 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
222
223         static void timer_hw_init(void)
224         {
225                 cpu_flags_t flags;
226                 IRQ_SAVE_DISABLE(flags);
227
228                 /* Reset Timer overflow flag */
229                 TIFR |= BV(TOV3);
230
231                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
232                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
233                         TCCR3A |= BV(WGM31);
234                         TCCR3A &= ~BV(WGM30);
235                         TCCR3B |= BV(WGM32) | BV(CS30);
236                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
237                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
238                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
239                         TCCR3A |= BV(WGM30);
240                         TCCR3A &= ~BV(WGM31);
241                         TCCR3B |= BV(WGM32) | BV(CS30);
242                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
243                 #else
244                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
245                 #endif
246
247                 TCNT3 = 0x00;         /* initialization of Timer/Counter */
248
249                 /* Enable timer interrupt: Timer/Counter3 Overflow */
250                 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
251                 ETIMSK |= BV(TOIE3);
252
253                 IRQ_RESTORE(flags);
254         }
255
256         INLINE hptime_t timer_hw_hpread(void)
257         {
258                 return TCNT3;
259         }
260
261 #else
262         #error Unimplemented value for CONFIG_TIMER
263 #endif /* CONFIG_TIMER */
264