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 const __code struct ao_serial_speed ao_serial_speeds[] = {
95 /* [AO_SERIAL_SPEED_4800] = */ {
97 /* .gcr = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
99 /* [AO_SERIAL_SPEED_9600] = */ {
101 /* .gcr = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
103 /* [AO_SERIAL_SPEED_19200] = */ {
105 /* .gcr = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
107 /* [AO_SERIAL_SPEED_57600] = */ {
109 /* .gcr = */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
114 ao_serial_set_speed(uint8_t speed)
117 if (speed > AO_SERIAL_SPEED_57600)
119 U1UCR |= UxUCR_FLUSH;
120 U1BAUD = ao_serial_speeds[speed].baud;
121 U1GCR = ao_serial_speeds[speed].gcr;
127 #if HAS_SERIAL_1_ALT_1
128 /* Set up the USART pin assignment */
129 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_1;
131 P2DIR = (P2DIR & ~P2DIR_PRIP0_MASK) | P2DIR_PRIP0_USART1_USART0;
133 /* Make the USART pins be controlled by the USART */
134 P0SEL |= (1 << 5) | (1 << 4);
135 #if HAS_SERIAL_1_HW_FLOW
136 P0SEL |= (1 << 3) | (1 << 2);
139 /* Set up the USART pin assignment */
140 PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
142 P2SEL = (P2SEL & ~(P2SEL_PRI3P1_MASK | P2SEL_PRI2P1_MASK)) |
143 (P2SEL_PRI3P1_USART1 | P2SEL_PRI2P1_USART1);
145 /* Make the USART pins be controlled by the USART */
146 P1SEL |= (1 << 6) | (1 << 7);
147 P1SEL |= (1 << 5) | (1 << 4);
150 /* UART mode with receiver enabled */
151 U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
153 /* Pick a 4800 baud rate */
154 ao_serial_set_speed(AO_SERIAL_SPEED_4800);
156 /* Reasonable serial parameters */
157 U1UCR = (UxUCR_FLUSH |
158 #if HAS_SERIAL_1_HW_FLOW
163 UxUCR_D9_EVEN_PARITY |
165 UxUCR_PARITY_DISABLE |
166 UxUCR_SPB_1_STOP_BIT |