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);
30 ao_wakeup(&ao_stdin_ready);
34 static __xdata uint8_t ao_serial_tx1_started;
37 ao_serial_tx1_start(void)
39 if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
40 !ao_serial_tx1_started)
42 ao_serial_tx1_started = 1;
43 ao_fifo_remove(ao_usart1_tx_fifo, U1DBUF);
48 ao_serial_tx1_isr(void) __interrupt 14
51 ao_serial_tx1_started = 0;
52 ao_serial_tx1_start();
53 ao_wakeup(&ao_usart1_tx_fifo);
57 ao_serial_getchar(void) __critical
60 while (ao_fifo_empty(ao_usart1_rx_fifo))
61 ao_sleep(&ao_usart1_rx_fifo);
62 ao_fifo_remove(ao_usart1_rx_fifo, c);
68 ao_serial_pollchar(void) __critical
71 if (ao_fifo_empty(ao_usart1_rx_fifo))
73 ao_fifo_remove(ao_usart1_rx_fifo,c);
79 ao_serial_putchar(char c) __critical
81 while (ao_fifo_full(ao_usart1_tx_fifo))
82 ao_sleep(&ao_usart1_tx_fifo);
83 ao_fifo_insert(ao_usart1_tx_fifo, c);
84 ao_serial_tx1_start();
88 ao_serial_drain(void) __critical
90 while (!ao_fifo_empty(ao_usart1_tx_fifo))
91 ao_sleep(&ao_usart1_tx_fifo);
94 static __code struct {
97 } ao_serial_speeds[] = {
98 /* [AO_SERIAL_SPEED_4800] = */ {
100 /* .gcr = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
102 /* [AO_SERIAL_SPEED_9600] = */ {
104 /* .gcr = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
106 /* [AO_SERIAL_SPEED_19200] = */ {
108 /* .gcr = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
110 /* [AO_SERIAL_SPEED_57600] = */ {
112 /* .gcr = */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
117 ao_serial_set_speed(uint8_t speed)
120 if (speed > AO_SERIAL_SPEED_57600)
122 U1UCR |= UxUCR_FLUSH;
123 U1BAUD = ao_serial_speeds[speed].baud;
124 U1GCR = ao_serial_speeds[speed].gcr;
130 #if HAS_SERIAL_1_ALT_1
131 /* Set up the USART pin assignment */
132 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_1;
134 P2DIR = (P2DIR & ~P2DIR_PRIP0_MASK) | P2DIR_PRIP0_USART1_USART0;
136 /* Make the USART pins be controlled by the USART */
137 P0SEL |= (1 << 5) | (1 << 4);
138 #if HAS_SERIAL_1_HW_FLOW
139 P0SEL |= (1 << 3) | (1 << 2);
142 /* Set up the USART pin assignment */
143 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
145 P2SEL = (P2SEL & ~(P2SEL_PRI3P1_MASK | P2SEL_PRI2P1_MASK)) |
146 (P2SEL_PRI3P1_USART1 | P2SEL_PRI2P1_USART1);
148 /* Make the USART pins be controlled by the USART */
149 P1SEL |= (1 << 6) | (1 << 7);
150 P1SEL |= (1 << 5) | (1 << 4);
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 |
161 #if HAS_SERIAL_1_HW_FLOW
166 UxUCR_D9_EVEN_PARITY |
168 UxUCR_PARITY_DISABLE |
169 UxUCR_SPB_1_STOP_BIT |