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.1 2004/12/31 17:40:00 bernie
17 *#* Add a simple serial emulation driver.
26 #include <mware/fifobuf.h>
28 #include <sys/types.h>
30 #include <fcntl.h> /* open() */
31 #include <unistd.h> /* read(), write() */
34 /* From the high-level serial driver */
35 extern struct Serial ser_handles[SER_CNT];
37 /* TX and RX buffers */
38 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
39 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
41 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
42 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
47 * Internal state structure
51 struct SerialHardware hw;
60 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
62 struct EmulSerial *hw = (struct EmulSerial *)_hw;
65 hw->fd = open("/dev/ttyS0", O_RDWR);
68 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
70 struct EmulSerial *hw = (struct EmulSerial *)_hw;
76 static void uart_enabletxirq(struct SerialHardware * _hw)
78 struct EmulSerial *hw = (struct EmulSerial *)_hw;
80 while(!fifo_isempty(&hw->ser->txfifo))
82 fputc(fifo_pop(&hw->ser->txfifo), hw->fd);
86 static void uart_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
88 TRACEMSG("rate=%lu", rate);
93 static void uart_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
95 TRACEMSG("parity=%d", parity);
100 // FIXME: move into compiler.h? Ditch?
102 #define C99INIT(name,val) .name = val
103 #elif defined(__GNUC__)
104 #define C99INIT(name,val) name: val
106 #warning No designated initializers, double check your code
107 #define C99INIT(name,val) (val)
111 * High-level interface data structures
113 static const struct SerialHardwareVT UART0_VT =
115 C99INIT(init, uart_init),
116 C99INIT(cleanup, uart_cleanup),
117 C99INIT(setbaudrate, uart_setbaudrate),
118 C99INIT(setparity, uart_setparity),
119 C99INIT(enabletxirq, uart_enabletxirq),
122 #if CONFIG_EMUL_UART1
123 static const struct SerialHardwareVT UART1_VT =
125 C99INIT(init, uart_init),
126 C99INIT(cleanup, uart_cleanup),
127 C99INIT(setbaudrate, uart_setbaudrate),
128 C99INIT(setparity, uart_setparity),
129 C99INIT(enabletxirq, uart_enabletxirq),
131 #endif // CONFIG_EMUL_UART1
133 static struct EmulSerial UARTDescs[SER_CNT] =
137 C99INIT(table, &UART0_VT),
138 C99INIT(txbuffer, uart0_txbuffer),
139 C99INIT(rxbuffer, uart0_rxbuffer),
140 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
141 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
145 #if CONFIG_EMUL_UART1
148 C99INIT(table, &UART1_VT),
149 C99INIT(txbuffer, uart1_txbuffer),
150 C99INIT(rxbuffer, uart1_rxbuffer),
151 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
152 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
159 struct SerialHardware* ser_hw_getdesc(int unit)
161 ASSERT(unit < SER_CNT);
162 return &UARTDescs[unit].hw;