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);
61 static __pdata serial_echo;
64 ao_serial_getchar(void) __critical
67 while (ao_fifo_empty(ao_usart1_rx_fifo))
68 ao_sleep(&ao_usart1_rx_fifo);
69 ao_fifo_remove(ao_usart1_rx_fifo, c);
71 printf("%02x ", ((int) c) & 0xff);
82 ao_serial_pollchar(void) __critical
89 if (ao_fifo_empty(ao_usart1_rx_fifo))
91 ao_fifo_remove(ao_usart1_rx_fifo,c);
96 ao_serial_set_stdin(uint8_t stdin)
98 ao_serial_stdin = stdin;
104 ao_serial_putchar(char c) __critical
106 while (ao_fifo_full(ao_usart1_tx_fifo))
107 ao_sleep(&ao_usart1_tx_fifo);
108 ao_fifo_insert(ao_usart1_tx_fifo, c);
109 ao_serial_tx1_start();
113 ao_serial_drain(void) __critical
115 while (!ao_fifo_empty(ao_usart1_tx_fifo))
116 ao_sleep(&ao_usart1_tx_fifo);
123 serial_echo = ao_cmd_lex_i != 0;
126 __code struct ao_cmds ao_serial_cmds[] = {
127 { monitor_serial, "M <enable>\0Monitor serial data" },
131 static const struct {
134 } ao_serial_speeds[] = {
135 /* [AO_SERIAL_SPEED_4800] = */ {
137 /* .gcr = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
139 /* [AO_SERIAL_SPEED_9600] = */ {
141 /* .gcr = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
143 /* [AO_SERIAL_SPEED_19200] = */ {
145 /* .gcr = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
147 /* [AO_SERIAL_SPEED_57600] = */ {
149 /* .gcr = */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
154 ao_serial_set_speed(uint8_t speed)
157 if (speed > AO_SERIAL_SPEED_57600)
159 U1BAUD = ao_serial_speeds[speed].baud;
160 U1GCR = ao_serial_speeds[speed].gcr;
166 /* Set up the USART pin assignment */
167 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
169 /* ee has already set the P2SEL bits */
171 /* Make the USART pins be controlled by the USART */
172 P1SEL |= (1 << 6) | (1 << 7);
174 /* UART mode with receiver enabled */
175 U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
177 /* Pick a 4800 baud rate */
178 ao_serial_set_speed(AO_SERIAL_SPEED_4800);
180 /* Reasonable serial parameters */
181 U1UCR = (UxUCR_FLUSH |
183 UxUCR_D9_ODD_PARITY |
185 UxUCR_PARITY_DISABLE |
186 UxUCR_SPB_1_STOP_BIT |
193 ao_cmd_register(&ao_serial_cmds[0]);
196 ao_add_stdio(ao_serial_pollchar,