bzr branch
http://bzr.ed.am/elec/audio-switcher
3
by Tim Marston
added timer utilities (wiring.c) from arduino library |
1 |
/* |
2 |
wiring.c - Partial implementation of the Wiring API for the ATmega8. |
|
3 |
Part of Arduino - http://www.arduino.cc/ |
|
4 |
||
5 |
Copyright (c) 2005-2006 David A. Mellis |
|
6 |
||
7 |
This library is free software; you can redistribute it and/or |
|
8 |
modify it under the terms of the GNU Lesser General Public |
|
9 |
License as published by the Free Software Foundation; either |
|
10 |
version 2.1 of the License, or (at your option) any later version. |
|
11 |
||
12 |
This library is distributed in the hope that it will be useful, |
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
Lesser General Public License for more details. |
|
16 |
||
17 |
You should have received a copy of the GNU Lesser General |
|
18 |
Public License along with this library; if not, write to the |
|
19 |
Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
20 |
Boston, MA 02111-1307 USA |
|
21 |
||
22 |
$Id$ |
|
23 |
*/ |
|
24 |
||
25 |
#include "wiring_private.h" |
|
26 |
||
27 |
||
28 |
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the |
|
29 |
// the overflow handler is called every 256 ticks. |
|
30 |
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) |
|
31 |
||
32 |
// the whole number of milliseconds per timer0 overflow |
|
33 |
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000) |
|
34 |
||
35 |
// the fractional number of milliseconds per timer0 overflow. we shift right |
|
36 |
// by three to fit these numbers into a byte. (for the clock speeds we care |
|
37 |
// about - 8 and 16 MHz - this doesn't lose precision.) |
|
38 |
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3) |
|
39 |
#define FRACT_MAX (1000 >> 3) |
|
40 |
||
41 |
volatile unsigned long timer0_overflow_count = 0; |
|
42 |
volatile unsigned long timer0_millis = 0; |
|
43 |
static unsigned char timer0_fract = 0; |
|
44 |
||
45 |
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) |
|
46 |
SIGNAL(TIM0_OVF_vect) |
|
47 |
#else |
|
48 |
SIGNAL(TIMER0_OVF_vect) |
|
49 |
#endif |
|
50 |
{ |
|
51 |
// copy these to local variables so they can be stored in registers |
|
52 |
// (volatile variables must be read from memory on every access) |
|
53 |
unsigned long m = timer0_millis; |
|
54 |
unsigned char f = timer0_fract; |
|
55 |
||
56 |
m += MILLIS_INC; |
|
57 |
f += FRACT_INC; |
|
58 |
if (f >= FRACT_MAX) { |
|
59 |
f -= FRACT_MAX; |
|
60 |
m += 1; |
|
61 |
} |
|
62 |
||
63 |
timer0_fract = f; |
|
64 |
timer0_millis = m; |
|
65 |
timer0_overflow_count++; |
|
66 |
} |
|
67 |
||
68 |
unsigned long millis(void) |
|
69 |
{ |
|
70 |
unsigned long m; |
|
71 |
uint8_t oldSREG = SREG; |
|
72 |
||
73 |
// disable interrupts while we read timer0_millis or we might get an |
|
74 |
// inconsistent value (e.g. in the middle of a write to timer0_millis) |
|
75 |
cli(); |
|
76 |
m = timer0_millis; |
|
77 |
SREG = oldSREG; |
|
78 |
||
79 |
return m; |
|
80 |
} |
|
81 |
||
82 |
unsigned long micros(void) { |
|
83 |
unsigned long m; |
|
84 |
uint8_t oldSREG = SREG, t; |
|
85 |
||
86 |
cli(); |
|
87 |
m = timer0_overflow_count; |
|
88 |
#if defined(TCNT0) |
|
89 |
t = TCNT0; |
|
90 |
#elif defined(TCNT0L) |
|
91 |
t = TCNT0L; |
|
92 |
#else |
|
93 |
#error TIMER 0 not defined |
|
94 |
#endif |
|
95 |
||
96 |
||
97 |
#ifdef TIFR0 |
|
98 |
if ((TIFR0 & _BV(TOV0)) && (t < 255)) |
|
99 |
m++; |
|
100 |
#else |
|
101 |
if ((TIFR & _BV(TOV0)) && (t < 255)) |
|
102 |
m++; |
|
103 |
#endif |
|
104 |
||
105 |
SREG = oldSREG; |
|
106 |
||
107 |
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond()); |
|
108 |
} |
|
109 |
||
110 |
void delay(unsigned long ms) |
|
111 |
{ |
|
112 |
uint16_t start = (uint16_t)micros(); |
|
113 |
||
114 |
while (ms > 0) { |
|
115 |
if (((uint16_t)micros() - start) >= 1000) { |
|
116 |
ms--; |
|
117 |
start += 1000; |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */ |
|
123 |
void delayMicroseconds(unsigned int us) |
|
124 |
{ |
|
125 |
// calling avrlib's delay_us() function with low values (e.g. 1 or |
|
126 |
// 2 microseconds) gives delays longer than desired. |
|
127 |
//delay_us(us); |
|
128 |
||
129 |
#if F_CPU >= 16000000L |
|
130 |
// for the 16 MHz clock on most Arduino boards |
|
131 |
||
132 |
// for a one-microsecond delay, simply return. the overhead |
|
133 |
// of the function call yields a delay of approximately 1 1/8 us. |
|
134 |
if (--us == 0) |
|
135 |
return; |
|
136 |
||
137 |
// the following loop takes a quarter of a microsecond (4 cycles) |
|
138 |
// per iteration, so execute it four times for each microsecond of |
|
139 |
// delay requested. |
|
140 |
us <<= 2; |
|
141 |
||
142 |
// account for the time taken in the preceeding commands. |
|
143 |
us -= 2; |
|
144 |
#else |
|
145 |
// for the 8 MHz internal clock on the ATmega168 |
|
146 |
||
147 |
// for a one- or two-microsecond delay, simply return. the overhead of |
|
148 |
// the function calls takes more than two microseconds. can't just |
|
149 |
// subtract two, since us is unsigned; we'd overflow. |
|
150 |
if (--us == 0) |
|
151 |
return; |
|
152 |
if (--us == 0) |
|
153 |
return; |
|
154 |
||
155 |
// the following loop takes half of a microsecond (4 cycles) |
|
156 |
// per iteration, so execute it twice for each microsecond of |
|
157 |
// delay requested. |
|
158 |
us <<= 1; |
|
159 |
||
160 |
// partially compensate for the time taken by the preceeding commands. |
|
161 |
// we can't subtract any more than this or we'd overflow w/ small delays. |
|
162 |
us--; |
|
163 |
#endif |
|
164 |
||
165 |
// busy wait |
|
166 |
__asm__ __volatile__ ( |
|
167 |
"1: sbiw %0,1" "\n\t" // 2 cycles |
|
168 |
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles |
|
169 |
); |
|
170 |
} |
|
171 |
||
172 |
void init_wiring_c(void) |
|
173 |
{ |
|
174 |
// this needs to be called before setup() or some functions won't |
|
175 |
// work there |
|
176 |
sei(); |
|
177 |
||
178 |
// on the ATmega168, timer 0 is also used for fast hardware pwm |
|
179 |
// (using phase-correct PWM would mean that timer 0 overflowed half as often |
|
180 |
// resulting in different millis() behavior on the ATmega8 and ATmega168) |
|
181 |
#if defined(TCCR0A) && defined(WGM01) |
|
182 |
sbi(TCCR0A, WGM01); |
|
183 |
sbi(TCCR0A, WGM00); |
|
184 |
#endif |
|
185 |
||
186 |
// set timer 0 prescale factor to 64 |
|
187 |
#if defined(__AVR_ATmega128__) |
|
188 |
// CPU specific: different values for the ATmega128 |
|
189 |
sbi(TCCR0, CS02); |
|
190 |
#elif defined(TCCR0) && defined(CS01) && defined(CS00) |
|
191 |
// this combination is for the standard atmega8 |
|
192 |
sbi(TCCR0, CS01); |
|
193 |
sbi(TCCR0, CS00); |
|
194 |
#elif defined(TCCR0B) && defined(CS01) && defined(CS00) |
|
195 |
// this combination is for the standard 168/328/1280/2560 |
|
196 |
sbi(TCCR0B, CS01); |
|
197 |
sbi(TCCR0B, CS00); |
|
198 |
#elif defined(TCCR0A) && defined(CS01) && defined(CS00) |
|
199 |
// this combination is for the __AVR_ATmega645__ series |
|
200 |
sbi(TCCR0A, CS01); |
|
201 |
sbi(TCCR0A, CS00); |
|
202 |
#else |
|
203 |
#error Timer 0 prescale factor 64 not set correctly |
|
204 |
#endif |
|
205 |
||
206 |
// enable timer 0 overflow interrupt |
|
207 |
#if defined(TIMSK) && defined(TOIE0) |
|
208 |
sbi(TIMSK, TOIE0); |
|
209 |
#elif defined(TIMSK0) && defined(TOIE0) |
|
210 |
sbi(TIMSK0, TOIE0); |
|
211 |
#else |
|
212 |
#error Timer 0 overflow interrupt not set correctly |
|
213 |
#endif |
|
214 |
||
215 |
// timers 1 and 2 are used for phase-correct hardware pwm |
|
216 |
// this is better for motors as it ensures an even waveform |
|
217 |
// note, however, that fast pwm mode can achieve a frequency of up |
|
218 |
// 8 MHz (with a 16 MHz clock) at 50% duty cycle |
|
219 |
||
220 |
#if defined(TCCR1B) && defined(CS11) && defined(CS10) |
|
221 |
TCCR1B = 0; |
|
222 |
||
223 |
// set timer 1 prescale factor to 64 |
|
224 |
sbi(TCCR1B, CS11); |
|
225 |
#if F_CPU >= 8000000L |
|
226 |
sbi(TCCR1B, CS10); |
|
227 |
#endif |
|
228 |
#elif defined(TCCR1) && defined(CS11) && defined(CS10) |
|
229 |
sbi(TCCR1, CS11); |
|
230 |
#if F_CPU >= 8000000L |
|
231 |
sbi(TCCR1, CS10); |
|
232 |
#endif |
|
233 |
#endif |
|
234 |
// put timer 1 in 8-bit phase correct pwm mode |
|
235 |
#if defined(TCCR1A) && defined(WGM10) |
|
236 |
sbi(TCCR1A, WGM10); |
|
237 |
#elif defined(TCCR1) |
|
238 |
#warning this needs to be finished |
|
239 |
#endif |
|
240 |
||
241 |
// set timer 2 prescale factor to 64 |
|
242 |
#if defined(TCCR2) && defined(CS22) |
|
243 |
sbi(TCCR2, CS22); |
|
244 |
#elif defined(TCCR2B) && defined(CS22) |
|
245 |
sbi(TCCR2B, CS22); |
|
246 |
#else |
|
247 |
#warning Timer 2 not finished (may not be present on this CPU) |
|
248 |
#endif |
|
249 |
||
250 |
// configure timer 2 for phase correct pwm (8-bit) |
|
251 |
#if defined(TCCR2) && defined(WGM20) |
|
252 |
sbi(TCCR2, WGM20); |
|
253 |
#elif defined(TCCR2A) && defined(WGM20) |
|
254 |
sbi(TCCR2A, WGM20); |
|
255 |
#else |
|
256 |
#warning Timer 2 not finished (may not be present on this CPU) |
|
257 |
#endif |
|
258 |
||
259 |
#if defined(TCCR3B) && defined(CS31) && defined(WGM30) |
|
260 |
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64 |
|
261 |
sbi(TCCR3B, CS30); |
|
262 |
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode |
|
263 |
#endif |
|
264 |
||
265 |
#if defined(TCCR4B) && defined(CS41) && defined(WGM40) |
|
266 |
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64 |
|
267 |
sbi(TCCR4B, CS40); |
|
268 |
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode |
|
269 |
#endif |
|
270 |
||
271 |
#if defined(TCCR5B) && defined(CS51) && defined(WGM50) |
|
272 |
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64 |
|
273 |
sbi(TCCR5B, CS50); |
|
274 |
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode |
|
275 |
#endif |
|
276 |
||
277 |
#if defined(ADCSRA) |
|
278 |
// set a2d prescale factor to 128 |
|
279 |
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. |
|
280 |
// XXX: this will not work properly for other clock speeds, and |
|
281 |
// this code should use F_CPU to determine the prescale factor. |
|
282 |
sbi(ADCSRA, ADPS2); |
|
283 |
sbi(ADCSRA, ADPS1); |
|
284 |
sbi(ADCSRA, ADPS0); |
|
285 |
||
286 |
// enable a2d conversions |
|
287 |
sbi(ADCSRA, ADEN); |
|
288 |
#endif |
|
289 |
||
290 |
// the bootloader connects pins 0 and 1 to the USART; disconnect them |
|
291 |
// here so they can be used as normal digital i/o; they will be |
|
292 |
// reconnected in Serial.begin() |
|
293 |
#if defined(UCSRB) |
|
294 |
UCSRB = 0; |
|
295 |
#elif defined(UCSR0B) |
|
296 |
UCSR0B = 0; |
|
297 |
#endif |
|
298 |
} |