4 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
10 * \author Bernardo Innocenti <bernie@develer.com>
11 * \author Francesco Sacchi <batt@develer.com>
13 * \brief Low-level timer module for AVR (implementation).
18 *#* Revision 1.2 2006/05/18 00:37:58 bernie
19 *#* Don't include unneeded header hw.h.
21 *#* Revision 1.1 2005/07/19 07:28:36 bernie
22 *#* Refactor to decouple timer ticks from milliseconds.
24 *#* Revision 1.1 2005/05/24 09:17:58 batt
25 *#* Move drivers to top-level.
28 #include <drv/timer_avr.h>
29 #include <cfg/macros.h> // BV()
31 #include <avr/signal.h>
34 /*! HW dependent timer initialization */
35 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
37 static void timer_hw_init(void)
40 IRQ_SAVE_DISABLE(flags);
42 /* Reset Timer flags */
43 TIFR = BV(OCF0) | BV(TOV0);
45 /* Setup Timer/Counter interrupt */
46 ASSR = 0x00; /* Internal system clock */
47 TCCR0 = BV(WGM01) /* Clear on Compare match */
48 #if TIMER_PRESCALER == 64
51 #error Unsupported value of TIMER_PRESCALER
54 TCNT0 = 0x00; /* Initialization of Timer/Counter */
55 OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
57 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
64 INLINE hptime_t timer_hw_hpread(void)
69 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
71 static void timer_hw_init(void)
74 IRQ_SAVE_DISABLE(flags);
76 /* Reset Timer overflow flag */
79 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
80 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
83 TCCR1B |= BV(WGM12) | BV(CS10);
84 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
85 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
86 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
89 TCCR1B |= BV(WGM12) | BV(CS10);
90 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
92 #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
95 TCNT1 = 0x00; /* initialization of Timer/Counter */
97 /* Enable timer interrupt: Timer/Counter1 Overflow */
103 INLINE hptime_t timer_hw_hpread(void)
108 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
110 static void timer_hw_init(void)
113 IRQ_SAVE_DISABLE(flags);
115 /* Reset Timer flags */
116 TIFR = BV(OCF2) | BV(TOV2);
118 /* Setup Timer/Counter interrupt */
120 #if TIMER_PRESCALER == 64
121 | BV(CS21) | BV(CS20)
123 #error Unsupported value of TIMER_PRESCALER
126 /* Clear on Compare match & prescaler = 64, internal sys clock.
127 When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
128 TCNT2 = 0x00; /* initialization of Timer/Counter */
129 OCR2 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
131 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
138 INLINE hptime_t timer_hw_hpread(void)
142 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
144 static void timer_hw_init(void)
147 IRQ_SAVE_DISABLE(flags);
149 /* Reset Timer overflow flag */
152 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
153 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
155 TCCR3A &= ~BV(WGM30);
156 TCCR3B |= BV(WGM32) | BV(CS30);
157 TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
158 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
159 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
161 TCCR3A &= ~BV(WGM31);
162 TCCR3B |= BV(WGM32) | BV(CS30);
163 TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
165 #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
168 TCNT3 = 0x00; /* initialization of Timer/Counter */
170 /* Enable timer interrupt: Timer/Counter3 Overflow */
171 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
177 INLINE hptime_t timer_hw_hpread(void)
183 #error Unimplemented value for CONFIG_TIMER
184 #endif /* CONFIG_TIMER */