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