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.9 2004/08/08 06:06:20 bernie
32 * Use new-style CONFIG_ idiom; Fix module-wide documentation.
34 * Revision 1.8 2004/07/29 22:57:09 bernie
35 * ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
37 * Revision 1.7 2004/07/18 21:49:03 bernie
38 * Make CONFIG_SER_DEFBAUDRATE optional.
40 * Revision 1.6 2004/06/07 15:56:28 aleph
41 * Remove cast-as-lvalue extension abuse
43 * Revision 1.5 2004/06/06 16:41:44 bernie
44 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
46 * Revision 1.4 2004/06/03 11:27:09 bernie
47 * Add dual-license information.
49 * Revision 1.3 2004/06/02 21:35:24 aleph
50 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
52 * Revision 1.2 2004/05/23 18:21:53 bernie
53 * Trim CVS logs and cleanup header info.
57 #include <mware/formatwr.h>
58 #include <drv/kdebug.h>
64 #include <kern/proc.h>
66 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
67 #include <drv/timer.h>
71 /* Serial configuration parameters */
72 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
73 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
74 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
77 struct Serial ser_handles[SER_CNT];
81 * Inserisce il carattere c nel buffer di trasmissione.
82 * Questa funzione mette il processo chiamante in attesa
83 * quando il buffer e' pieno.
85 * \return EOF in caso di errore o timeout, altrimenti
86 * il carattere inviato.
88 int ser_putchar(int c, struct Serial *port)
90 if (fifo_isfull_locked(&port->txfifo))
92 #if CONFIG_SER_TXTIMEOUT != -1
93 time_t start_time = timer_gettick();
96 /* Attende finche' il buffer e' pieno... */
99 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
100 /* Give up timeslice to other processes. */
103 #if CONFIG_SER_TXTIMEOUT != -1
104 if (timer_gettick() - start_time >= port->txtimeout)
106 port->status |= SERRF_TXTIMEOUT;
109 #endif /* CONFIG_SER_TXTIMEOUT */
111 while (fifo_isfull_locked(&port->txfifo));
114 fifo_push_locked(&port->txfifo, (unsigned char)c);
116 /* (re)trigger tx interrupt */
117 port->hw->table->enabletxirq(port->hw);
119 /* Avoid returning signed estended char */
120 return (int)((unsigned char)c);
125 * Preleva un carattere dal buffer di ricezione.
126 * Questa funzione mette il processo chiamante in attesa
127 * quando il buffer e' vuoto. L'attesa ha un timeout
128 * di ser_rxtimeout millisecondi.
130 * \return EOF in caso di errore o timeout, altrimenti
131 * il carattere ricevuto.
133 int ser_getchar(struct Serial *port)
137 if (fifo_isempty_locked(&port->rxfifo))
139 #if CONFIG_SER_RXTIMEOUT != -1
140 time_t start_time = timer_gettick();
142 /* Wait while buffer is empty */
145 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
146 /* Give up timeslice to other processes. */
149 #if CONFIG_SER_RXTIMEOUT != -1
150 if (timer_gettick() - start_time >= port->rxtimeout)
152 port->status |= SERRF_RXTIMEOUT;
155 #endif /* CONFIG_SER_RXTIMEOUT */
157 while (fifo_isempty_locked(&port->rxfifo));
161 * Get a byte from the FIFO (avoiding sign-extension),
162 * re-enable RTS, then return result.
164 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
165 return port->status ? EOF : result;
170 * Preleva un carattere dal buffer di ricezione.
171 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
172 * immediatamente EOF.
174 int ser_getchar_nowait(struct Serial *port)
176 if (fifo_isempty_locked(&port->rxfifo))
179 /* NOTE: the double cast prevents unwanted sign extension */
180 return (int)(unsigned char)fifo_pop(&port->rxfifo);
186 * Read a line long at most as size and puts it
188 * \return number of chars read or EOF in case
191 int ser_gets(struct Serial *port, char *buf, int size)
193 return ser_gets_echo(port, buf, size, false);
198 * Read a line long at most as size and put it
199 * in buf, with optional echo.
201 * \return number of chars read, or EOF in case
204 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
211 if ((c = ser_getchar(port)) == EOF)
218 if (c == '\r' || c == '\n' || i >= size-1)
222 ser_print(port, "\r\n");
227 ser_putchar(c, port);
232 #endif /* !CONFIG_SER_GETS */
236 * Read at most size bytes and puts them
238 * \return number of bytes read or EOF in case
241 int ser_read(struct Serial *port, char *buf, size_t size)
248 if ((c = ser_getchar(port)) == EOF)
258 * Write a string to serial.
259 * \return 0 if OK, EOF in case of error.
261 int ser_print(struct Serial *port, const char *s)
265 if (ser_putchar(*s++, port) == EOF)
273 * \brief Write a buffer to serial.
275 * \return 0 if OK, EOF in case of error.
277 * \todo Optimize with fifo_pushblock()
279 int ser_write(struct Serial *port, const void *_buf, size_t len)
281 const char *buf = _buf;
285 if (ser_putchar(*buf++, port) == EOF)
296 int ser_printf(struct Serial *port, const char *format, ...)
301 ser_setstatus(port, 0);
302 va_start(ap, format);
303 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
308 #endif /* CONFIG_PRINTF */
311 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
312 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
314 port->rxtimeout = rxtimeout;
315 port->txtimeout = txtimeout;
317 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
320 void ser_setbaudrate(struct Serial *port, unsigned long rate)
322 port->hw->table->setbaudrate(port->hw, rate);
326 void ser_setparity(struct Serial *port, int parity)
328 port->hw->table->setparity(port->hw, parity);
333 * Flush both the RX and TX buffers.
335 void ser_purge(struct Serial *ser)
337 fifo_flush_locked(&ser->rxfifo);
338 fifo_flush_locked(&ser->txfifo);
342 * Wait until all pending output is completely
343 * transmitted to the other end.
345 * \note The current implementation only checks the
346 * software transmission queue. Any hardware
349 void ser_drain(struct Serial *ser)
351 while(!fifo_isempty(&ser->txfifo))
353 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
354 /* Give up timeslice to other processes. */
364 struct Serial *ser_open(unsigned int unit)
368 ASSERT(unit < countof(ser_handles));
369 port = &ser_handles[unit];
371 ASSERT(!port->is_open);
372 DB(port->is_open = true;)
376 /* Initialize circular buffer */
377 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
378 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
380 port->hw = ser_hw_getdesc(unit);
381 port->hw->table->init(port->hw, port);
383 /* Set default values */
384 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
385 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
387 #if CONFIG_SER_DEFBAUDRATE
388 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
396 * Clean up serial port, disabling the associated hardware.
398 void ser_close(struct Serial *port)
400 ASSERT(port->is_open);
401 DB(port->is_open = false;)
403 port->hw->table->cleanup(port->hw);