2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 volatile AO_TICK_TYPE ao_tick_count;
21 static volatile AO_TICK_TYPE ao_wakeup_count;
23 ISR(TIMER1_COMPA_vect)
27 if ((int16_t) (ao_tick_count - ao_wakeup_count) >= 0)
28 ao_wakeup((void *) &ao_tick_count);
46 CLKPR = (1 << CLKPCE);
50 /* Overall division ratio is 512 * 125,
51 * so our 8MHz base clock ends up as a 125Hz
54 TCCR1 = ((1 << CTC1) | /* Clear timer on match */
55 (0 << PWM1A) | /* Not PWM mode */
56 (0 << COM1A0) | /* Don't change output pins */
57 (0 << COM1A1) | /* ... */
58 (1 << CS13) | /* Prescale by 512 */
59 (0 << CS12) | /* ... */
60 (1 << CS11) | /* ... */
61 (0 << CS10)); /* ... */
62 GTCCR = ((0 << PWM1B) | /* Not PWM mode */
63 (0 << COM1B1) | /* Don't change output pins */
64 (0 << COM1B0) | /* ... */
65 (0 << FOC1B) | /* Don't force output compare */
66 (0 << FOC1A) | /* ... */
67 (0 << PSR1)); /* Don't bother to reset scaler */
71 OCR1C = 124; /* Divide by as many 5s as we can (5^3 = 125) */
73 TIMSK = ((1 << OCIE1A) | /* Enable TIMER1_COMPA interrupt */
74 (0 << OCIE1B) | /* Disable TIMER1_COMPB interrupt */
75 (0 << TOIE1)); /* Disable TIMER1_OVF interrupt */
80 #define US_LOOPS ((AVR_CLOCK / 1000000) / PER_LOOP)
82 void ao_delay_us(uint16_t us)
92 /* A bit funky to keep the optimizer
93 * from short-circuiting the test */
94 if (!((uint8_t) (us | (us >> 8))))
100 ao_delay_until(uint16_t target)
103 ao_wakeup_count = target;
104 while ((int16_t) (target - ao_tick_count) > 0)
105 ao_sleep((void *) &ao_tick_count);
110 ao_delay(uint16_t ticks)
112 ao_delay_until(ao_time() + ticks);