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.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Low-level timer module for AVR
18 *#* Revision 1.16 2004/09/06 21:49:26 bernie
19 *#* CONFIG_TIMER_STROBE: be tolerant with missing optional macro.
21 *#* Revision 1.15 2004/08/25 14:12:08 rasky
22 *#* Aggiornato il comment block dei log RCS
24 *#* Revision 1.14 2004/08/24 16:27:01 bernie
25 *#* Add missing headers.
27 *#* Revision 1.13 2004/08/24 14:30:11 bernie
28 *#* Use new-style config macros for drv/timer.c
30 *#* Revision 1.12 2004/08/10 06:59:45 bernie
31 *#* CONFIG_TIMER_STROBE: Define no-op default macros.
33 *#* Revision 1.11 2004/08/03 15:53:17 aleph
36 *#* Revision 1.10 2004/08/02 20:20:29 aleph
37 *#* Merge from project_ks
39 *#* Revision 1.9 2004/07/22 02:01:14 bernie
40 *#* Use TIMER_PRESCALER consistently.
42 #ifndef DRV_TIMER_AVR_H
43 #define DRV_TIMER_AVR_H
45 #include <arch_config.h> // ARCH_BOARD_KC
47 #include <avr/signal.h>
49 #if defined(ARCH_BOARD_KC) && (ARCH & ARCH_BOARD_KC)
55 * Values for CONFIG_TIMER.
57 * Select which hardware timer interrupt to use for system clock and softtimers.
58 * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
60 #define TIMER_ON_OUTPUT_COMPARE0 1
61 #define TIMER_ON_OVERFLOW1 2
62 #define TIMER_ON_OUTPUT_COMPARE2 3
66 * \def CONFIG_TIMER_STROBE
68 * This is a debug facility that can be used to
69 * monitor timer interrupt activity on an external pin.
71 * To use strobes, redefine the macros TIMER_STROBE_ON,
72 * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
73 * CONFIG_TIMER_STROBE to 1.
75 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
76 #define TIMER_STROBE_ON do {/*nop*/} while(0)
77 #define TIMER_STROBE_OFF do {/*nop*/} while(0)
78 #define TIMER_STROBE_INIT do {/*nop*/} while(0)
82 /* Not needed, IRQ timer flag cleared automatically */
83 #define timer_hw_irq() do {} while (0)
85 #define TIMER_PRESCALER 64
88 * System timer: additional division after the prescaler
89 * 12288000 / 64 / 192 (0..191) = 1 ms
91 #define OCR_DIVISOR (CLOCK_FREQ / TIMER_PRESCALER / TICKS_PER_SEC - 1) /* 191 */
93 /*! HW dependent timer initialization */
94 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
96 //! Type of time expressed in ticks of the hardware high-precision timer
97 typedef uint8_t hptime_t;
99 static void timer_hw_init(void)
102 DISABLE_IRQSAVE(flags);
104 /* Reset Timer flags */
105 TIFR = BV(OCF0) | BV(TOV0);
107 /* Setup Timer/Counter interrupt */
108 ASSR = 0x00; /* Internal system clock */
109 TCCR0 = BV(WGM01) /* Clear on Compare match */
110 #if TIMER_PRESCALER == 64
113 #error Unsupported value of TIMER_PRESCALER
116 TCNT0 = 0x00; /* Initialization of Timer/Counter */
117 OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
119 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
123 ENABLE_IRQRESTORE(flags);
126 //! Frequency of the hardware high precision timer
127 #define TIMER_HW_HPTICKS_PER_SEC (CLOCK_FREQ / TIMER_PRESCALER)
129 INLINE hptime_t timer_hw_hpread(void)
134 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
136 //! Type of time expressed in ticks of the hardware high precision timer
137 typedef uint16_t hptime_t;
139 static void timer_hw_init(void)
142 DISABLE_IRQSAVE(flags);
144 /* Reset Timer overflow flag */
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 */
150 TCCR1A &= ~BV(WGM10);
151 TCCR1B |= BV(WGM12) | BV(CS10);
152 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
154 TCNT1 = 0x00; /* initialization of Timer/Counter */
156 /* Enable timer interrupt: Timer/Counter1 Overflow */
159 ENABLE_IRQRESTORE(flags);
162 //! Frequency of the hardware high precision timer
163 #define TIMER_HW_HPTICKS_PER_SEC (24000ul * 512)
165 INLINE hptime_t timer_hw_hpread(void)
170 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
172 //! Type of time expressed in ticks of the hardware high precision timer
173 typedef uint8_t hptime_t;
175 static void timer_hw_init(void)
178 DISABLE_IRQSAVE(flags);
180 /* Reset Timer flags */
181 TIFR = BV(OCF2) | BV(TOV2);
183 /* Setup Timer/Counter interrupt */
185 #if TIMER_PRESCALER == 64
186 | BV(CS21) | BV(CS20)
188 #error Unsupported value of TIMER_PRESCALER
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 */
196 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
200 ENABLE_IRQRESTORE(flags);
203 //! Frequency of the hardware high precision timer
204 #define TIMER_HW_HPTICKS_PER_SEC (CLOCK_FREQ / TIMER_PRESCALER)
206 INLINE hptime_t timer_hw_hpread(void)
212 #error Unimplemented value for CONFIG_TIMER
213 #endif /* CONFIG_TIMER */
216 #if (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
218 #define DEFINE_TIMER_ISR \
219 static void timer_handler(void)
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
229 SIGNAL(SIG_OVERFLOW1)
232 * How many overflow we have to count before calling the true timer handler.
233 * If timer overflow is at 24 kHz, with a value of 24 we have 1 ms between
236 #define TIMER1_OVF_COUNT 24
238 static uint8_t count = TIMER1_OVF_COUNT;
244 count = TIMER1_OVF_COUNT;
247 #if (ARCH & ARCH_BOARD_KC)
249 * Super-optimization-hack: switch CPU ADC mux here, ASAP after the start
250 * of conversion (auto-triggered with timer 1 overflow).
251 * The switch can be done 2 ADC cycles after start of conversion.
252 * The handler prologue takes a little more than 32 CPU cycles: with
253 * the prescaler at 1/16 the timing should be correct even at the start
256 * The switch is synchronized with the ADC handler using _adc_trigger_lock.
258 extern uint8_t _adc_idx_next;
259 extern bool _adc_trigger_lock;
261 if (!_adc_trigger_lock)
264 ADC_SETCHN(_adc_idx_next);
266 _adc_trigger_lock = true;
271 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
273 #define DEFINE_TIMER_ISR \
274 SIGNAL(SIG_OUTPUT_COMPARE0)
276 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
278 #define DEFINE_TIMER_ISR \
279 SIGNAL(SIG_OUTPUT_COMPARE2)
282 #error Unimplemented value for CONFIG_TIMER
283 #endif /* CONFIG_TIMER */
285 #endif /* DRV_TIMER_AVR_H */