Drop almost all the Qt3 cruft.
[bertos.git] / drv / timer_avr.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  * \author Francesco Sacchi <batt@develer.com>
12  *
13  * \brief Low-level timer module for AVR (implementation).
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.2  2006/05/18 00:37:58  bernie
19  *#* Don't include unneeded header hw.h.
20  *#*
21  *#* Revision 1.1  2005/07/19 07:28:36  bernie
22  *#* Refactor to decouple timer ticks from milliseconds.
23  *#*
24  *#* Revision 1.1  2005/05/24 09:17:58  batt
25  *#* Move drivers to top-level.
26  *#*
27  *#*/
28 #include <drv/timer_avr.h>
29 #include <cfg/macros.h> // BV()
30
31 #include <avr/signal.h>
32 #include <avr/io.h>
33
34 /*! HW dependent timer initialization  */
35 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
36
37         static void timer_hw_init(void)
38         {
39                 cpuflags_t flags;
40                 IRQ_SAVE_DISABLE(flags);
41
42                 /* Reset Timer flags */
43                 TIFR = BV(OCF0) | BV(TOV0);
44
45                 /* Setup Timer/Counter interrupt */
46                 ASSR = 0x00;                  /* Internal system clock */
47                 TCCR0 = BV(WGM01)             /* Clear on Compare match */
48                         #if TIMER_PRESCALER == 64
49                                 | BV(CS02)
50                         #else
51                                 #error Unsupported value of TIMER_PRESCALER
52                         #endif
53                 ;
54                 TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
55                 OCR0 = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
56
57                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
58                 TIMSK &= ~BV(TOIE0);
59                 TIMSK |= BV(OCIE0);
60
61                 IRQ_RESTORE(flags);
62         }
63
64         INLINE hptime_t timer_hw_hpread(void)
65         {
66                 return TCNT0;
67         }
68
69 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
70
71         static void timer_hw_init(void)
72         {
73                 cpuflags_t flags;
74                 IRQ_SAVE_DISABLE(flags);
75
76                 /* Reset Timer overflow flag */
77                 TIFR |= BV(TOV1);
78
79                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
80                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
81                         TCCR1A |= BV(WGM11);
82                         TCCR1A &= ~BV(WGM10);
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)
87                         TCCR1A |= BV(WGM10);
88                         TCCR1A &= ~BV(WGM11);
89                         TCCR1B |= BV(WGM12) | BV(CS10);
90                         TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
91                 #else
92                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
93                 #endif
94
95                 TCNT1 = 0x00;         /* initialization of Timer/Counter */
96
97                 /* Enable timer interrupt: Timer/Counter1 Overflow */
98                 TIMSK |= BV(TOIE1);
99
100                 IRQ_RESTORE(flags);
101         }
102
103         INLINE hptime_t timer_hw_hpread(void)
104         {
105                 return TCNT1;
106         }
107
108 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
109
110         static void timer_hw_init(void)
111         {
112                 cpuflags_t flags;
113                 IRQ_SAVE_DISABLE(flags);
114
115                 /* Reset Timer flags */
116                 TIFR = BV(OCF2) | BV(TOV2);
117
118                 /* Setup Timer/Counter interrupt */
119                 TCCR2 = BV(WGM21)
120                         #if TIMER_PRESCALER == 64
121                                 | BV(CS21) | BV(CS20)
122                         #else
123                                 #error Unsupported value of TIMER_PRESCALER
124                         #endif
125                 ;
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 */
130
131                 /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
132                 TIMSK &= ~BV(TOIE2);
133                 TIMSK |= BV(OCIE2);
134
135                 IRQ_RESTORE(flags);
136         }
137
138         INLINE hptime_t timer_hw_hpread(void)
139         {
140                 return TCNT2;
141         }
142 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
143
144         static void timer_hw_init(void)
145         {
146                 cpuflags_t flags;
147                 IRQ_SAVE_DISABLE(flags);
148
149                 /* Reset Timer overflow flag */
150                 TIFR |= BV(TOV3);
151
152                 /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
153                 #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
154                         TCCR3A |= BV(WGM31);
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)
160                         TCCR3A |= BV(WGM30);
161                         TCCR3A &= ~BV(WGM31);
162                         TCCR3B |= BV(WGM32) | BV(CS30);
163                         TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
164                 #else
165                         #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
166                 #endif
167
168                 TCNT3 = 0x00;         /* initialization of Timer/Counter */
169
170                 /* Enable timer interrupt: Timer/Counter3 Overflow */
171                 /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */
172                 ETIMSK |= BV(TOIE3);
173
174                 IRQ_RESTORE(flags);
175         }
176
177         INLINE hptime_t timer_hw_hpread(void)
178         {
179                 return TCNT3;
180         }
181
182 #else
183         #error Unimplemented value for CONFIG_TIMER
184 #endif /* CONFIG_TIMER */
185