e9373e232cb700fd735a3a72968b04d1c787c078
[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 #if USE_SERIAL_STDIN
24 __pdata uint8_t ao_serial_stdin;
25 #endif
26
27 void
28 ao_serial_rx1_isr(void) __interrupt 3
29 {
30         if (!ao_fifo_full(ao_usart1_rx_fifo))
31                 ao_fifo_insert(ao_usart1_rx_fifo, U1DBUF);
32         ao_wakeup(&ao_usart1_rx_fifo);
33 #if USE_SERIAL_STDIN
34         if (ao_serial_stdin)
35                 ao_wakeup(&ao_stdin_ready);
36 #endif
37 }
38
39 static __xdata uint8_t ao_serial_tx1_started;
40
41 static void
42 ao_serial_tx1_start(void)
43 {
44         if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
45             !ao_serial_tx1_started)
46         {
47                 ao_serial_tx1_started = 1;
48                 ao_fifo_remove(ao_usart1_tx_fifo, U1DBUF);
49         }
50 }
51
52 void
53 ao_serial_tx1_isr(void) __interrupt 14
54 {
55         UTX1IF = 0;
56         ao_serial_tx1_started = 0;
57         ao_serial_tx1_start();
58         ao_wakeup(&ao_usart1_tx_fifo);
59 }
60
61 char
62 ao_serial_getchar(void) __critical
63 {
64         char    c;
65         while (ao_fifo_empty(ao_usart1_rx_fifo))
66                 ao_sleep(&ao_usart1_rx_fifo);
67         ao_fifo_remove(ao_usart1_rx_fifo, c);
68         return c;
69 }
70
71 #if USE_SERIAL_STDIN
72 char
73 ao_serial_pollchar(void) __critical
74 {
75         char    c;
76 #if 0
77         if (!ao_serial_stdin)
78                 return AO_READ_AGAIN;
79 #endif
80         if (ao_fifo_empty(ao_usart1_rx_fifo))
81                 return AO_READ_AGAIN;
82         ao_fifo_remove(ao_usart1_rx_fifo,c);
83         return c;
84 }
85
86 void
87 ao_serial_set_stdin(uint8_t stdin)
88 {
89         ao_serial_stdin = stdin;
90 }
91
92 #endif
93
94 void
95 ao_serial_putchar(char c) __critical
96 {
97         while (ao_fifo_full(ao_usart1_tx_fifo))
98                 ao_sleep(&ao_usart1_tx_fifo);
99         ao_fifo_insert(ao_usart1_tx_fifo, c);
100         ao_serial_tx1_start();
101 }
102
103 void
104 ao_serial_drain(void) __critical
105 {
106         while (!ao_fifo_empty(ao_usart1_tx_fifo))
107                 ao_sleep(&ao_usart1_tx_fifo);
108 }
109
110 static const struct {
111         uint8_t baud;
112         uint8_t gcr;
113 } ao_serial_speeds[] = {
114         /* [AO_SERIAL_SPEED_4800] = */ {
115                 /* .baud = */ 163,
116                 /* .gcr  = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
117         },
118         /* [AO_SERIAL_SPEED_9600] = */ {
119                 /* .baud = */ 163,
120                 /* .gcr  = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
121         },
122         /* [AO_SERIAL_SPEED_19200] = */ {
123                 /* .baud = */ 163,
124                 /* .gcr  = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
125         },
126         /* [AO_SERIAL_SPEED_57600] = */ {
127                 /* .baud = */ 59,
128                 /* .gcr =  */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
129         },
130 };
131
132 void
133 ao_serial_set_speed(uint8_t speed)
134 {
135         ao_serial_drain();
136         if (speed > AO_SERIAL_SPEED_57600)
137                 return;
138         U1BAUD = ao_serial_speeds[speed].baud;
139         U1GCR = ao_serial_speeds[speed].gcr;
140 }
141
142 void
143 ao_serial_init(void)
144 {
145         /* Set up the USART pin assignment */
146         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
147
148         /* ee has already set the P2SEL bits */
149
150         /* Make the USART pins be controlled by the USART */
151         P1SEL |= (1 << 6) | (1 << 7);
152
153         /* UART mode with receiver enabled */
154         U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
155
156         /* Pick a 4800 baud rate */
157         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
158
159         /* Reasonable serial parameters */
160         U1UCR = (UxUCR_FLUSH |
161                  UxUCR_FLOW_DISABLE |
162                  UxUCR_D9_ODD_PARITY |
163                  UxUCR_BIT9_8_BITS |
164                  UxUCR_PARITY_DISABLE |
165                  UxUCR_SPB_1_STOP_BIT |
166                  UxUCR_STOP_HIGH |
167                  UxUCR_START_LOW);
168
169         IEN0 |= IEN0_URX1IE;
170         IEN2 |= IEN2_UTX1IE;
171 #if 0
172 #if USE_SERIAL_STDIN
173         ao_add_stdio(ao_serial_pollchar,
174                      ao_serial_putchar,
175                      NULL);
176 #endif
177 #endif
178 }