Log GPS data on pad after boost detect.
[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 }
30
31 static __xdata uint8_t ao_serial_tx1_started;
32
33 static void
34 ao_serial_tx1_start(void)
35 {
36         if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
37             !ao_serial_tx1_started)
38         {
39                 ao_serial_tx1_started = 1;
40                 ao_fifo_remove(ao_usart1_tx_fifo, U1DBUF);
41         }
42 }
43
44 void
45 ao_serial_tx1_isr(void) interrupt 14
46 {
47         UTX1IF = 0;
48         ao_serial_tx1_started = 0;
49         ao_serial_tx1_start();
50         ao_wakeup(&ao_usart1_tx_fifo);
51 }
52
53 static __pdata serial_echo;
54
55 char
56 ao_serial_getchar(void) __critical
57 {
58         char    c;
59         while (ao_fifo_empty(ao_usart1_rx_fifo))
60                 ao_sleep(&ao_usart1_rx_fifo);
61         ao_fifo_remove(ao_usart1_rx_fifo, c);
62         if (serial_echo) {
63                 printf("%02x ", ((int) c) & 0xff);
64                 if (c >= ' ')
65                         putchar(c);
66                 putchar('\n');
67                 flush();
68         }
69         return c;
70 }
71
72 void
73 ao_serial_putchar(char c) __critical
74 {
75         while (ao_fifo_full(ao_usart1_tx_fifo))
76                 ao_sleep(&ao_usart1_tx_fifo);
77         ao_fifo_insert(ao_usart1_tx_fifo, c);
78         ao_serial_tx1_start();
79 }
80
81 static void
82 ao_serial_drain(void) __critical
83 {
84         while (!ao_fifo_empty(ao_usart1_tx_fifo))
85                 ao_sleep(&ao_usart1_tx_fifo);
86 }
87
88 static void
89 monitor_serial(void)
90 {
91         ao_cmd_hex();
92         serial_echo = ao_cmd_lex_i != 0;
93 }
94
95 __code struct ao_cmds ao_serial_cmds[] = {
96         { 'M', monitor_serial,          "M <enable>                         Monitor serial data" },
97         { 0, monitor_serial, NULL },
98 };
99
100 static const struct {
101         uint8_t baud;
102         uint8_t gcr;
103 } ao_serial_speeds[] = {
104         /* [AO_SERIAL_SPEED_4800] = */ {
105                 /* .baud = */ 163,
106                 /* .gcr  = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
107         },
108         /* [AO_SERIAL_SPEED_9600] = */ {
109                 /* .baud = */ 163,
110                 /* .gcr  = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
111         },
112         /* [AO_SERIAL_SPEED_57600] = */ {
113                 /* .baud = */ 59,
114                 /* .gcr =  */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
115         },
116 };
117
118 void
119 ao_serial_set_speed(uint8_t speed)
120 {
121         ao_serial_drain();
122         if (speed > AO_SERIAL_SPEED_57600)
123                 return;
124         U1BAUD = ao_serial_speeds[speed].baud;
125         U1GCR = ao_serial_speeds[speed].gcr;
126 }
127
128 void
129 ao_serial_init(void)
130 {
131         /* Set up the USART pin assignment */
132         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
133
134         /* ee has already set the P2SEL bits */
135
136         /* Make the USART pins be controlled by the USART */
137         P1SEL |= (1 << 6) | (1 << 7);
138
139         /* UART mode with receiver enabled */
140         U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
141
142         /* Pick a 4800 baud rate */
143         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
144
145         /* Reasonable serial parameters */
146         U1UCR = (UxUCR_FLUSH |
147                  UxUCR_FLOW_DISABLE |
148                  UxUCR_D9_ODD_PARITY |
149                  UxUCR_BIT9_8_BITS |
150                  UxUCR_PARITY_DISABLE |
151                  UxUCR_SPB_1_STOP_BIT |
152                  UxUCR_STOP_HIGH |
153                  UxUCR_START_LOW);
154
155         IEN0 |= IEN0_URX1IE;
156         IEN2 |= IEN2_UTX1IE;
157
158         ao_cmd_register(&ao_serial_cmds[0]);
159 }