Refactor BeRTOS to be in his own directory.
[bertos.git] / bertos / drv / ser_posix.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Serial port emulator for hosted environments.
34  *
35  * \version $Id$
36  * \author Bernardo Innocenti <bernie@develer.com>
37  */
38
39 /*#*
40  *#* $Log$
41  *#* Revision 1.2  2006/07/19 12:56:26  bernie
42  *#* Convert to new Doxygen style.
43  *#*
44  *#* Revision 1.1  2006/02/17 22:28:00  bernie
45  *#* Rename ser_emul.c to ser_posix.c.
46  *#*
47  *#* Revision 1.4  2006/02/17 22:23:06  bernie
48  *#* Update POSIX serial emulator.
49  *#*
50  *#* Revision 1.3  2005/11/04 16:20:02  bernie
51  *#* Fix reference to README.devlib in header.
52  *#*
53  *#* Revision 1.2  2005/04/11 19:10:27  bernie
54  *#* Include top-level headers from cfg/ subdir.
55  *#*
56  *#* Revision 1.1  2004/12/31 17:40:00  bernie
57  *#* Add a simple serial emulation driver.
58  *#*
59  *#*/
60
61 #include "ser.h"
62 #include "ser_p.h"
63
64 #include <cfg/debug.h>
65 #include <cfg/compiler.h>
66 #include <mware/fifobuf.h>
67
68 #include <appconfig.h>
69
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <fcntl.h> /* open() */
73 #include <unistd.h> /* read(), write() */
74
75
76 /* From the high-level serial driver */
77 extern struct Serial ser_handles[SER_CNT];
78
79 /* TX and RX buffers */
80 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
81 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
82 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
83 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
84
85
86 /**
87  * Internal state structure
88  */
89 struct EmulSerial
90 {
91         struct SerialHardware hw;
92         struct Serial *ser;
93         int fd;
94 };
95
96
97 /*
98  * Callbacks
99  */
100 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
101 {
102         struct EmulSerial *hw = (struct EmulSerial *)_hw;
103
104         hw->ser = ser;
105         hw->fd = open("/dev/ttyS0", O_RDWR);
106 }
107
108 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
109 {
110         struct EmulSerial *hw = (struct EmulSerial *)_hw;
111
112         close(hw->fd);
113         hw->fd = -1;
114 }
115
116 static void uart_txStart(struct SerialHardware * _hw)
117 {
118         struct EmulSerial *hw = (struct EmulSerial *)_hw;
119
120         while(!fifo_isempty(&hw->ser->txfifo))
121         {
122                 char c = fifo_pop(&hw->ser->txfifo);
123                 write(hw->fd, &c, 1);
124         }
125 }
126
127 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
128 {
129         return false;
130 }
131
132
133 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
134 {
135         TRACEMSG("rate=%lu", rate);
136         // TODO
137
138 }
139
140 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
141 {
142         TRACEMSG("parity=%d", parity);
143         // TODO
144 }
145
146 // FIXME: move into compiler.h?  Ditch?
147 #if COMPILER_C99
148         #define C99INIT(name,val) .name = val
149 #elif defined(__GNUC__)
150         #define C99INIT(name,val) name: val
151 #else
152         #warning No designated initializers, double check your code
153         #define C99INIT(name,val) (val)
154 #endif
155
156 /*
157  * High-level interface data structures.
158  */
159 static const struct SerialHardwareVT uart_vtable =
160 {
161         C99INIT(init, uart_init),
162         C99INIT(cleanup, uart_cleanup),
163         C99INIT(setBaudrate, uart_setBaudrate),
164         C99INIT(setParity, uart_setParity),
165         C99INIT(txStart, uart_txStart),
166         C99INIT(txSending, uart_txSending),
167 };
168
169 static struct EmulSerial UARTDescs[SER_CNT] =
170 {
171         {
172                 C99INIT(hw, /**/) {
173                         C99INIT(table, &uart_vtable),
174                         C99INIT(txbuffer, uart0_txbuffer),
175                         C99INIT(rxbuffer, uart0_rxbuffer),
176                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
177                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
178                 },
179                 C99INIT(ser, NULL),
180                 C99INIT(fd, -1),
181         },
182         {
183                 C99INIT(hw, /**/) {
184                         C99INIT(table, &uart_vtable),
185                         C99INIT(txbuffer, uart1_txbuffer),
186                         C99INIT(rxbuffer, uart1_rxbuffer),
187                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
188                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
189                 },
190                 C99INIT(ser, NULL),
191                 C99INIT(fd, -1),
192         },
193 };
194
195 struct SerialHardware *ser_hw_getdesc(int unit)
196 {
197         ASSERT(unit < SER_CNT);
198         return &UARTDescs[unit].hw;
199 }