From 02a6da7d7d4f37a602a23f7950d668f3de8393e3 Mon Sep 17 00:00:00 2001 From: bernie Date: Fri, 31 Dec 2004 17:40:00 +0000 Subject: [PATCH] Add a simple serial emulation driver. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@325 38d2e660-2303-0410-9eaa-f027e97ec537 --- drv/ser_emul.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100755 drv/ser_emul.c diff --git a/drv/ser_emul.c b/drv/ser_emul.c new file mode 100755 index 00000000..53298672 --- /dev/null +++ b/drv/ser_emul.c @@ -0,0 +1,163 @@ +/*! + * \file + * + * + * \brief Serial port emulator for hosted environments. + * + * \version $Id$ + * \author Bernardo Innocenti + */ + +/*#* + *#* $Log$ + *#* Revision 1.1 2004/12/31 17:40:00 bernie + *#* Add a simple serial emulation driver. + *#* + *#*/ + +#include "ser.h" +#include "ser_p.h" +#include "config.h" + +#include +#include + +#include +#include +#include /* open() */ +#include /* read(), write() */ + + +/* From the high-level serial driver */ +extern struct Serial ser_handles[SER_CNT]; + +/* TX and RX buffers */ +static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE]; +static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE]; +#if CONFIG_EMUL_UART1 + static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE]; + static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE]; +#endif + + +/*! + * Internal state structure + */ +struct EmulSerial +{ + struct SerialHardware hw; + struct Serial *ser; + int fd; +}; + + +/* + * Callbacks + */ +static void uart_init(struct SerialHardware *_hw, struct Serial *ser) +{ + struct EmulSerial *hw = (struct EmulSerial *)_hw; + + hw->ser = ser; + hw->fd = open("/dev/ttyS0", O_RDWR); +} + +static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw)) +{ + struct EmulSerial *hw = (struct EmulSerial *)_hw; + + close(hw->fd); + hw->fd = -1; +} + +static void uart_enabletxirq(struct SerialHardware * _hw) +{ + struct EmulSerial *hw = (struct EmulSerial *)_hw; + + while(!fifo_isempty(&hw->ser->txfifo)) + { + fputc(fifo_pop(&hw->ser->txfifo), hw->fd); + } +} + +static void uart_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate) +{ + TRACEMSG("rate=%lu", rate); + // TODO + +} + +static void uart_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity) +{ + TRACEMSG("parity=%d", parity); + // TODO +} + + +// FIXME: move into compiler.h? Ditch? +#if COMPILER_C99 + #define C99INIT(name,val) .name = val +#elif defined(__GNUC__) + #define C99INIT(name,val) name: val +#else + #warning No designated initializers, double check your code + #define C99INIT(name,val) (val) +#endif + +/* + * High-level interface data structures + */ +static const struct SerialHardwareVT UART0_VT = +{ + C99INIT(init, uart_init), + C99INIT(cleanup, uart_cleanup), + C99INIT(setbaudrate, uart_setbaudrate), + C99INIT(setparity, uart_setparity), + C99INIT(enabletxirq, uart_enabletxirq), +}; + +#if CONFIG_EMUL_UART1 +static const struct SerialHardwareVT UART1_VT = +{ + C99INIT(init, uart_init), + C99INIT(cleanup, uart_cleanup), + C99INIT(setbaudrate, uart_setbaudrate), + C99INIT(setparity, uart_setparity), + C99INIT(enabletxirq, uart_enabletxirq), +}; +#endif // CONFIG_EMUL_UART1 + +static struct EmulSerial UARTDescs[SER_CNT] = +{ + { + C99INIT(hw, /**/) { + C99INIT(table, &UART0_VT), + C99INIT(txbuffer, uart0_txbuffer), + C99INIT(rxbuffer, uart0_rxbuffer), + C99INIT(txbuffer_size, sizeof(uart0_txbuffer)), + C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)), + }, + C99INIT(fd, -1), + }, +#if CONFIG_EMUL_UART1 + { + C99INIT(hw, /**/) { + C99INIT(table, &UART1_VT), + C99INIT(txbuffer, uart1_txbuffer), + C99INIT(rxbuffer, uart1_rxbuffer), + C99INIT(txbuffer_size, sizeof(uart1_txbuffer)), + C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)), + }, + C99INIT(fd, -1), + }, +#endif +}; + +struct SerialHardware* ser_hw_getdesc(int unit) +{ + ASSERT(unit < SER_CNT); + return &UARTDescs[unit].hw; +} -- 2.25.1