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.
20 __xdata struct ao_fifo ao_usart1_rx_fifo;
21 __xdata struct ao_fifo ao_usart1_tx_fifo;
25 if (!ao_fifo_full(ao_usart1_rx_fifo))
26 ao_fifo_insert(ao_usart1_rx_fifo, UDR1);
27 ao_wakeup(&ao_usart1_rx_fifo);
29 ao_wakeup(&ao_stdin_ready);
33 static __xdata uint8_t ao_serial_tx1_started;
36 ao_serial_tx1_start(void)
38 if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
39 !ao_serial_tx1_started)
41 ao_serial_tx1_started = 1;
42 ao_fifo_remove(ao_usart1_tx_fifo, UDR1);
48 ao_serial_tx1_started = 0;
49 ao_serial_tx1_start();
50 ao_wakeup(&ao_usart1_tx_fifo);
54 ao_serial_getchar(void) __critical
58 while (ao_fifo_empty(ao_usart1_rx_fifo))
59 ao_sleep(&ao_usart1_rx_fifo);
60 ao_fifo_remove(ao_usart1_rx_fifo, c);
67 ao_serial_pollchar(void) __critical
71 if (ao_fifo_empty(ao_usart1_rx_fifo)) {
75 ao_fifo_remove(ao_usart1_rx_fifo,c);
82 ao_serial_putchar(char c) __critical
85 while (ao_fifo_full(ao_usart1_tx_fifo))
86 ao_sleep(&ao_usart1_tx_fifo);
87 ao_fifo_insert(ao_usart1_tx_fifo, c);
88 ao_serial_tx1_start();
93 ao_serial_drain(void) __critical
96 while (!ao_fifo_empty(ao_usart1_tx_fifo))
97 ao_sleep(&ao_usart1_tx_fifo);
101 static const struct {
103 } ao_serial_speeds[] = {
104 /* [AO_SERIAL_SPEED_4800] = */ {
105 F_CPU / (16UL * 4800UL) - 1
107 /* [AO_SERIAL_SPEED_9600] = */ {
108 F_CPU / (16UL * 9600UL) - 1
110 /* [AO_SERIAL_SPEED_19200] = */ {
111 F_CPU / (16UL * 19200UL) - 1
113 /* [AO_SERIAL_SPEED_57600] = */ {
114 F_CPU / (16UL * 57600UL) - 1
119 ao_serial_set_speed(uint8_t speed)
122 if (speed > AO_SERIAL_SPEED_57600)
124 UBRR1L = ao_serial_speeds[speed].ubrr;
125 UBRR1H = ao_serial_speeds[speed].ubrr >> 8;
131 /* Ensure the uart is powered up */
133 PRR1 &= ~(1 << PRUSART1);
135 /* Pick a 9600 baud rate */
136 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
139 UCSR1C = ((0 << UMSEL10) | /* Asynchronous mode */
140 (0 << UPM10) | /* No parity */
141 (0 << USBS1) | /* 1 stop bit */
142 (3 << UCSZ10) | /* 8 bit characters */
143 (0 << UCPOL1)); /* MBZ for async mode */
144 UCSR1B = ((1 << RXEN1) | /* Enable receiver */
145 (1 << TXEN1) | /* Enable transmitter */
146 (1 << RXCIE1) | /* Enable receive interrupts */
147 (1 << UDRIE1)); /* Enable transmit empty interrupts */
150 i = ao_add_stdio(ao_serial_pollchar,
153 printf("Register serial stdio as %d\n", i);