5118305956bcc7ebd3f717de67d3d15a474020fa
[bertos.git] / bertos / emul / 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 #include "cfg/cfg_ser.h"
40
41 #include <cfg/debug.h>
42 #include <cfg/compiler.h>
43
44 #include <drv/ser.h>
45 #include <drv/ser_p.h>
46
47 #include <mware/fifobuf.h>
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 #include <fcntl.h> /* open() */
53 #include <unistd.h> /* read(), write() */
54
55
56 /* From the high-level serial driver */
57 extern struct Serial ser_handles[SER_CNT];
58
59 /* TX and RX buffers */
60 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
61 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
62 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
63 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
64
65
66 /**
67  * Internal state structure
68  */
69 struct EmulSerial
70 {
71         struct SerialHardware hw;
72         struct Serial *ser;
73         int fd;
74 };
75
76
77 /*
78  * Callbacks
79  */
80 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
81 {
82         struct EmulSerial *hw = (struct EmulSerial *)_hw;
83
84         hw->ser = ser;
85         hw->fd = open("/dev/ttyS0", O_RDWR);
86 }
87
88 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
89 {
90         struct EmulSerial *hw = (struct EmulSerial *)_hw;
91
92         close(hw->fd);
93         hw->fd = -1;
94 }
95
96 static void uart_txStart(struct SerialHardware * _hw)
97 {
98         struct EmulSerial *hw = (struct EmulSerial *)_hw;
99
100         while(!fifo_isempty(&hw->ser->txfifo))
101         {
102                 char c = fifo_pop(&hw->ser->txfifo);
103                 write(hw->fd, &c, 1);
104         }
105 }
106
107 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
108 {
109         return false;
110 }
111
112
113 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
114 {
115         TRACEMSG("rate=%lu", rate);
116         // TODO
117
118 }
119
120 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
121 {
122         TRACEMSG("parity=%d", parity);
123         // TODO
124 }
125
126 // FIXME: move into compiler.h?  Ditch?
127 #if COMPILER_C99
128         #define C99INIT(name,val) .name = val
129 #elif defined(__GNUC__)
130         #define C99INIT(name,val) name: val
131 #else
132         #warning No designated initializers, double check your code
133         #define C99INIT(name,val) (val)
134 #endif
135
136 /*
137  * High-level interface data structures.
138  */
139 static const struct SerialHardwareVT uart_vtable =
140 {
141         C99INIT(init, uart_init),
142         C99INIT(cleanup, uart_cleanup),
143         C99INIT(setBaudrate, uart_setBaudrate),
144         C99INIT(setParity, uart_setParity),
145         C99INIT(txStart, uart_txStart),
146         C99INIT(txSending, uart_txSending),
147 };
148
149 static struct EmulSerial UARTDescs[SER_CNT] =
150 {
151         {
152                 C99INIT(hw, /**/) {
153                         C99INIT(table, &uart_vtable),
154                         C99INIT(txbuffer, uart0_txbuffer),
155                         C99INIT(rxbuffer, uart0_rxbuffer),
156                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
157                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
158                 },
159                 C99INIT(ser, NULL),
160                 C99INIT(fd, -1),
161         },
162         {
163                 C99INIT(hw, /**/) {
164                         C99INIT(table, &uart_vtable),
165                         C99INIT(txbuffer, uart1_txbuffer),
166                         C99INIT(rxbuffer, uart1_rxbuffer),
167                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
168                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
169                 },
170                 C99INIT(ser, NULL),
171                 C99INIT(fd, -1),
172         },
173 };
174
175 struct SerialHardware *ser_hw_getdesc(int unit)
176 {
177         ASSERT(unit < SER_CNT);
178         return &UARTDescs[unit].hw;
179 }