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.3 2006/06/12 21:37:02 marco
19 *#* implemented some commands (ver and sleep)
21 *#* Revision 1.2 2006/05/18 00:37:58 bernie
22 *#* Don't include unneeded header hw.h.
24 *#* Revision 1.1 2005/07/19 07:28:36 bernie
25 *#* Refactor to decouple timer ticks from milliseconds.
27 *#* Revision 1.1 2005/05/24 09:17:58 batt
28 *#* Move drivers to top-level.
31 #include <drv/timer_avr.h>
32 #include <cfg/macros.h> // BV()
34 #include <avr/interrupt.h>
37 /*! HW dependent timer initialization */
38 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
40 static void timer_hw_init(void)
43 IRQ_SAVE_DISABLE(flags);
45 /* Reset Timer flags */
46 TIFR = BV(OCF0) | BV(TOV0);
48 /* Setup Timer/Counter interrupt */
49 ASSR = 0x00; /* Internal system clock */
50 TCCR0 = BV(WGM01) /* Clear on Compare match */
51 #if TIMER_PRESCALER == 64
54 #error Unsupported value of TIMER_PRESCALER
57 TCNT0 = 0x00; /* Initialization of Timer/Counter */
58 OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
60 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
67 INLINE hptime_t timer_hw_hpread(void)
72 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
74 static void timer_hw_init(void)
77 IRQ_SAVE_DISABLE(flags);
79 /* Reset Timer overflow flag */
82 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
83 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
86 TCCR1B |= BV(WGM12) | BV(CS10);
87 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
88 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
89 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
92 TCCR1B |= BV(WGM12) | BV(CS10);
93 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
95 #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
98 TCNT1 = 0x00; /* initialization of Timer/Counter */
100 /* Enable timer interrupt: Timer/Counter1 Overflow */
106 INLINE hptime_t timer_hw_hpread(void)
111 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
113 static void timer_hw_init(void)
116 IRQ_SAVE_DISABLE(flags);
118 /* Reset Timer flags */
119 TIFR = BV(OCF2) | BV(TOV2);
121 /* Setup Timer/Counter interrupt */
123 #if TIMER_PRESCALER == 64
124 | BV(CS21) | BV(CS20)
126 #error Unsupported value of TIMER_PRESCALER
129 /* Clear on Compare match & prescaler = 64, internal sys clock.
130 When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
131 TCNT2 = 0x00; /* initialization of Timer/Counter */
132 OCR2 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
134 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
141 INLINE hptime_t timer_hw_hpread(void)
145 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
147 static void timer_hw_init(void)
150 IRQ_SAVE_DISABLE(flags);
152 /* Reset Timer overflow flag */
155 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
156 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
158 TCCR3A &= ~BV(WGM30);
159 TCCR3B |= BV(WGM32) | BV(CS30);
160 TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
161 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
162 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
164 TCCR3A &= ~BV(WGM31);
165 TCCR3B |= BV(WGM32) | BV(CS30);
166 TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
168 #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
171 TCNT3 = 0x00; /* initialization of Timer/Counter */
173 /* Enable timer interrupt: Timer/Counter3 Overflow */
174 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
180 INLINE hptime_t timer_hw_hpread(void)
186 #error Unimplemented value for CONFIG_TIMER
187 #endif /* CONFIG_TIMER */