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.1 2005/07/19 07:28:36 bernie
19 *#* Refactor to decouple timer ticks from milliseconds.
21 *#* Revision 1.1 2005/05/24 09:17:58 batt
22 *#* Move drivers to top-level.
25 #include <drv/timer_avr.h>
26 #include <arch_config.h> // ARCH_BOARD_KC
27 #include <cfg/macros.h> // BV()
30 #include <avr/signal.h>
33 /*! HW dependent timer initialization */
34 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
36 static void timer_hw_init(void)
39 IRQ_SAVE_DISABLE(flags);
41 /* Reset Timer flags */
42 TIFR = BV(OCF0) | BV(TOV0);
44 /* Setup Timer/Counter interrupt */
45 ASSR = 0x00; /* Internal system clock */
46 TCCR0 = BV(WGM01) /* Clear on Compare match */
47 #if TIMER_PRESCALER == 64
50 #error Unsupported value of TIMER_PRESCALER
53 TCNT0 = 0x00; /* Initialization of Timer/Counter */
54 OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
56 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
63 INLINE hptime_t timer_hw_hpread(void)
68 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
70 static void timer_hw_init(void)
73 IRQ_SAVE_DISABLE(flags);
75 /* Reset Timer overflow flag */
78 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
79 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
82 TCCR1B |= BV(WGM12) | BV(CS10);
83 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
84 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
85 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
88 TCCR1B |= BV(WGM12) | BV(CS10);
89 TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
91 #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
94 TCNT1 = 0x00; /* initialization of Timer/Counter */
96 /* Enable timer interrupt: Timer/Counter1 Overflow */
102 INLINE hptime_t timer_hw_hpread(void)
107 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
109 static void timer_hw_init(void)
112 IRQ_SAVE_DISABLE(flags);
114 /* Reset Timer flags */
115 TIFR = BV(OCF2) | BV(TOV2);
117 /* Setup Timer/Counter interrupt */
119 #if TIMER_PRESCALER == 64
120 | BV(CS21) | BV(CS20)
122 #error Unsupported value of TIMER_PRESCALER
125 /* Clear on Compare match & prescaler = 64, internal sys clock.
126 When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
127 TCNT2 = 0x00; /* initialization of Timer/Counter */
128 OCR2 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */
130 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
137 INLINE hptime_t timer_hw_hpread(void)
141 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
143 static void timer_hw_init(void)
146 IRQ_SAVE_DISABLE(flags);
148 /* Reset Timer overflow flag */
151 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
152 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
154 TCCR3A &= ~BV(WGM30);
155 TCCR3B |= BV(WGM32) | BV(CS30);
156 TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
157 /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
158 #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
160 TCCR3A &= ~BV(WGM31);
161 TCCR3B |= BV(WGM32) | BV(CS30);
162 TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
164 #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
167 TCNT3 = 0x00; /* initialization of Timer/Counter */
169 /* Enable timer interrupt: Timer/Counter3 Overflow */
170 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
176 INLINE hptime_t timer_hw_hpread(void)
182 #error Unimplemented value for CONFIG_TIMER
183 #endif /* CONFIG_TIMER */