ao-bringup-avr: Create common 'ao-bringup' interfaces
[fw/altos] / ao-bringup-avr / ao-bringup.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 "ao-bringup.h"
19
20 static void
21 clock_init(void)
22 {
23         /* disable RC clock */
24         CLKSEL0 &= ~(1 << RCE);
25
26         /* Disable PLL */
27         PLLCSR &= ~(1 << PLLE);
28
29         /* Enable external clock */
30         CLKSEL0 |= (1 << EXTE);
31
32         /* wait for external clock to be ready */
33         while ((CLKSTA & (1 << EXTON)) == 0)
34                 ;
35
36         /* select external clock */
37         CLKSEL0 |= (1 << CLKS);
38
39         /* Disable the clock prescaler */
40         cli();
41         CLKPR = (1 << CLKPCE);
42         CLKPR = 0;
43         sei();
44
45         /* Set up the PLL to use the crystal */
46
47         /* Use primary system clock as PLL source */
48         PLLFRQ = ((0 << PINMUX) |       /* Use primary clock */
49                   (0 << PLLUSB) |       /* No divide by 2 for USB */
50                   (0 << PLLTM0) |       /* Disable high speed timer */
51                   (0x4 << PDIV0));      /* 48MHz PLL clock */
52
53         /* Set the frequency of the crystal */
54 #if TEENSY
55         PLLCSR |= (1 << PINDIV);        /* For 16MHz crystal on Teensy board */
56 #else
57         PLLCSR &= ~(1 << PINDIV);       /* For 8MHz crystal on TeleScience board */
58 #endif
59
60         /* Enable the PLL */
61         PLLCSR |= (1 << PLLE);
62         while (!(PLLCSR & (1 << PLOCK)))
63                 ;
64 }
65
66 void
67 uart_send(char c)
68 {
69         LEDPORT ^= (1 << LEDOUT);
70         loop_until_bit_is_set(UCSR1A, UDRE1);
71         UDR1 = c;
72 }
73
74 int
75 uart_put(char c, FILE *stream)
76 {
77         if (c == '\n')
78                 uart_send('\r');
79         uart_send(c);
80         return 0;
81 }
82
83 int
84 uart_get(FILE *stream)
85 {
86         loop_until_bit_is_set(UCSR1A, RXC1);
87         return (int) UDR1 & 0xff;
88 }
89
90 void
91 uart_init(uint16_t baud)
92 {
93         PRR1 &= ~(1 << PRUSART1);
94         UBRR1L = baud;
95         UBRR1H = baud >> 8;
96         UCSR1A = 0;
97         UCSR1B = ((1 << RXEN1) |        /* Enable receiver */
98                   (1 << TXEN1));        /* Enable transmitter */
99         UCSR1C = ((0 << UMSEL10) |      /* Asynchronous mode */
100                   (0 << UPM10) |        /* No parity */
101                   (0 << USBS1) |        /* 1 stop bit */
102                   (3 << UCSZ10) |       /* 8 bit characters */
103                   (0 << UCPOL1));       /* MBZ for async mode */
104 }
105
106 static FILE mystdout = FDEV_SETUP_STREAM(uart_put, NULL, _FDEV_SETUP_WRITE);
107
108 static FILE mystdin = FDEV_SETUP_STREAM(NULL, uart_get, _FDEV_SETUP_READ);
109
110 void    ao_bringup_init(void)
111 {
112         clock_init();
113         uart_init(F_CPU / (16UL * 9600UL) - 1);
114
115         stdout = &mystdout;
116         stdin = &mystdin;
117
118         LEDDDR |= (1 << LEDDDRPIN);
119 }