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.37 2007/06/07 14:35:12 batt
32 *#* Merge from project_ks.
34 *#* Revision 1.36 2007/01/29 11:30:29 batt
35 *#* Reimplement ser_clearstatus as a macro.
37 *#* Revision 1.35 2007/01/27 20:47:12 batt
40 *#* Revision 1.34 2006/11/20 15:07:40 batt
41 *#* Revert unneeded locked functions.
43 *#* Revision 1.33 2006/11/17 18:15:55 batt
44 *#* Avoid race conditions.
46 *#* Revision 1.32 2006/11/17 17:03:58 batt
47 *#* Implement ser_setstatus and ser_getstatus as functions to avoid race conditions.
49 *#* Revision 1.31 2006/07/21 10:58:00 batt
50 *#* Use timer_clock() instead of obsolete timer_ticks().
52 *#* Revision 1.30 2006/07/19 12:56:26 bernie
53 *#* Convert to new Doxygen style.
55 *#* Revision 1.29 2006/05/18 00:39:30 bernie
56 *#* ser_open(): Document a bit more.
58 *#* Revision 1.28 2006/02/17 22:23:06 bernie
59 *#* Update POSIX serial emulator.
61 *#* Revision 1.27 2005/11/27 23:33:40 bernie
62 *#* Use appconfig.h instead of cfg/config.h.
64 *#* Revision 1.26 2005/11/04 16:20:02 bernie
65 *#* Fix reference to README.devlib in header.
67 *#* Revision 1.25 2005/04/11 19:10:27 bernie
68 *#* Include top-level headers from cfg/ subdir.
70 *#* Revision 1.24 2005/01/21 20:13:15 aleph
71 *#* Fix drain at ser_close()
73 *#* Revision 1.23 2005/01/14 00:47:07 aleph
74 *#* ser_drain(): Wait for hw transmission complete.
76 *#* Revision 1.22 2004/12/08 08:56:14 bernie
77 *#* Rename time_t to mtime_t.
79 *#* Revision 1.21 2004/11/16 18:10:13 bernie
80 *#* Add sanity checks for missing configuration parameters.
82 *#* Revision 1.20 2004/10/19 11:48:00 bernie
83 *#* Remove unused variable.
85 *#* Revision 1.19 2004/10/19 08:14:13 bernie
86 *#* Fix a few longstanding bugs wrt status handling (made by rasky on scfirm).
88 *#* Revision 1.18 2004/09/20 03:31:15 bernie
91 *#* Revision 1.17 2004/09/14 21:06:07 bernie
92 *#* Use debug.h instead of kdebug.h; Spelling fixes.
94 *#* Revision 1.16 2004/09/06 21:40:50 bernie
95 *#* Move buffer handling in chip-specific driver.
97 *#* Revision 1.15 2004/08/25 14:12:08 rasky
98 *#* Aggiornato il comment block dei log RCS
100 *#* Revision 1.14 2004/08/24 16:22:57 bernie
101 *#* Thinkos; Doxygen fixes
103 *#* Revision 1.13 2004/08/24 16:20:48 bernie
104 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
106 *#* Revision 1.12 2004/08/24 13:49:39 bernie
109 *#* Revision 1.11 2004/08/15 05:32:22 bernie
110 *#* ser_resync(): New function.
112 *#* Revision 1.10 2004/08/10 06:29:50 bernie
113 *#* Rename timer_gettick() to timer_ticks().
115 *#* Revision 1.9 2004/08/08 06:06:20 bernie
116 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
118 *#* Revision 1.8 2004/07/29 22:57:09 bernie
119 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
121 *#* Revision 1.7 2004/07/18 21:49:03 bernie
122 *#* Make CONFIG_SER_DEFBAUDRATE optional.
124 *#* Revision 1.6 2004/06/07 15:56:28 aleph
125 *#* Remove cast-as-lvalue extension abuse
127 *#* Revision 1.5 2004/06/06 16:41:44 bernie
128 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
130 *#* Revision 1.4 2004/06/03 11:27:09 bernie
131 *#* Add dual-license information.
133 *#* Revision 1.3 2004/06/02 21:35:24 aleph
134 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
136 *#* Revision 1.2 2004/05/23 18:21:53 bernie
137 *#* Trim CVS logs and cleanup header info.
148 #include <mware/formatwr.h>
149 #include <cfg/debug.h>
150 #include <appconfig.h>
153 * Sanity check for config parameters required by this module.
155 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
156 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
158 #if !defined(CONFIG_SER_RXTIMEOUT)
159 #error CONFIG_SER_TXTIMEOUT missing in config.h
161 #if !defined(CONFIG_SER_RXTIMEOUT)
162 #error CONFIG_SER_RXTIMEOUT missing in config.h
164 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
165 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
167 #if !defined(CONFIG_SER_DEFBAUDRATE)
168 #error CONFIG_SER_DEFBAUDRATE missing in config.h
170 #if !defined(CONFIG_PRINTF)
171 #error CONFIG_PRINTF missing in config.h
175 #include <kern/proc.h>
178 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
179 #include <drv/timer.h>
183 /* Serial configuration parameters */
184 #define SER_CTSDELAY 70 /**< CTS line retry interval (ms) */
185 #define SER_TXPOLLDELAY 2 /**< Transmit buffer full retry interval (ms) */
186 #define SER_RXPOLLDELAY 2 /**< Receive buffer empty retry interval (ms) */
189 struct Serial ser_handles[SER_CNT];
193 * Inserisce il carattere c nel buffer di trasmissione.
194 * Questa funzione mette il processo chiamante in attesa
195 * quando il buffer e' pieno.
197 * \return EOF in caso di errore o timeout, altrimenti
198 * il carattere inviato.
200 int ser_putchar(int c, struct Serial *port)
202 //ASSERT_VALID_FIFO(&port->txfifo);
203 if (fifo_isfull_locked(&port->txfifo))
205 #if CONFIG_SER_TXTIMEOUT != -1
206 ticks_t start_time = timer_clock();
209 /* Attende finche' il buffer e' pieno... */
215 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
216 /* Give up timeslice to other processes. */
219 #if CONFIG_SER_TXTIMEOUT != -1
220 if (timer_clock() - start_time >= port->txtimeout)
222 ATOMIC(port->status |= SERRF_TXTIMEOUT);
225 #endif /* CONFIG_SER_TXTIMEOUT */
227 while (fifo_isfull_locked(&port->txfifo));
230 fifo_push_locked(&port->txfifo, (unsigned char)c);
232 /* (re)trigger tx interrupt */
233 port->hw->table->txStart(port->hw);
235 /* Avoid returning signed extended char */
236 return (int)((unsigned char)c);
241 * Preleva un carattere dal buffer di ricezione.
242 * Questa funzione mette il processo chiamante in attesa
243 * quando il buffer e' vuoto. L'attesa ha un timeout
244 * di ser_rxtimeout millisecondi.
246 * \return EOF in caso di errore o timeout, altrimenti
247 * il carattere ricevuto.
249 int ser_getchar(struct Serial *port)
251 if (fifo_isempty_locked(&port->rxfifo))
253 #if CONFIG_SER_RXTIMEOUT != -1
254 ticks_t start_time = timer_clock();
256 /* Wait while buffer is empty */
262 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
263 /* Give up timeslice to other processes. */
266 #if CONFIG_SER_RXTIMEOUT != -1
267 if (timer_clock() - start_time >= port->rxtimeout)
269 ATOMIC(port->status |= SERRF_RXTIMEOUT);
272 #endif /* CONFIG_SER_RXTIMEOUT */
274 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
278 * Get a byte from the FIFO (avoiding sign-extension),
279 * re-enable RTS, then return result.
281 if (ser_getstatus(port) & SERRF_RX)
283 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
288 * Preleva un carattere dal buffer di ricezione.
289 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
290 * immediatamente EOF.
292 int ser_getchar_nowait(struct Serial *port)
294 if (fifo_isempty_locked(&port->rxfifo))
297 /* NOTE: the double cast prevents unwanted sign extension */
298 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
304 * Read a line long at most as size and put it
306 * \return number of chars read or EOF in case
309 int ser_gets(struct Serial *port, char *buf, int size)
311 return ser_gets_echo(port, buf, size, false);
316 * Read a line long at most as size and put it
317 * in buf, with optional echo.
319 * \return number of chars read, or EOF in case
322 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
329 if ((c = ser_getchar(port)) == EOF)
336 if (c == '\r' || c == '\n' || i >= size-1)
340 ser_print(port, "\r\n");
345 ser_putchar(c, port);
350 #endif /* !CONFIG_SER_GETS */
354 * Read at most \a size bytes from \a port and put them in \a buf
356 * \return number of bytes actually read, or EOF in
359 int ser_read(struct Serial *port, void *buf, size_t size)
362 char *_buf = (char *)buf;
367 if ((c = ser_getchar(port)) == EOF)
377 * Write a string to serial.
378 * \return 0 if OK, EOF in case of error.
380 int ser_print(struct Serial *port, const char *s)
384 if (ser_putchar(*s++, port) == EOF)
392 * \brief Write a buffer to serial.
394 * \return 0 if OK, EOF in case of error.
396 * \todo Optimize with fifo_pushblock()
398 int ser_write(struct Serial *port, const void *_buf, size_t len)
400 const char *buf = (const char *)_buf;
404 if (ser_putchar(*buf++, port) == EOF)
415 int ser_printf(struct Serial *port, const char *format, ...)
420 va_start(ap, format);
421 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
426 #endif /* CONFIG_PRINTF */
429 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
430 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
432 port->rxtimeout = ms_to_ticks(rxtimeout);
433 port->txtimeout = ms_to_ticks(txtimeout);
435 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
437 #if CONFIG_SER_RXTIMEOUT != -1
439 * Discard input to resynchronize with remote end.
441 * Discard incoming data until the port stops receiving
442 * characters for at least \a delay milliseconds.
444 * \note Serial errors are reset before and after executing the purge.
446 void ser_resync(struct Serial *port, mtime_t delay)
448 mtime_t old_rxtimeout = ticks_to_ms(port->rxtimeout);
450 ser_settimeouts(port, delay, ticks_to_ms(port->txtimeout));
453 ser_setstatus(port, 0);
456 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
458 /* Restore port to an usable status */
459 ser_setstatus(port, 0);
460 ser_settimeouts(port, old_rxtimeout, ticks_to_ms(port->txtimeout));
462 #endif /* CONFIG_SER_RXTIMEOUT */
465 void ser_setbaudrate(struct Serial *port, unsigned long rate)
467 port->hw->table->setBaudrate(port->hw, rate);
471 void ser_setparity(struct Serial *port, int parity)
473 port->hw->table->setParity(port->hw, parity);
478 * Flush both the RX and TX buffers.
480 void ser_purge(struct Serial *port)
482 fifo_flush_locked(&port->rxfifo);
483 fifo_flush_locked(&port->txfifo);
488 * Wait until all pending output is completely
489 * transmitted to the other end.
491 * \note The current implementation only checks the
492 * software transmission queue. Any hardware
495 void ser_drain(struct Serial *ser)
498 * Wait until the FIFO becomes empty, and then until the byte currently in
499 * the hardware register gets shifted out.
501 while (!fifo_isempty(&ser->txfifo)
502 || ser->hw->table->txSending(ser->hw))
504 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
505 /* Give up timeslice to other processes. */
516 * Initialize a serial port.
518 * \param unit Serial unit to open. Possible values are architecture dependant.
520 struct Serial *ser_open(unsigned int unit)
524 ASSERT(unit < countof(ser_handles));
525 port = &ser_handles[unit];
527 ASSERT(!port->is_open);
528 DB(port->is_open = true;)
532 port->hw = ser_hw_getdesc(unit);
534 /* Initialize circular buffers */
535 ASSERT(port->hw->txbuffer);
536 ASSERT(port->hw->rxbuffer);
537 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
538 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
540 port->hw->table->init(port->hw, port);
542 /* Set default values */
543 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
544 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
546 #if CONFIG_SER_DEFBAUDRATE
547 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
550 /* Clear error flags */
551 ser_setstatus(port, 0);
558 * Clean up serial port, disabling the associated hardware.
560 void ser_close(struct Serial *port)
562 ASSERT(port->is_open);
563 DB(port->is_open = false;)
565 // Wait until we finish sending everything
568 port->hw->table->cleanup(port->hw);
572 * We purge the FIFO buffer only after the low-level cleanup, so that
573 * we are sure that there are no more interrupts.