lpc: Initial lpcxpresso bits
[fw/altos] / src / lpc / ao_serial_lpc.c
1 /*
2  * Copyright © 2013 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 #include <ao_serial.h>
20
21 struct ao_fifo  ao_usart_rx_fifo;
22 struct ao_fifo  ao_usart_tx_fifo;
23 uint8_t         ao_usart_tx_started;
24
25 void
26 ao_debug_out(char c)
27 {
28         if (c == '\n')
29                 ao_debug_out('\r');
30 #if 0
31         while (!(stm_usart1.sr & (1 << STM_USART_SR_TXE)));
32         stm_usart1.dr = c;
33 #endif
34 }
35
36 static void
37 _ao_serial_tx_start(void)
38 {
39         if (!ao_fifo_empty(ao_usart_tx_fifo) & !ao_usart_tx_started)
40         {
41                 ao_usart_tx_started = 1;
42 #if 0
43                 ao_fifo_remove(ao_usart_tx_fifo, usart->reg->dr);
44 #endif
45         }
46 }
47
48 void
49 lpc_usart_isr(void)
50 {
51 #if 0
52         uint32_t        sr;
53
54         sr = usart->reg->sr;
55         usart->reg->sr = 0;
56
57         if (sr & (1 << STM_USART_SR_RXNE)) {
58                 char c = usart->reg->dr;
59                 if (!ao_fifo_full(ao_usart_rx_fifo))
60                         ao_fifo_insert(ao_usart_rx_fifo, c);
61                 ao_wakeup(ao_usart_rx_fifo);
62                 if (stdin)
63                         ao_wakeup(&ao_stdin_ready);
64         }
65         if (sr & (1 << STM_USART_SR_TC)) {
66                 ao_usart_tx_started = 0;
67                 _ao_usart_tx_start(usart);
68                 ao_wakeup(ao_usart_tx_fifo);
69         }
70 #endif
71 }
72
73 int
74 _ao_serial_pollchar(void)
75 {
76         int     c;
77         
78         if (ao_fifo_empty(ao_usart_rx_fifo))
79                 c = AO_READ_AGAIN;
80         else {
81                 uint8_t u;
82                 ao_fifo_remove(ao_usart_rx_fifo,u);
83                 c = u;
84         }
85         return c;
86 }
87
88 char
89 ao_serial_getchar(void)
90 {
91         int c;
92         ao_arch_block_interrupts();
93         while ((c = _ao_serial_pollchar()) == AO_READ_AGAIN)
94                 ao_sleep(&ao_usart_rx_fifo);
95         ao_arch_release_interrupts();
96         return (char) c;
97 }
98
99 void
100 ao_serial_putchar(char c)
101 {
102         ao_arch_block_interrupts();
103         while (ao_fifo_full(ao_usart_tx_fifo))
104                 ao_sleep(&ao_usart_tx_fifo);
105         ao_fifo_insert(ao_usart_tx_fifo, c);
106         _ao_serial_tx_start();
107         ao_arch_release_interrupts();
108 }
109
110 void
111 ao_serial_drain(void)
112 {
113         ao_arch_block_interrupts();
114         while (!ao_fifo_empty(ao_usart_tx_fifo))
115                 ao_sleep(&ao_usart_tx_fifo);
116         ao_arch_release_interrupts();
117 }
118
119 void
120 ao_serial_set_speed(uint8_t speed)
121 {
122         if (speed > AO_SERIAL_SPEED_115200)
123                 return;
124 #if 0
125         usart->reg->brr = ao_usart_speeds[speed].brr;
126 #endif
127 }
128
129 #include "ao_serial_lpc.h"
130
131 void
132 ao_serial_init(void)
133 {
134         /* Turn on the USART clock */
135         lpc_scb.uartclkdiv = 1;
136
137 #if SERIAL_0_18_19
138         lpc_ioconf.pio0_18 = ((LPC_IOCONF_FUNC_PIO0_18_RXD << LPC_IOCONF_FUNC) |
139                               (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
140                               (0 << LPC_IOCONF_HYS) |
141                               (0 << LPC_IOCONF_INV) |
142                               (0 << LPC_IOCONF_OD));
143         lpc_ioconf.pio0_19 = ((LPC_IOCONF_FUNC_PIO0_19_TXD << LPC_IOCONF_FUNC) |
144                               (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
145                               (0 << LPC_IOCONF_HYS) |
146                               (0 << LPC_IOCONF_INV) |
147                               (0 << LPC_IOCONF_OD));
148 #endif
149
150         /* Turn on the USART */
151         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_USART);
152 }
153
154