Import drv/ modules.
[bertos.git] / drv / ser.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
5  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
6  * All Rights Reserved.
7  * -->
8  *
9  * \brief Buffered serial I/O driver
10  *
11  * The serial rx interrupt buffers incoming data in a software FIFO
12  * to decouple the higher level protocols from the line speed.
13  * Outgoing data is buffered as well for better performance.
14  * This driver is not optimized for best performance, but it
15  * has proved to be fast enough to handle transfer rates up to
16  * 38400bps on a 16MHz 80196.
17  *
18  * MODULE CONFIGURATION
19  *      \li \c CONFIG_SER_HWHANDSHAKE define this preprocessor symbol to enable
20  *      support for RTS/CTS handshake. Support is incomplete/untested
21  *      for 80196.
22  *  \li \c CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
23  *
24  *
25  * \version $Id$
26  * \author Bernardo Innocenti <bernie@develer.com>
27  */
28
29 /*
30  * $Log$
31  * Revision 1.1  2004/05/23 18:10:11  bernie
32  * Import drv/ modules.
33  *
34  * Revision 1.5  2004/05/14 12:47:26  rasky
35  * Importato nuovo supporto seriale per AVR da Stefano
36  *
37  * Revision 1.4  2004/04/29 16:40:23  rasky
38  * Durante i busy loop della seriale, chiama la proc_switch() per cambiare processo
39  *
40  * Revision 1.3  2004/04/21 17:38:24  rasky
41  * New application
42  *
43  * Revision 1.16  2004/04/03 18:30:49  aleph
44  * Move timeout defines in config, private define in .c
45  *
46  * Revision 1.15  2004/03/29 17:01:02  aleph
47  * Add function to set serial parity, fix it when ser_open is used
48  *
49  * Revision 1.14  2004/03/29 16:19:33  aleph
50  * Add ser_cleanup function; Various code improvements
51  *
52  * Revision 1.13  2004/03/24 15:48:53  bernie
53  * Remove Copyright messages from Doxygen output
54  */
55
56 #include <mware/formatwr.h>
57 #include <drv/kdebug.h>
58 #include "ser.h"
59 #include "ser_p.h"
60 #include "hw.h"
61
62 #ifdef CONFIG_KERNEL
63         #include <kern/proc.h>
64 #endif
65 #if defined(CONFIG_SER_TXTIMEOUT) || defined(CONFIG_SER_RXTIMEOUT)
66         #include <drv/timer.h>
67 #endif
68
69
70 /* Serial configuration parameters */
71 #define SER_CTSDELAY        70  /*!< CTS line retry interval (ms) */
72 #define SER_TXPOLLDELAY      2  /*!< Transmit buffer full retry interval (ms) */
73 #define SER_RXPOLLDELAY      2  /*!< Receive buffer empty retry interval (ms) */
74
75
76 struct Serial ser_handles[SER_CNT];
77
78
79 /*!
80  * Inserisce il carattere c nel buffer di trasmissione.
81  * Questa funzione mette il processo chiamante in attesa
82  * quando il buffer e' pieno.
83  *
84  * \return EOF in caso di errore o timeout, altrimenti
85  *         il carattere inviato.
86  */
87 int ser_putchar(int c, struct Serial *port)
88 {
89         if (fifo_isfull_locked(&port->txfifo))
90         {
91 #ifdef CONFIG_SER_TXTIMEOUT
92                 time_t start_time = timer_gettick();
93 #endif
94
95                 /* Attende finche' il buffer e' pieno... */
96                 do
97                 {
98 #ifdef CONFIG_KERN_SCHED
99                         /* Give up timeslice to other processes. */
100                         proc_switch();
101 #endif
102 #ifdef CONFIG_SER_TXTIMEOUT
103                         if (timer_gettick() - start_time >= port->txtimeout)
104                         {
105                                 port->status |= SERRF_TXTIMEOUT;
106                                 return EOF;
107                         }
108 #endif /* CONFIG_SER_TXTIMEOUT */
109                 }
110                 while (fifo_isfull_locked(&port->txfifo));
111         }
112
113         fifo_push(&port->txfifo, (unsigned char)c);
114
115         /* (re)trigger tx interrupt */
116         port->hw->table->enabletxirq(port->hw);
117
118         /* Avoid returning signed estended char */
119         return (int)((unsigned char)c);
120 }
121
122
123 /*!
124  * Preleva un carattere dal buffer di ricezione.
125  * Questa funzione mette il processo chiamante in attesa
126  * quando il buffer e' vuoto. L'attesa ha un timeout
127  * di ser_rxtimeout millisecondi.
128  *
129  * \return EOF in caso di errore o timeout, altrimenti
130  *         il carattere ricevuto.
131  */
132 int ser_getchar(struct Serial *port)
133 {
134         int result;
135
136         if (fifo_isempty_locked(&port->rxfifo))
137         {
138 #ifdef CONFIG_SER_RXTIMEOUT
139                 time_t start_time = timer_gettick();
140 #endif
141                 /* Wait while buffer is empty */
142                 do
143                 {
144 #ifdef CONFIG_KERN_SCHED
145                         /* Give up timeslice to other processes. */
146                         proc_switch();
147 #endif
148 #ifdef CONFIG_SER_RXTIMEOUT
149                         if (timer_gettick() - start_time >= port->rxtimeout)
150                         {
151                                 port->status |= SERRF_RXTIMEOUT;
152                                 return EOF;
153                         }
154 #endif /* CONFIG_SER_RXTIMEOUT */
155                 }
156                 while (fifo_isempty_locked(&port->rxfifo));
157         }
158
159         /*
160          * Get a byte from the FIFO (avoiding sign-extension),
161          * re-enable RTS, then return result.
162          */
163         result = (int)(unsigned char)fifo_pop(&port->rxfifo);
164         return port->status ? EOF : result;
165 }
166
167
168 /*!
169  * Preleva un carattere dal buffer di ricezione.
170  * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
171  * immediatamente EOF.
172  */
173 int ser_getchar_nowait(struct Serial *port)
174 {
175         if (fifo_isempty_locked(&port->rxfifo))
176                 return EOF;
177
178         /* NOTE: the double cast prevents unwanted sign extension */
179         return (int)(unsigned char)fifo_pop(&port->rxfifo);
180 }
181
182
183 /*!
184  * Read a line long at most as size and puts it
185  * in buf.
186  * \return number of chars read or EOF in case
187  *         of error.
188  */
189 int ser_gets(struct Serial *port, char *buf, int size)
190 {
191         return ser_gets_echo(port, buf, size, false);
192 }
193
194
195 /*!
196  * Read a line long at most as size and puts it
197  * in buf, with optional echo.
198  * \return number of chars read or EOF in case
199  *         of error.
200  */
201 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
202 {
203         int i = 0;
204         int c;
205
206         for (;;)
207         {
208                 if ((c = ser_getchar(port)) == EOF)
209                         return -1;
210                 /* FIXME */
211                 if (c == '\r' || c == '\n' || i >= size-1)
212                 {
213                         buf[i] = '\0';
214                         if (echo)
215                                 ser_print(port, "\r\n");
216                         break;
217                 }
218                 buf[i++] = c;
219                 if (echo)
220                         ser_putchar(c, port);
221         }
222
223         return i;
224 }
225
226
227 /*!
228  * Read at most size bytes and puts them
229  * in buf.
230  * \return number of bytes read or EOF in case
231  *         of error.
232  */
233 int ser_read(struct Serial *port, char *buf, size_t size)
234 {
235         size_t i = 0;
236         int c;
237
238         while (i < size)
239         {
240                 if ((c = ser_getchar(port)) == EOF)
241                         return EOF;
242                 buf[i++] = c;
243         }
244
245         return i;
246 }
247
248
249 /*!
250  * Write a string to serial.
251  * \return 0 if OK, EOF in case of error.
252  */
253 int ser_print(struct Serial *port, const char *s)
254 {
255         while (*s)
256         {
257                 if (ser_putchar(*s++, port) == EOF)
258                         return EOF;
259         }
260         return 0;
261 }
262
263
264 /*!
265  * \brief Write a buffer to serial.
266  *
267  * \return 0 if OK, EOF in case of error.
268  */
269 int ser_write(struct Serial *port, const void *buf, size_t len)
270 {
271         while (len--)
272         {
273                 if (ser_putchar(*((const char *)buf)++, port) == EOF)
274                         return EOF;
275         }
276         return 0;
277 }
278
279
280 /*!
281  * Formatted write
282  */
283 int ser_printf(struct Serial *port, const char *format, ...)
284 {
285         va_list ap;
286         int len;
287
288         ser_setstatus(port, 0);
289         va_start(ap, format);
290         len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
291         va_end(ap);
292
293         return len;
294 }
295
296 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
297 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
298 {
299         port->rxtimeout = rxtimeout;
300         port->txtimeout = txtimeout;
301 }
302 #endif /* defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT) */
303
304
305 void ser_setbaudrate(struct Serial *port, unsigned long rate)
306 {
307         port->hw->table->setbaudrate(port->hw, rate);
308 }
309
310
311 void ser_setparity(struct Serial *port, int parity)
312 {
313         port->hw->table->setparity(port->hw, parity);
314 }
315
316
317 /*!
318  * Flush both the RX and TX buffers.
319  */
320 void ser_purge(struct Serial *ser)
321 {
322         fifo_flush(&ser->rxfifo);
323         fifo_flush(&ser->txfifo);
324 }
325
326
327 /*!
328  * Initialize serial
329  */
330 struct Serial *ser_open(unsigned int unit)
331 {
332         struct Serial *port;
333
334         ASSERT(unit < countof(ser_handles));
335
336         port = &ser_handles[unit];
337
338         ASSERT(!port->is_open);
339
340         port->unit = unit;
341         port->is_open = true;
342
343         /* Initialize circular buffer */
344         fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
345         fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
346
347         port->hw = ser_hw_getdesc(unit);
348         port->hw->table->init(port->hw, port);
349
350         /* Set default values */
351 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
352         ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
353 #endif
354         ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
355
356         return port;
357 }
358
359
360 /*!
361  * Clean up serial port, disabling the associated hardware.
362  */
363 void ser_close(struct Serial *port)
364 {
365         ASSERT(port->is_open);
366
367         port->is_open = false;
368         port->hw->table->cleanup(port->hw);
369         port->hw = NULL;        
370 }