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.2 2006/07/19 12:56:26 bernie
17 *#* Convert to new Doxygen style.
19 *#* Revision 1.1 2006/02/17 22:28:00 bernie
20 *#* Rename ser_emul.c to ser_posix.c.
22 *#* Revision 1.4 2006/02/17 22:23:06 bernie
23 *#* Update POSIX serial emulator.
25 *#* Revision 1.3 2005/11/04 16:20:02 bernie
26 *#* Fix reference to README.devlib in header.
28 *#* Revision 1.2 2005/04/11 19:10:27 bernie
29 *#* Include top-level headers from cfg/ subdir.
31 *#* Revision 1.1 2004/12/31 17:40:00 bernie
32 *#* Add a simple serial emulation driver.
39 #include <cfg/debug.h>
40 #include <cfg/compiler.h>
41 #include <mware/fifobuf.h>
43 #include <appconfig.h>
45 #include <sys/types.h>
47 #include <fcntl.h> /* open() */
48 #include <unistd.h> /* read(), write() */
51 /* From the high-level serial driver */
52 extern struct Serial ser_handles[SER_CNT];
54 /* TX and RX buffers */
55 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
56 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
57 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
58 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
62 * Internal state structure
66 struct SerialHardware hw;
75 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
77 struct EmulSerial *hw = (struct EmulSerial *)_hw;
80 hw->fd = open("/dev/ttyS0", O_RDWR);
83 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
85 struct EmulSerial *hw = (struct EmulSerial *)_hw;
91 static void uart_txStart(struct SerialHardware * _hw)
93 struct EmulSerial *hw = (struct EmulSerial *)_hw;
95 while(!fifo_isempty(&hw->ser->txfifo))
97 char c = fifo_pop(&hw->ser->txfifo);
102 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
108 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
110 TRACEMSG("rate=%lu", rate);
115 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
117 TRACEMSG("parity=%d", parity);
121 // FIXME: move into compiler.h? Ditch?
123 #define C99INIT(name,val) .name = val
124 #elif defined(__GNUC__)
125 #define C99INIT(name,val) name: val
127 #warning No designated initializers, double check your code
128 #define C99INIT(name,val) (val)
132 * High-level interface data structures.
134 static const struct SerialHardwareVT uart_vtable =
136 C99INIT(init, uart_init),
137 C99INIT(cleanup, uart_cleanup),
138 C99INIT(setBaudrate, uart_setBaudrate),
139 C99INIT(setParity, uart_setParity),
140 C99INIT(txStart, uart_txStart),
141 C99INIT(txSending, uart_txSending),
144 static struct EmulSerial UARTDescs[SER_CNT] =
148 C99INIT(table, &uart_vtable),
149 C99INIT(txbuffer, uart0_txbuffer),
150 C99INIT(rxbuffer, uart0_rxbuffer),
151 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
152 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
159 C99INIT(table, &uart_vtable),
160 C99INIT(txbuffer, uart1_txbuffer),
161 C99INIT(rxbuffer, uart1_rxbuffer),
162 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
163 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
170 struct SerialHardware *ser_hw_getdesc(int unit)
172 ASSERT(unit < SER_CNT);
173 return &UARTDescs[unit].hw;