Move avr timer to avr cpu dir.
[bertos.git] / 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 Bernardo Innocenti <bernie@develer.com>
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  * \brief Low-level timer module for AVR (implementation).
39  */
40
41 /*#*
42  *#* $Log$
43  *#* Revision 1.6  2007/06/07 14:35:12  batt
44  *#* Merge from project_ks.
45  *#*
46  *#* Revision 1.5  2007/03/21 11:03:56  batt
47  *#* Add missing support for ATMega1281.
48  *#*
49  *#* Revision 1.4  2006/07/19 12:56:26  bernie
50  *#* Convert to new Doxygen style.
51  *#*
52  *#* Revision 1.3  2006/06/12 21:37:02  marco
53  *#* implemented some commands (ver and sleep)
54  *#*
55  *#* Revision 1.2  2006/05/18 00:37:58  bernie
56  *#* Don't include unneeded header hw.h.
57  *#*
58  *#* Revision 1.1  2005/07/19 07:28:36  bernie
59  *#* Refactor to decouple timer ticks from milliseconds.
60  *#*
61  *#* Revision 1.1  2005/05/24 09:17:58  batt
62  *#* Move drivers to top-level.
63  *#*
64  *#*/
65 #include <drv/timer_avr.h>
66 #include <cfg/macros.h> // BV()
67
68 #include <cpu/cpu.h>
69
70 #include <avr/interrupt.h>
71 #include <avr/io.h>
72
73 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
74         #define REG_TIFR0 TIFR0
75         #define REG_TIFR2 TIFR2
76
77         #define REG_TIMSK0 TIMSK0
78         #define REG_TIMSK2 TIMSK2
79
80         #define REG_TCCR2A TCCR2A
81         #define REG_TCCR2B TCCR2B
82
83         #define REG_OCR2A  OCR2A
84
85         #define BIT_OCF0A  OCF0A
86         #define BIT_OCF2A  OCF2A
87
88         #define BIT_OCIE0A OCIE0A
89         #define BIT_OCIE2A OCIE2A
90 #else
91         #define REG_TIFR0 TIFR
92         #define REG_TIFR2 TIFR
93
94         #define REG_TIMSK0 TIMSK
95         #define REG_TIMSK2 TIMSK
96
97         #define REG_TCCR2A TCCR2
98         #define REG_TCCR2B TCCR2
99
100         #define REG_OCR2A  OCR2
101
102         #define BIT_OCF0A  OCF0
103         #define BIT_OCF2A  OCF2
104
105         #define BIT_OCIE0A OCIE0
106         #define BIT_OCIE2A OCIE2
107 #endif
108
109
110 /** HW dependent timer initialization  */
111 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
112
113         static void timer_hw_init(void)
114         {
115                 cpuflags_t flags;
116                 IRQ_SAVE_DISABLE(flags);
117
118                 /* Reset Timer flags */
119                 REG_TIFR0 = BV(BIT_OCF0A) | BV(TOV0);
120
121                 /* Setup Timer/Counter interrupt */
122                 ASSR = 0x00;                  /* Internal system clock */
123                 TCCR0 = BV(WGM01)             /* Clear on Compare match */
124                         #if TIMER_PRESCALER == 64
125                                 | BV(CS02)
126                         #else
127                                 #error Unsupported value of TIMER_PRESCALER
128                         #endif
129                 ;
130                 TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
131                 OCR0 = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
132
133                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
134                 REG_TIMSK0 &= ~BV(TOIE0);
135                 REG_TIMSK0 |= BV(OCIE0);
136
137                 IRQ_RESTORE(flags);
138         }
139
140         INLINE hptime_t timer_hw_hpread(void)
141         {
142                 return TCNT0;
143         }
144
145 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
146
147         static void timer_hw_init(void)
148         {
149                 cpuflags_t flags;
150                 IRQ_SAVE_DISABLE(flags);
151
152                 /* Reset Timer overflow flag */
153                 TIFR |= BV(TOV1);
154
155                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
156                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
157                         TCCR1A |= BV(WGM11);
158                         TCCR1A &= ~BV(WGM10);
159                         TCCR1B |= BV(WGM12) | BV(CS10);
160                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
161                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
162                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
163                         TCCR1A |= BV(WGM10);
164                         TCCR1A &= ~BV(WGM11);
165                         TCCR1B |= BV(WGM12) | BV(CS10);
166                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
167                 #else
168                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
169                 #endif
170
171                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
172
173                 /* Enable timer interrupt: Timer/Counter1 Overflow */
174                 TIMSK |= BV(TOIE1);
175
176                 IRQ_RESTORE(flags);
177         }
178
179         INLINE hptime_t timer_hw_hpread(void)
180         {
181                 return TCNT1;
182         }
183
184 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
185         static void timer_hw_init(void)
186         {
187                 cpuflags_t flags;
188                 IRQ_SAVE_DISABLE(flags);
189
190                 /* Reset Timer flags */
191                 REG_TIFR2 = BV(BIT_OCF2A) | BV(TOV2);
192
193                 /* Setup Timer/Counter interrupt */
194                 REG_TCCR2A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
195                 REG_TCCR2B = 0; // ensure correct initialization.
196
197                 REG_TCCR2A = BV(WGM21);
198                 #if TIMER_PRESCALER == 64
199                 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
200                         // ATMega1281 & ATMega168 have undocumented differences in timer2 prescaler!
201                         REG_TCCR2B |= BV(CS22);
202                 #else
203                         REG_TCCR2B |= BV(CS21) | BV(CS20);
204                 #endif
205                 #else
206                         #error Unsupported value of TIMER_PRESCALER
207                 #endif
208
209                 /* Clear on Compare match & prescaler = 64, internal sys clock.
210                    When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
211                 TCNT2 = 0x00;         /* initialization of Timer/Counter */
212                 REG_OCR2A = OCR_DIVISOR;   /* Timer/Counter Output Compare Register */
213
214                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
215                 REG_TIMSK2 &= ~BV(TOIE2);
216                 REG_TIMSK2 |= BV(BIT_OCIE2A);
217
218                 IRQ_RESTORE(flags);
219         }
220
221         INLINE hptime_t timer_hw_hpread(void)
222         {
223                 return TCNT2;
224         }
225 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
226
227         static void timer_hw_init(void)
228         {
229                 cpuflags_t flags;
230                 IRQ_SAVE_DISABLE(flags);
231
232                 /* Reset Timer overflow flag */
233                 TIFR |= BV(TOV3);
234
235                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
236                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
237                         TCCR3A |= BV(WGM31);
238                         TCCR3A &= ~BV(WGM30);
239                         TCCR3B |= BV(WGM32) | BV(CS30);
240                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
241                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
242                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
243                         TCCR3A |= BV(WGM30);
244                         TCCR3A &= ~BV(WGM31);
245                         TCCR3B |= BV(WGM32) | BV(CS30);
246                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
247                 #else
248                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
249                 #endif
250
251                 TCNT3 = 0x00;         /* initialization of Timer/Counter */
252
253                 /* Enable timer interrupt: Timer/Counter3 Overflow */
254                 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
255                 ETIMSK |= BV(TOIE3);
256
257                 IRQ_RESTORE(flags);
258         }
259
260         INLINE hptime_t timer_hw_hpread(void)
261         {
262                 return TCNT3;
263         }
264
265 #else
266         #error Unimplemented value for CONFIG_TIMER
267 #endif /* CONFIG_TIMER */
268