Update POSIX timer emulator.
[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 README.devlib 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.4  2006/02/17 22:23:06  bernie
17  *#* Update POSIX serial emulator.
18  *#*
19  *#* Revision 1.3  2005/11/04 16:20:02  bernie
20  *#* Fix reference to README.devlib in header.
21  *#*
22  *#* Revision 1.2  2005/04/11 19:10:27  bernie
23  *#* Include top-level headers from cfg/ subdir.
24  *#*
25  *#* Revision 1.1  2004/12/31 17:40:00  bernie
26  *#* Add a simple serial emulation driver.
27  *#*
28  *#*/
29
30 #include "ser.h"
31 #include "ser_p.h"
32
33 #include <cfg/debug.h>
34 #include <cfg/compiler.h>
35 #include <mware/fifobuf.h>
36
37 #include <appconfig.h>
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h> /* open() */
42 #include <unistd.h> /* read(), write() */
43
44
45 /* From the high-level serial driver */
46 extern struct Serial ser_handles[SER_CNT];
47
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];
53
54
55 /*!
56  * Internal state structure
57  */
58 struct EmulSerial
59 {
60         struct SerialHardware hw;
61         struct Serial *ser;
62         int fd;
63 };
64
65
66 /*
67  * Callbacks
68  */
69 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
70 {
71         struct EmulSerial *hw = (struct EmulSerial *)_hw;
72
73         hw->ser = ser;
74         hw->fd = open("/dev/ttyS0", O_RDWR);
75 }
76
77 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
78 {
79         struct EmulSerial *hw = (struct EmulSerial *)_hw;
80
81         close(hw->fd);
82         hw->fd = -1;
83 }
84
85 static void uart_txStart(struct SerialHardware * _hw)
86 {
87         struct EmulSerial *hw = (struct EmulSerial *)_hw;
88
89         while(!fifo_isempty(&hw->ser->txfifo))
90         {
91                 char c = fifo_pop(&hw->ser->txfifo);
92                 write(hw->fd, &c, 1);
93         }
94 }
95
96 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
97 {
98         return false;
99 }
100
101
102 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
103 {
104         TRACEMSG("rate=%lu", rate);
105         // TODO
106
107 }
108
109 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
110 {
111         TRACEMSG("parity=%d", parity);
112         // TODO
113 }
114
115 // FIXME: move into compiler.h?  Ditch?
116 #if COMPILER_C99
117         #define C99INIT(name,val) .name = val
118 #elif defined(__GNUC__)
119         #define C99INIT(name,val) name: val
120 #else
121         #warning No designated initializers, double check your code
122         #define C99INIT(name,val) (val)
123 #endif
124
125 /*
126  * High-level interface data structures.
127  */
128 static const struct SerialHardwareVT uart_vtable =
129 {
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),
136 };
137
138 static struct EmulSerial UARTDescs[SER_CNT] =
139 {
140         {
141                 C99INIT(hw, /**/) {
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)),
147                 },
148                 C99INIT(ser, NULL),
149                 C99INIT(fd, -1),
150         },
151         {
152                 C99INIT(hw, /**/) {
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)),
158                 },
159                 C99INIT(ser, NULL),
160                 C99INIT(fd, -1),
161         },
162 };
163
164 struct SerialHardware *ser_hw_getdesc(int unit)
165 {
166         ASSERT(unit < SER_CNT);
167         return &UARTDescs[unit].hw;
168 }