ao-bringup-avr: clean up alternate stack code
[fw/altos] / ao-bringup-avr / ao-blink.c
1 /*
2  * Copyright © 2011 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 <avr/io.h>
19 #include <avr/interrupt.h>
20 #define TEENSY 0
21 #if TEENSY
22 #define F_CPU 16000000UL        // 16 MHz
23 #else
24 #define F_CPU  8000000UL        // 8 MHz
25 #endif
26 #include <util/delay.h>
27
28 #define LEDOUT          PORTB7
29 #define LEDPORT         PORTB
30 #define LEDDDR          DDRB
31 #define LEDDDRPIN       DD7
32
33 static void
34 adc_start(void);
35
36 ISR(TIMER1_COMPA_vect)
37 {
38         adc_start();
39 }
40
41 static void
42 timer1_init(void)
43 {
44         TCCR1A = ((0 << WGM11) |        /* CTC mode, OCR1A */
45                   (0 << WGM10));        /* CTC mode, OCR1A */
46         TCCR1B = ((0 << ICNC1) |        /* no input capture noise canceler */
47                   (0 << ICES1) |        /* input capture on falling edge (don't care) */
48                   (0 << WGM13) |        /* CTC mode, OCR1A */
49                   (1 << WGM12) |        /* CTC mode, OCR1A */
50                   (3 << CS10));         /* clk/64 from prescaler */
51
52 #if TEENSY
53         OCR1A = 2500;                   /* 16MHz clock */
54 #else
55         OCR1A = 1250;                   /* 8MHz clock */
56 #endif
57
58         TIMSK1 = (1 << OCIE1A);         /* Interrupt on compare match */
59 }
60
61 static void
62 clock_init(void)
63 {
64         /* disable RC clock */
65         CLKSEL0 &= ~(1 << RCE);
66
67         /* Disable PLL */
68         PLLCSR &= ~(1 << PLLE);
69
70         /* Enable external clock */
71         CLKSEL0 |= (1 << EXTE);
72
73         /* wait for external clock to be ready */
74         while ((CLKSTA & (1 << EXTON)) == 0)
75                 ;
76
77         /* select external clock */
78         CLKSEL0 |= (1 << CLKS);
79
80         /* Disable the clock prescaler */
81         cli();
82         CLKPR = (1 << CLKPCE);
83         CLKPR = 0;
84         sei();
85
86         /* Set up the PLL to use the crystal */
87
88         /* Use primary system clock as PLL source */
89         PLLFRQ = ((0 << PINMUX) |       /* Use primary clock */
90                   (0 << PLLUSB) |       /* No divide by 2 for USB */
91                   (0 << PLLTM0) |       /* Disable high speed timer */
92                   (0x4 << PDIV0));      /* 48MHz PLL clock */
93
94         /* Set the frequency of the crystal */
95 #if TEENSY
96         PLLCSR |= (1 << PINDIV);        /* For 16MHz crystal on Teensy board */
97 #else
98         PLLCSR &= ~(1 << PINDIV);       /* For 8MHz crystal on TeleScience board */
99 #endif
100
101         /* Enable the PLL */
102         PLLCSR |= (1 << PLLE);
103         while (!(PLLCSR & (1 << PLOCK)))
104                 ;
105 }
106
107 static void
108 timer0_init(void)
109 {
110         TCCR0A = ((1 << COM0A1) |
111                   (0 << COM0A0) |
112                   (1 << WGM01) |
113                   (1 << WGM00));
114
115         OCR0A = 0x10;
116         OCR0B = 0;
117         TIMSK0 = 0;
118
119         TCCR0B = ((0 << WGM02) |
120                   (5 << CS00));
121 }
122
123 ISR(ADC_vect)
124 {
125         OCR0A = ADCH;
126 }
127
128 #define ADC_CHANNEL     0x25            /* Channel ADC13 */
129 #define ADC_CHANNEL_LOW(c)      (((c) & 0x1f) << MUX0)
130 #define ADC_CHANNEL_HIGH(c)     ((((c) & 0x20) >> 5) << MUX5)
131
132 #define ADCSRA_INIT     ((1 << ADEN) |          /* Enable ADC */                \
133                          (0 << ADATE) |         /* No auto ADC trigger */       \
134                          (1 << ADIE) |          /* Enable interrupt */  \
135                          (6 << ADPS0))          /* Prescale clock by 64 */
136
137 #define ADCSRB_INIT     ((0 << ADHSM) |         /* No high-speed mode */ \
138                          (0 << ACME) |          /* Some comparitor thing */ \
139                          (0 << ADTS0))          /* Free running mode (don't care) */
140
141 static void
142 adc_start(void)
143 {
144         ADMUX = ((0 << REFS1) |                         /* AVcc reference */
145                  (1 << REFS0) |                         /* AVcc reference */
146                  (1 << ADLAR) |                         /* Left-shift results */
147                  (ADC_CHANNEL_LOW(ADC_CHANNEL)));       /* Select channel */
148
149         ADCSRB = ADCSRB_INIT | ADC_CHANNEL_HIGH(ADC_CHANNEL);
150
151         ADCSRA = ADCSRA_INIT | (1 << ADSC);             /* Start conversion */
152 }
153
154 static void
155 adc_init(void)
156 {
157         ADCSRA = ADCSRA_INIT;
158         ADCSRB = ADCSRB_INIT;
159         DIDR0 |= (1 << 0);
160 }
161
162 int main(void)
163 {
164         clock_init();
165         adc_init();
166         timer1_init();
167         timer0_init();
168
169         LEDDDR |= (1 << LEDDDRPIN);
170
171         for (;;)
172                 ;
173 }