Refactor to decouple timer ticks from milliseconds.
[bertos.git] / drv / ser_emul.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See devlib/README for information.
6  * -->
7  *
8  * \brief Serial port emulator for hosted environments.
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.2  2005/04/11 19:10:27  bernie
17  *#* Include top-level headers from cfg/ subdir.
18  *#*
19  *#* Revision 1.1  2004/12/31 17:40:00  bernie
20  *#* Add a simple serial emulation driver.
21  *#*
22  *#*/
23
24 #include "ser.h"
25 #include "ser_p.h"
26 #include <cfg/config.h>
27
28 #include <cfg/debug.h>
29 #include <mware/fifobuf.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h> /* open() */
34 #include <unistd.h> /* read(), write() */
35
36
37 /* From the high-level serial driver */
38 extern struct Serial ser_handles[SER_CNT];
39
40 /* TX and RX buffers */
41 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
42 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
43 #if CONFIG_EMUL_UART1
44         static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
45         static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
46 #endif
47
48
49 /*!
50  * Internal state structure
51  */
52 struct EmulSerial
53 {
54         struct SerialHardware hw;
55         struct Serial *ser;
56         int fd;
57 };
58
59
60 /*
61  * Callbacks
62  */
63 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
64 {
65         struct EmulSerial *hw = (struct EmulSerial *)_hw;
66
67         hw->ser = ser;
68         hw->fd = open("/dev/ttyS0", O_RDWR);
69 }
70
71 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
72 {
73         struct EmulSerial *hw = (struct EmulSerial *)_hw;
74
75         close(hw->fd);
76         hw->fd = -1;
77 }
78
79 static void uart_enabletxirq(struct SerialHardware * _hw)
80 {
81         struct EmulSerial *hw = (struct EmulSerial *)_hw;
82
83         while(!fifo_isempty(&hw->ser->txfifo))
84         {
85                 fputc(fifo_pop(&hw->ser->txfifo), hw->fd);
86         }
87 }
88
89 static void uart_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
90 {
91         TRACEMSG("rate=%lu", rate);
92         // TODO
93
94 }
95
96 static void uart_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
97 {
98         TRACEMSG("parity=%d", parity);
99         // TODO
100 }
101
102
103 // FIXME: move into compiler.h?  Ditch?
104 #if COMPILER_C99
105         #define C99INIT(name,val) .name = val
106 #elif defined(__GNUC__)
107         #define C99INIT(name,val) name: val
108 #else
109         #warning No designated initializers, double check your code
110         #define C99INIT(name,val) (val)
111 #endif
112
113 /*
114  * High-level interface data structures
115  */
116 static const struct SerialHardwareVT UART0_VT =
117 {
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),
123 };
124
125 #if CONFIG_EMUL_UART1
126 static const struct SerialHardwareVT UART1_VT =
127 {
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),
133 };
134 #endif // CONFIG_EMUL_UART1
135
136 static struct EmulSerial UARTDescs[SER_CNT] =
137 {
138         {
139                 C99INIT(hw, /**/) {
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)),
145                 },
146                 C99INIT(fd, -1),
147         },
148 #if CONFIG_EMUL_UART1
149         {
150                 C99INIT(hw, /**/) {
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)),
156                 },
157                 C99INIT(fd, -1),
158         },
159 #endif
160 };
161
162 struct SerialHardware* ser_hw_getdesc(int unit)
163 {
164         ASSERT(unit < SER_CNT);
165         return &UARTDescs[unit].hw;
166 }