DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
[bertos.git] / drv / timer_avr.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Low-level timer module for AVR
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.21  2004/12/13 12:07:06  bernie
19  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
20  *#*
21  *#* Revision 1.20  2004/11/16 20:59:46  bernie
22  *#* Include <avr/io.h> explicitly.
23  *#*
24  *#* Revision 1.19  2004/10/19 08:56:41  bernie
25  *#* TIMER_STROBE_ON, TIMER_STROBE_OFF, TIMER_STROBE_INIT: Move from timer_avr.h to timer.h, where they really belong.
26  *#*
27  *#* Revision 1.18  2004/09/20 03:31:03  bernie
28  *#* Fix racy racy code.
29  *#*
30  *#* Revision 1.17  2004/09/14 21:07:09  bernie
31  *#* Include hw.h explicitly.
32  *#*
33  *#* Revision 1.16  2004/09/06 21:49:26  bernie
34  *#* CONFIG_TIMER_STROBE: be tolerant with missing optional macro.
35  *#*
36  *#* Revision 1.15  2004/08/25 14:12:08  rasky
37  *#* Aggiornato il comment block dei log RCS
38  *#*
39  *#* Revision 1.14  2004/08/24 16:27:01  bernie
40  *#* Add missing headers.
41  *#*
42  *#* Revision 1.13  2004/08/24 14:30:11  bernie
43  *#* Use new-style config macros for drv/timer.c
44  *#*
45  *#* Revision 1.12  2004/08/10 06:59:45  bernie
46  *#* CONFIG_TIMER_STROBE: Define no-op default macros.
47  *#*
48  *#* Revision 1.11  2004/08/03 15:53:17  aleph
49  *#* Fix spacing
50  *#*
51  *#* Revision 1.10  2004/08/02 20:20:29  aleph
52  *#* Merge from project_ks
53  *#*
54  *#* Revision 1.9  2004/07/22 02:01:14  bernie
55  *#* Use TIMER_PRESCALER consistently.
56  *#*/
57 #ifndef DRV_TIMER_AVR_H
58 #define DRV_TIMER_AVR_H
59
60 #include <arch_config.h> // ARCH_BOARD_KC
61 #include "hw.h"
62
63 #include <avr/signal.h>
64 #include <avr/io.h>
65
66 #if defined(ARCH_BOARD_KC) && (ARCH & ARCH_BOARD_KC)
67         #include <drv/adc.h>
68 #endif
69
70
71 /*!
72  * Values for CONFIG_TIMER.
73  *
74  * Select which hardware timer interrupt to use for system clock and softtimers.
75  * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
76  */
77 #define TIMER_ON_OUTPUT_COMPARE0  1
78 #define TIMER_ON_OVERFLOW1        2
79 #define TIMER_ON_OUTPUT_COMPARE2  3
80
81
82 /* Not needed, IRQ timer flag cleared automatically */
83 #define timer_hw_irq() do {} while (0)
84
85 #define TIMER_PRESCALER 64
86
87 /*!
88  * System timer: additional division after the prescaler
89  * 12288000 / 64 / 192 (0..191) = 1 ms
90  */
91 #define OCR_DIVISOR  (CLOCK_FREQ / TIMER_PRESCALER / TICKS_PER_SEC - 1) /* 191 */
92
93 /*! HW dependent timer initialization  */
94 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
95
96         //! Type of time expressed in ticks of the hardware high-precision timer
97         typedef uint8_t hptime_t;
98
99         static void timer_hw_init(void)
100         {
101                 cpuflags_t flags;
102                 IRQ_SAVE_DISABLE(flags);
103
104                 /* Reset Timer flags */
105                 TIFR = BV(OCF0) | BV(TOV0);
106
107                 /* Setup Timer/Counter interrupt */
108                 ASSR = 0x00;                  /* Internal system clock */
109                 TCCR0 = BV(WGM01)             /* Clear on Compare match */
110                         #if TIMER_PRESCALER == 64
111                                 | BV(CS02)
112                         #else
113                                 #error Unsupported value of TIMER_PRESCALER
114                         #endif
115                 ;
116                 TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
117                 OCR0 = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
118
119                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
120                 TIMSK &= ~BV(TOIE0);
121                 TIMSK |= BV(OCIE0);
122
123                 IRQ_RESTORE(flags);
124         }
125
126         //! Frequency of the hardware high precision timer
127         #define TIMER_HW_HPTICKS_PER_SEC  (CLOCK_FREQ / TIMER_PRESCALER)
128
129         INLINE hptime_t timer_hw_hpread(void)
130         {
131                 return TCNT0;
132         }
133
134 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
135
136         //! Type of time expressed in ticks of the hardware high precision timer
137         typedef uint16_t hptime_t;
138
139         static void timer_hw_init(void)
140         {
141                 cpuflags_t flags;
142                 IRQ_SAVE_DISABLE(flags);
143
144                 /* Reset Timer overflow flag */
145                 TIFR |= BV(TOV1);
146
147                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. When changing freq or
148                    resolution (top of TCNT), change TIMER_HW_HPTICKS_PER_SEC too */
149                 TCCR1A |= BV(WGM11);
150                 TCCR1A &= ~BV(WGM10);
151                 TCCR1B |= BV(WGM12) | BV(CS10);
152                 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
153
154                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
155
156                 /* Enable timer interrupt: Timer/Counter1 Overflow */
157                 TIMSK |= BV(TOIE1);
158
159                 IRQ_RESTORE(flags);
160         }
161
162         //! Frequency of the hardware high precision timer
163         #define TIMER_HW_HPTICKS_PER_SEC  (24000ul * 512)
164
165         INLINE hptime_t timer_hw_hpread(void)
166         {
167                 return TCNT1;
168         }
169
170 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
171
172         //! Type of time expressed in ticks of the hardware high precision timer
173         typedef uint8_t hptime_t;
174
175         static void timer_hw_init(void)
176         {
177                 cpuflags_t flags;
178                 IRQ_SAVE_DISABLE(flags);
179
180                 /* Reset Timer flags */
181                 TIFR = BV(OCF2) | BV(TOV2);
182
183                 /* Setup Timer/Counter interrupt */
184                 TCCR2 = BV(WGM21)
185                         #if TIMER_PRESCALER == 64
186                                 | BV(CS21) | BV(CS20)
187                         #else
188                                 #error Unsupported value of TIMER_PRESCALER
189                         #endif
190                 ;
191                 /* Clear on Compare match & prescaler = 64, internal sys clock.
192                    When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
193                 TCNT2 = 0x00;         /* initialization of Timer/Counter */
194                 OCR2 = OCR_DIVISOR;   /* Timer/Counter Output Compare Register */
195
196                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
197                 TIMSK &= ~BV(TOIE2);
198                 TIMSK |= BV(OCIE2);
199
200                 IRQ_RESTORE(flags);
201         }
202
203         //! Frequency of the hardware high precision timer
204         #define TIMER_HW_HPTICKS_PER_SEC  (CLOCK_FREQ / TIMER_PRESCALER)
205
206         INLINE hptime_t timer_hw_hpread(void)
207         {
208                 return TCNT2;
209         }
210
211 #else
212         #error Unimplemented value for CONFIG_TIMER
213 #endif /* CONFIG_TIMER */
214
215
216 #if (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
217
218         #define DEFINE_TIMER_ISR        \
219                 static void timer_handler(void)
220
221         DEFINE_TIMER_ISR;
222
223         /*
224          * Timer 1 overflow irq handler. It's called at the frequency of the timer 1
225          * PWM (should be 24 kHz). It's too much for timer purposes, so the interrupt
226          * handler is really a counter that call the true handler in timer.c
227          * every 1 ms.
228          */
229         SIGNAL(SIG_OVERFLOW1)
230         {
231         #if (ARCH & ARCH_BOARD_KC)
232                 /*
233                  * Super-optimization-hack: switch CPU ADC mux here, ASAP after the start
234                  * of conversion (auto-triggered with timer 1 overflow).
235                  * The switch can be done 2 ADC cycles after start of conversion.
236                  * The handler prologue takes a little more than 32 CPU cycles: with
237                  * the prescaler at 1/16 the timing should be correct even at the start
238                  * of the handler.
239                  *
240                  * The switch is synchronized with the ADC handler using _adc_trigger_lock.
241                  *
242                  *      Mel (A Real Programmer)
243                  */
244                 extern uint8_t _adc_idx_next;
245                 extern bool _adc_trigger_lock;
246
247                 if (!_adc_trigger_lock)
248                 {
249                         /*
250                          * Disable free-running mode to avoid starting a
251                          * new conversion before the ADC handler has read
252                          * the ongoing one.  This condition could occur
253                          * under very high interrupt load and would have the
254                          * unwanted effect of reading from the wrong ADC
255                          * channel.
256                          *
257                          * NOTE: writing 0 to ADSC and ADIF has no effect.
258                          */
259                         ADCSRA = ADCSRA & ~(BV(ADFR) | BV(ADIF) | BV(ADSC));
260
261                         ADC_SETCHN(_adc_idx_next);
262                         _adc_trigger_lock = true;
263                 }
264         #endif // ARCH_BOARD_KC
265
266                 /*!
267                  * How many timer overflows we must count before calling the real
268                  * timer handler.
269                  * When the timer is programmed to overflow at 24 kHz, a value of
270                  * 24 will result in 1ms between each call.
271                  */
272                 #define TIMER1_OVF_COUNT 24
273                 //#warning TIMER1_OVF_COUNT for timer at 12 kHz
274                 //#define TIMER1_OVF_COUNT 12
275
276                 static uint8_t count = TIMER1_OVF_COUNT;
277
278                 count--;
279                 if (!count)
280                 {
281                         timer_handler();
282                         count = TIMER1_OVF_COUNT;
283                 }
284         }
285
286 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
287
288         #define DEFINE_TIMER_ISR        \
289                 SIGNAL(SIG_OUTPUT_COMPARE0)
290
291 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
292
293         #define DEFINE_TIMER_ISR        \
294                 SIGNAL(SIG_OUTPUT_COMPARE2)
295
296 #else
297         #error Unimplemented value for CONFIG_TIMER
298 #endif /* CONFIG_TIMER */
299
300 #endif /* DRV_TIMER_AVR_H */