Remove bogus Doxygen markup.
[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.14  2004/08/24 16:27:01  bernie
19  * Add missing headers.
20  *
21  * Revision 1.13  2004/08/24 14:30:11  bernie
22  * Use new-style config macros for drv/timer.c
23  *
24  * Revision 1.12  2004/08/10 06:59:45  bernie
25  * CONFIG_TIMER_STROBE: Define no-op default macros.
26  *
27  * Revision 1.11  2004/08/03 15:53:17  aleph
28  * Fix spacing
29  *
30  * Revision 1.10  2004/08/02 20:20:29  aleph
31  * Merge from project_ks
32  *
33  * Revision 1.9  2004/07/22 02:01:14  bernie
34  * Use TIMER_PRESCALER consistently.
35  */
36 #ifndef DRV_TIMER_AVR_H
37 #define DRV_TIMER_AVR_H
38
39 #include <arch_config.h> // ARCH_BOARD_KC
40 #include <avr/wdt.h>
41 #include <avr/signal.h>
42
43 #if defined(ARCH_BOARD_KC) && (ARCH & ARCH_BOARD_KC)
44         #include <drv/adc.h>
45 #endif
46
47
48 /*!
49  * Values for CONFIG_TIMER.
50  *
51  * Select which hardware timer interrupt to use for system clock and softtimers.
52  * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
53  */
54 #define TIMER_ON_OUTPUT_COMPARE0  1
55 #define TIMER_ON_OVERFLOW1        2
56 #define TIMER_ON_OUTPUT_COMPARE2  3
57
58
59 /*!
60  * \def CONFIG_TIMER_STROBE
61  *
62  * This is a debug facility that can be used to
63  * monitor timer interrupt activity on an external pin.
64  *
65  * To use strobes, redefine the macros TIMER_STROBE_ON,
66  * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
67  * CONFIG_TIMER_STROBE to 1.
68  */
69 #ifndef CONFIG_TIMER_STROBE
70         #define TIMER_STROBE_ON    do {/*nop*/} while(0)
71         #define TIMER_STROBE_OFF   do {/*nop*/} while(0)
72         #define TIMER_STROBE_INIT  do {/*nop*/} while(0)
73 #endif
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                 /*!
226                  * How many overflow we have to count before calling the true timer handler.
227                  * If timer overflow is at 24 kHz, with a value of 24 we have 1 ms between
228                  * each call.
229                  */
230                 #define TIMER1_OVF_COUNT 24
231
232                 static uint8_t count = TIMER1_OVF_COUNT;
233
234                 count--;
235                 if (!count)
236                 {
237                         timer_handler();
238                         count = TIMER1_OVF_COUNT;
239                 }
240
241         #if (ARCH & ARCH_BOARD_KC)
242                 /*
243                  * Super-optimization-hack: switch CPU ADC mux here, ASAP after the start
244                  * of conversion (auto-triggered with timer 1 overflow).
245                  * The switch can be done 2 ADC cycles after start of conversion.
246                  * The handler prologue takes a little more than 32 CPU cycles: with
247                  * the prescaler at 1/16 the timing should be correct even at the start
248                  * of the handler.
249                  *
250                  * The switch is synchronized with the ADC handler using _adc_trigger_lock.
251                  */
252                 extern uint8_t _adc_idx_next;
253                 extern bool _adc_trigger_lock;
254
255                 if (!_adc_trigger_lock)
256                 {
257                         TIMER_STROBE_ON;
258                         ADC_SETCHN(_adc_idx_next);
259                         TIMER_STROBE_OFF;
260                         _adc_trigger_lock = true;
261                 }
262         #endif
263         }
264
265 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
266
267         #define DEFINE_TIMER_ISR        \
268                 SIGNAL(SIG_OUTPUT_COMPARE0)
269
270 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
271
272         #define DEFINE_TIMER_ISR        \
273                 SIGNAL(SIG_OUTPUT_COMPARE2)
274
275 #else
276         #error Unimplemented value for CONFIG_TIMER
277 #endif /* CONFIG_TIMER */
278
279 #endif /* DRV_TIMER_AVR_H */