2 * Copyright © 2009 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 volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
21 volatile __xdata struct ao_fifo ao_usart1_tx_fifo;
24 __pdata uint8_t ao_serial_stdin;
28 ao_serial_rx1_isr(void) __interrupt 3
30 if (!ao_fifo_full(ao_usart1_rx_fifo))
31 ao_fifo_insert(ao_usart1_rx_fifo, U1DBUF);
32 ao_wakeup(&ao_usart1_rx_fifo);
35 ao_wakeup(&ao_stdin_ready);
39 static __xdata uint8_t ao_serial_tx1_started;
42 ao_serial_tx1_start(void)
44 if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
45 !ao_serial_tx1_started)
47 ao_serial_tx1_started = 1;
48 ao_fifo_remove(ao_usart1_tx_fifo, U1DBUF);
53 ao_serial_tx1_isr(void) __interrupt 14
56 ao_serial_tx1_started = 0;
57 ao_serial_tx1_start();
58 ao_wakeup(&ao_usart1_tx_fifo);
62 ao_serial_getchar(void) __critical
65 while (ao_fifo_empty(ao_usart1_rx_fifo))
66 ao_sleep(&ao_usart1_rx_fifo);
67 ao_fifo_remove(ao_usart1_rx_fifo, c);
73 ao_serial_pollchar(void) __critical
80 if (ao_fifo_empty(ao_usart1_rx_fifo))
82 ao_fifo_remove(ao_usart1_rx_fifo,c);
87 ao_serial_set_stdin(uint8_t stdin)
89 ao_serial_stdin = stdin;
95 ao_serial_putchar(char c) __critical
97 while (ao_fifo_full(ao_usart1_tx_fifo))
98 ao_sleep(&ao_usart1_tx_fifo);
99 ao_fifo_insert(ao_usart1_tx_fifo, c);
100 ao_serial_tx1_start();
104 ao_serial_drain(void) __critical
106 while (!ao_fifo_empty(ao_usart1_tx_fifo))
107 ao_sleep(&ao_usart1_tx_fifo);
110 static const struct {
113 } ao_serial_speeds[] = {
114 /* [AO_SERIAL_SPEED_4800] = */ {
116 /* .gcr = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
118 /* [AO_SERIAL_SPEED_9600] = */ {
120 /* .gcr = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
122 /* [AO_SERIAL_SPEED_19200] = */ {
124 /* .gcr = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
126 /* [AO_SERIAL_SPEED_57600] = */ {
128 /* .gcr = */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
133 ao_serial_set_speed(uint8_t speed)
136 if (speed > AO_SERIAL_SPEED_57600)
138 U1BAUD = ao_serial_speeds[speed].baud;
139 U1GCR = ao_serial_speeds[speed].gcr;
145 /* Set up the USART pin assignment */
146 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
148 /* ee has already set the P2SEL bits */
150 /* Make the USART pins be controlled by the USART */
151 P1SEL |= (1 << 6) | (1 << 7);
153 /* UART mode with receiver enabled */
154 U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
156 /* Pick a 4800 baud rate */
157 ao_serial_set_speed(AO_SERIAL_SPEED_4800);
159 /* Reasonable serial parameters */
160 U1UCR = (UxUCR_FLUSH |
162 UxUCR_D9_ODD_PARITY |
164 UxUCR_PARITY_DISABLE |
165 UxUCR_SPB_1_STOP_BIT |
173 ao_add_stdio(ao_serial_pollchar,