4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See devlib/README for information.
9 * \brief Buffered serial I/O driver
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.
18 * MODULE CONFIGURATION
20 * \li \c CONFIG_SER_HWHANDSHAKE - set to 1 to enable RTS/CTS handshake.
21 * Support is incomplete/untested.
22 * \li \c CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
26 * \author Bernardo Innocenti <bernie@develer.com>
31 *#* Revision 1.22 2004/12/08 08:56:14 bernie
32 *#* Rename time_t to mtime_t.
34 *#* Revision 1.21 2004/11/16 18:10:13 bernie
35 *#* Add sanity checks for missing configuration parameters.
37 *#* Revision 1.20 2004/10/19 11:48:00 bernie
38 *#* Remove unused variable.
40 *#* Revision 1.19 2004/10/19 08:14:13 bernie
41 *#* Fix a few longstanding bugs wrt status handling (made by rasky on scfirm).
43 *#* Revision 1.18 2004/09/20 03:31:15 bernie
46 *#* Revision 1.17 2004/09/14 21:06:07 bernie
47 *#* Use debug.h instead of kdebug.h; Spelling fixes.
49 *#* Revision 1.16 2004/09/06 21:40:50 bernie
50 *#* Move buffer handling in chip-specific driver.
52 *#* Revision 1.15 2004/08/25 14:12:08 rasky
53 *#* Aggiornato il comment block dei log RCS
55 *#* Revision 1.14 2004/08/24 16:22:57 bernie
56 *#* Thinkos; Doxygen fixes
58 *#* Revision 1.13 2004/08/24 16:20:48 bernie
59 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
61 *#* Revision 1.12 2004/08/24 13:49:39 bernie
64 *#* Revision 1.11 2004/08/15 05:32:22 bernie
65 *#* ser_resync(): New function.
67 *#* Revision 1.10 2004/08/10 06:29:50 bernie
68 *#* Rename timer_gettick() to timer_ticks().
70 *#* Revision 1.9 2004/08/08 06:06:20 bernie
71 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
73 *#* Revision 1.8 2004/07/29 22:57:09 bernie
74 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
76 *#* Revision 1.7 2004/07/18 21:49:03 bernie
77 *#* Make CONFIG_SER_DEFBAUDRATE optional.
79 *#* Revision 1.6 2004/06/07 15:56:28 aleph
80 *#* Remove cast-as-lvalue extension abuse
82 *#* Revision 1.5 2004/06/06 16:41:44 bernie
83 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
85 *#* Revision 1.4 2004/06/03 11:27:09 bernie
86 *#* Add dual-license information.
88 *#* Revision 1.3 2004/06/02 21:35:24 aleph
89 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
91 *#* Revision 1.2 2004/05/23 18:21:53 bernie
92 *#* Trim CVS logs and cleanup header info.
98 #include <mware/formatwr.h>
104 * Sanity check for config parameters required by this module.
106 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
107 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
109 #if !defined(CONFIG_SER_RXTIMEOUT)
110 #error CONFIG_SER_TXTIMEOUT missing in config.h
112 #if !defined(CONFIG_SER_RXTIMEOUT)
113 #error CONFIG_SER_RXTIMEOUT missing in config.h
115 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
116 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
118 #if !defined(CONFIG_SER_DEFBAUDRATE)
119 #error CONFIG_SER_DEFBAUDRATE missing in config.h
121 #if !defined(CONFIG_PRINTF)
122 #error CONFIG_PRINTF missing in config.h
126 #include <kern/proc.h>
129 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
130 #include <drv/timer.h>
134 /* Serial configuration parameters */
135 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
136 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
137 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
140 struct Serial ser_handles[SER_CNT];
144 * Inserisce il carattere c nel buffer di trasmissione.
145 * Questa funzione mette il processo chiamante in attesa
146 * quando il buffer e' pieno.
148 * \return EOF in caso di errore o timeout, altrimenti
149 * il carattere inviato.
151 int ser_putchar(int c, struct Serial *port)
153 //ASSERT_VALID_FIFO(&port->txfifo);
154 if (fifo_isfull_locked(&port->txfifo))
156 #if CONFIG_SER_TXTIMEOUT != -1
157 mtime_t start_time = timer_ticks();
160 /* Attende finche' il buffer e' pieno... */
163 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
164 /* Give up timeslice to other processes. */
167 #if CONFIG_SER_TXTIMEOUT != -1
168 if (timer_ticks() - start_time >= port->txtimeout)
170 port->status |= SERRF_TXTIMEOUT;
173 #endif /* CONFIG_SER_TXTIMEOUT */
175 while (fifo_isfull_locked(&port->txfifo));
178 fifo_push_locked(&port->txfifo, (unsigned char)c);
180 /* (re)trigger tx interrupt */
181 port->hw->table->enabletxirq(port->hw);
183 /* Avoid returning signed extended char */
184 return (int)((unsigned char)c);
189 * Preleva un carattere dal buffer di ricezione.
190 * Questa funzione mette il processo chiamante in attesa
191 * quando il buffer e' vuoto. L'attesa ha un timeout
192 * di ser_rxtimeout millisecondi.
194 * \return EOF in caso di errore o timeout, altrimenti
195 * il carattere ricevuto.
197 int ser_getchar(struct Serial *port)
199 if (fifo_isempty_locked(&port->rxfifo))
201 #if CONFIG_SER_RXTIMEOUT != -1
202 mtime_t start_time = timer_ticks();
204 /* Wait while buffer is empty */
207 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
208 /* Give up timeslice to other processes. */
211 #if CONFIG_SER_RXTIMEOUT != -1
212 if (timer_ticks() - start_time >= port->rxtimeout)
214 port->status |= SERRF_RXTIMEOUT;
217 #endif /* CONFIG_SER_RXTIMEOUT */
219 while (fifo_isempty_locked(&port->rxfifo) && (port->status & SERRF_RX) == 0);
223 * Get a byte from the FIFO (avoiding sign-extension),
224 * re-enable RTS, then return result.
226 if (port->status & SERRF_RX)
228 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
233 * Preleva un carattere dal buffer di ricezione.
234 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
235 * immediatamente EOF.
237 int ser_getchar_nowait(struct Serial *port)
239 if (fifo_isempty_locked(&port->rxfifo))
242 /* NOTE: the double cast prevents unwanted sign extension */
243 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
249 * Read a line long at most as size and put it
251 * \return number of chars read or EOF in case
254 int ser_gets(struct Serial *port, char *buf, int size)
256 return ser_gets_echo(port, buf, size, false);
261 * Read a line long at most as size and put it
262 * in buf, with optional echo.
264 * \return number of chars read, or EOF in case
267 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
274 if ((c = ser_getchar(port)) == EOF)
281 if (c == '\r' || c == '\n' || i >= size-1)
285 ser_print(port, "\r\n");
290 ser_putchar(c, port);
295 #endif /* !CONFIG_SER_GETS */
299 * Read at most \a size bytes from \a port and put them in \a buf
301 * \return number of bytes actually read, or EOF in
304 int ser_read(struct Serial *port, void *buf, size_t size)
307 char *_buf = (char *)buf;
312 if ((c = ser_getchar(port)) == EOF)
322 * Write a string to serial.
323 * \return 0 if OK, EOF in case of error.
325 int ser_print(struct Serial *port, const char *s)
329 if (ser_putchar(*s++, port) == EOF)
337 * \brief Write a buffer to serial.
339 * \return 0 if OK, EOF in case of error.
341 * \todo Optimize with fifo_pushblock()
343 int ser_write(struct Serial *port, const void *_buf, size_t len)
345 const char *buf = (const char *)_buf;
349 if (ser_putchar(*buf++, port) == EOF)
360 int ser_printf(struct Serial *port, const char *format, ...)
365 va_start(ap, format);
366 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
371 #endif /* CONFIG_PRINTF */
374 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
375 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
377 port->rxtimeout = rxtimeout;
378 port->txtimeout = txtimeout;
380 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
382 #if CONFIG_SER_RXTIMEOUT != -1
384 * Discard input to resynchronize with remote end.
386 * Discard incoming data until the port stops receiving
387 * characters for at least \a delay milliseconds.
389 * \note Serial errors are reset before and after executing the purge.
391 void ser_resync(struct Serial *port, mtime_t delay)
393 mtime_t old_rxtimeout = port->rxtimeout;
395 ser_settimeouts(port, delay, port->txtimeout);
398 ser_setstatus(port, 0);
401 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
403 /* Restore port to an usable status */
404 ser_setstatus(port, 0);
405 ser_settimeouts(port, old_rxtimeout, port->txtimeout);
407 #endif /* CONFIG_SER_RXTIMEOUT */
410 void ser_setbaudrate(struct Serial *port, unsigned long rate)
412 port->hw->table->setbaudrate(port->hw, rate);
416 void ser_setparity(struct Serial *port, int parity)
418 port->hw->table->setparity(port->hw, parity);
423 * Flush both the RX and TX buffers.
425 void ser_purge(struct Serial *port)
427 fifo_flush_locked(&port->rxfifo);
428 fifo_flush_locked(&port->txfifo);
433 * Wait until all pending output is completely
434 * transmitted to the other end.
436 * \note The current implementation only checks the
437 * software transmission queue. Any hardware
440 void ser_drain(struct Serial *ser)
442 while (!fifo_isempty(&ser->txfifo))
444 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
445 /* Give up timeslice to other processes. */
455 struct Serial *ser_open(unsigned int unit)
459 ASSERT(unit < countof(ser_handles));
460 port = &ser_handles[unit];
462 ASSERT(!port->is_open);
463 DB(port->is_open = true;)
467 port->hw = ser_hw_getdesc(unit);
469 /* Initialize circular buffers */
470 ASSERT(port->hw->txbuffer);
471 ASSERT(port->hw->rxbuffer);
472 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
473 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
475 port->hw->table->init(port->hw, port);
477 /* Set default values */
478 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
479 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
481 #if CONFIG_SER_DEFBAUDRATE
482 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
485 /* Clear error flags */
486 ser_setstatus(port, 0);
493 * Clean up serial port, disabling the associated hardware.
495 void ser_close(struct Serial *port)
497 ASSERT(port->is_open);
498 DB(port->is_open = false;)
500 port->hw->table->cleanup(port->hw);