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_usart0_rx_fifo;
21 volatile __xdata struct ao_fifo ao_usart0_tx_fifo;
24 ao_serial0_rx0_isr(void) __interrupt 2
26 if (!ao_fifo_full(ao_usart0_rx_fifo))
27 ao_fifo_insert(ao_usart0_rx_fifo, U0DBUF);
28 ao_wakeup(&ao_usart0_rx_fifo);
31 static __xdata uint8_t ao_serial0_tx0_started;
34 ao_serial0_tx0_start(void)
36 if (!ao_fifo_empty(ao_usart0_tx_fifo) &&
37 !ao_serial0_tx0_started)
39 ao_serial0_tx0_started = 1;
40 ao_fifo_remove(ao_usart0_tx_fifo, U0DBUF);
45 ao_serial0_tx0_isr(void) __interrupt 7
48 ao_serial0_tx0_started = 0;
49 ao_serial0_tx0_start();
50 ao_wakeup(&ao_usart0_tx_fifo);
54 ao_serial0_getchar(void) __critical
57 while (ao_fifo_empty(ao_usart0_rx_fifo))
58 ao_sleep(&ao_usart0_rx_fifo);
59 ao_fifo_remove(ao_usart0_rx_fifo, c);
65 ao_serial0_pollchar(void) __critical
68 if (ao_fifo_empty(ao_usart0_rx_fifo))
70 ao_fifo_remove(ao_usart0_rx_fifo,c);
76 ao_serial0_putchar(char c) __critical
78 while (ao_fifo_full(ao_usart0_tx_fifo))
79 ao_sleep(&ao_usart0_tx_fifo);
80 ao_fifo_insert(ao_usart0_tx_fifo, c);
81 ao_serial0_tx0_start();
85 ao_serial0_drain(void) __critical
87 while (!ao_fifo_empty(ao_usart0_tx_fifo))
88 ao_sleep(&ao_usart0_tx_fifo);
92 ao_serial0_set_speed(uint8_t speed)
95 if (speed > AO_SERIAL_SPEED_57600)
98 U0BAUD = ao_serial_speeds[speed].baud;
99 U0GCR = ao_serial_speeds[speed].gcr;
103 ao_serial0_init(void)
105 #if HAS_SERIAL_0_ALT_1
106 /* Set up the USART pin assignment */
107 PERCFG = (PERCFG & ~PERCFG_U0CFG_ALT_MASK) | PERCFG_U0CFG_ALT_1;
109 P2DIR = (P2DIR & ~P2DIR_PRIP0_MASK) | P2DIR_PRIP0_USART0_USART1;
111 /* Make the USART pins be controlled by the USART */
112 P0SEL |= (1 << 2) | (1 << 3);
113 #if HAS_SERIAL_0_HW_FLOW
114 P0SEL |= (1 << 4) | (1 << 5);
117 /* Set up the USART pin assignment */
118 PERCFG = (PERCFG & ~PERCFG_U0CFG_ALT_MASK) | PERCFG_U0CFG_ALT_2;
120 P2SEL = (P2SEL & ~(P2SEL_PRI3P1_MASK | P2SEL_PRI0P1_MASK)) |
121 (P2SEL_PRI3P1_USART0 | P2SEL_PRI0P1_USART0);
123 /* Make the USART pins be controlled by the USART */
124 P1SEL |= (1 << 2) | (1 << 3);
125 #if HAS_SERIAL_0_HW_FLOW
126 P1SEL |= (1 << 5) | (1 << 4);
130 /* UART mode with receiver enabled */
131 U0CSR = (UxCSR_MODE_UART | UxCSR_RE);
133 /* Pick a 9600 baud rate */
134 ao_serial0_set_speed(AO_SERIAL_SPEED_9600);
136 /* Reasonable serial parameters */
137 U0UCR = (UxUCR_FLUSH |
138 #if HAS_SERIAL_0_HW_FLOW
143 UxUCR_D9_EVEN_PARITY |
145 UxUCR_PARITY_DISABLE |
146 UxUCR_SPB_1_STOP_BIT |