4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief Serial port emulator for hosted environments.
11 * \author Bernardo Innocenti <bernie@develer.com>
16 *#* Revision 1.1 2006/02/17 22:28:00 bernie
17 *#* Rename ser_emul.c to ser_posix.c.
19 *#* Revision 1.4 2006/02/17 22:23:06 bernie
20 *#* Update POSIX serial emulator.
22 *#* Revision 1.3 2005/11/04 16:20:02 bernie
23 *#* Fix reference to README.devlib in header.
25 *#* Revision 1.2 2005/04/11 19:10:27 bernie
26 *#* Include top-level headers from cfg/ subdir.
28 *#* Revision 1.1 2004/12/31 17:40:00 bernie
29 *#* Add a simple serial emulation driver.
36 #include <cfg/debug.h>
37 #include <cfg/compiler.h>
38 #include <mware/fifobuf.h>
40 #include <appconfig.h>
42 #include <sys/types.h>
44 #include <fcntl.h> /* open() */
45 #include <unistd.h> /* read(), write() */
48 /* From the high-level serial driver */
49 extern struct Serial ser_handles[SER_CNT];
51 /* TX and RX buffers */
52 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
53 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
54 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
55 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
59 * Internal state structure
63 struct SerialHardware hw;
72 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
74 struct EmulSerial *hw = (struct EmulSerial *)_hw;
77 hw->fd = open("/dev/ttyS0", O_RDWR);
80 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
82 struct EmulSerial *hw = (struct EmulSerial *)_hw;
88 static void uart_txStart(struct SerialHardware * _hw)
90 struct EmulSerial *hw = (struct EmulSerial *)_hw;
92 while(!fifo_isempty(&hw->ser->txfifo))
94 char c = fifo_pop(&hw->ser->txfifo);
99 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
105 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
107 TRACEMSG("rate=%lu", rate);
112 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
114 TRACEMSG("parity=%d", parity);
118 // FIXME: move into compiler.h? Ditch?
120 #define C99INIT(name,val) .name = val
121 #elif defined(__GNUC__)
122 #define C99INIT(name,val) name: val
124 #warning No designated initializers, double check your code
125 #define C99INIT(name,val) (val)
129 * High-level interface data structures.
131 static const struct SerialHardwareVT uart_vtable =
133 C99INIT(init, uart_init),
134 C99INIT(cleanup, uart_cleanup),
135 C99INIT(setBaudrate, uart_setBaudrate),
136 C99INIT(setParity, uart_setParity),
137 C99INIT(txStart, uart_txStart),
138 C99INIT(txSending, uart_txSending),
141 static struct EmulSerial UARTDescs[SER_CNT] =
145 C99INIT(table, &uart_vtable),
146 C99INIT(txbuffer, uart0_txbuffer),
147 C99INIT(rxbuffer, uart0_rxbuffer),
148 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
149 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
156 C99INIT(table, &uart_vtable),
157 C99INIT(txbuffer, uart1_txbuffer),
158 C99INIT(rxbuffer, uart1_rxbuffer),
159 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
160 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
167 struct SerialHardware *ser_hw_getdesc(int unit)
169 ASSERT(unit < SER_CNT);
170 return &UARTDescs[unit].hw;