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.13 2004/08/24 16:20:48 bernie
32 * ser_read(): Make buffer argument void * for consistency with ANSI C and ser_write()
34 * Revision 1.12 2004/08/24 13:49:39 bernie
37 * Revision 1.11 2004/08/15 05:32:22 bernie
38 * ser_resync(): New function.
40 * Revision 1.10 2004/08/10 06:29:50 bernie
41 * Rename timer_gettick() to timer_ticks().
43 * Revision 1.9 2004/08/08 06:06:20 bernie
44 * Use new-style CONFIG_ idiom; Fix module-wide documentation.
46 * Revision 1.8 2004/07/29 22:57:09 bernie
47 * ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
49 * Revision 1.7 2004/07/18 21:49:03 bernie
50 * Make CONFIG_SER_DEFBAUDRATE optional.
52 * Revision 1.6 2004/06/07 15:56:28 aleph
53 * Remove cast-as-lvalue extension abuse
55 * Revision 1.5 2004/06/06 16:41:44 bernie
56 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
58 * Revision 1.4 2004/06/03 11:27:09 bernie
59 * Add dual-license information.
61 * Revision 1.3 2004/06/02 21:35:24 aleph
62 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
64 * Revision 1.2 2004/05/23 18:21:53 bernie
65 * Trim CVS logs and cleanup header info.
69 #include <mware/formatwr.h>
70 #include <drv/kdebug.h>
76 #include <kern/proc.h>
78 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
79 #include <drv/timer.h>
83 /* Serial configuration parameters */
84 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
85 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
86 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
89 struct Serial ser_handles[SER_CNT];
93 * Inserisce il carattere c nel buffer di trasmissione.
94 * Questa funzione mette il processo chiamante in attesa
95 * quando il buffer e' pieno.
97 * \return EOF in caso di errore o timeout, altrimenti
98 * il carattere inviato.
100 int ser_putchar(int c, struct Serial *port)
102 if (fifo_isfull_locked(&port->txfifo))
104 #if CONFIG_SER_TXTIMEOUT != -1
105 time_t start_time = timer_ticks();
108 /* Attende finche' il buffer e' pieno... */
111 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
112 /* Give up timeslice to other processes. */
115 #if CONFIG_SER_TXTIMEOUT != -1
116 if (timer_ticks() - start_time >= port->txtimeout)
118 port->status |= SERRF_TXTIMEOUT;
121 #endif /* CONFIG_SER_TXTIMEOUT */
123 while (fifo_isfull_locked(&port->txfifo));
126 fifo_push_locked(&port->txfifo, (unsigned char)c);
128 /* (re)trigger tx interrupt */
129 port->hw->table->enabletxirq(port->hw);
131 /* Avoid returning signed estended char */
132 return (int)((unsigned char)c);
137 * Preleva un carattere dal buffer di ricezione.
138 * Questa funzione mette il processo chiamante in attesa
139 * quando il buffer e' vuoto. L'attesa ha un timeout
140 * di ser_rxtimeout millisecondi.
142 * \return EOF in caso di errore o timeout, altrimenti
143 * il carattere ricevuto.
145 int ser_getchar(struct Serial *port)
149 if (fifo_isempty_locked(&port->rxfifo))
151 #if CONFIG_SER_RXTIMEOUT != -1
152 time_t start_time = timer_ticks();
154 /* Wait while buffer is empty */
157 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
158 /* Give up timeslice to other processes. */
161 #if CONFIG_SER_RXTIMEOUT != -1
162 if (timer_ticks() - start_time >= port->rxtimeout)
164 port->status |= SERRF_RXTIMEOUT;
167 #endif /* CONFIG_SER_RXTIMEOUT */
169 while (fifo_isempty_locked(&port->rxfifo));
173 * Get a byte from the FIFO (avoiding sign-extension),
174 * re-enable RTS, then return result.
176 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
177 return port->status ? EOF : result;
182 * Preleva un carattere dal buffer di ricezione.
183 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
184 * immediatamente EOF.
186 int ser_getchar_nowait(struct Serial *port)
188 if (fifo_isempty_locked(&port->rxfifo))
191 /* NOTE: the double cast prevents unwanted sign extension */
192 return (int)(unsigned char)fifo_pop(&port->rxfifo);
198 * Read a line long at most as size and puts it
200 * \return number of chars read or EOF in case
203 int ser_gets(struct Serial *port, char *buf, int size)
205 return ser_gets_echo(port, buf, size, false);
210 * Read a line long at most as size and put it
211 * in buf, with optional echo.
213 * \return number of chars read, or EOF in case
216 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
223 if ((c = ser_getchar(port)) == EOF)
230 if (c == '\r' || c == '\n' || i >= size-1)
234 ser_print(port, "\r\n");
239 ser_putchar(c, port);
244 #endif /* !CONFIG_SER_GETS */
248 * Read at most size bytes and puts them
250 * \return number of bytes read or EOF in case
253 int ser_read(struct Serial *port, void *buf, size_t size)
260 if ((c = ser_getchar(port)) == EOF)
270 * Write a string to serial.
271 * \return 0 if OK, EOF in case of error.
273 int ser_print(struct Serial *port, const char *s)
277 if (ser_putchar(*s++, port) == EOF)
285 * \brief Write a buffer to serial.
287 * \return 0 if OK, EOF in case of error.
289 * \todo Optimize with fifo_pushblock()
291 int ser_write(struct Serial *port, const void *_buf, size_t len)
293 const char *buf = _buf;
297 if (ser_putchar(*buf++, port) == EOF)
308 int ser_printf(struct Serial *port, const char *format, ...)
313 ser_setstatus(port, 0);
314 va_start(ap, format);
315 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
320 #endif /* CONFIG_PRINTF */
323 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
324 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
326 port->rxtimeout = rxtimeout;
327 port->txtimeout = txtimeout;
329 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
331 #if CONFIG_SER_RXTIMEOUT != -1
333 * Discard input to resynchronize with remote end
335 * Discard incoming data until the port stops receiving
336 * characters for at least \a delay milliseconds.
338 * \note Serial errors are reset before and after executing the purge.
340 void ser_resync(struct Serial *port, time_t delay)
342 time_t old_rxtimeout = port->rxtimeout;
344 ser_settimeouts(delay, ser->txtimeout);
347 ser_setstatus(port, 0);
350 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
352 /* Restore port to an usable status */
353 ser_setstatus(port, 0);
354 ser_settimeouts(old_rxtimeout, ser->txtimeout);
356 #endif /* CONFIG_SER_RXTIMEOUT */
359 void ser_setbaudrate(struct Serial *port, unsigned long rate)
361 port->hw->table->setbaudrate(port->hw, rate);
365 void ser_setparity(struct Serial *port, int parity)
367 port->hw->table->setparity(port->hw, parity);
372 * Flush both the RX and TX buffers.
374 void ser_purge(struct Serial *port)
376 fifo_flush_locked(&port->rxfifo);
377 fifo_flush_locked(&port->txfifo);
382 * Wait until all pending output is completely
383 * transmitted to the other end.
385 * \note The current implementation only checks the
386 * software transmission queue. Any hardware
389 void ser_drain(struct Serial *ser)
391 while (!fifo_isempty(&ser->txfifo))
393 #if defined(CONFIG_KERN_SCHED) && CONFIG_KERN_SCHED
394 /* Give up timeslice to other processes. */
404 struct Serial *ser_open(unsigned int unit)
408 ASSERT(unit < countof(ser_handles));
409 port = &ser_handles[unit];
411 ASSERT(!port->is_open);
412 DB(port->is_open = true;)
416 /* Initialize circular buffer */
417 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
418 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
420 port->hw = ser_hw_getdesc(unit);
421 port->hw->table->init(port->hw, port);
423 /* Set default values */
424 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
425 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
427 #if CONFIG_SER_DEFBAUDRATE
428 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
436 * Clean up serial port, disabling the associated hardware.
438 void ser_close(struct Serial *port)
440 ASSERT(port->is_open);
441 DB(port->is_open = false;)
443 port->hw->table->cleanup(port->hw);