Clean up code. Add comments. Init module with callback to register all
[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  * \author Bernie Innocenti <bernie@codewiz.org>
36  *
37  * Updated by Robin Gilham to include reading from serial port and setting port speed <Robin@inventech.co.za>
38  */
39
40 #include "cfg/cfg_ser.h"
41
42 #include <cfg/debug.h>
43 #include <cfg/compiler.h>
44
45 #include <drv/ser.h>
46 #include <drv/ser_p.h>
47 #include <drv/timer.h>
48 #include <cpu/power.h>
49
50 #include <struct/fifobuf.h>
51
52 #include <sys/types.h>
53 #include <sys/stat.h>
54
55 #include <fcntl.h> /* open() */
56 #include <unistd.h> /* read(), write() */
57 #include <stdlib.h>
58 #include <termios.h>
59
60 static unsigned long BaudRate[] = {300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200};
61 static unsigned long BaudSetting[] = {B300,B600,B1200,B1800,B2400,B4800,B9600,B19200,B38400,B57600,B115200};
62
63
64 /* From the high-level serial driver */
65 extern struct Serial ser_handles[SER_CNT];
66
67 /* TX and RX buffers */
68 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
69 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
70 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
71 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
72 static unsigned char uart2_txbuffer[CONFIG_UART2_TXBUFSIZE];
73 static unsigned char uart2_rxbuffer[CONFIG_UART2_RXBUFSIZE];
74
75
76 //Change these to map to the Serial port I use USB connected serial ports
77 static char* devFile[SER_CNT] = {
78                 "/dev/ttyS0",
79                 "/dev/ttyUSB0",
80                 "/dev/ttyUSB1"
81 };
82
83 //Make this big enough not criticul as it is running in emulated enviroment
84 #define SERIAL_RX_STACK_SIZE (KERN_MINSTACKSIZE*3)
85 PROC_DEFINE_STACK(serial_rx_stack0, SERIAL_RX_STACK_SIZE);
86 PROC_DEFINE_STACK(serial_rx_stack1, SERIAL_RX_STACK_SIZE);
87 PROC_DEFINE_STACK(serial_rx_stack2, SERIAL_RX_STACK_SIZE);
88
89 cpu_stack_t *serail_rx_stack[] = {serial_rx_stack0,serial_rx_stack1,serial_rx_stack2};
90
91
92 /**
93  * Internal state structure
94  */
95 struct EmulSerial
96 {
97         struct SerialHardware hw;
98         struct Serial *ser;
99         int fd;
100         Process *ser_rcv_proc;
101 };
102
103 static void poll_serial_rcv(void);
104
105 static struct termios oldtio,newtio;
106
107
108 /*
109  * Callbacks
110  */
111 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
112 {
113         struct EmulSerial *hw = (struct EmulSerial *)_hw;
114         TRACEMSG("uart_init %d\n",ser->unit);
115         hw->ser = ser;
116         hw->fd = open(devFile[ser->unit], O_RDWR | O_NOCTTY | O_NDELAY);
117         ASSERT(hw->fd);
118     /* Make the file descriptor asynchronous (the manual page says only
119        O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
120         fcntl(hw->fd, F_SETFL, FNDELAY);
121         tcflush(hw->fd, TCIFLUSH);
122         tcgetattr(hw->fd,&oldtio); /* save current port settings */
123         hw->ser_rcv_proc = proc_new(poll_serial_rcv,hw,SERIAL_RX_STACK_SIZE,serail_rx_stack[ser->unit]);
124 }
125
126 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
127 {
128         struct EmulSerial *hw = (struct EmulSerial *)_hw;
129         tcsetattr(hw->fd,TCSANOW,&oldtio);
130         close(hw->fd);
131         hw->fd = -1;
132 }
133
134 static void uart_txStart(struct SerialHardware * _hw)
135 {
136         struct EmulSerial *hw = (struct EmulSerial *)_hw;
137
138         while(!fifo_isempty(&hw->ser->txfifo))
139         {
140                 char c = fifo_pop(&hw->ser->txfifo);
141                 write(hw->fd, &c, 1);
142         }
143 }
144
145 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
146 {
147         return false;
148 }
149
150
151 static void uart_setBaudrate(struct SerialHardware * _hw, unsigned long rate)
152 {
153         int i;
154         struct EmulSerial *hw = (struct EmulSerial *)_hw;
155         TRACEMSG("rate=%d", rate);
156         for (i=0;i<sizeof(BaudRate)/sizeof(unsigned long);i++)
157                 if (BaudRate[i]==rate)
158                         break;
159         if (i<sizeof(BaudRate)/sizeof(unsigned long))
160         {
161         bzero(&newtio, sizeof(newtio));
162         newtio.c_cflag = BaudSetting[i] | CS8 | CLOCAL | CREAD;
163         newtio.c_iflag = IGNPAR ;
164         newtio.c_oflag  &= ~OPOST;
165
166
167         /* set input mode (non-canonical, no echo,...) */
168         newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
169
170         tcsetattr(hw->fd,TCSANOW,&newtio);
171         }
172         else
173         {
174                 TRACEMSG("invalid rate %d", rate);
175                 ASSERT(i<sizeof(BaudRate)/sizeof(unsigned long));
176         }
177
178
179 }
180
181 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
182 {
183         TRACEMSG("parity=%d", parity);
184         // TODO
185 }
186
187 // FIXME: move into compiler.h?  Ditch?
188 #if COMPILER_C99
189         #define C99INIT(name,val) .name = val
190 #elif defined(__GNUC__)
191         #define C99INIT(name,val) name: val
192 #else
193         #warning No designated initializers, double check your code
194         #define C99INIT(name,val) (val)
195 #endif
196
197 /*
198  * High-level interface data structures.
199  */
200 static const struct SerialHardwareVT uart_vtable =
201 {
202         C99INIT(init, uart_init),
203         C99INIT(cleanup, uart_cleanup),
204         C99INIT(setBaudrate, uart_setBaudrate),
205         C99INIT(setParity, uart_setParity),
206         C99INIT(txStart, uart_txStart),
207         C99INIT(txSending, uart_txSending),
208 };
209
210 static struct EmulSerial UARTDescs[SER_CNT] =
211 {
212         {
213                 C99INIT(hw, /**/) {
214                         C99INIT(table, &uart_vtable),
215                         C99INIT(txbuffer, uart0_txbuffer),
216                         C99INIT(rxbuffer, uart0_rxbuffer),
217                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
218                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
219                 },
220                 C99INIT(ser, NULL),
221                 C99INIT(fd, -1),
222         },
223         {
224                 C99INIT(hw, /**/) {
225                         C99INIT(table, &uart_vtable),
226                         C99INIT(txbuffer, uart1_txbuffer),
227                         C99INIT(rxbuffer, uart1_rxbuffer),
228                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
229                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
230                 },
231                 C99INIT(ser, NULL),
232                 C99INIT(fd, -1),
233         },
234         {
235                 C99INIT(hw, /**/) {
236                         C99INIT(table, &uart_vtable),
237                         C99INIT(txbuffer, uart2_txbuffer),
238                         C99INIT(rxbuffer, uart2_rxbuffer),
239                         C99INIT(txbuffer_size, sizeof(uart2_txbuffer)),
240                         C99INIT(rxbuffer_size, sizeof(uart2_rxbuffer)),
241                 },
242                 C99INIT(ser, NULL),
243                 C99INIT(fd, -1),
244         },
245 };
246
247 struct SerialHardware *ser_hw_getdesc(int unit)
248 {
249         ASSERT(unit < SER_CNT);
250         return &UARTDescs[unit].hw;
251 }
252 static void poll_serial_rcv(void)
253 {
254         int res;
255         int p;
256         struct EmulSerial *hw = (struct EmulSerial *)proc_currentUserData();
257         //TRACEMSG("poll_serial_rcv dev %d\n",hw->ser->unit);
258         for(;;)
259         {
260                 while(!fifo_isfull(&hw->ser->rxfifo))
261                 {
262
263                         res = read(hw->fd,&p,1);
264                         //TRACEMSG("rcv %c res %d\n",(unsigned char)p,res);
265                         if (res>0)
266                         {
267                                 //TRACEMSG("rcv %02x ",p);
268                                 //printf("rcv %02x ", (unsigned char)p);
269                                 fifo_push_locked(&hw->ser->rxfifo, (unsigned char)p);
270                         }
271                         else if (res==0)
272                                 //Delay for 2 ticks if no characters are read
273                                 timer_delay(2);
274                         else
275                                 //exit if there is and error i.e. the port closes
276                                 goto exit_rcv;
277                 }
278                 cpu_relax();
279         }
280 exit_rcv:
281         return;
282 }