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.16 2004/09/06 21:40:50 bernie
32 *#* Move buffer handling in chip-specific driver.
34 *#* Revision 1.15 2004/08/25 14:12:08 rasky
35 *#* Aggiornato il comment block dei log RCS
37 *#* Revision 1.14 2004/08/24 16:22:57 bernie
38 *#* Thinkos; Doxygen fixes
40 *#* Revision 1.13 2004/08/24 16:20:48 bernie
41 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
43 *#* Revision 1.12 2004/08/24 13:49:39 bernie
46 *#* Revision 1.11 2004/08/15 05:32:22 bernie
47 *#* ser_resync(): New function.
49 *#* Revision 1.10 2004/08/10 06:29:50 bernie
50 *#* Rename timer_gettick() to timer_ticks().
52 *#* Revision 1.9 2004/08/08 06:06:20 bernie
53 *#* Use new-style CONFIG_ idiom; Fix module-wide documentation.
55 *#* Revision 1.8 2004/07/29 22:57:09 bernie
56 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
58 *#* Revision 1.7 2004/07/18 21:49:03 bernie
59 *#* Make CONFIG_SER_DEFBAUDRATE optional.
61 *#* Revision 1.6 2004/06/07 15:56:28 aleph
62 *#* Remove cast-as-lvalue extension abuse
64 *#* Revision 1.5 2004/06/06 16:41:44 bernie
65 *#* ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
67 *#* Revision 1.4 2004/06/03 11:27:09 bernie
68 *#* Add dual-license information.
70 *#* Revision 1.3 2004/06/02 21:35:24 aleph
71 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
73 *#* Revision 1.2 2004/05/23 18:21:53 bernie
74 *#* Trim CVS logs and cleanup header info.
78 #include <mware/formatwr.h>
79 #include <drv/kdebug.h>
85 #include <kern/proc.h>
87 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
88 #include <drv/timer.h>
92 /* Serial configuration parameters */
93 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
94 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
95 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
98 struct Serial ser_handles[SER_CNT];
102 * Inserisce il carattere c nel buffer di trasmissione.
103 * Questa funzione mette il processo chiamante in attesa
104 * quando il buffer e' pieno.
106 * \return EOF in caso di errore o timeout, altrimenti
107 * il carattere inviato.
109 int ser_putchar(int c, struct Serial *port)
111 //ASSERT_VALID_FIFO(&port->txfifo);
112 if (fifo_isfull_locked(&port->txfifo))
114 #if CONFIG_SER_TXTIMEOUT != -1
115 time_t start_time = timer_ticks();
118 /* Attende finche' il buffer e' pieno... */
121 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
122 /* Give up timeslice to other processes. */
125 #if CONFIG_SER_TXTIMEOUT != -1
126 if (timer_ticks() - start_time >= port->txtimeout)
128 port->status |= SERRF_TXTIMEOUT;
131 #endif /* CONFIG_SER_TXTIMEOUT */
133 while (fifo_isfull_locked(&port->txfifo));
136 fifo_push_locked(&port->txfifo, (unsigned char)c);
138 /* (re)trigger tx interrupt */
139 port->hw->table->enabletxirq(port->hw);
141 /* Avoid returning signed estended char */
142 return (int)((unsigned char)c);
147 * Preleva un carattere dal buffer di ricezione.
148 * Questa funzione mette il processo chiamante in attesa
149 * quando il buffer e' vuoto. L'attesa ha un timeout
150 * di ser_rxtimeout millisecondi.
152 * \return EOF in caso di errore o timeout, altrimenti
153 * il carattere ricevuto.
155 int ser_getchar(struct Serial *port)
159 if (fifo_isempty_locked(&port->rxfifo))
161 #if CONFIG_SER_RXTIMEOUT != -1
162 time_t start_time = timer_ticks();
164 /* Wait while buffer is empty */
167 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
168 /* Give up timeslice to other processes. */
171 #if CONFIG_SER_RXTIMEOUT != -1
172 if (timer_ticks() - start_time >= port->rxtimeout)
174 port->status |= SERRF_RXTIMEOUT;
177 #endif /* CONFIG_SER_RXTIMEOUT */
179 while (fifo_isempty_locked(&port->rxfifo));
183 * Get a byte from the FIFO (avoiding sign-extension),
184 * re-enable RTS, then return result.
186 result = (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
187 return port->status ? EOF : result;
192 * Preleva un carattere dal buffer di ricezione.
193 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
194 * immediatamente EOF.
196 int ser_getchar_nowait(struct Serial *port)
198 if (fifo_isempty_locked(&port->rxfifo))
201 /* NOTE: the double cast prevents unwanted sign extension */
202 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
208 * Read a line long at most as size and puts it
210 * \return number of chars read or EOF in case
213 int ser_gets(struct Serial *port, char *buf, int size)
215 return ser_gets_echo(port, buf, size, false);
220 * Read a line long at most as size and put it
221 * in buf, with optional echo.
223 * \return number of chars read, or EOF in case
226 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
233 if ((c = ser_getchar(port)) == EOF)
240 if (c == '\r' || c == '\n' || i >= size-1)
244 ser_print(port, "\r\n");
249 ser_putchar(c, port);
254 #endif /* !CONFIG_SER_GETS */
258 * Read at most \a size bytes from \a port and put them in \a buf
260 * \return number of bytes actually read, or EOF in
263 int ser_read(struct Serial *port, void *buf, size_t size)
266 char *_buf = (char *)buf;
271 if ((c = ser_getchar(port)) == EOF)
281 * Write a string to serial.
282 * \return 0 if OK, EOF in case of error.
284 int ser_print(struct Serial *port, const char *s)
288 if (ser_putchar(*s++, port) == EOF)
296 * \brief Write a buffer to serial.
298 * \return 0 if OK, EOF in case of error.
300 * \todo Optimize with fifo_pushblock()
302 int ser_write(struct Serial *port, const void *_buf, size_t len)
304 const char *buf = _buf;
308 if (ser_putchar(*buf++, port) == EOF)
319 int ser_printf(struct Serial *port, const char *format, ...)
324 ser_setstatus(port, 0);
325 va_start(ap, format);
326 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
331 #endif /* CONFIG_PRINTF */
334 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
335 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
337 port->rxtimeout = rxtimeout;
338 port->txtimeout = txtimeout;
340 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
342 #if CONFIG_SER_RXTIMEOUT != -1
344 * Discard input to resynchronize with remote end
346 * Discard incoming data until the port stops receiving
347 * characters for at least \a delay milliseconds.
349 * \note Serial errors are reset before and after executing the purge.
351 void ser_resync(struct Serial *port, time_t delay)
353 time_t old_rxtimeout = port->rxtimeout;
355 ser_settimeouts(port, delay, port->txtimeout);
358 ser_setstatus(port, 0);
361 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
363 /* Restore port to an usable status */
364 ser_setstatus(port, 0);
365 ser_settimeouts(port, old_rxtimeout, port->txtimeout);
367 #endif /* CONFIG_SER_RXTIMEOUT */
370 void ser_setbaudrate(struct Serial *port, unsigned long rate)
372 port->hw->table->setbaudrate(port->hw, rate);
376 void ser_setparity(struct Serial *port, int parity)
378 port->hw->table->setparity(port->hw, parity);
383 * Flush both the RX and TX buffers.
385 void ser_purge(struct Serial *port)
387 fifo_flush_locked(&port->rxfifo);
388 fifo_flush_locked(&port->txfifo);
393 * Wait until all pending output is completely
394 * transmitted to the other end.
396 * \note The current implementation only checks the
397 * software transmission queue. Any hardware
400 void ser_drain(struct Serial *ser)
402 while (!fifo_isempty(&ser->txfifo))
404 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
405 /* Give up timeslice to other processes. */
415 struct Serial *ser_open(unsigned int unit)
419 ASSERT(unit < countof(ser_handles));
420 port = &ser_handles[unit];
422 ASSERT(!port->is_open);
423 DB(port->is_open = true;)
427 port->hw = ser_hw_getdesc(unit);
429 /* Initialize circular buffers */
430 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
431 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
433 port->hw->table->init(port->hw, port);
435 /* Set default values */
436 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
437 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
439 #if CONFIG_SER_DEFBAUDRATE
440 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
448 * Clean up serial port, disabling the associated hardware.
450 void ser_close(struct Serial *port)
452 ASSERT(port->is_open);
453 DB(port->is_open = false;)
455 port->hw->table->cleanup(port->hw);