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