4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
33 * \brief Serial port emulator for hosted environments.
35 * \author Bernie Innocenti <bernie@codewiz.org>
37 * Updated by Robin Gilham to include reading from serial port and setting port speed <Robin@inventech.co.za>
40 #include "cfg/cfg_ser.h"
42 #include <cfg/debug.h>
43 #include <cfg/compiler.h>
46 #include <drv/ser_p.h>
47 #include <drv/timer.h>
48 #include <cpu/power.h>
50 #include <struct/fifobuf.h>
52 #include <sys/types.h>
55 #include <fcntl.h> /* open() */
56 #include <unistd.h> /* read(), write() */
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};
64 /* From the high-level serial driver */
65 extern struct Serial ser_handles[SER_CNT];
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];
76 //Change these to map to the Serial port I use USB connected serial ports
77 static char* devFile[SER_CNT] = {
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);
89 cpu_stack_t *serail_rx_stack[] = {serial_rx_stack0,serial_rx_stack1,serial_rx_stack2};
93 * Internal state structure
97 struct SerialHardware hw;
100 Process *ser_rcv_proc;
103 static void poll_serial_rcv(void);
105 static struct termios oldtio,newtio;
111 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
113 struct EmulSerial *hw = (struct EmulSerial *)_hw;
114 TRACEMSG("uart_init %d\n",ser->unit);
116 hw->fd = open(devFile[ser->unit], O_RDWR | O_NOCTTY | O_NDELAY);
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]);
126 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
128 struct EmulSerial *hw = (struct EmulSerial *)_hw;
129 tcsetattr(hw->fd,TCSANOW,&oldtio);
134 static void uart_txStart(struct SerialHardware * _hw)
136 struct EmulSerial *hw = (struct EmulSerial *)_hw;
138 while(!fifo_isempty(&hw->ser->txfifo))
140 char c = fifo_pop(&hw->ser->txfifo);
141 write(hw->fd, &c, 1);
145 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
151 static void uart_setBaudrate(struct SerialHardware * _hw, unsigned long rate)
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)
159 if (i<sizeof(BaudRate)/sizeof(unsigned long))
161 bzero(&newtio, sizeof(newtio));
162 newtio.c_cflag = BaudSetting[i] | CS8 | CLOCAL | CREAD;
163 newtio.c_iflag = IGNPAR ;
164 newtio.c_oflag &= ~OPOST;
167 /* set input mode (non-canonical, no echo,...) */
168 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
170 tcsetattr(hw->fd,TCSANOW,&newtio);
174 TRACEMSG("invalid rate %d", rate);
175 ASSERT(i<sizeof(BaudRate)/sizeof(unsigned long));
181 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
183 TRACEMSG("parity=%d", parity);
187 // FIXME: move into compiler.h? Ditch?
189 #define C99INIT(name,val) .name = val
190 #elif defined(__GNUC__)
191 #define C99INIT(name,val) name: val
193 #warning No designated initializers, double check your code
194 #define C99INIT(name,val) (val)
198 * High-level interface data structures.
200 static const struct SerialHardwareVT uart_vtable =
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),
210 static struct EmulSerial UARTDescs[SER_CNT] =
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)),
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)),
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)),
247 struct SerialHardware *ser_hw_getdesc(int unit)
249 ASSERT(unit < SER_CNT);
250 return &UARTDescs[unit].hw;
252 static void poll_serial_rcv(void)
256 struct EmulSerial *hw = (struct EmulSerial *)proc_currentUserData();
257 //TRACEMSG("poll_serial_rcv dev %d\n",hw->ser->unit);
260 while(!fifo_isfull(&hw->ser->rxfifo))
263 res = read(hw->fd,&p,1);
264 //TRACEMSG("rcv %c res %d\n",(unsigned char)p,res);
267 //TRACEMSG("rcv %02x ",p);
268 //printf("rcv %02x ", (unsigned char)p);
269 fifo_push_locked(&hw->ser->rxfifo, (unsigned char)p);
272 //Delay for 2 ticks if no characters are read
275 //exit if there is and error i.e. the port closes