2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <ao_serial.h>
21 struct ao_fifo ao_usart_rx_fifo;
22 struct ao_fifo ao_usart_tx_fifo;
23 uint8_t ao_usart_tx_started;
31 while (!(stm_usart1.sr & (1 << STM_USART_SR_TXE)));
37 _ao_serial_tx_start(void)
39 if (!ao_fifo_empty(ao_usart_tx_fifo) & !ao_usart_tx_started)
41 ao_usart_tx_started = 1;
43 ao_fifo_remove(ao_usart_tx_fifo, usart->reg->dr);
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);
63 ao_wakeup(&ao_stdin_ready);
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);
74 _ao_serial_pollchar(void)
78 if (ao_fifo_empty(ao_usart_rx_fifo))
82 ao_fifo_remove(ao_usart_rx_fifo,u);
89 ao_serial_getchar(void)
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();
100 ao_serial_putchar(char c)
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();
111 ao_serial_drain(void)
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();
120 ao_serial_set_speed(uint8_t speed)
122 if (speed > AO_SERIAL_SPEED_115200)
125 usart->reg->brr = ao_usart_speeds[speed].brr;
129 #include "ao_serial_lpc.h"
134 /* Turn on the USART clock */
135 lpc_scb.uartclkdiv = 1;
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));
150 /* Turn on the USART */
151 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_USART);