update changelogs for Debian build
[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\n", ((int) c) & 0xff);
64                 flush();
65         }
66         return c;
67 }
68
69 void
70 ao_serial_putchar(char c) __critical
71 {
72         while (ao_fifo_full(ao_usart1_tx_fifo))
73                 ao_sleep(&ao_usart1_tx_fifo);
74         ao_fifo_insert(ao_usart1_tx_fifo, c);
75         ao_serial_tx1_start();
76 }
77
78 static void
79 ao_serial_drain(void) __critical
80 {
81         while (!ao_fifo_empty(ao_usart1_tx_fifo))
82                 ao_sleep(&ao_usart1_tx_fifo);
83 }
84
85 static void
86 send_serial(void)
87 {
88         ao_cmd_white();
89         while (ao_cmd_lex_c != '\n') {
90                 ao_serial_putchar(ao_cmd_lex_c);
91                 ao_cmd_lex();
92         }
93 }
94
95 static void
96 monitor_serial(void)
97 {
98         ao_cmd_hex();
99         serial_echo = ao_cmd_lex_i != 0;
100 }
101
102 static void
103 serial_baud(void)
104 {
105         ao_cmd_hex();
106         ao_serial_set_speed(ao_cmd_lex_i);
107 }
108
109 __code struct ao_cmds ao_serial_cmds[] = {
110         { 'S', send_serial,             "S <data>                           Send data to serial line" },
111         { 'M', monitor_serial,          "M <enable>                         Monitor serial data" },
112         { 'B', serial_baud,             "B <0 = 4800, 1 = 57600>            Set serial baud rate" },
113         { 0, send_serial, NULL },
114 };
115
116 static const struct {
117         uint8_t baud;
118         uint8_t gcr;
119 } ao_serial_speeds[] = {
120         /* [AO_SERIAL_SPEED_4800] = */ {
121                 /* .baud = */ 163,
122                 /* .gcr  = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
123         },
124         /* [AO_SERIAL_SPEED_57600] = */ {
125                 /* .baud = */ 59,
126                 /* .gcr =  */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
127         },
128 };
129
130 void
131 ao_serial_set_speed(uint8_t speed)
132 {
133         ao_serial_drain();
134         if (speed > AO_SERIAL_SPEED_57600)
135                 return;
136         U1BAUD = ao_serial_speeds[speed].baud;
137         U1GCR = ao_serial_speeds[speed].gcr;
138 }
139
140 void
141 ao_serial_init(void)
142 {
143         /* Set up the USART pin assignment */
144         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
145
146         /* ee has already set the P2SEL bits */
147
148         /* Make the USART pins be controlled by the USART */
149         P1SEL |= (1 << 6) | (1 << 7);
150
151         /* UART mode with receiver enabled */
152         U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
153
154         /* Pick a 4800 baud rate */
155         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
156
157         /* Reasonable serial parameters */
158         U1UCR = (UxUCR_FLUSH |
159                  UxUCR_FLOW_DISABLE |
160                  UxUCR_D9_ODD_PARITY |
161                  UxUCR_BIT9_8_BITS |
162                  UxUCR_PARITY_DISABLE |
163                  UxUCR_SPB_1_STOP_BIT |
164                  UxUCR_STOP_HIGH |
165                  UxUCR_START_LOW);
166
167         IEN0 |= IEN0_URX1IE;
168         IEN2 |= IEN2_UTX1IE;
169
170         ao_cmd_register(&ao_serial_cmds[0]);
171 }