4d5b9abd5693c048e61c737944965c42d50bb17b
[fw/altos] / src / cc1111 / 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 const __code struct ao_serial_speed ao_serial_speeds[] = {
95         /* [AO_SERIAL_SPEED_4800] = */ {
96                 /* .baud = */ 163,
97                 /* .gcr  = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
98         },
99         /* [AO_SERIAL_SPEED_9600] = */ {
100                 /* .baud = */ 163,
101                 /* .gcr  = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
102         },
103         /* [AO_SERIAL_SPEED_19200] = */ {
104                 /* .baud = */ 163,
105                 /* .gcr  = */ (9 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
106         },
107         /* [AO_SERIAL_SPEED_57600] = */ {
108                 /* .baud = */ 59,
109                 /* .gcr =  */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
110         },
111 };
112
113 void
114 ao_serial_set_speed(uint8_t speed)
115 {
116         ao_serial_drain();
117         if (speed > AO_SERIAL_SPEED_57600)
118                 return;
119         U1UCR |= UxUCR_FLUSH;
120         U1BAUD = ao_serial_speeds[speed].baud;
121         U1GCR = ao_serial_speeds[speed].gcr;
122 }
123
124 void
125 ao_serial_init(void)
126 {
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;
130
131         P2DIR = (P2DIR & ~P2DIR_PRIP0_MASK) | P2DIR_PRIP0_USART1_USART0;
132
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);
137 #endif
138 #else
139         /* Set up the USART pin assignment */
140         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
141
142         P2SEL = (P2SEL & ~(P2SEL_PRI3P1_MASK | P2SEL_PRI2P1_MASK)) |
143                 (P2SEL_PRI3P1_USART1 | P2SEL_PRI2P1_USART1);
144
145         /* Make the USART pins be controlled by the USART */
146         P1SEL |= (1 << 6) | (1 << 7);
147         P1SEL |= (1 << 5) | (1 << 4);
148 #endif
149
150         /* UART mode with receiver enabled */
151         U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
152
153         /* Pick a 4800 baud rate */
154         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
155
156         /* Reasonable serial parameters */
157         U1UCR = (UxUCR_FLUSH |
158 #if HAS_SERIAL_1_HW_FLOW
159                  UxUCR_FLOW_ENABLE |
160 #else
161                  UxUCR_FLOW_DISABLE |
162 #endif
163                  UxUCR_D9_EVEN_PARITY |
164                  UxUCR_BIT9_8_BITS |
165                  UxUCR_PARITY_DISABLE |
166                  UxUCR_SPB_1_STOP_BIT |
167                  UxUCR_STOP_HIGH |
168                  UxUCR_START_LOW);
169
170         IEN0 |= IEN0_URX1IE;
171         IEN2 |= IEN2_UTX1IE;
172 }