2 * Copyright © 2011 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.
18 #include "ao-bringup.h"
23 /* disable RC clock */
24 CLKSEL0 &= ~(1 << RCE);
27 PLLCSR &= ~(1 << PLLE);
29 /* Enable external clock */
30 CLKSEL0 |= (1 << EXTE);
32 /* wait for external clock to be ready */
33 while ((CLKSTA & (1 << EXTON)) == 0)
36 /* select external clock */
37 CLKSEL0 |= (1 << CLKS);
39 /* Disable the clock prescaler */
41 CLKPR = (1 << CLKPCE);
45 /* Set up the PLL to use the crystal */
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 */
53 /* Set the frequency of the crystal */
55 PLLCSR |= (1 << PINDIV); /* For 16MHz crystal on Teensy board */
57 PLLCSR &= ~(1 << PINDIV); /* For 8MHz crystal on TeleScience board */
61 PLLCSR |= (1 << PLLE);
62 while (!(PLLCSR & (1 << PLOCK)))
69 LEDPORT ^= (1 << LEDOUT);
70 loop_until_bit_is_set(UCSR1A, UDRE1);
75 uart_put(char c, FILE *stream)
84 uart_get(FILE *stream)
86 loop_until_bit_is_set(UCSR1A, RXC1);
87 return (int) UDR1 & 0xff;
91 uart_init(uint16_t baud)
93 PRR1 &= ~(1 << PRUSART1);
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 */
106 static FILE mystdout = FDEV_SETUP_STREAM(uart_put, NULL, _FDEV_SETUP_WRITE);
108 static FILE mystdin = FDEV_SETUP_STREAM(NULL, uart_get, _FDEV_SETUP_READ);
110 void ao_bringup_init(void)
113 uart_init(F_CPU / (16UL * 9600UL) - 1);
118 LEDDDR |= (1 << LEDDDRPIN);