e926b4e4fb24d656a04fbea8434e2b169440669d
[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 char
54 ao_serial_getchar(void) __critical
55 {
56         char    c;
57         while (ao_fifo_empty(ao_usart1_rx_fifo))
58                 ao_sleep(&ao_usart1_rx_fifo);
59         ao_fifo_remove(ao_usart1_rx_fifo, c);
60         return c;
61 }
62
63 void
64 ao_serial_putchar(char c) __critical
65 {
66         while (ao_fifo_full(ao_usart1_tx_fifo))
67                 ao_sleep(&ao_usart1_tx_fifo);
68         ao_fifo_insert(ao_usart1_tx_fifo, c);
69         ao_serial_tx1_start();
70 }
71
72 static void
73 send_serial(void)
74 {
75         ao_cmd_white();
76         while (ao_cmd_lex_c != '\n') {
77                 ao_serial_putchar(ao_cmd_lex_c);
78                 ao_cmd_lex();
79         }
80 }
81
82 __code struct ao_cmds ao_serial_cmds[] = {
83         { 'S', send_serial,             "S <data>                           Send data to serial line" },
84         { 0, send_serial, NULL },
85 };
86
87 static const struct {
88         uint8_t baud;
89         uint8_t gcr;
90 } ao_serial_speeds[] = {
91         /* [AO_SERIAL_SPEED_4800] = */ {
92                 /* .baud = */ 163,
93                 /* .gcr  = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
94         },
95         /* [AO_SERIAL_SPEED_57600] = */ {
96                 /* .baud = */ 59,
97                 /* .gcr =  */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB
98         },
99 };
100
101 void
102 ao_serial_set_speed(uint8_t speed)
103 {
104         U1BAUD = ao_serial_speeds[speed].baud;
105         U1GCR = ao_serial_speeds[speed].gcr;
106 }
107
108 void
109 ao_serial_init(void)
110 {
111         /* Set up the USART pin assignment */
112         PERCFG = (PERCFG & ~PERCFG_U1CFG_ALT_MASK) | PERCFG_U1CFG_ALT_2;
113
114         /* ee has already set the P2SEL bits */
115
116         /* Make the USART pins be controlled by the USART */
117         P1SEL |= (1 << 6) | (1 << 7);
118
119         /* UART mode with receiver enabled */
120         U1CSR = (UxCSR_MODE_UART | UxCSR_RE);
121
122         /* Pick a 4800 baud rate */
123         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
124
125         /* Reasonable serial parameters */
126         U1UCR = (UxUCR_FLUSH |
127                  UxUCR_FLOW_DISABLE |
128                  UxUCR_D9_ODD_PARITY |
129                  UxUCR_BIT9_8_BITS |
130                  UxUCR_PARITY_DISABLE |
131                  UxUCR_SPB_1_STOP_BIT |
132                  UxUCR_STOP_HIGH |
133                  UxUCR_START_LOW);
134
135         IEN0 |= IEN0_URX1IE;
136         IEN2 |= IEN2_UTX1IE;
137
138         ao_cmd_register(&ao_serial_cmds[0]);
139 }