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