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.24 2005/01/21 20:13:15 aleph
32 *#* Fix drain at ser_close()
34 *#* Revision 1.23 2005/01/14 00:47:07 aleph
35 *#* ser_drain(): Wait for hw transmission complete.
37 *#* Revision 1.22 2004/12/08 08:56:14 bernie
38 *#* Rename time_t to mtime_t.
40 *#* Revision 1.21 2004/11/16 18:10:13 bernie
41 *#* Add sanity checks for missing configuration parameters.
43 *#* Revision 1.20 2004/10/19 11:48:00 bernie
44 *#* Remove unused variable.
46 *#* Revision 1.19 2004/10/19 08:14:13 bernie
47 *#* Fix a few longstanding bugs wrt status handling (made by rasky on scfirm).
49 *#* Revision 1.18 2004/09/20 03:31:15 bernie
52 *#* Revision 1.17 2004/09/14 21:06:07 bernie
53 *#* Use debug.h instead of kdebug.h; Spelling fixes.
55 *#* Revision 1.16 2004/09/06 21:40:50 bernie
56 *#* Move buffer handling in chip-specific driver.
58 *#* Revision 1.15 2004/08/25 14:12:08 rasky
59 *#* Aggiornato il comment block dei log RCS
61 *#* Revision 1.14 2004/08/24 16:22:57 bernie
62 *#* Thinkos; Doxygen fixes
64 *#* Revision 1.13 2004/08/24 16:20:48 bernie
65 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
67 *#* Revision 1.12 2004/08/24 13:49:39 bernie
70 *#* Revision 1.11 2004/08/15 05:32:22 bernie
71 *#* ser_resync(): New function.
73 *#* Revision 1.10 2004/08/10 06:29:50 bernie
74 *#* Rename timer_gettick() to timer_ticks().
76 *#* Revision 1.9 2004/08/08 06:06:20 bernie
77 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
79 *#* Revision 1.8 2004/07/29 22:57:09 bernie
80 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
82 *#* Revision 1.7 2004/07/18 21:49:03 bernie
83 *#* Make CONFIG_SER_DEFBAUDRATE optional.
85 *#* Revision 1.6 2004/06/07 15:56:28 aleph
86 *#* Remove cast-as-lvalue extension abuse
88 *#* Revision 1.5 2004/06/06 16:41:44 bernie
89 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
91 *#* Revision 1.4 2004/06/03 11:27:09 bernie
92 *#* Add dual-license information.
94 *#* Revision 1.3 2004/06/02 21:35:24 aleph
95 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
97 *#* Revision 1.2 2004/05/23 18:21:53 bernie
98 *#* Trim CVS logs and cleanup header info.
104 #include <mware/formatwr.h>
110 * Sanity check for config parameters required by this module.
112 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
113 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
115 #if !defined(CONFIG_SER_RXTIMEOUT)
116 #error CONFIG_SER_TXTIMEOUT missing in config.h
118 #if !defined(CONFIG_SER_RXTIMEOUT)
119 #error CONFIG_SER_RXTIMEOUT missing in config.h
121 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
122 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
124 #if !defined(CONFIG_SER_DEFBAUDRATE)
125 #error CONFIG_SER_DEFBAUDRATE missing in config.h
127 #if !defined(CONFIG_PRINTF)
128 #error CONFIG_PRINTF missing in config.h
132 #include <kern/proc.h>
135 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
136 #include <drv/timer.h>
140 /* Serial configuration parameters */
141 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
142 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
143 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
146 struct Serial ser_handles[SER_CNT];
150 * Inserisce il carattere c nel buffer di trasmissione.
151 * Questa funzione mette il processo chiamante in attesa
152 * quando il buffer e' pieno.
154 * \return EOF in caso di errore o timeout, altrimenti
155 * il carattere inviato.
157 int ser_putchar(int c, struct Serial *port)
159 //ASSERT_VALID_FIFO(&port->txfifo);
160 if (fifo_isfull_locked(&port->txfifo))
162 #if CONFIG_SER_TXTIMEOUT != -1
163 mtime_t start_time = timer_ticks();
166 /* Attende finche' il buffer e' pieno... */
169 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
170 /* Give up timeslice to other processes. */
173 #if CONFIG_SER_TXTIMEOUT != -1
174 if (timer_ticks() - start_time >= port->txtimeout)
176 port->status |= SERRF_TXTIMEOUT;
179 #endif /* CONFIG_SER_TXTIMEOUT */
181 while (fifo_isfull_locked(&port->txfifo));
184 fifo_push_locked(&port->txfifo, (unsigned char)c);
186 /* (re)trigger tx interrupt */
187 port->hw->table->txStart(port->hw);
189 /* Avoid returning signed extended char */
190 return (int)((unsigned char)c);
195 * Preleva un carattere dal buffer di ricezione.
196 * Questa funzione mette il processo chiamante in attesa
197 * quando il buffer e' vuoto. L'attesa ha un timeout
198 * di ser_rxtimeout millisecondi.
200 * \return EOF in caso di errore o timeout, altrimenti
201 * il carattere ricevuto.
203 int ser_getchar(struct Serial *port)
205 if (fifo_isempty_locked(&port->rxfifo))
207 #if CONFIG_SER_RXTIMEOUT != -1
208 mtime_t start_time = timer_ticks();
210 /* Wait while buffer is empty */
213 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
214 /* Give up timeslice to other processes. */
217 #if CONFIG_SER_RXTIMEOUT != -1
218 if (timer_ticks() - start_time >= port->rxtimeout)
220 port->status |= SERRF_RXTIMEOUT;
223 #endif /* CONFIG_SER_RXTIMEOUT */
225 while (fifo_isempty_locked(&port->rxfifo) && (port->status & SERRF_RX) == 0);
229 * Get a byte from the FIFO (avoiding sign-extension),
230 * re-enable RTS, then return result.
232 if (port->status & SERRF_RX)
234 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
239 * Preleva un carattere dal buffer di ricezione.
240 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
241 * immediatamente EOF.
243 int ser_getchar_nowait(struct Serial *port)
245 if (fifo_isempty_locked(&port->rxfifo))
248 /* NOTE: the double cast prevents unwanted sign extension */
249 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
255 * Read a line long at most as size and put it
257 * \return number of chars read or EOF in case
260 int ser_gets(struct Serial *port, char *buf, int size)
262 return ser_gets_echo(port, buf, size, false);
267 * Read a line long at most as size and put it
268 * in buf, with optional echo.
270 * \return number of chars read, or EOF in case
273 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
280 if ((c = ser_getchar(port)) == EOF)
287 if (c == '\r' || c == '\n' || i >= size-1)
291 ser_print(port, "\r\n");
296 ser_putchar(c, port);
301 #endif /* !CONFIG_SER_GETS */
305 * Read at most \a size bytes from \a port and put them in \a buf
307 * \return number of bytes actually read, or EOF in
310 int ser_read(struct Serial *port, void *buf, size_t size)
313 char *_buf = (char *)buf;
318 if ((c = ser_getchar(port)) == EOF)
328 * Write a string to serial.
329 * \return 0 if OK, EOF in case of error.
331 int ser_print(struct Serial *port, const char *s)
335 if (ser_putchar(*s++, port) == EOF)
343 * \brief Write a buffer to serial.
345 * \return 0 if OK, EOF in case of error.
347 * \todo Optimize with fifo_pushblock()
349 int ser_write(struct Serial *port, const void *_buf, size_t len)
351 const char *buf = (const char *)_buf;
355 if (ser_putchar(*buf++, port) == EOF)
366 int ser_printf(struct Serial *port, const char *format, ...)
371 va_start(ap, format);
372 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
377 #endif /* CONFIG_PRINTF */
380 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
381 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
383 port->rxtimeout = rxtimeout;
384 port->txtimeout = txtimeout;
386 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
388 #if CONFIG_SER_RXTIMEOUT != -1
390 * Discard input to resynchronize with remote end.
392 * Discard incoming data until the port stops receiving
393 * characters for at least \a delay milliseconds.
395 * \note Serial errors are reset before and after executing the purge.
397 void ser_resync(struct Serial *port, mtime_t delay)
399 mtime_t old_rxtimeout = port->rxtimeout;
401 ser_settimeouts(port, delay, port->txtimeout);
404 ser_setstatus(port, 0);
407 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
409 /* Restore port to an usable status */
410 ser_setstatus(port, 0);
411 ser_settimeouts(port, old_rxtimeout, port->txtimeout);
413 #endif /* CONFIG_SER_RXTIMEOUT */
416 void ser_setbaudrate(struct Serial *port, unsigned long rate)
418 port->hw->table->setBaudrate(port->hw, rate);
422 void ser_setparity(struct Serial *port, int parity)
424 port->hw->table->setParity(port->hw, parity);
429 * Flush both the RX and TX buffers.
431 void ser_purge(struct Serial *port)
433 fifo_flush_locked(&port->rxfifo);
434 fifo_flush_locked(&port->txfifo);
439 * Wait until all pending output is completely
440 * transmitted to the other end.
442 * \note The current implementation only checks the
443 * software transmission queue. Any hardware
446 void ser_drain(struct Serial *ser)
449 * Wait until the FIFO is empty, and then until the byte currently in
450 * the hardware register gets shifted out.
452 while (!fifo_isempty(&ser->txfifo)
453 || ser->hw->table->txSending(ser->hw))
455 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
456 /* Give up timeslice to other processes. */
466 struct Serial *ser_open(unsigned int unit)
470 ASSERT(unit < countof(ser_handles));
471 port = &ser_handles[unit];
473 ASSERT(!port->is_open);
474 DB(port->is_open = true;)
478 port->hw = ser_hw_getdesc(unit);
480 /* Initialize circular buffers */
481 ASSERT(port->hw->txbuffer);
482 ASSERT(port->hw->rxbuffer);
483 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
484 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
486 port->hw->table->init(port->hw, port);
488 /* Set default values */
489 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
490 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
492 #if CONFIG_SER_DEFBAUDRATE
493 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
496 /* Clear error flags */
497 ser_setstatus(port, 0);
504 * Clean up serial port, disabling the associated hardware.
506 void ser_close(struct Serial *port)
508 ASSERT(port->is_open);
509 DB(port->is_open = false;)
511 // Wait until we finish sending everything
514 port->hw->table->cleanup(port->hw);
518 * We purge the FIFO buffer only after the low-level cleanup, so that
519 * we are sure that there are no more interrupts.