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 ao_serial_rx1_isr(void) interrupt 3
26 if (!ao_fifo_full(ao_usart1_rx_fifo))
27 ao_fifo_insert(ao_usart1_rx_fifo, U1DBUF);
28 ao_wakeup(&ao_usart1_rx_fifo);
31 static __xdata uint8_t ao_serial_tx1_started;
34 ao_serial_tx1_start(void)
36 if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
37 !ao_serial_tx1_started)
39 ao_serial_tx1_started = 1;
40 ao_fifo_remove(ao_usart1_tx_fifo, U1DBUF);
45 ao_serial_tx1_isr(void) interrupt 14
48 ao_serial_tx1_started = 0;
49 ao_serial_tx1_start();
50 ao_wakeup(&ao_usart1_tx_fifo);
53 static __pdata serial_echo;
56 ao_serial_getchar(void) __critical
59 while (ao_fifo_empty(ao_usart1_rx_fifo))
60 ao_sleep(&ao_usart1_rx_fifo);
61 ao_fifo_remove(ao_usart1_rx_fifo, c);
63 printf("%02x ", ((int) c) & 0xff);
73 ao_serial_putchar(char c) __critical
75 while (ao_fifo_full(ao_usart1_tx_fifo))
76 ao_sleep(&ao_usart1_tx_fifo);
77 ao_fifo_insert(ao_usart1_tx_fifo, c);
78 ao_serial_tx1_start();
82 ao_serial_drain(void) __critical
84 while (!ao_fifo_empty(ao_usart1_tx_fifo))
85 ao_sleep(&ao_usart1_tx_fifo);
92 while (ao_cmd_lex_c != '\n') {
93 ao_serial_putchar(ao_cmd_lex_c);
102 serial_echo = ao_cmd_lex_i != 0;
109 ao_serial_set_speed(ao_cmd_lex_i);
112 __code struct ao_cmds ao_serial_cmds[] = {
113 { 'S', send_serial, "S <data> Send data to serial line" },
114 { 'M', monitor_serial, "M <enable> Monitor serial data" },
115 { 'B', serial_baud, "B <0 = 4800, 1 = 57600> Set serial baud rate" },
116 { 0, send_serial, NULL },
119 static const struct {
122 } ao_serial_speeds[] = {
123 /* [AO_SERIAL_SPEED_4800] = */ {
125 /* .gcr = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
127 /* [AO_SERIAL_SPEED_9600] = */ {
129 /* .gcr = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
131 /* [AO_SERIAL_SPEED_57600] = */ {
133 /* .gcr = */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
138 ao_serial_set_speed(uint8_t speed)
141 if (speed > AO_SERIAL_SPEED_57600)
143 U1BAUD = ao_serial_speeds[speed].baud;
144 U1GCR = ao_serial_speeds[speed].gcr;
150 /* Set up the USART pin assignment */
151 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
153 /* ee has already set the P2SEL bits */
155 /* Make the USART pins be controlled by the USART */
156 P1SEL |= (1 << 6) | (1 << 7);
158 /* UART mode with receiver enabled */
159 U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
161 /* Pick a 4800 baud rate */
162 ao_serial_set_speed(AO_SERIAL_SPEED_4800);
164 /* Reasonable serial parameters */
165 U1UCR = (UxUCR_FLUSH |
167 UxUCR_D9_ODD_PARITY |
169 UxUCR_PARITY_DISABLE |
170 UxUCR_SPB_1_STOP_BIT |
177 ao_cmd_register(&ao_serial_cmds[0]);