altos: Shrink ao_add_task by rolling up a memset loop
[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         U1UCR |= UxUCR_FLUSH;
123         U1BAUD = ao_serial_speeds[speed].baud;
124         U1GCR = ao_serial_speeds[speed].gcr;
125 }
126
127 void
128 ao_serial_init(void)
129 {
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;
133
134         P2DIR = (P2DIR & ~P2DIR_PRIP0_MASK) | P2DIR_PRIP0_USART1_USART0;
135
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);
140 #endif
141 #else
142         /* Set up the USART pin assignment */
143         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
144
145         P2SEL = (P2SEL & ~(P2SEL_PRI3P1_MASK | P2SEL_PRI2P1_MASK)) |
146                 (P2SEL_PRI3P1_USART1 | P2SEL_PRI2P1_USART1);
147
148         /* Make the USART pins be controlled by the USART */
149         P1SEL |= (1 << 6) | (1 << 7);
150         P1SEL |= (1 << 5) | (1 << 4);
151 #endif
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 #if HAS_SERIAL_1_HW_FLOW
162                  UxUCR_FLOW_ENABLE |
163 #else
164                  UxUCR_FLOW_DISABLE |
165 #endif
166                  UxUCR_D9_EVEN_PARITY |
167                  UxUCR_BIT9_8_BITS |
168                  UxUCR_PARITY_DISABLE |
169                  UxUCR_SPB_1_STOP_BIT |
170                  UxUCR_STOP_HIGH |
171                  UxUCR_START_LOW);
172
173         IEN0 |= IEN0_URX1IE;
174         IEN2 |= IEN2_UTX1IE;
175 }