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.36 2007/01/29 11:30:29 batt
32 *#* Reimplement ser_clearstatus as a macro.
34 *#* Revision 1.35 2007/01/27 20:47:12 batt
37 *#* Revision 1.34 2006/11/20 15:07:40 batt
38 *#* Revert unneeded locked functions.
40 *#* Revision 1.33 2006/11/17 18:15:55 batt
41 *#* Avoid race conditions.
43 *#* Revision 1.32 2006/11/17 17:03:58 batt
44 *#* Implement ser_setstatus and ser_getstatus as functions to avoid race conditions.
46 *#* Revision 1.31 2006/07/21 10:58:00 batt
47 *#* Use timer_clock() instead of obsolete timer_ticks().
49 *#* Revision 1.30 2006/07/19 12:56:26 bernie
50 *#* Convert to new Doxygen style.
52 *#* Revision 1.29 2006/05/18 00:39:30 bernie
53 *#* ser_open(): Document a bit more.
55 *#* Revision 1.28 2006/02/17 22:23:06 bernie
56 *#* Update POSIX serial emulator.
58 *#* Revision 1.27 2005/11/27 23:33:40 bernie
59 *#* Use appconfig.h instead of cfg/config.h.
61 *#* Revision 1.26 2005/11/04 16:20:02 bernie
62 *#* Fix reference to README.devlib in header.
64 *#* Revision 1.25 2005/04/11 19:10:27 bernie
65 *#* Include top-level headers from cfg/ subdir.
67 *#* Revision 1.24 2005/01/21 20:13:15 aleph
68 *#* Fix drain at ser_close()
70 *#* Revision 1.23 2005/01/14 00:47:07 aleph
71 *#* ser_drain(): Wait for hw transmission complete.
73 *#* Revision 1.22 2004/12/08 08:56:14 bernie
74 *#* Rename time_t to mtime_t.
76 *#* Revision 1.21 2004/11/16 18:10:13 bernie
77 *#* Add sanity checks for missing configuration parameters.
79 *#* Revision 1.20 2004/10/19 11:48:00 bernie
80 *#* Remove unused variable.
82 *#* Revision 1.19 2004/10/19 08:14:13 bernie
83 *#* Fix a few longstanding bugs wrt status handling (made by rasky on scfirm).
85 *#* Revision 1.18 2004/09/20 03:31:15 bernie
88 *#* Revision 1.17 2004/09/14 21:06:07 bernie
89 *#* Use debug.h instead of kdebug.h; Spelling fixes.
91 *#* Revision 1.16 2004/09/06 21:40:50 bernie
92 *#* Move buffer handling in chip-specific driver.
94 *#* Revision 1.15 2004/08/25 14:12:08 rasky
95 *#* Aggiornato il comment block dei log RCS
97 *#* Revision 1.14 2004/08/24 16:22:57 bernie
98 *#* Thinkos; Doxygen fixes
100 *#* Revision 1.13 2004/08/24 16:20:48 bernie
101 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
103 *#* Revision 1.12 2004/08/24 13:49:39 bernie
106 *#* Revision 1.11 2004/08/15 05:32:22 bernie
107 *#* ser_resync(): New function.
109 *#* Revision 1.10 2004/08/10 06:29:50 bernie
110 *#* Rename timer_gettick() to timer_ticks().
112 *#* Revision 1.9 2004/08/08 06:06:20 bernie
113 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
115 *#* Revision 1.8 2004/07/29 22:57:09 bernie
116 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
118 *#* Revision 1.7 2004/07/18 21:49:03 bernie
119 *#* Make CONFIG_SER_DEFBAUDRATE optional.
121 *#* Revision 1.6 2004/06/07 15:56:28 aleph
122 *#* Remove cast-as-lvalue extension abuse
124 *#* Revision 1.5 2004/06/06 16:41:44 bernie
125 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
127 *#* Revision 1.4 2004/06/03 11:27:09 bernie
128 *#* Add dual-license information.
130 *#* Revision 1.3 2004/06/02 21:35:24 aleph
131 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
133 *#* Revision 1.2 2004/05/23 18:21:53 bernie
134 *#* Trim CVS logs and cleanup header info.
140 #include <mware/formatwr.h>
141 #include <cfg/debug.h>
142 #include <appconfig.h>
145 * Sanity check for config parameters required by this module.
147 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
148 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
150 #if !defined(CONFIG_SER_RXTIMEOUT)
151 #error CONFIG_SER_TXTIMEOUT missing in config.h
153 #if !defined(CONFIG_SER_RXTIMEOUT)
154 #error CONFIG_SER_RXTIMEOUT missing in config.h
156 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
157 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
159 #if !defined(CONFIG_SER_DEFBAUDRATE)
160 #error CONFIG_SER_DEFBAUDRATE missing in config.h
162 #if !defined(CONFIG_PRINTF)
163 #error CONFIG_PRINTF missing in config.h
167 #include <kern/proc.h>
170 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
171 #include <drv/timer.h>
175 /* Serial configuration parameters */
176 #define SER_CTSDELAY 70 /**< CTS line retry interval (ms) */
177 #define SER_TXPOLLDELAY 2 /**< Transmit buffer full retry interval (ms) */
178 #define SER_RXPOLLDELAY 2 /**< Receive buffer empty retry interval (ms) */
181 struct Serial ser_handles[SER_CNT];
185 * Inserisce il carattere c nel buffer di trasmissione.
186 * Questa funzione mette il processo chiamante in attesa
187 * quando il buffer e' pieno.
189 * \return EOF in caso di errore o timeout, altrimenti
190 * il carattere inviato.
192 int ser_putchar(int c, struct Serial *port)
194 //ASSERT_VALID_FIFO(&port->txfifo);
195 if (fifo_isfull_locked(&port->txfifo))
197 #if CONFIG_SER_TXTIMEOUT != -1
198 ticks_t start_time = timer_clock();
201 /* Attende finche' il buffer e' pieno... */
204 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
205 /* Give up timeslice to other processes. */
208 #if CONFIG_SER_TXTIMEOUT != -1
209 if (timer_clock() - start_time >= port->txtimeout)
211 ATOMIC(port->status |= SERRF_TXTIMEOUT);
214 #endif /* CONFIG_SER_TXTIMEOUT */
216 while (fifo_isfull_locked(&port->txfifo));
219 fifo_push_locked(&port->txfifo, (unsigned char)c);
221 /* (re)trigger tx interrupt */
222 port->hw->table->txStart(port->hw);
224 /* Avoid returning signed extended char */
225 return (int)((unsigned char)c);
230 * Preleva un carattere dal buffer di ricezione.
231 * Questa funzione mette il processo chiamante in attesa
232 * quando il buffer e' vuoto. L'attesa ha un timeout
233 * di ser_rxtimeout millisecondi.
235 * \return EOF in caso di errore o timeout, altrimenti
236 * il carattere ricevuto.
238 int ser_getchar(struct Serial *port)
240 if (fifo_isempty_locked(&port->rxfifo))
242 #if CONFIG_SER_RXTIMEOUT != -1
243 ticks_t start_time = timer_clock();
245 /* Wait while buffer is empty */
248 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
249 /* Give up timeslice to other processes. */
252 #if CONFIG_SER_RXTIMEOUT != -1
253 if (timer_clock() - start_time >= port->rxtimeout)
255 ATOMIC(port->status |= SERRF_RXTIMEOUT);
258 #endif /* CONFIG_SER_RXTIMEOUT */
260 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
264 * Get a byte from the FIFO (avoiding sign-extension),
265 * re-enable RTS, then return result.
267 if (ser_getstatus(port) & SERRF_RX)
269 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
274 * Preleva un carattere dal buffer di ricezione.
275 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
276 * immediatamente EOF.
278 int ser_getchar_nowait(struct Serial *port)
280 if (fifo_isempty_locked(&port->rxfifo))
283 /* NOTE: the double cast prevents unwanted sign extension */
284 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
290 * Read a line long at most as size and put it
292 * \return number of chars read or EOF in case
295 int ser_gets(struct Serial *port, char *buf, int size)
297 return ser_gets_echo(port, buf, size, false);
302 * Read a line long at most as size and put it
303 * in buf, with optional echo.
305 * \return number of chars read, or EOF in case
308 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
315 if ((c = ser_getchar(port)) == EOF)
322 if (c == '\r' || c == '\n' || i >= size-1)
326 ser_print(port, "\r\n");
331 ser_putchar(c, port);
336 #endif /* !CONFIG_SER_GETS */
340 * Read at most \a size bytes from \a port and put them in \a buf
342 * \return number of bytes actually read, or EOF in
345 int ser_read(struct Serial *port, void *buf, size_t size)
348 char *_buf = (char *)buf;
353 if ((c = ser_getchar(port)) == EOF)
363 * Write a string to serial.
364 * \return 0 if OK, EOF in case of error.
366 int ser_print(struct Serial *port, const char *s)
370 if (ser_putchar(*s++, port) == EOF)
378 * \brief Write a buffer to serial.
380 * \return 0 if OK, EOF in case of error.
382 * \todo Optimize with fifo_pushblock()
384 int ser_write(struct Serial *port, const void *_buf, size_t len)
386 const char *buf = (const char *)_buf;
390 if (ser_putchar(*buf++, port) == EOF)
401 int ser_printf(struct Serial *port, const char *format, ...)
406 va_start(ap, format);
407 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
412 #endif /* CONFIG_PRINTF */
415 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
416 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
418 port->rxtimeout = ms_to_ticks(rxtimeout);
419 port->txtimeout = ms_to_ticks(txtimeout);
421 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
423 #if CONFIG_SER_RXTIMEOUT != -1
425 * Discard input to resynchronize with remote end.
427 * Discard incoming data until the port stops receiving
428 * characters for at least \a delay milliseconds.
430 * \note Serial errors are reset before and after executing the purge.
432 void ser_resync(struct Serial *port, mtime_t delay)
434 mtime_t old_rxtimeout = ticks_to_ms(port->rxtimeout);
436 ser_settimeouts(port, delay, ticks_to_ms(port->txtimeout));
439 ser_setstatus(port, 0);
442 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
444 /* Restore port to an usable status */
445 ser_setstatus(port, 0);
446 ser_settimeouts(port, old_rxtimeout, ticks_to_ms(port->txtimeout));
448 #endif /* CONFIG_SER_RXTIMEOUT */
451 void ser_setbaudrate(struct Serial *port, unsigned long rate)
453 port->hw->table->setBaudrate(port->hw, rate);
457 void ser_setparity(struct Serial *port, int parity)
459 port->hw->table->setParity(port->hw, parity);
464 * Flush both the RX and TX buffers.
466 void ser_purge(struct Serial *port)
468 fifo_flush_locked(&port->rxfifo);
469 fifo_flush_locked(&port->txfifo);
474 * Wait until all pending output is completely
475 * transmitted to the other end.
477 * \note The current implementation only checks the
478 * software transmission queue. Any hardware
481 void ser_drain(struct Serial *ser)
484 * Wait until the FIFO becomes empty, and then until the byte currently in
485 * the hardware register gets shifted out.
487 while (!fifo_isempty(&ser->txfifo)
488 || ser->hw->table->txSending(ser->hw))
490 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
491 /* Give up timeslice to other processes. */
499 * Initialize a serial port.
501 * \param unit Serial unit to open. Possible values are architecture dependant.
503 struct Serial *ser_open(unsigned int unit)
507 ASSERT(unit < countof(ser_handles));
508 port = &ser_handles[unit];
510 ASSERT(!port->is_open);
511 DB(port->is_open = true;)
515 port->hw = ser_hw_getdesc(unit);
517 /* Initialize circular buffers */
518 ASSERT(port->hw->txbuffer);
519 ASSERT(port->hw->rxbuffer);
520 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
521 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
523 port->hw->table->init(port->hw, port);
525 /* Set default values */
526 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
527 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
529 #if CONFIG_SER_DEFBAUDRATE
530 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
533 /* Clear error flags */
534 ser_setstatus(port, 0);
541 * Clean up serial port, disabling the associated hardware.
543 void ser_close(struct Serial *port)
545 ASSERT(port->is_open);
546 DB(port->is_open = false;)
548 // Wait until we finish sending everything
551 port->hw->table->cleanup(port->hw);
555 * We purge the FIFO buffer only after the low-level cleanup, so that
556 * we are sure that there are no more interrupts.