e8c1eb351f5a116c4fcb29b91e4437ff0e2291b3
[fw/altos] / src / cc1111 / ao_serial0.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_usart0_rx_fifo;
21 volatile __xdata struct ao_fifo ao_usart0_tx_fifo;
22
23 void
24 ao_serial0_rx0_isr(void) __interrupt 2
25 {
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);
29 }
30
31 static __xdata uint8_t ao_serial0_tx0_started;
32
33 static void
34 ao_serial0_tx0_start(void)
35 {
36         if (!ao_fifo_empty(ao_usart0_tx_fifo) &&
37             !ao_serial0_tx0_started)
38         {
39                 ao_serial0_tx0_started = 1;
40                 ao_fifo_remove(ao_usart0_tx_fifo, U0DBUF);
41         }
42 }
43
44 void
45 ao_serial0_tx0_isr(void) __interrupt 7
46 {
47         UTX0IF = 0;
48         ao_serial0_tx0_started = 0;
49         ao_serial0_tx0_start();
50         ao_wakeup(&ao_usart0_tx_fifo);
51 }
52
53 char
54 ao_serial0_getchar(void) __critical
55 {
56         char    c;
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);
60         return c;
61 }
62
63 #if USE_SERIAL_STDIN
64 char
65 ao_serial0_pollchar(void) __critical
66 {
67         char    c;
68         if (ao_fifo_empty(ao_usart0_rx_fifo))
69                 return AO_READ_AGAIN;
70         ao_fifo_remove(ao_usart0_rx_fifo,c);
71         return c;
72 }
73 #endif
74
75 void
76 ao_serial0_putchar(char c) __critical
77 {
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();
82 }
83
84 void
85 ao_serial0_drain(void) __critical
86 {
87         while (!ao_fifo_empty(ao_usart0_tx_fifo))
88                 ao_sleep(&ao_usart0_tx_fifo);
89 }
90
91 void
92 ao_serial0_set_speed(uint8_t speed)
93 {
94         ao_serial0_drain();
95         if (speed > AO_SERIAL_SPEED_57600)
96                 return;
97         U0UCR |= UxUCR_FLUSH;
98         U0BAUD = ao_serial_speeds[speed].baud;
99         U0GCR = ao_serial_speeds[speed].gcr;
100 }
101
102 void
103 ao_serial0_init(void)
104 {
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;
108
109         P2DIR = (P2DIR & ~P2DIR_PRIP0_MASK) | P2DIR_PRIP0_USART0_USART1;
110
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);
115 #endif
116 #else
117         /* Set up the USART pin assignment */
118         PERCFG = (PERCFG & ~PERCFG_U0CFG_ALT_MASK) | PERCFG_U0CFG_ALT_2;
119
120         P2SEL = (P2SEL & ~(P2SEL_PRI3P1_MASK | P2SEL_PRI0P1_MASK)) |
121                 (P2SEL_PRI3P1_USART0 | P2SEL_PRI0P1_USART0);
122
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);
127 #endif
128 #endif
129
130         /* UART mode with receiver enabled */
131         U0CSR = (UxCSR_MODE_UART | UxCSR_RE);
132
133         /* Pick a 9600 baud rate */
134         ao_serial0_set_speed(AO_SERIAL_SPEED_9600);
135
136         /* Reasonable serial parameters */
137         U0UCR = (UxUCR_FLUSH |
138 #if HAS_SERIAL_0_HW_FLOW
139                  UxUCR_FLOW_ENABLE |
140 #else
141                  UxUCR_FLOW_DISABLE |
142 #endif
143                  UxUCR_D9_EVEN_PARITY |
144                  UxUCR_BIT9_8_BITS |
145                  UxUCR_PARITY_DISABLE |
146                  UxUCR_SPB_1_STOP_BIT |
147                  UxUCR_STOP_HIGH |
148                  UxUCR_START_LOW);
149
150         IEN0 |= IEN0_URX0IE;
151         IEN2 |= IEN2_UTX0IE;
152 }