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.4 2006/02/17 22:23:06 bernie
17 *#* Update POSIX serial emulator.
19 *#* Revision 1.3 2005/11/04 16:20:02 bernie
20 *#* Fix reference to README.devlib in header.
22 *#* Revision 1.2 2005/04/11 19:10:27 bernie
23 *#* Include top-level headers from cfg/ subdir.
25 *#* Revision 1.1 2004/12/31 17:40:00 bernie
26 *#* Add a simple serial emulation driver.
33 #include <cfg/debug.h>
34 #include <cfg/compiler.h>
35 #include <mware/fifobuf.h>
37 #include <appconfig.h>
39 #include <sys/types.h>
41 #include <fcntl.h> /* open() */
42 #include <unistd.h> /* read(), write() */
45 /* From the high-level serial driver */
46 extern struct Serial ser_handles[SER_CNT];
48 /* TX and RX buffers */
49 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
50 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
51 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
52 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
56 * Internal state structure
60 struct SerialHardware hw;
69 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
71 struct EmulSerial *hw = (struct EmulSerial *)_hw;
74 hw->fd = open("/dev/ttyS0", O_RDWR);
77 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
79 struct EmulSerial *hw = (struct EmulSerial *)_hw;
85 static void uart_txStart(struct SerialHardware * _hw)
87 struct EmulSerial *hw = (struct EmulSerial *)_hw;
89 while(!fifo_isempty(&hw->ser->txfifo))
91 char c = fifo_pop(&hw->ser->txfifo);
96 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
102 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
104 TRACEMSG("rate=%lu", rate);
109 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
111 TRACEMSG("parity=%d", parity);
115 // FIXME: move into compiler.h? Ditch?
117 #define C99INIT(name,val) .name = val
118 #elif defined(__GNUC__)
119 #define C99INIT(name,val) name: val
121 #warning No designated initializers, double check your code
122 #define C99INIT(name,val) (val)
126 * High-level interface data structures.
128 static const struct SerialHardwareVT uart_vtable =
130 C99INIT(init, uart_init),
131 C99INIT(cleanup, uart_cleanup),
132 C99INIT(setBaudrate, uart_setBaudrate),
133 C99INIT(setParity, uart_setParity),
134 C99INIT(txStart, uart_txStart),
135 C99INIT(txSending, uart_txSending),
138 static struct EmulSerial UARTDescs[SER_CNT] =
142 C99INIT(table, &uart_vtable),
143 C99INIT(txbuffer, uart0_txbuffer),
144 C99INIT(rxbuffer, uart0_rxbuffer),
145 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
146 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
153 C99INIT(table, &uart_vtable),
154 C99INIT(txbuffer, uart1_txbuffer),
155 C99INIT(rxbuffer, uart1_rxbuffer),
156 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
157 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
164 struct SerialHardware *ser_hw_getdesc(int unit)
166 ASSERT(unit < SER_CNT);
167 return &UARTDescs[unit].hw;