4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README for information.
8 * \brief Serial port emulator for hosted environments.
11 * \author Bernardo Innocenti <bernie@develer.com>
16 *#* Revision 1.2 2005/04/11 19:10:27 bernie
17 *#* Include top-level headers from cfg/ subdir.
19 *#* Revision 1.1 2004/12/31 17:40:00 bernie
20 *#* Add a simple serial emulation driver.
26 #include <cfg/config.h>
28 #include <cfg/debug.h>
29 #include <mware/fifobuf.h>
31 #include <sys/types.h>
33 #include <fcntl.h> /* open() */
34 #include <unistd.h> /* read(), write() */
37 /* From the high-level serial driver */
38 extern struct Serial ser_handles[SER_CNT];
40 /* TX and RX buffers */
41 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
42 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
44 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
45 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
50 * Internal state structure
54 struct SerialHardware hw;
63 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
65 struct EmulSerial *hw = (struct EmulSerial *)_hw;
68 hw->fd = open("/dev/ttyS0", O_RDWR);
71 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
73 struct EmulSerial *hw = (struct EmulSerial *)_hw;
79 static void uart_enabletxirq(struct SerialHardware * _hw)
81 struct EmulSerial *hw = (struct EmulSerial *)_hw;
83 while(!fifo_isempty(&hw->ser->txfifo))
85 fputc(fifo_pop(&hw->ser->txfifo), hw->fd);
89 static void uart_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
91 TRACEMSG("rate=%lu", rate);
96 static void uart_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
98 TRACEMSG("parity=%d", parity);
103 // FIXME: move into compiler.h? Ditch?
105 #define C99INIT(name,val) .name = val
106 #elif defined(__GNUC__)
107 #define C99INIT(name,val) name: val
109 #warning No designated initializers, double check your code
110 #define C99INIT(name,val) (val)
114 * High-level interface data structures
116 static const struct SerialHardwareVT UART0_VT =
118 C99INIT(init, uart_init),
119 C99INIT(cleanup, uart_cleanup),
120 C99INIT(setbaudrate, uart_setbaudrate),
121 C99INIT(setparity, uart_setparity),
122 C99INIT(enabletxirq, uart_enabletxirq),
125 #if CONFIG_EMUL_UART1
126 static const struct SerialHardwareVT UART1_VT =
128 C99INIT(init, uart_init),
129 C99INIT(cleanup, uart_cleanup),
130 C99INIT(setbaudrate, uart_setbaudrate),
131 C99INIT(setparity, uart_setparity),
132 C99INIT(enabletxirq, uart_enabletxirq),
134 #endif // CONFIG_EMUL_UART1
136 static struct EmulSerial UARTDescs[SER_CNT] =
140 C99INIT(table, &UART0_VT),
141 C99INIT(txbuffer, uart0_txbuffer),
142 C99INIT(rxbuffer, uart0_rxbuffer),
143 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
144 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
148 #if CONFIG_EMUL_UART1
151 C99INIT(table, &UART1_VT),
152 C99INIT(txbuffer, uart1_txbuffer),
153 C99INIT(rxbuffer, uart1_rxbuffer),
154 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
155 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
162 struct SerialHardware* ser_hw_getdesc(int unit)
164 ASSERT(unit < SER_CNT);
165 return &UARTDescs[unit].hw;