2e3f5a7389d3c910ae80e8e79dfdca7745c3dad3
[fw/altos] / ao-bringup-avr / ao-switch.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 #define AO_STACK_SIZE   128
163
164 uint8_t new_stack[512];
165 int     stack_count;
166
167 #define PUSH8(stack, val)       (*--(stack) = (val))
168
169 #define PUSH16(stack, val)      PUSH8(stack, ((uint16_t) (val))); PUSH8(stack, ((uint16_t) (val)) >> 8)
170
171 void
172 count(int count)
173 {
174
175 }
176
177 void
178 blink(void)
179 {
180         while (1) {
181                 LEDPORT ^= (1 << LEDOUT);
182                 _delay_ms(200);
183         }
184 }
185
186 void
187 function(void)
188 {
189         return;
190 }
191
192 void
193 init_stack(void (*f) (void))
194 {
195         uint8_t         *stack = new_stack + AO_STACK_SIZE;
196         uint8_t         i;
197
198         /* Return address */
199         PUSH16(stack, f);
200
201 #if 0
202         /* Clear register values */
203         i = 32;
204         while (i--)
205                 PUSH8(stack, 0);
206
207         /* SREG with interrupts enabled */
208         PUSH8(stack, 0x80);
209 #endif
210         stack_count = stack - new_stack;
211 }
212
213 void
214 switch_stack(void) __attribute__((naked));
215
216 void
217 switch_stack(void)
218 {
219 #if 0
220         uint16_t        a;
221         uint8_t         l, h;
222
223         a = (uint16_t) function;
224         l = a;
225         h = a >> 8;
226         asm("push %0" : : "r" (l));
227         asm("push %0" : : "r" (h));
228         asm("ret");
229 #endif
230 #if 0
231         asm("push r31; push r30");
232         asm("push r29; push r28; push r27; push r26; push r25");
233         asm("push r24; push r23; push r22; push r21; push r20");
234         asm("push r19; push r18; push r17; push r16; push r15");
235         asm("push r14; push r13; push r12; push r11; push r10");
236         asm("push r9; push r8; push r7; push r6; push r5");
237         asm("push r4; push r3; push r2; push r1; push r0");
238         asm("in r0, __SREG__" "\n\t"
239             "push r0");
240 #endif
241         uint8_t *sp = (new_stack + stack_count);
242         uint8_t sp_l, sp_h;
243
244         sp_l = (uint16_t) sp;
245         sp_h = ((uint16_t) sp) >> 8;
246         asm("out __SP_H__,%0" : : "r" (sp_h) );
247         asm("out __SP_L__,%0" : : "r" (sp_l) );
248         blink();
249 #if 0
250         asm("pop r0"    "\n\t"
251             "out __SREG__, r0");
252         asm("pop r0; pop r1; pop r2; pop r3; pop r4");
253         asm("pop r5; pop r6; pop r7; pop r8; pop r9");
254         asm("pop r10; pop r11; pop r12; pop r13; pop r14");
255         asm("pop r15; pop r16; pop r17; pop r18; pop r19");
256         asm("pop r20; pop r21; pop r22; pop r23; pop r24");
257         asm("pop r25; pop r26; pop r27; pop r28; pop r29");
258         asm("pop r30; pop r31");
259 #endif
260         asm("ret");
261 }
262
263 void back(void)
264 {
265         switch_stack();
266         blink();
267 }
268
269 void main(void)
270 {
271 #if 0
272         clock_init();
273         adc_init();
274         timer1_init();
275         timer0_init();
276 #endif
277
278         LEDDDR |= (1 << LEDDDRPIN);
279
280         init_stack(blink);
281         switch_stack();
282 }