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.14 2004/08/24 16:27:01 bernie
19 * Add missing headers.
21 * Revision 1.13 2004/08/24 14:30:11 bernie
22 * Use new-style config macros for drv/timer.c
24 * Revision 1.12 2004/08/10 06:59:45 bernie
25 * CONFIG_TIMER_STROBE: Define no-op default macros.
27 * Revision 1.11 2004/08/03 15:53:17 aleph
30 * Revision 1.10 2004/08/02 20:20:29 aleph
31 * Merge from project_ks
33 * Revision 1.9 2004/07/22 02:01:14 bernie
34 * Use TIMER_PRESCALER consistently.
36 #ifndef DRV_TIMER_AVR_H
37 #define DRV_TIMER_AVR_H
39 #include <arch_config.h> // ARCH_BOARD_KC
41 #include <avr/signal.h>
43 #if defined(ARCH_BOARD_KC) && (ARCH & ARCH_BOARD_KC)
49 * Values for CONFIG_TIMER.
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.
54 #define TIMER_ON_OUTPUT_COMPARE0 1
55 #define TIMER_ON_OVERFLOW1 2
56 #define TIMER_ON_OUTPUT_COMPARE2 3
60 * \def CONFIG_TIMER_STROBE
62 * This is a debug facility that can be used to
63 * monitor timer interrupt activity on an external pin.
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.
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)
76 /* Not needed, IRQ timer flag cleared automatically */
77 #define timer_hw_irq() do {} while (0)
79 #define TIMER_PRESCALER 64
82 * System timer: additional division after the prescaler
83 * 12288000 / 64 / 192 (0..191) = 1 ms
85 #define OCR_DIVISOR (CLOCK_FREQ / TIMER_PRESCALER / TICKS_PER_SEC - 1) /* 191 */
87 /*! HW dependent timer initialization */
88 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
90 //! Type of time expressed in ticks of the hardware high-precision timer
91 typedef uint8_t hptime_t;
93 static void timer_hw_init(void)
96 DISABLE_IRQSAVE(flags);
98 /* Reset Timer flags */
99 TIFR = BV(OCF0) | BV(TOV0);
101 /* Setup Timer/Counter interrupt */
102 ASSR = 0x00; /* Internal system clock */
103 TCCR0 = BV(WGM01) /* Clear on Compare match */
104 #if TIMER_PRESCALER == 64
107 #error Unsupported value of TIMER_PRESCALER
110 TCNT0 = 0x00; /* Initialization of Timer/Counter */
111 OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
113 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
117 ENABLE_IRQRESTORE(flags);
120 //! Frequency of the hardware high precision timer
121 #define TIMER_HW_HPTICKS_PER_SEC (CLOCK_FREQ / TIMER_PRESCALER)
123 INLINE hptime_t timer_hw_hpread(void)
128 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
130 //! Type of time expressed in ticks of the hardware high precision timer
131 typedef uint16_t hptime_t;
133 static void timer_hw_init(void)
136 DISABLE_IRQSAVE(flags);
138 /* Reset Timer overflow flag */
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 */
144 TCCR1A &= ~BV(WGM10);
145 TCCR1B |= BV(WGM12) | BV(CS10);
146 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
148 TCNT1 = 0x00; /* initialization of Timer/Counter */
150 /* Enable timer interrupt: Timer/Counter1 Overflow */
153 ENABLE_IRQRESTORE(flags);
156 //! Frequency of the hardware high precision timer
157 #define TIMER_HW_HPTICKS_PER_SEC (24000ul * 512)
159 INLINE hptime_t timer_hw_hpread(void)
164 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
166 //! Type of time expressed in ticks of the hardware high precision timer
167 typedef uint8_t hptime_t;
169 static void timer_hw_init(void)
172 DISABLE_IRQSAVE(flags);
174 /* Reset Timer flags */
175 TIFR = BV(OCF2) | BV(TOV2);
177 /* Setup Timer/Counter interrupt */
179 #if TIMER_PRESCALER == 64
180 | BV(CS21) | BV(CS20)
182 #error Unsupported value of TIMER_PRESCALER
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 */
190 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
194 ENABLE_IRQRESTORE(flags);
197 //! Frequency of the hardware high precision timer
198 #define TIMER_HW_HPTICKS_PER_SEC (CLOCK_FREQ / TIMER_PRESCALER)
200 INLINE hptime_t timer_hw_hpread(void)
206 #error Unimplemented value for CONFIG_TIMER
207 #endif /* CONFIG_TIMER */
210 #if (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
212 #define DEFINE_TIMER_ISR \
213 static void timer_handler(void)
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
223 SIGNAL(SIG_OVERFLOW1)
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
230 #define TIMER1_OVF_COUNT 24
232 static uint8_t count = TIMER1_OVF_COUNT;
238 count = TIMER1_OVF_COUNT;
241 #if (ARCH & ARCH_BOARD_KC)
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
250 * The switch is synchronized with the ADC handler using _adc_trigger_lock.
252 extern uint8_t _adc_idx_next;
253 extern bool _adc_trigger_lock;
255 if (!_adc_trigger_lock)
258 ADC_SETCHN(_adc_idx_next);
260 _adc_trigger_lock = true;
265 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
267 #define DEFINE_TIMER_ISR \
268 SIGNAL(SIG_OUTPUT_COMPARE0)
270 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
272 #define DEFINE_TIMER_ISR \
273 SIGNAL(SIG_OUTPUT_COMPARE2)
276 #error Unimplemented value for CONFIG_TIMER
277 #endif /* CONFIG_TIMER */
279 #endif /* DRV_TIMER_AVR_H */