4 * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See README.devlib 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.35 2007/01/27 20:47:12 batt
34 *#* Revision 1.34 2006/11/20 15:07:40 batt
35 *#* Revert unneeded locked functions.
37 *#* Revision 1.33 2006/11/17 18:15:55 batt
38 *#* Avoid race conditions.
40 *#* Revision 1.32 2006/11/17 17:03:58 batt
41 *#* Implement ser_setstatus and ser_getstatus as functions to avoid race conditions.
43 *#* Revision 1.31 2006/07/21 10:58:00 batt
44 *#* Use timer_clock() instead of obsolete timer_ticks().
46 *#* Revision 1.30 2006/07/19 12:56:26 bernie
47 *#* Convert to new Doxygen style.
49 *#* Revision 1.29 2006/05/18 00:39:30 bernie
50 *#* ser_open(): Document a bit more.
52 *#* Revision 1.28 2006/02/17 22:23:06 bernie
53 *#* Update POSIX serial emulator.
55 *#* Revision 1.27 2005/11/27 23:33:40 bernie
56 *#* Use appconfig.h instead of cfg/config.h.
58 *#* Revision 1.26 2005/11/04 16:20:02 bernie
59 *#* Fix reference to README.devlib in header.
61 *#* Revision 1.25 2005/04/11 19:10:27 bernie
62 *#* Include top-level headers from cfg/ subdir.
64 *#* Revision 1.24 2005/01/21 20:13:15 aleph
65 *#* Fix drain at ser_close()
67 *#* Revision 1.23 2005/01/14 00:47:07 aleph
68 *#* ser_drain(): Wait for hw transmission complete.
70 *#* Revision 1.22 2004/12/08 08:56:14 bernie
71 *#* Rename time_t to mtime_t.
73 *#* Revision 1.21 2004/11/16 18:10:13 bernie
74 *#* Add sanity checks for missing configuration parameters.
76 *#* Revision 1.20 2004/10/19 11:48:00 bernie
77 *#* Remove unused variable.
79 *#* Revision 1.19 2004/10/19 08:14:13 bernie
80 *#* Fix a few longstanding bugs wrt status handling (made by rasky on scfirm).
82 *#* Revision 1.18 2004/09/20 03:31:15 bernie
85 *#* Revision 1.17 2004/09/14 21:06:07 bernie
86 *#* Use debug.h instead of kdebug.h; Spelling fixes.
88 *#* Revision 1.16 2004/09/06 21:40:50 bernie
89 *#* Move buffer handling in chip-specific driver.
91 *#* Revision 1.15 2004/08/25 14:12:08 rasky
92 *#* Aggiornato il comment block dei log RCS
94 *#* Revision 1.14 2004/08/24 16:22:57 bernie
95 *#* Thinkos; Doxygen fixes
97 *#* Revision 1.13 2004/08/24 16:20:48 bernie
98 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
100 *#* Revision 1.12 2004/08/24 13:49:39 bernie
103 *#* Revision 1.11 2004/08/15 05:32:22 bernie
104 *#* ser_resync(): New function.
106 *#* Revision 1.10 2004/08/10 06:29:50 bernie
107 *#* Rename timer_gettick() to timer_ticks().
109 *#* Revision 1.9 2004/08/08 06:06:20 bernie
110 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
112 *#* Revision 1.8 2004/07/29 22:57:09 bernie
113 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
115 *#* Revision 1.7 2004/07/18 21:49:03 bernie
116 *#* Make CONFIG_SER_DEFBAUDRATE optional.
118 *#* Revision 1.6 2004/06/07 15:56:28 aleph
119 *#* Remove cast-as-lvalue extension abuse
121 *#* Revision 1.5 2004/06/06 16:41:44 bernie
122 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
124 *#* Revision 1.4 2004/06/03 11:27:09 bernie
125 *#* Add dual-license information.
127 *#* Revision 1.3 2004/06/02 21:35:24 aleph
128 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
130 *#* Revision 1.2 2004/05/23 18:21:53 bernie
131 *#* Trim CVS logs and cleanup header info.
137 #include <mware/formatwr.h>
138 #include <cfg/debug.h>
139 #include <appconfig.h>
142 * Sanity check for config parameters required by this module.
144 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
145 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
147 #if !defined(CONFIG_SER_RXTIMEOUT)
148 #error CONFIG_SER_TXTIMEOUT missing in config.h
150 #if !defined(CONFIG_SER_RXTIMEOUT)
151 #error CONFIG_SER_RXTIMEOUT missing in config.h
153 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
154 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
156 #if !defined(CONFIG_SER_DEFBAUDRATE)
157 #error CONFIG_SER_DEFBAUDRATE missing in config.h
159 #if !defined(CONFIG_PRINTF)
160 #error CONFIG_PRINTF missing in config.h
164 #include <kern/proc.h>
167 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
168 #include <drv/timer.h>
172 /* Serial configuration parameters */
173 #define SER_CTSDELAY 70 /**< CTS line retry interval (ms) */
174 #define SER_TXPOLLDELAY 2 /**< Transmit buffer full retry interval (ms) */
175 #define SER_RXPOLLDELAY 2 /**< Receive buffer empty retry interval (ms) */
178 struct Serial ser_handles[SER_CNT];
182 * Inserisce il carattere c nel buffer di trasmissione.
183 * Questa funzione mette il processo chiamante in attesa
184 * quando il buffer e' pieno.
186 * \return EOF in caso di errore o timeout, altrimenti
187 * il carattere inviato.
189 int ser_putchar(int c, struct Serial *port)
191 //ASSERT_VALID_FIFO(&port->txfifo);
192 if (fifo_isfull_locked(&port->txfifo))
194 #if CONFIG_SER_TXTIMEOUT != -1
195 ticks_t start_time = timer_clock();
198 /* Attende finche' il buffer e' pieno... */
201 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
202 /* Give up timeslice to other processes. */
205 #if CONFIG_SER_TXTIMEOUT != -1
206 if (timer_clock() - start_time >= port->txtimeout)
208 ATOMIC(port->status |= SERRF_TXTIMEOUT);
211 #endif /* CONFIG_SER_TXTIMEOUT */
213 while (fifo_isfull_locked(&port->txfifo));
216 fifo_push_locked(&port->txfifo, (unsigned char)c);
218 /* (re)trigger tx interrupt */
219 port->hw->table->txStart(port->hw);
221 /* Avoid returning signed extended char */
222 return (int)((unsigned char)c);
227 * Preleva un carattere dal buffer di ricezione.
228 * Questa funzione mette il processo chiamante in attesa
229 * quando il buffer e' vuoto. L'attesa ha un timeout
230 * di ser_rxtimeout millisecondi.
232 * \return EOF in caso di errore o timeout, altrimenti
233 * il carattere ricevuto.
235 int ser_getchar(struct Serial *port)
237 if (fifo_isempty_locked(&port->rxfifo))
239 #if CONFIG_SER_RXTIMEOUT != -1
240 ticks_t start_time = timer_clock();
242 /* Wait while buffer is empty */
245 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
246 /* Give up timeslice to other processes. */
249 #if CONFIG_SER_RXTIMEOUT != -1
250 if (timer_clock() - start_time >= port->rxtimeout)
252 ATOMIC(port->status |= SERRF_RXTIMEOUT);
255 #endif /* CONFIG_SER_RXTIMEOUT */
257 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
261 * Get a byte from the FIFO (avoiding sign-extension),
262 * re-enable RTS, then return result.
264 if (ser_getstatus(port) & SERRF_RX)
266 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
271 * Preleva un carattere dal buffer di ricezione.
272 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
273 * immediatamente EOF.
275 int ser_getchar_nowait(struct Serial *port)
277 if (fifo_isempty_locked(&port->rxfifo))
280 /* NOTE: the double cast prevents unwanted sign extension */
281 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
287 * Read a line long at most as size and put it
289 * \return number of chars read or EOF in case
292 int ser_gets(struct Serial *port, char *buf, int size)
294 return ser_gets_echo(port, buf, size, false);
299 * Read a line long at most as size and put it
300 * in buf, with optional echo.
302 * \return number of chars read, or EOF in case
305 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
312 if ((c = ser_getchar(port)) == EOF)
319 if (c == '\r' || c == '\n' || i >= size-1)
323 ser_print(port, "\r\n");
328 ser_putchar(c, port);
333 #endif /* !CONFIG_SER_GETS */
337 * Read at most \a size bytes from \a port and put them in \a buf
339 * \return number of bytes actually read, or EOF in
342 int ser_read(struct Serial *port, void *buf, size_t size)
345 char *_buf = (char *)buf;
350 if ((c = ser_getchar(port)) == EOF)
360 * Write a string to serial.
361 * \return 0 if OK, EOF in case of error.
363 int ser_print(struct Serial *port, const char *s)
367 if (ser_putchar(*s++, port) == EOF)
375 * \brief Write a buffer to serial.
377 * \return 0 if OK, EOF in case of error.
379 * \todo Optimize with fifo_pushblock()
381 int ser_write(struct Serial *port, const void *_buf, size_t len)
383 const char *buf = (const char *)_buf;
387 if (ser_putchar(*buf++, port) == EOF)
398 int ser_printf(struct Serial *port, const char *format, ...)
403 va_start(ap, format);
404 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
409 #endif /* CONFIG_PRINTF */
412 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
413 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
415 port->rxtimeout = ms_to_ticks(rxtimeout);
416 port->txtimeout = ms_to_ticks(txtimeout);
418 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
420 #if CONFIG_SER_RXTIMEOUT != -1
422 * Discard input to resynchronize with remote end.
424 * Discard incoming data until the port stops receiving
425 * characters for at least \a delay milliseconds.
427 * \note Serial errors are reset before and after executing the purge.
429 void ser_resync(struct Serial *port, mtime_t delay)
431 mtime_t old_rxtimeout = ticks_to_ms(port->rxtimeout);
433 ser_settimeouts(port, delay, ticks_to_ms(port->txtimeout));
436 ser_setstatus(port, 0);
439 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
441 /* Restore port to an usable status */
442 ser_setstatus(port, 0);
443 ser_settimeouts(port, old_rxtimeout, ticks_to_ms(port->txtimeout));
445 #endif /* CONFIG_SER_RXTIMEOUT */
448 void ser_setbaudrate(struct Serial *port, unsigned long rate)
450 port->hw->table->setBaudrate(port->hw, rate);
454 void ser_setparity(struct Serial *port, int parity)
456 port->hw->table->setParity(port->hw, parity);
459 void ser_clearstatus(struct Serial *port)
461 ser_setstatus(port, 0);
466 * Flush both the RX and TX buffers.
468 void ser_purge(struct Serial *port)
470 fifo_flush_locked(&port->rxfifo);
471 fifo_flush_locked(&port->txfifo);
476 * Wait until all pending output is completely
477 * transmitted to the other end.
479 * \note The current implementation only checks the
480 * software transmission queue. Any hardware
483 void ser_drain(struct Serial *ser)
486 * Wait until the FIFO becomes empty, and then until the byte currently in
487 * the hardware register gets shifted out.
489 while (!fifo_isempty(&ser->txfifo)
490 || ser->hw->table->txSending(ser->hw))
492 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
493 /* Give up timeslice to other processes. */
501 * Initialize a serial port.
503 * \param unit Serial unit to open. Possible values are architecture dependant.
505 struct Serial *ser_open(unsigned int unit)
509 ASSERT(unit < countof(ser_handles));
510 port = &ser_handles[unit];
512 ASSERT(!port->is_open);
513 DB(port->is_open = true;)
517 port->hw = ser_hw_getdesc(unit);
519 /* Initialize circular buffers */
520 ASSERT(port->hw->txbuffer);
521 ASSERT(port->hw->rxbuffer);
522 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
523 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
525 port->hw->table->init(port->hw, port);
527 /* Set default values */
528 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
529 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
531 #if CONFIG_SER_DEFBAUDRATE
532 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
535 /* Clear error flags */
536 ser_setstatus(port, 0);
543 * Clean up serial port, disabling the associated hardware.
545 void ser_close(struct Serial *port)
547 ASSERT(port->is_open);
548 DB(port->is_open = false;)
550 // Wait until we finish sending everything
553 port->hw->table->cleanup(port->hw);
557 * We purge the FIFO buffer only after the low-level cleanup, so that
558 * we are sure that there are no more interrupts.