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>
41 *#* Revision 1.2 2006/07/19 12:56:26 bernie
42 *#* Convert to new Doxygen style.
44 *#* Revision 1.1 2006/02/17 22:28:00 bernie
45 *#* Rename ser_emul.c to ser_posix.c.
47 *#* Revision 1.4 2006/02/17 22:23:06 bernie
48 *#* Update POSIX serial emulator.
50 *#* Revision 1.3 2005/11/04 16:20:02 bernie
51 *#* Fix reference to README.devlib in header.
53 *#* Revision 1.2 2005/04/11 19:10:27 bernie
54 *#* Include top-level headers from cfg/ subdir.
56 *#* Revision 1.1 2004/12/31 17:40:00 bernie
57 *#* Add a simple serial emulation driver.
64 #include <cfg/debug.h>
65 #include <cfg/compiler.h>
66 #include <mware/fifobuf.h>
68 #include <appconfig.h>
70 #include <sys/types.h>
72 #include <fcntl.h> /* open() */
73 #include <unistd.h> /* read(), write() */
76 /* From the high-level serial driver */
77 extern struct Serial ser_handles[SER_CNT];
79 /* TX and RX buffers */
80 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
81 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
82 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
83 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
87 * Internal state structure
91 struct SerialHardware hw;
100 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
102 struct EmulSerial *hw = (struct EmulSerial *)_hw;
105 hw->fd = open("/dev/ttyS0", O_RDWR);
108 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
110 struct EmulSerial *hw = (struct EmulSerial *)_hw;
116 static void uart_txStart(struct SerialHardware * _hw)
118 struct EmulSerial *hw = (struct EmulSerial *)_hw;
120 while(!fifo_isempty(&hw->ser->txfifo))
122 char c = fifo_pop(&hw->ser->txfifo);
123 write(hw->fd, &c, 1);
127 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
133 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
135 TRACEMSG("rate=%lu", rate);
140 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
142 TRACEMSG("parity=%d", parity);
146 // FIXME: move into compiler.h? Ditch?
148 #define C99INIT(name,val) .name = val
149 #elif defined(__GNUC__)
150 #define C99INIT(name,val) name: val
152 #warning No designated initializers, double check your code
153 #define C99INIT(name,val) (val)
157 * High-level interface data structures.
159 static const struct SerialHardwareVT uart_vtable =
161 C99INIT(init, uart_init),
162 C99INIT(cleanup, uart_cleanup),
163 C99INIT(setBaudrate, uart_setBaudrate),
164 C99INIT(setParity, uart_setParity),
165 C99INIT(txStart, uart_txStart),
166 C99INIT(txSending, uart_txSending),
169 static struct EmulSerial UARTDescs[SER_CNT] =
173 C99INIT(table, &uart_vtable),
174 C99INIT(txbuffer, uart0_txbuffer),
175 C99INIT(rxbuffer, uart0_rxbuffer),
176 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
177 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
184 C99INIT(table, &uart_vtable),
185 C99INIT(txbuffer, uart1_txbuffer),
186 C99INIT(rxbuffer, uart1_rxbuffer),
187 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
188 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
195 struct SerialHardware *ser_hw_getdesc(int unit)
197 ASSERT(unit < SER_CNT);
198 return &UARTDescs[unit].hw;