altos: Clean up usage of serial port for stdio
[fw/altos] / src / ao_serial.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
21 volatile __xdata struct ao_fifo ao_usart1_tx_fifo;
22
23 void
24 ao_serial_rx1_isr(void) __interrupt 3
25 {
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);
29 #if USE_SERIAL_STDIN
30         ao_wakeup(&ao_stdin_ready);
31 #endif
32 }
33
34 static __xdata uint8_t ao_serial_tx1_started;
35
36 static void
37 ao_serial_tx1_start(void)
38 {
39         if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
40             !ao_serial_tx1_started)
41         {
42                 ao_serial_tx1_started = 1;
43                 ao_fifo_remove(ao_usart1_tx_fifo, U1DBUF);
44         }
45 }
46
47 void
48 ao_serial_tx1_isr(void) __interrupt 14
49 {
50         UTX1IF = 0;
51         ao_serial_tx1_started = 0;
52         ao_serial_tx1_start();
53         ao_wakeup(&ao_usart1_tx_fifo);
54 }
55
56 char
57 ao_serial_getchar(void) __critical
58 {
59         char    c;
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);
63         return c;
64 }
65
66 #if USE_SERIAL_STDIN
67 char
68 ao_serial_pollchar(void) __critical
69 {
70         char    c;
71         if (ao_fifo_empty(ao_usart1_rx_fifo))
72                 return AO_READ_AGAIN;
73         ao_fifo_remove(ao_usart1_rx_fifo,c);
74         return c;
75 }
76 #endif
77
78 void
79 ao_serial_putchar(char c) __critical
80 {
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();
85 }
86
87 void
88 ao_serial_drain(void) __critical
89 {
90         while (!ao_fifo_empty(ao_usart1_tx_fifo))
91                 ao_sleep(&ao_usart1_tx_fifo);
92 }
93
94 static const struct {
95         uint8_t baud;
96         uint8_t gcr;
97 } ao_serial_speeds[] = {
98         /* [AO_SERIAL_SPEED_4800] = */ {
99                 /* .baud = */ 163,
100                 /* .gcr  = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
101         },
102         /* [AO_SERIAL_SPEED_9600] = */ {
103                 /* .baud = */ 163,
104                 /* .gcr  = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
105         },
106         /* [AO_SERIAL_SPEED_19200] = */ {
107                 /* .baud = */ 163,
108                 /* .gcr  = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
109         },
110         /* [AO_SERIAL_SPEED_57600] = */ {
111                 /* .baud = */ 59,
112                 /* .gcr =  */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
113         },
114 };
115
116 void
117 ao_serial_set_speed(uint8_t speed)
118 {
119         ao_serial_drain();
120         if (speed > AO_SERIAL_SPEED_57600)
121                 return;
122         U1BAUD = ao_serial_speeds[speed].baud;
123         U1GCR = ao_serial_speeds[speed].gcr;
124 }
125
126 void
127 ao_serial_init(void)
128 {
129         /* Set up the USART pin assignment */
130         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
131
132         /* ee has already set the P2SEL bits */
133
134         /* Make the USART pins be controlled by the USART */
135         P1SEL |= (1 << 6) | (1 << 7);
136
137         /* UART mode with receiver enabled */
138         U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
139
140         /* Pick a 4800 baud rate */
141         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
142
143         /* Reasonable serial parameters */
144         U1UCR = (UxUCR_FLUSH |
145                  UxUCR_FLOW_DISABLE |
146                  UxUCR_D9_ODD_PARITY |
147                  UxUCR_BIT9_8_BITS |
148                  UxUCR_PARITY_DISABLE |
149                  UxUCR_SPB_1_STOP_BIT |
150                  UxUCR_STOP_HIGH |
151                  UxUCR_START_LOW);
152
153         IEN0 |= IEN0_URX1IE;
154         IEN2 |= IEN2_UTX1IE;
155 }