4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
33 * \brief Serial port emulator for hosted environments.
36 * \author Bernardo Innocenti <bernie@develer.com>
39 #include "cfg/cfg_ser.h"
41 #include <cfg/debug.h>
42 #include <cfg/compiler.h>
45 #include <drv/ser_p.h>
47 #include <mware/fifobuf.h>
49 #include <sys/types.h>
52 #include <fcntl.h> /* open() */
53 #include <unistd.h> /* read(), write() */
56 /* From the high-level serial driver */
57 extern struct Serial ser_handles[SER_CNT];
59 /* TX and RX buffers */
60 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
61 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
62 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
63 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
67 * Internal state structure
71 struct SerialHardware hw;
80 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
82 struct EmulSerial *hw = (struct EmulSerial *)_hw;
85 hw->fd = open("/dev/ttyS0", O_RDWR);
88 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
90 struct EmulSerial *hw = (struct EmulSerial *)_hw;
96 static void uart_txStart(struct SerialHardware * _hw)
98 struct EmulSerial *hw = (struct EmulSerial *)_hw;
100 while(!fifo_isempty(&hw->ser->txfifo))
102 char c = fifo_pop(&hw->ser->txfifo);
103 write(hw->fd, &c, 1);
107 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
113 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
115 TRACEMSG("rate=%lu", rate);
120 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
122 TRACEMSG("parity=%d", parity);
126 // FIXME: move into compiler.h? Ditch?
128 #define C99INIT(name,val) .name = val
129 #elif defined(__GNUC__)
130 #define C99INIT(name,val) name: val
132 #warning No designated initializers, double check your code
133 #define C99INIT(name,val) (val)
137 * High-level interface data structures.
139 static const struct SerialHardwareVT uart_vtable =
141 C99INIT(init, uart_init),
142 C99INIT(cleanup, uart_cleanup),
143 C99INIT(setBaudrate, uart_setBaudrate),
144 C99INIT(setParity, uart_setParity),
145 C99INIT(txStart, uart_txStart),
146 C99INIT(txSending, uart_txSending),
149 static struct EmulSerial UARTDescs[SER_CNT] =
153 C99INIT(table, &uart_vtable),
154 C99INIT(txbuffer, uart0_txbuffer),
155 C99INIT(rxbuffer, uart0_rxbuffer),
156 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
157 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
164 C99INIT(table, &uart_vtable),
165 C99INIT(txbuffer, uart1_txbuffer),
166 C99INIT(rxbuffer, uart1_rxbuffer),
167 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
168 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
175 struct SerialHardware *ser_hw_getdesc(int unit)
177 ASSERT(unit < SER_CNT);
178 return &UARTDescs[unit].hw;