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.12 2004/08/24 13:49:39 bernie
34 * Revision 1.11 2004/08/15 05:32:22 bernie
35 * ser_resync(): New function.
37 * Revision 1.10 2004/08/10 06:29:50 bernie
38 * Rename timer_gettick() to timer_ticks().
40 * Revision 1.9 2004/08/08 06:06:20 bernie
41 * Use new-style CONFIG_ idiom; Fix module-wide documentation.
43 * Revision 1.8 2004/07/29 22:57:09 bernie
44 * ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
46 * Revision 1.7 2004/07/18 21:49:03 bernie
47 * Make CONFIG_SER_DEFBAUDRATE optional.
49 * Revision 1.6 2004/06/07 15:56:28 aleph
50 * Remove cast-as-lvalue extension abuse
52 * Revision 1.5 2004/06/06 16:41:44 bernie
53 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
55 * Revision 1.4 2004/06/03 11:27:09 bernie
56 * Add dual-license information.
58 * Revision 1.3 2004/06/02 21:35:24 aleph
59 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
61 * Revision 1.2 2004/05/23 18:21:53 bernie
62 * Trim CVS logs and cleanup header info.
66 #include <mware/formatwr.h>
67 #include <drv/kdebug.h>
73 #include <kern/proc.h>
75 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
76 #include <drv/timer.h>
80 /* Serial configuration parameters */
81 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
82 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
83 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
86 struct Serial ser_handles[SER_CNT];
90 * Inserisce il carattere c nel buffer di trasmissione.
91 * Questa funzione mette il processo chiamante in attesa
92 * quando il buffer e' pieno.
94 * \return EOF in caso di errore o timeout, altrimenti
95 * il carattere inviato.
97 int ser_putchar(int c, struct Serial *port)
99 if (fifo_isfull_locked(&port->txfifo))
101 #if CONFIG_SER_TXTIMEOUT != -1
102 time_t start_time = timer_ticks();
105 /* Attende finche' il buffer e' pieno... */
108 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
109 /* Give up timeslice to other processes. */
112 #if CONFIG_SER_TXTIMEOUT != -1
113 if (timer_ticks() - start_time >= port->txtimeout)
115 port->status |= SERRF_TXTIMEOUT;
118 #endif /* CONFIG_SER_TXTIMEOUT */
120 while (fifo_isfull_locked(&port->txfifo));
123 fifo_push_locked(&port->txfifo, (unsigned char)c);
125 /* (re)trigger tx interrupt */
126 port->hw->table->enabletxirq(port->hw);
128 /* Avoid returning signed estended char */
129 return (int)((unsigned char)c);
134 * Preleva un carattere dal buffer di ricezione.
135 * Questa funzione mette il processo chiamante in attesa
136 * quando il buffer e' vuoto. L'attesa ha un timeout
137 * di ser_rxtimeout millisecondi.
139 * \return EOF in caso di errore o timeout, altrimenti
140 * il carattere ricevuto.
142 int ser_getchar(struct Serial *port)
146 if (fifo_isempty_locked(&port->rxfifo))
148 #if CONFIG_SER_RXTIMEOUT != -1
149 time_t start_time = timer_ticks();
151 /* Wait while buffer is empty */
154 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
155 /* Give up timeslice to other processes. */
158 #if CONFIG_SER_RXTIMEOUT != -1
159 if (timer_ticks() - start_time >= port->rxtimeout)
161 port->status |= SERRF_RXTIMEOUT;
164 #endif /* CONFIG_SER_RXTIMEOUT */
166 while (fifo_isempty_locked(&port->rxfifo));
170 * Get a byte from the FIFO (avoiding sign-extension),
171 * re-enable RTS, then return result.
173 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
174 return port->status ? EOF : result;
179 * Preleva un carattere dal buffer di ricezione.
180 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
181 * immediatamente EOF.
183 int ser_getchar_nowait(struct Serial *port)
185 if (fifo_isempty_locked(&port->rxfifo))
188 /* NOTE: the double cast prevents unwanted sign extension */
189 return (int)(unsigned char)fifo_pop(&port->rxfifo);
195 * Read a line long at most as size and puts it
197 * \return number of chars read or EOF in case
200 int ser_gets(struct Serial *port, char *buf, int size)
202 return ser_gets_echo(port, buf, size, false);
207 * Read a line long at most as size and put it
208 * in buf, with optional echo.
210 * \return number of chars read, or EOF in case
213 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
220 if ((c = ser_getchar(port)) == EOF)
227 if (c == '\r' || c == '\n' || i >= size-1)
231 ser_print(port, "\r\n");
236 ser_putchar(c, port);
241 #endif /* !CONFIG_SER_GETS */
245 * Read at most size bytes and puts them
247 * \return number of bytes read or EOF in case
250 int ser_read(struct Serial *port, char *buf, size_t size)
257 if ((c = ser_getchar(port)) == EOF)
267 * Write a string to serial.
268 * \return 0 if OK, EOF in case of error.
270 int ser_print(struct Serial *port, const char *s)
274 if (ser_putchar(*s++, port) == EOF)
282 * \brief Write a buffer to serial.
284 * \return 0 if OK, EOF in case of error.
286 * \todo Optimize with fifo_pushblock()
288 int ser_write(struct Serial *port, const void *_buf, size_t len)
290 const char *buf = _buf;
294 if (ser_putchar(*buf++, port) == EOF)
305 int ser_printf(struct Serial *port, const char *format, ...)
310 ser_setstatus(port, 0);
311 va_start(ap, format);
312 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
317 #endif /* CONFIG_PRINTF */
320 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
321 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
323 port->rxtimeout = rxtimeout;
324 port->txtimeout = txtimeout;
326 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
328 #if CONFIG_SER_RXTIMEOUT != -1
330 * Discard input to resynchronize with remote end
332 * Discard incoming data until the port stops receiving
333 * characters for at least \a delay milliseconds.
335 * \note Serial errors are reset before and after executing the purge.
337 void ser_resync(struct Serial *port, time_t delay)
339 time_t old_rxtimeout = port->rxtimeout;
341 ser_settimeouts(delay, ser->txtimeout);
344 ser_setstatus(port, 0);
347 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
349 /* Restore port to an usable status */
350 ser_setstatus(port, 0);
351 ser_settimeouts(old_rxtimeout, ser->txtimeout);
353 #endif /* CONFIG_SER_RXTIMEOUT */
356 void ser_setbaudrate(struct Serial *port, unsigned long rate)
358 port->hw->table->setbaudrate(port->hw, rate);
362 void ser_setparity(struct Serial *port, int parity)
364 port->hw->table->setparity(port->hw, parity);
369 * Flush both the RX and TX buffers.
371 void ser_purge(struct Serial *port)
373 fifo_flush_locked(&port->rxfifo);
374 fifo_flush_locked(&port->txfifo);
379 * Wait until all pending output is completely
380 * transmitted to the other end.
382 * \note The current implementation only checks the
383 * software transmission queue. Any hardware
386 void ser_drain(struct Serial *ser)
388 while (!fifo_isempty(&ser->txfifo))
390 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
391 /* Give up timeslice to other processes. */
401 struct Serial *ser_open(unsigned int unit)
405 ASSERT(unit < countof(ser_handles));
406 port = &ser_handles[unit];
408 ASSERT(!port->is_open);
409 DB(port->is_open = true;)
413 /* Initialize circular buffer */
414 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
415 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
417 port->hw = ser_hw_getdesc(unit);
418 port->hw->table->init(port->hw, port);
420 /* Set default values */
421 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
422 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
424 #if CONFIG_SER_DEFBAUDRATE
425 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
433 * Clean up serial port, disabling the associated hardware.
435 void ser_close(struct Serial *port)
437 ASSERT(port->is_open);
438 DB(port->is_open = false;)
440 port->hw->table->cleanup(port->hw);