src-avr: Get USB on AVR working
[fw/altos] / src-avr / ao_timer.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 static volatile __data uint16_t ao_tick_count;
21
22 uint16_t ao_time(void) __critical
23 {
24         return ao_tick_count;
25 }
26
27 static __xdata uint8_t ao_forever;
28
29 void
30 ao_delay(uint16_t ticks)
31 {
32         ao_alarm(ticks);
33         ao_sleep(&ao_forever);
34 }
35
36 #define T1_CLOCK_DIVISOR        8       /* 24e6/8 = 3e6 */
37 #define T1_SAMPLE_TIME          30000   /* 3e6/30000 = 100 */
38
39 #if HAS_ADC
40 volatile __data uint8_t ao_adc_interval = 1;
41 volatile __data uint8_t ao_adc_count;
42 #endif
43
44 #ifdef AVR
45 ISR(TIMER1_COMPA_vect)
46 #else
47 void ao_timer_isr(void) __interrupt(9)
48 #endif
49 {
50         ++ao_tick_count;
51 #if HAS_ADC
52         if (++ao_adc_count == ao_adc_interval) {
53                 ao_adc_count = 0;
54                 ao_adc_poll();
55         }
56 #endif
57 }
58
59 #if HAS_ADC
60 void
61 ao_timer_set_adc_interval(uint8_t interval) __critical
62 {
63         ao_adc_interval = interval;
64         ao_adc_count = 0;
65 }
66 #endif
67
68 void
69 ao_timer_init(void)
70 {
71 #ifdef AVR
72         TCCR1A = ((0 << WGM11) |        /* CTC mode, OCR1A */
73                   (0 << WGM10));        /* CTC mode, OCR1A */
74         TCCR1B = ((0 << ICNC1) |        /* no input capture noise canceler */
75                   (0 << ICES1) |        /* input capture on falling edge (don't care) */
76                   (0 << WGM13) |        /* CTC mode, OCR1A */
77                   (1 << WGM12) |        /* CTC mode, OCR1A */
78                   (3 << CS10));         /* clk/64 from prescaler */
79
80 #if TEENSY
81         OCR1A = 2500;                   /* 16MHz clock */
82 #else
83         OCR1A = 1250;                   /* 8MHz clock */
84 #endif
85
86         TIMSK1 = (1 << OCIE1A);         /* Interrupt on compare match */
87 #else
88         /* NOTE:  This uses a timer only present on cc1111 architecture. */
89
90         /* disable timer 1 */
91         T1CTL = 0;
92
93         /* set the sample rate */
94         T1CC0H = T1_SAMPLE_TIME >> 8;
95         T1CC0L = (uint8_t) T1_SAMPLE_TIME;
96
97         T1CCTL0 = T1CCTL_MODE_COMPARE;
98         T1CCTL1 = 0;
99         T1CCTL2 = 0;
100
101         /* clear timer value */
102         T1CNTL = 0;
103
104         /* enable overflow interrupt */
105         OVFIM = 1;
106         /* enable timer 1 interrupt */
107         T1IE = 1;
108
109         /* enable timer 1 in module mode, dividing by 8 */
110         T1CTL = T1CTL_MODE_MODULO | T1CTL_DIV_8;
111 #endif
112 }
113
114 /*
115  * AltOS always cranks the clock to the max frequency
116  */
117 void
118 ao_clock_init(void)
119 {
120 #ifdef AVR
121         /* disable RC clock */
122         CLKSEL0 &= ~(1 << RCE);
123
124         /* Disable PLL */
125         PLLCSR &= ~(1 << PLLE);
126
127         /* Enable external clock */
128         CLKSEL0 |= (1 << EXTE);
129
130         /* wait for external clock to be ready */
131         while ((CLKSTA & (1 << EXTON)) == 0)
132                 ;
133
134         /* select external clock */
135         CLKSEL0 |= (1 << CLKS);
136
137         /* Disable the clock prescaler */
138         cli();
139         CLKPR = (1 << CLKPCE);
140         CLKPR = 0;
141         sei();
142
143         /* Set up the PLL to use the crystal */
144
145         /* Use primary system clock as PLL source */
146         PLLFRQ = ((0 << PINMUX) |       /* Use primary clock */
147                   (0 << PLLUSB) |       /* No divide by 2 for USB */
148                   (0 << PLLTM0) |       /* Disable high speed timer */
149                   (0x4 << PDIV0));      /* 48MHz PLL clock */
150
151         /* Set the frequency of the crystal */
152 #if AVR_CLOCK > 12000000UL
153         PLLCSR |= (1 << PINDIV);        /* For 16MHz crystal on Teensy board */
154 #else
155         PLLCSR &= ~(1 << PINDIV);       /* For 8MHz crystal on TeleScience board */
156 #endif
157
158         /* Enable the PLL */
159         PLLCSR |= (1 << PLLE);
160         while (!(PLLCSR & (1 << PLOCK)))
161                 ;
162 #else
163         /* Switch system clock to crystal oscilator */
164         CLKCON = (CLKCON & ~CLKCON_OSC_MASK) | (CLKCON_OSC_XTAL);
165
166         while (!(SLEEP & SLEEP_XOSC_STB))
167                 ;
168
169         /* Crank up the timer tick and system clock speed */
170         CLKCON = ((CLKCON & ~(CLKCON_TICKSPD_MASK | CLKCON_CLKSPD_MASK)) |
171                   (CLKCON_TICKSPD_1 | CLKCON_CLKSPD_1));
172
173         while ((CLKCON & (CLKCON_TICKSPD_MASK|CLKCON_CLKSPD_MASK)) !=
174                (CLKCON_TICKSPD_1 | CLKCON_CLKSPD_1))
175                 ;
176 #endif
177 }