Finally remove redundant protos.
[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.6  2004/07/20 23:48:16  bernie
19  * Finally remove redundant protos.
20  *
21  * Revision 1.5  2004/07/18 22:16:35  bernie
22  * Add missing header; Prevent warning for project_ks-specific code.
23  *
24  * Revision 1.4  2004/06/27 15:22:15  aleph
25  * Fix spacing
26  *
27  * Revision 1.3  2004/06/07 15:57:40  aleph
28  * Update to latest AVR timer code
29  *
30  * Revision 1.2  2004/06/03 11:27:09  bernie
31  * Add dual-license information.
32  *
33  * Revision 1.1  2004/05/23 18:23:30  bernie
34  * Import drv/timer module.
35  *
36  */
37
38 #ifndef DRV_TIMER_AVR_H
39 #define DRV_TIMER_AVR_H
40
41 #include <avr/wdt.h>
42 #include <avr/signal.h>
43
44 #if defined(ARCH_BOARD_KC) && (ARCH & ARCH_BOARD_KC)
45         #include <drv/adc.h>
46 #endif
47
48
49 #define timer_hw_irq()  /* Not needed, IRQ timer flag cleared automatically */
50
51 /*!
52  * System timer: additional division after the prescaler
53  * 12288000 / 64 / 192 (0..191) = 1 ms
54  */
55 #define OCR_DIVISOR 191
56
57 /*! HW dependent timer initialization  */
58 #if defined(CONFIG_TIMER_ON_TIMER0)
59
60         //! Type of time expressed in ticks of the hardware high-precision timer
61         typedef uint8_t hptime_t;
62
63         static void timer_hw_init(void)
64         {
65                 cpuflags_t flags;
66                 DISABLE_IRQSAVE(flags);
67
68                 /* Reset Timer flags */
69                 TIFR = BV(OCF0) | BV(TOV0);
70
71                 /* Setup Timer/Counter interrupt */
72                 ASSR = 0x00;                  /* internal system clock */
73                 TCCR0 = BV(WGM01) | BV(CS02); /* Clear on Compare match & prescaler = 64. When changing
74                                                  prescaler change TIMER_HW_HPTICKS_PER_SEC too */
75                 TCNT0 = 0x00;                 /* initialization of Timer/Counter */
76                 OCR0 = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
77
78                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
79                 TIMSK &= ~BV(TOIE0);
80                 TIMSK |= BV(OCIE0);
81
82                 ENABLE_IRQRESTORE(flags);
83         }
84
85         //! Frequency of the hardware high precision timer
86         #define TIMER_HW_HPTICKS_PER_SEC  (CLOCK_FREQ / 64)
87
88         INLINE hptime_t timer_hw_hpread(void)
89         {
90                 return TCNT0;
91         }
92
93 #elif defined(CONFIG_TIMER_ON_TIMER1_OVERFLOW)
94
95         //! Type of time expressed in ticks of the hardware high precision timer
96         typedef uint16_t hptime_t;
97
98         static void timer_hw_init(void)
99         {
100                 cpuflags_t flags;
101                 DISABLE_IRQSAVE(flags);
102
103                 /* Reset Timer overflow flag */
104                 TIFR |= BV(TOV1);
105
106                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. When changing freq or
107                    resolution (top of TCNT), change TIMER_HW_HPTICKS_PER_SEC too */
108                 TCCR1A |= BV(WGM11);
109                 TCCR1A &= ~BV(WGM10);
110                 TCCR1B |= BV(WGM12) | BV(CS10);
111                 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
112
113                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
114
115                 /* Enable timer interrupt: Timer/Counter1 Overflow */
116                 TIMSK |= BV(TOIE1);
117
118                 ENABLE_IRQRESTORE(flags);
119         }
120
121         //! Frequency of the hardware high precision timer
122         #define TIMER_HW_HPTICKS_PER_SEC  (24000ul * 512)
123
124         INLINE hptime_t timer_hw_hpread(void)
125         {
126                 return TCNT1;
127         }
128
129 #elif defined(CONFIG_TIMER_ON_TIMER2)
130
131         //! Type of time expressed in ticks of the hardware high precision timer
132         typedef uint8_t hptime_t;
133
134         static void timer_hw_init(void)
135         {
136                 cpuflags_t flags;
137                 DISABLE_IRQSAVE(flags);
138
139                 /* Reset Timer flags */
140                 TIFR = BV(OCF2) | BV(TOV2);
141
142                 /* Setup Timer/Counter interrupt */
143                 TCCR2 = BV(WGM21) | BV(CS21) | BV(CS20);
144                 /* Clear on Compare match & prescaler = 64, internal sys clock.
145                    When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
146                 TCNT2 = 0x00;         /* initialization of Timer/Counter */
147                 OCR2 = OCR_DIVISOR;   /* Timer/Counter Output Compare Register */
148
149                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
150                 TIMSK &= ~BV(TOIE2);
151                 TIMSK |= BV(OCIE2);
152
153                 ENABLE_IRQRESTORE(flags);
154         }
155
156         //! Frequency of the hardware high precision timer
157         #define TIMER_HW_HPTICKS_PER_SEC  (CLOCK_FREQ / 64)
158
159         INLINE hptime_t timer_hw_hpread(void)
160         {
161                 return TCNT2;
162         }
163
164 #else
165         #error Choose witch timer to use with CONFIG_TIMER_ON_TIMERx
166 #endif /* CONFIG_TIMER_ON_TIMERx */
167
168
169 #if defined(CONFIG_TIMER_ON_TIMER1_OVERFLOW)
170
171         #define DEFINE_TIMER_ISR        \
172                 static void timer_handler(void)
173
174         DEFINE_TIMER_ISR;
175
176         /*
177          * Timer 1 overflow irq handler. It's called at the frequency of the timer 1
178          * PWM (should be 24 kHz). It's too much for timer purposes, so the interrupt
179          * handler is really a counter that call the true handler in timer.c
180          * every 1 ms.
181          */
182         SIGNAL(SIG_OVERFLOW1)
183         {
184                 /*!
185                  * How many overflow we have to count before calling the true timer handler.
186                  * If timer overflow is at 24 kHz, with a value of 24 we have 1 ms between
187                  * each call.
188                  */
189                 #define TIMER1_OVF_COUNT 24
190
191                 static uint8_t count = TIMER1_OVF_COUNT;
192
193                 count--;
194                 if (!count)
195                 {
196                         timer_handler();
197                         count = TIMER1_OVF_COUNT;
198                 }
199
200         #if (ARCH & ARCH_BOARD_KC)
201                 /*
202                  * Super-optimization-hack: switch CPU ADC mux here, ASAP after the start
203                  * of conversion (auto-triggered with timer 1 overflow).
204                  * The switch can be done 2 ADC cycles after start of conversion.
205                  * The handler prologue takes a little more than 32 CPU cycles: with
206                  * the prescaler at 1/16 the timing should be correct even at the start
207                  * of the handler.
208                  *
209                  * The switch is synchronized with the ADC handler using _adc_trigger_lock.
210                  */
211                 extern uint8_t _adc_idx_next;
212                 extern bool _adc_trigger_lock;
213
214                 if (!_adc_trigger_lock)
215                 {
216                         TIMER_STROBE_ON;
217                         ADC_SETCHN(_adc_idx_next);
218                         TIMER_STROBE_OFF;
219                         _adc_trigger_lock = true;
220                 }
221         #endif
222         }
223
224 #elif defined (CONFIG_TIMER_ON_TIMER0)
225
226         #define DEFINE_TIMER_ISR        \
227                 SIGNAL(SIG_OUTPUT_COMPARE0)
228
229 #elif defined(CONFIG_TIMER_ON_TIMER2)
230
231         #define DEFINE_TIMER_ISR        \
232                 SIGNAL(SIG_OUTPUT_COMPARE2)
233
234 #else
235         #error Choose witch timer to use with CONFIG_TIMER_ON_TIMERx
236 #endif /* CONFIG_TIMER_ON_TIMERx */
237
238 #endif /* DRV_TIMER_AVR_H */