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.23 2005/01/14 00:47:07 aleph
32 *#* ser_drain(): Wait for hw transmission complete.
34 *#* Revision 1.22 2004/12/08 08:56:14 bernie
35 *#* Rename time_t to mtime_t.
37 *#* Revision 1.21 2004/11/16 18:10:13 bernie
38 *#* Add sanity checks for missing configuration parameters.
40 *#* Revision 1.20 2004/10/19 11:48:00 bernie
41 *#* Remove unused variable.
43 *#* Revision 1.19 2004/10/19 08:14:13 bernie
44 *#* Fix a few longstanding bugs wrt status handling (made by rasky on scfirm).
46 *#* Revision 1.18 2004/09/20 03:31:15 bernie
49 *#* Revision 1.17 2004/09/14 21:06:07 bernie
50 *#* Use debug.h instead of kdebug.h; Spelling fixes.
52 *#* Revision 1.16 2004/09/06 21:40:50 bernie
53 *#* Move buffer handling in chip-specific driver.
55 *#* Revision 1.15 2004/08/25 14:12:08 rasky
56 *#* Aggiornato il comment block dei log RCS
58 *#* Revision 1.14 2004/08/24 16:22:57 bernie
59 *#* Thinkos; Doxygen fixes
61 *#* Revision 1.13 2004/08/24 16:20:48 bernie
62 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
64 *#* Revision 1.12 2004/08/24 13:49:39 bernie
67 *#* Revision 1.11 2004/08/15 05:32:22 bernie
68 *#* ser_resync(): New function.
70 *#* Revision 1.10 2004/08/10 06:29:50 bernie
71 *#* Rename timer_gettick() to timer_ticks().
73 *#* Revision 1.9 2004/08/08 06:06:20 bernie
74 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
76 *#* Revision 1.8 2004/07/29 22:57:09 bernie
77 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
79 *#* Revision 1.7 2004/07/18 21:49:03 bernie
80 *#* Make CONFIG_SER_DEFBAUDRATE optional.
82 *#* Revision 1.6 2004/06/07 15:56:28 aleph
83 *#* Remove cast-as-lvalue extension abuse
85 *#* Revision 1.5 2004/06/06 16:41:44 bernie
86 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
88 *#* Revision 1.4 2004/06/03 11:27:09 bernie
89 *#* Add dual-license information.
91 *#* Revision 1.3 2004/06/02 21:35:24 aleph
92 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
94 *#* Revision 1.2 2004/05/23 18:21:53 bernie
95 *#* Trim CVS logs and cleanup header info.
101 #include <mware/formatwr.h>
107 * Sanity check for config parameters required by this module.
109 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
110 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
112 #if !defined(CONFIG_SER_RXTIMEOUT)
113 #error CONFIG_SER_TXTIMEOUT missing in config.h
115 #if !defined(CONFIG_SER_RXTIMEOUT)
116 #error CONFIG_SER_RXTIMEOUT missing in config.h
118 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
119 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
121 #if !defined(CONFIG_SER_DEFBAUDRATE)
122 #error CONFIG_SER_DEFBAUDRATE missing in config.h
124 #if !defined(CONFIG_PRINTF)
125 #error CONFIG_PRINTF missing in config.h
129 #include <kern/proc.h>
132 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
133 #include <drv/timer.h>
137 /* Serial configuration parameters */
138 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
139 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
140 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
143 struct Serial ser_handles[SER_CNT];
147 * Inserisce il carattere c nel buffer di trasmissione.
148 * Questa funzione mette il processo chiamante in attesa
149 * quando il buffer e' pieno.
151 * \return EOF in caso di errore o timeout, altrimenti
152 * il carattere inviato.
154 int ser_putchar(int c, struct Serial *port)
156 //ASSERT_VALID_FIFO(&port->txfifo);
157 if (fifo_isfull_locked(&port->txfifo))
159 #if CONFIG_SER_TXTIMEOUT != -1
160 mtime_t start_time = timer_ticks();
163 /* Attende finche' il buffer e' pieno... */
166 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
167 /* Give up timeslice to other processes. */
170 #if CONFIG_SER_TXTIMEOUT != -1
171 if (timer_ticks() - start_time >= port->txtimeout)
173 port->status |= SERRF_TXTIMEOUT;
176 #endif /* CONFIG_SER_TXTIMEOUT */
178 while (fifo_isfull_locked(&port->txfifo));
181 fifo_push_locked(&port->txfifo, (unsigned char)c);
183 /* (re)trigger tx interrupt */
184 port->hw->table->txStart(port->hw);
186 /* Avoid returning signed extended char */
187 return (int)((unsigned char)c);
192 * Preleva un carattere dal buffer di ricezione.
193 * Questa funzione mette il processo chiamante in attesa
194 * quando il buffer e' vuoto. L'attesa ha un timeout
195 * di ser_rxtimeout millisecondi.
197 * \return EOF in caso di errore o timeout, altrimenti
198 * il carattere ricevuto.
200 int ser_getchar(struct Serial *port)
202 if (fifo_isempty_locked(&port->rxfifo))
204 #if CONFIG_SER_RXTIMEOUT != -1
205 mtime_t start_time = timer_ticks();
207 /* Wait while buffer is empty */
210 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
211 /* Give up timeslice to other processes. */
214 #if CONFIG_SER_RXTIMEOUT != -1
215 if (timer_ticks() - start_time >= port->rxtimeout)
217 port->status |= SERRF_RXTIMEOUT;
220 #endif /* CONFIG_SER_RXTIMEOUT */
222 while (fifo_isempty_locked(&port->rxfifo) && (port->status & SERRF_RX) == 0);
226 * Get a byte from the FIFO (avoiding sign-extension),
227 * re-enable RTS, then return result.
229 if (port->status & SERRF_RX)
231 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
236 * Preleva un carattere dal buffer di ricezione.
237 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
238 * immediatamente EOF.
240 int ser_getchar_nowait(struct Serial *port)
242 if (fifo_isempty_locked(&port->rxfifo))
245 /* NOTE: the double cast prevents unwanted sign extension */
246 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
252 * Read a line long at most as size and put it
254 * \return number of chars read or EOF in case
257 int ser_gets(struct Serial *port, char *buf, int size)
259 return ser_gets_echo(port, buf, size, false);
264 * Read a line long at most as size and put it
265 * in buf, with optional echo.
267 * \return number of chars read, or EOF in case
270 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
277 if ((c = ser_getchar(port)) == EOF)
284 if (c == '\r' || c == '\n' || i >= size-1)
288 ser_print(port, "\r\n");
293 ser_putchar(c, port);
298 #endif /* !CONFIG_SER_GETS */
302 * Read at most \a size bytes from \a port and put them in \a buf
304 * \return number of bytes actually read, or EOF in
307 int ser_read(struct Serial *port, void *buf, size_t size)
310 char *_buf = (char *)buf;
315 if ((c = ser_getchar(port)) == EOF)
325 * Write a string to serial.
326 * \return 0 if OK, EOF in case of error.
328 int ser_print(struct Serial *port, const char *s)
332 if (ser_putchar(*s++, port) == EOF)
340 * \brief Write a buffer to serial.
342 * \return 0 if OK, EOF in case of error.
344 * \todo Optimize with fifo_pushblock()
346 int ser_write(struct Serial *port, const void *_buf, size_t len)
348 const char *buf = (const char *)_buf;
352 if (ser_putchar(*buf++, port) == EOF)
363 int ser_printf(struct Serial *port, const char *format, ...)
368 va_start(ap, format);
369 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
374 #endif /* CONFIG_PRINTF */
377 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
378 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
380 port->rxtimeout = rxtimeout;
381 port->txtimeout = txtimeout;
383 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
385 #if CONFIG_SER_RXTIMEOUT != -1
387 * Discard input to resynchronize with remote end.
389 * Discard incoming data until the port stops receiving
390 * characters for at least \a delay milliseconds.
392 * \note Serial errors are reset before and after executing the purge.
394 void ser_resync(struct Serial *port, mtime_t delay)
396 mtime_t old_rxtimeout = port->rxtimeout;
398 ser_settimeouts(port, delay, port->txtimeout);
401 ser_setstatus(port, 0);
404 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
406 /* Restore port to an usable status */
407 ser_setstatus(port, 0);
408 ser_settimeouts(port, old_rxtimeout, port->txtimeout);
410 #endif /* CONFIG_SER_RXTIMEOUT */
413 void ser_setbaudrate(struct Serial *port, unsigned long rate)
415 port->hw->table->setBaudrate(port->hw, rate);
419 void ser_setparity(struct Serial *port, int parity)
421 port->hw->table->setParity(port->hw, parity);
426 * Flush both the RX and TX buffers.
428 void ser_purge(struct Serial *port)
430 fifo_flush_locked(&port->rxfifo);
431 fifo_flush_locked(&port->txfifo);
436 * Wait until all pending output is completely
437 * transmitted to the other end.
439 * \note The current implementation only checks the
440 * software transmission queue. Any hardware
443 void ser_drain(struct Serial *ser)
446 * Wait until the FIFO is empty, and then until the byte currently in
447 * the hardware register gets shifted out.
449 while (!fifo_isempty(&ser->txfifo)
450 || ser->hw->table->txSending(ser->hw))
452 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
453 /* Give up timeslice to other processes. */
463 struct Serial *ser_open(unsigned int unit)
467 ASSERT(unit < countof(ser_handles));
468 port = &ser_handles[unit];
470 ASSERT(!port->is_open);
471 DB(port->is_open = true;)
475 port->hw = ser_hw_getdesc(unit);
477 /* Initialize circular buffers */
478 ASSERT(port->hw->txbuffer);
479 ASSERT(port->hw->rxbuffer);
480 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
481 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
483 port->hw->table->init(port->hw, port);
485 /* Set default values */
486 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
487 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
489 #if CONFIG_SER_DEFBAUDRATE
490 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
493 /* Clear error flags */
494 ser_setstatus(port, 0);
501 * Clean up serial port, disabling the associated hardware.
503 void ser_close(struct Serial *port)
505 ASSERT(port->is_open);
506 DB(port->is_open = false;)
508 // Wait until we finish sending everything
512 port->hw->table->cleanup(port->hw);