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.14 2004/08/24 16:22:57 bernie
32 * Thinkos; Doxygen fixes
34 * Revision 1.13 2004/08/24 16:20:48 bernie
35 * ser_read(): Make buffer argument void * for consistency with ANSI C and ser_write()
37 * Revision 1.12 2004/08/24 13:49:39 bernie
40 * Revision 1.11 2004/08/15 05:32:22 bernie
41 * ser_resync(): New function.
43 * Revision 1.10 2004/08/10 06:29:50 bernie
44 * Rename timer_gettick() to timer_ticks().
46 * Revision 1.9 2004/08/08 06:06:20 bernie
47 * Use new-style CONFIG_ idiom; Fix module-wide documentation.
49 * Revision 1.8 2004/07/29 22:57:09 bernie
50 * ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
52 * Revision 1.7 2004/07/18 21:49:03 bernie
53 * Make CONFIG_SER_DEFBAUDRATE optional.
55 * Revision 1.6 2004/06/07 15:56:28 aleph
56 * Remove cast-as-lvalue extension abuse
58 * Revision 1.5 2004/06/06 16:41:44 bernie
59 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
61 * Revision 1.4 2004/06/03 11:27:09 bernie
62 * Add dual-license information.
64 * Revision 1.3 2004/06/02 21:35:24 aleph
65 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
67 * Revision 1.2 2004/05/23 18:21:53 bernie
68 * Trim CVS logs and cleanup header info.
72 #include <mware/formatwr.h>
73 #include <drv/kdebug.h>
79 #include <kern/proc.h>
81 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
82 #include <drv/timer.h>
86 /* Serial configuration parameters */
87 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
88 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
89 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
92 struct Serial ser_handles[SER_CNT];
96 * Inserisce il carattere c nel buffer di trasmissione.
97 * Questa funzione mette il processo chiamante in attesa
98 * quando il buffer e' pieno.
100 * \return EOF in caso di errore o timeout, altrimenti
101 * il carattere inviato.
103 int ser_putchar(int c, struct Serial *port)
105 if (fifo_isfull_locked(&port->txfifo))
107 #if CONFIG_SER_TXTIMEOUT != -1
108 time_t start_time = timer_ticks();
111 /* Attende finche' il buffer e' pieno... */
114 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
115 /* Give up timeslice to other processes. */
118 #if CONFIG_SER_TXTIMEOUT != -1
119 if (timer_ticks() - start_time >= port->txtimeout)
121 port->status |= SERRF_TXTIMEOUT;
124 #endif /* CONFIG_SER_TXTIMEOUT */
126 while (fifo_isfull_locked(&port->txfifo));
129 fifo_push_locked(&port->txfifo, (unsigned char)c);
131 /* (re)trigger tx interrupt */
132 port->hw->table->enabletxirq(port->hw);
134 /* Avoid returning signed estended char */
135 return (int)((unsigned char)c);
140 * Preleva un carattere dal buffer di ricezione.
141 * Questa funzione mette il processo chiamante in attesa
142 * quando il buffer e' vuoto. L'attesa ha un timeout
143 * di ser_rxtimeout millisecondi.
145 * \return EOF in caso di errore o timeout, altrimenti
146 * il carattere ricevuto.
148 int ser_getchar(struct Serial *port)
152 if (fifo_isempty_locked(&port->rxfifo))
154 #if CONFIG_SER_RXTIMEOUT != -1
155 time_t start_time = timer_ticks();
157 /* Wait while buffer is empty */
160 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
161 /* Give up timeslice to other processes. */
164 #if CONFIG_SER_RXTIMEOUT != -1
165 if (timer_ticks() - start_time >= port->rxtimeout)
167 port->status |= SERRF_RXTIMEOUT;
170 #endif /* CONFIG_SER_RXTIMEOUT */
172 while (fifo_isempty_locked(&port->rxfifo));
176 * Get a byte from the FIFO (avoiding sign-extension),
177 * re-enable RTS, then return result.
179 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
180 return port->status ? EOF : result;
185 * Preleva un carattere dal buffer di ricezione.
186 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
187 * immediatamente EOF.
189 int ser_getchar_nowait(struct Serial *port)
191 if (fifo_isempty_locked(&port->rxfifo))
194 /* NOTE: the double cast prevents unwanted sign extension */
195 return (int)(unsigned char)fifo_pop(&port->rxfifo);
201 * Read a line long at most as size and puts it
203 * \return number of chars read or EOF in case
206 int ser_gets(struct Serial *port, char *buf, int size)
208 return ser_gets_echo(port, buf, size, false);
213 * Read a line long at most as size and put it
214 * in buf, with optional echo.
216 * \return number of chars read, or EOF in case
219 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
226 if ((c = ser_getchar(port)) == EOF)
233 if (c == '\r' || c == '\n' || i >= size-1)
237 ser_print(port, "\r\n");
242 ser_putchar(c, port);
247 #endif /* !CONFIG_SER_GETS */
251 * Read at most \a size bytes from \a port and put them in \a buf
253 * \return number of bytes actually read, or EOF in
256 int ser_read(struct Serial *port, void *buf, size_t size)
259 char *_buf = (char *)buf;
264 if ((c = ser_getchar(port)) == EOF)
274 * Write a string to serial.
275 * \return 0 if OK, EOF in case of error.
277 int ser_print(struct Serial *port, const char *s)
281 if (ser_putchar(*s++, port) == EOF)
289 * \brief Write a buffer to serial.
291 * \return 0 if OK, EOF in case of error.
293 * \todo Optimize with fifo_pushblock()
295 int ser_write(struct Serial *port, const void *_buf, size_t len)
297 const char *buf = _buf;
301 if (ser_putchar(*buf++, port) == EOF)
312 int ser_printf(struct Serial *port, const char *format, ...)
317 ser_setstatus(port, 0);
318 va_start(ap, format);
319 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
324 #endif /* CONFIG_PRINTF */
327 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
328 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
330 port->rxtimeout = rxtimeout;
331 port->txtimeout = txtimeout;
333 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
335 #if CONFIG_SER_RXTIMEOUT != -1
337 * Discard input to resynchronize with remote end
339 * Discard incoming data until the port stops receiving
340 * characters for at least \a delay milliseconds.
342 * \note Serial errors are reset before and after executing the purge.
344 void ser_resync(struct Serial *port, time_t delay)
346 time_t old_rxtimeout = port->rxtimeout;
348 ser_settimeouts(port, delay, port->txtimeout);
351 ser_setstatus(port, 0);
354 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
356 /* Restore port to an usable status */
357 ser_setstatus(port, 0);
358 ser_settimeouts(port, old_rxtimeout, port->txtimeout);
360 #endif /* CONFIG_SER_RXTIMEOUT */
363 void ser_setbaudrate(struct Serial *port, unsigned long rate)
365 port->hw->table->setbaudrate(port->hw, rate);
369 void ser_setparity(struct Serial *port, int parity)
371 port->hw->table->setparity(port->hw, parity);
376 * Flush both the RX and TX buffers.
378 void ser_purge(struct Serial *port)
380 fifo_flush_locked(&port->rxfifo);
381 fifo_flush_locked(&port->txfifo);
386 * Wait until all pending output is completely
387 * transmitted to the other end.
389 * \note The current implementation only checks the
390 * software transmission queue. Any hardware
393 void ser_drain(struct Serial *ser)
395 while (!fifo_isempty(&ser->txfifo))
397 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
398 /* Give up timeslice to other processes. */
408 struct Serial *ser_open(unsigned int unit)
412 ASSERT(unit < countof(ser_handles));
413 port = &ser_handles[unit];
415 ASSERT(!port->is_open);
416 DB(port->is_open = true;)
420 /* Initialize circular buffer */
421 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
422 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
424 port->hw = ser_hw_getdesc(unit);
425 port->hw->table->init(port->hw, port);
427 /* Set default values */
428 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
429 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
431 #if CONFIG_SER_DEFBAUDRATE
432 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
440 * Clean up serial port, disabling the associated hardware.
442 void ser_close(struct Serial *port)
444 ASSERT(port->is_open);
445 DB(port->is_open = false;)
447 port->hw->table->cleanup(port->hw);