Refactor timer to not include cpu specific C files.
[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/io.h>
52
53 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
54         #define REG_TIFR0 TIFR0
55         #define REG_TIFR1 TIFR1
56         #define REG_TIFR2 TIFR2
57         #if CPU_AVR_ATMEGA1281
58                 #define REG_TIFR3 TIFR3
59         #endif
60
61         #define REG_TIMSK0 TIMSK0
62         #define REG_TIMSK1 TIMSK1
63         #define REG_TIMSK2 TIMSK2
64         #if CPU_AVR_ATMEGA1281
65                 #define REG_TIMSK3 TIMSK3
66         #endif
67
68         #define REG_TCCR0A TCCR0A
69         #define REG_TCCR0B TCCR0B
70
71         #define REG_TCCR2A TCCR2A
72         #define REG_TCCR2B TCCR2B
73
74         #define REG_OCR0A  OCR0A
75         #define REG_OCR2A  OCR2A
76
77         #define BIT_OCF0A  OCF0A
78         #define BIT_OCF2A  OCF2A
79
80         #define BIT_OCIE0A OCIE0A
81         #define BIT_OCIE2A OCIE2A
82 #else
83         #define REG_TIFR0 TIFR
84         #define REG_TIFR1 TIFR
85         #define REG_TIFR2 TIFR
86
87         #define REG_TIMSK0 TIMSK
88         #define REG_TIMSK1 TIMSK
89         #define REG_TIMSK2 TIMSK
90         #define REG_TIMSK3 ETIMSK
91
92         #define REG_TCCR0A TCCR0
93         #define REG_TCCR0B TCCR0
94
95         #define REG_TCCR2A TCCR2
96         #define REG_TCCR2B TCCR2
97
98         #define REG_OCR0A  OCR0
99         #define REG_OCR2A  OCR2
100
101         #define BIT_OCF0A  OCF0
102         #define BIT_OCF2A  OCF2
103
104         #define BIT_OCIE0A OCIE0
105         #define BIT_OCIE2A OCIE2
106 #endif
107
108 #if CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA103
109     /* These ATMega have different prescaler options. */
110     #define TIMER0_PRESCALER_64 BV(CS02)
111     #define TIMER2_PRESCALER_64 (BV(CS21) | BV(CS20))
112 #else
113     #define TIMER0_PRESCALER_64 (BV(CS01) | BV(CS00))
114     #define TIMER2_PRESCALER_64 BV(CS22)
115 #endif
116
117 /** HW dependent timer initialization  */
118 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
119
120         void timer_hw_init(void)
121         {
122                 cpu_flags_t flags;
123                 IRQ_SAVE_DISABLE(flags);
124
125                 /* Reset Timer flags */
126                 REG_TIFR0 = BV(BIT_OCF0A) | BV(TOV0);
127
128                 /* Setup Timer/Counter interrupt */
129                 ASSR = 0x00;                  /* Internal system clock */
130
131                 REG_TCCR0A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
132                 REG_TCCR0B = 0;
133
134                 REG_TCCR0A = BV(WGM01);             /* Clear on Compare match */
135                         #if TIMER_PRESCALER == 64
136                         REG_TCCR0B |= TIMER0_PRESCALER_64;
137                         #else
138                                 #error Unsupported value of TIMER_PRESCALER
139                         #endif
140                 ;
141                 TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
142                 REG_OCR0A = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
143
144                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
145                 REG_TIMSK0 &= ~BV(TOIE0);
146                 REG_TIMSK0 |= BV(BIT_OCIE0A);
147
148                 IRQ_RESTORE(flags);
149         }
150
151 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
152
153         void timer_hw_init(void)
154         {
155                 cpu_flags_t flags;
156                 IRQ_SAVE_DISABLE(flags);
157
158                 /* Reset Timer overflow flag */
159                 REG_TIFR1 |= BV(TOV1);
160
161                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
162                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
163                         TCCR1A |= BV(WGM11);
164                         TCCR1A &= ~BV(WGM10);
165                         TCCR1B |= BV(WGM12) | BV(CS10);
166                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
167                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
168                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
169                         TCCR1A |= BV(WGM10);
170                         TCCR1A &= ~BV(WGM11);
171                         TCCR1B |= BV(WGM12) | BV(CS10);
172                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
173                 #else
174                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
175                 #endif
176
177                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
178
179                 /* Enable timer interrupt: Timer/Counter1 Overflow */
180                 REG_TIMSK1 |= BV(TOIE1);
181
182                 IRQ_RESTORE(flags);
183         }
184
185 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
186         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 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
218
219         void timer_hw_init(void)
220         {
221                 cpu_flags_t flags;
222                 IRQ_SAVE_DISABLE(flags);
223
224                 /* Reset Timer overflow flag */
225                 REG_TIFR3 |= BV(TOV3);
226
227                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
228                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
229                         TCCR3A |= BV(WGM31);
230                         TCCR3A &= ~BV(WGM30);
231                         TCCR3B |= BV(WGM32) | BV(CS30);
232                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
233                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
234                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
235                         TCCR3A |= BV(WGM30);
236                         TCCR3A &= ~BV(WGM31);
237                         TCCR3B |= BV(WGM32) | BV(CS30);
238                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
239                 #else
240                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
241                 #endif
242
243                 /* initialization of Timer/Counter */
244                 TCNT3 = 0x00;
245
246                 /* Enable timer interrupt: Timer/Counter3 Overflow */
247                 REG_TIMSK3 = |= BV(TOIE3);
248
249                 IRQ_RESTORE(flags);
250         }
251
252 #else
253         #error Unimplemented value for CONFIG_TIMER
254 #endif /* CONFIG_TIMER */
255