b6771a92da8984b5d2985ac79ec4ff4f075d2480
[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_TCCR2A TCCR2A
62         #define REG_TCCR2B TCCR2B
63
64         #define REG_OCR2A  OCR2A
65
66         #define BIT_OCF0A  OCF0A
67         #define BIT_OCF2A  OCF2A
68
69         #define BIT_OCIE0A OCIE0A
70         #define BIT_OCIE2A OCIE2A
71 #else
72         #define REG_TIFR0 TIFR
73         #define REG_TIFR2 TIFR
74
75         #define REG_TIMSK0 TIMSK
76         #define REG_TIMSK2 TIMSK
77
78         #define REG_TCCR2A TCCR2
79         #define REG_TCCR2B TCCR2
80
81         #define REG_OCR2A  OCR2
82
83         #define BIT_OCF0A  OCF0
84         #define BIT_OCF2A  OCF2
85
86         #define BIT_OCIE0A OCIE0
87         #define BIT_OCIE2A OCIE2
88 #endif
89
90
91 /** HW dependent timer initialization  */
92 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
93
94         static void timer_hw_init(void)
95         {
96                 cpuflags_t flags;
97                 IRQ_SAVE_DISABLE(flags);
98
99                 /* Reset Timer flags */
100                 REG_TIFR0 = BV(BIT_OCF0A) | BV(TOV0);
101
102                 /* Setup Timer/Counter interrupt */
103                 ASSR = 0x00;                  /* Internal system clock */
104                 TCCR0 = BV(WGM01)             /* Clear on Compare match */
105                         #if TIMER_PRESCALER == 64
106                                 | BV(CS02)
107                         #else
108                                 #error Unsupported value of TIMER_PRESCALER
109                         #endif
110                 ;
111                 TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
112                 OCR0 = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
113
114                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
115                 REG_TIMSK0 &= ~BV(TOIE0);
116                 REG_TIMSK0 |= BV(OCIE0);
117
118                 IRQ_RESTORE(flags);
119         }
120
121         INLINE hptime_t timer_hw_hpread(void)
122         {
123                 return TCNT0;
124         }
125
126 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
127
128         static void timer_hw_init(void)
129         {
130                 cpuflags_t flags;
131                 IRQ_SAVE_DISABLE(flags);
132
133                 /* Reset Timer overflow flag */
134                 TIFR |= BV(TOV1);
135
136                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
137                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
138                         TCCR1A |= BV(WGM11);
139                         TCCR1A &= ~BV(WGM10);
140                         TCCR1B |= BV(WGM12) | BV(CS10);
141                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
142                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
143                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
144                         TCCR1A |= BV(WGM10);
145                         TCCR1A &= ~BV(WGM11);
146                         TCCR1B |= BV(WGM12) | BV(CS10);
147                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
148                 #else
149                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
150                 #endif
151
152                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
153
154                 /* Enable timer interrupt: Timer/Counter1 Overflow */
155                 TIMSK |= BV(TOIE1);
156
157                 IRQ_RESTORE(flags);
158         }
159
160         INLINE hptime_t timer_hw_hpread(void)
161         {
162                 return TCNT1;
163         }
164
165 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
166         static void timer_hw_init(void)
167         {
168                 cpuflags_t flags;
169                 IRQ_SAVE_DISABLE(flags);
170
171                 /* Reset Timer flags */
172                 REG_TIFR2 = BV(BIT_OCF2A) | BV(TOV2);
173
174                 /* Setup Timer/Counter interrupt */
175                 REG_TCCR2A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
176                 REG_TCCR2B = 0; // ensure correct initialization.
177
178                 REG_TCCR2A = BV(WGM21);
179                 #if TIMER_PRESCALER == 64
180                 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
181                         // ATMega1281 & ATMega168 have undocumented differences in timer2 prescaler!
182                         REG_TCCR2B |= BV(CS22);
183                 #else
184                         REG_TCCR2B |= BV(CS21) | BV(CS20);
185                 #endif
186                 #else
187                         #error Unsupported value of TIMER_PRESCALER
188                 #endif
189
190                 /* Clear on Compare match & prescaler = 64, internal sys clock.
191                    When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
192                 TCNT2 = 0x00;         /* initialization of Timer/Counter */
193                 REG_OCR2A = OCR_DIVISOR;   /* Timer/Counter Output Compare Register */
194
195                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
196                 REG_TIMSK2 &= ~BV(TOIE2);
197                 REG_TIMSK2 |= BV(BIT_OCIE2A);
198
199                 IRQ_RESTORE(flags);
200         }
201
202         INLINE hptime_t timer_hw_hpread(void)
203         {
204                 return TCNT2;
205         }
206 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
207
208         static void timer_hw_init(void)
209         {
210                 cpuflags_t flags;
211                 IRQ_SAVE_DISABLE(flags);
212
213                 /* Reset Timer overflow flag */
214                 TIFR |= BV(TOV3);
215
216                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
217                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
218                         TCCR3A |= BV(WGM31);
219                         TCCR3A &= ~BV(WGM30);
220                         TCCR3B |= BV(WGM32) | BV(CS30);
221                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
222                 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
223                 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
224                         TCCR3A |= BV(WGM30);
225                         TCCR3A &= ~BV(WGM31);
226                         TCCR3B |= BV(WGM32) | BV(CS30);
227                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
228                 #else
229                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
230                 #endif
231
232                 TCNT3 = 0x00;         /* initialization of Timer/Counter */
233
234                 /* Enable timer interrupt: Timer/Counter3 Overflow */
235                 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
236                 ETIMSK |= BV(TOIE3);
237
238                 IRQ_RESTORE(flags);
239         }
240
241         INLINE hptime_t timer_hw_hpread(void)
242         {
243                 return TCNT3;
244         }
245
246 #else
247         #error Unimplemented value for CONFIG_TIMER
248 #endif /* CONFIG_TIMER */
249