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.10 2004/08/10 06:29:50 bernie
32 * Rename timer_gettick() to timer_ticks().
34 * Revision 1.9 2004/08/08 06:06:20 bernie
35 * Use new-style CONFIG_ idiom; Fix module-wide documentation.
37 * Revision 1.8 2004/07/29 22:57:09 bernie
38 * ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
40 * Revision 1.7 2004/07/18 21:49:03 bernie
41 * Make CONFIG_SER_DEFBAUDRATE optional.
43 * Revision 1.6 2004/06/07 15:56:28 aleph
44 * Remove cast-as-lvalue extension abuse
46 * Revision 1.5 2004/06/06 16:41:44 bernie
47 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
49 * Revision 1.4 2004/06/03 11:27:09 bernie
50 * Add dual-license information.
52 * Revision 1.3 2004/06/02 21:35:24 aleph
53 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
55 * Revision 1.2 2004/05/23 18:21:53 bernie
56 * Trim CVS logs and cleanup header info.
60 #include <mware/formatwr.h>
61 #include <drv/kdebug.h>
67 #include <kern/proc.h>
69 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
70 #include <drv/timer.h>
74 /* Serial configuration parameters */
75 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
76 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
77 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
80 struct Serial ser_handles[SER_CNT];
84 * Inserisce il carattere c nel buffer di trasmissione.
85 * Questa funzione mette il processo chiamante in attesa
86 * quando il buffer e' pieno.
88 * \return EOF in caso di errore o timeout, altrimenti
89 * il carattere inviato.
91 int ser_putchar(int c, struct Serial *port)
93 if (fifo_isfull_locked(&port->txfifo))
95 #if CONFIG_SER_TXTIMEOUT != -1
96 time_t start_time = timer_ticks();
99 /* Attende finche' il buffer e' pieno... */
102 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
103 /* Give up timeslice to other processes. */
106 #if CONFIG_SER_TXTIMEOUT != -1
107 if (timer_ticks() - start_time >= port->txtimeout)
109 port->status |= SERRF_TXTIMEOUT;
112 #endif /* CONFIG_SER_TXTIMEOUT */
114 while (fifo_isfull_locked(&port->txfifo));
117 fifo_push_locked(&port->txfifo, (unsigned char)c);
119 /* (re)trigger tx interrupt */
120 port->hw->table->enabletxirq(port->hw);
122 /* Avoid returning signed estended char */
123 return (int)((unsigned char)c);
128 * Preleva un carattere dal buffer di ricezione.
129 * Questa funzione mette il processo chiamante in attesa
130 * quando il buffer e' vuoto. L'attesa ha un timeout
131 * di ser_rxtimeout millisecondi.
133 * \return EOF in caso di errore o timeout, altrimenti
134 * il carattere ricevuto.
136 int ser_getchar(struct Serial *port)
140 if (fifo_isempty_locked(&port->rxfifo))
142 #if CONFIG_SER_RXTIMEOUT != -1
143 time_t start_time = timer_ticks();
145 /* Wait while buffer is empty */
148 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
149 /* Give up timeslice to other processes. */
152 #if CONFIG_SER_RXTIMEOUT != -1
153 if (timer_ticks() - start_time >= port->rxtimeout)
155 port->status |= SERRF_RXTIMEOUT;
158 #endif /* CONFIG_SER_RXTIMEOUT */
160 while (fifo_isempty_locked(&port->rxfifo));
164 * Get a byte from the FIFO (avoiding sign-extension),
165 * re-enable RTS, then return result.
167 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
168 return port->status ? EOF : result;
173 * Preleva un carattere dal buffer di ricezione.
174 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
175 * immediatamente EOF.
177 int ser_getchar_nowait(struct Serial *port)
179 if (fifo_isempty_locked(&port->rxfifo))
182 /* NOTE: the double cast prevents unwanted sign extension */
183 return (int)(unsigned char)fifo_pop(&port->rxfifo);
189 * Read a line long at most as size and puts it
191 * \return number of chars read or EOF in case
194 int ser_gets(struct Serial *port, char *buf, int size)
196 return ser_gets_echo(port, buf, size, false);
201 * Read a line long at most as size and put it
202 * in buf, with optional echo.
204 * \return number of chars read, or EOF in case
207 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
214 if ((c = ser_getchar(port)) == EOF)
221 if (c == '\r' || c == '\n' || i >= size-1)
225 ser_print(port, "\r\n");
230 ser_putchar(c, port);
235 #endif /* !CONFIG_SER_GETS */
239 * Read at most size bytes and puts them
241 * \return number of bytes read or EOF in case
244 int ser_read(struct Serial *port, char *buf, size_t size)
251 if ((c = ser_getchar(port)) == EOF)
261 * Write a string to serial.
262 * \return 0 if OK, EOF in case of error.
264 int ser_print(struct Serial *port, const char *s)
268 if (ser_putchar(*s++, port) == EOF)
276 * \brief Write a buffer to serial.
278 * \return 0 if OK, EOF in case of error.
280 * \todo Optimize with fifo_pushblock()
282 int ser_write(struct Serial *port, const void *_buf, size_t len)
284 const char *buf = _buf;
288 if (ser_putchar(*buf++, port) == EOF)
299 int ser_printf(struct Serial *port, const char *format, ...)
304 ser_setstatus(port, 0);
305 va_start(ap, format);
306 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
311 #endif /* CONFIG_PRINTF */
314 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
315 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
317 port->rxtimeout = rxtimeout;
318 port->txtimeout = txtimeout;
320 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
323 void ser_setbaudrate(struct Serial *port, unsigned long rate)
325 port->hw->table->setbaudrate(port->hw, rate);
329 void ser_setparity(struct Serial *port, int parity)
331 port->hw->table->setparity(port->hw, parity);
336 * Flush both the RX and TX buffers.
338 void ser_purge(struct Serial *ser)
340 fifo_flush_locked(&ser->rxfifo);
341 fifo_flush_locked(&ser->txfifo);
345 * Wait until all pending output is completely
346 * transmitted to the other end.
348 * \note The current implementation only checks the
349 * software transmission queue. Any hardware
352 void ser_drain(struct Serial *ser)
354 while(!fifo_isempty(&ser->txfifo))
356 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
357 /* Give up timeslice to other processes. */
367 struct Serial *ser_open(unsigned int unit)
371 ASSERT(unit < countof(ser_handles));
372 port = &ser_handles[unit];
374 ASSERT(!port->is_open);
375 DB(port->is_open = true;)
379 /* Initialize circular buffer */
380 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
381 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
383 port->hw = ser_hw_getdesc(unit);
384 port->hw->table->init(port->hw, port);
386 /* Set default values */
387 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
388 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
390 #if CONFIG_SER_DEFBAUDRATE
391 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
399 * Clean up serial port, disabling the associated hardware.
401 void ser_close(struct Serial *port)
403 ASSERT(port->is_open);
404 DB(port->is_open = false;)
406 port->hw->table->cleanup(port->hw);