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
19 * \li \c CONFIG_SER_HWHANDSHAKE define this preprocessor symbol to enable
20 * support for RTS/CTS handshake. 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.6 2004/06/07 15:56:28 aleph
32 * Remove cast-as-lvalue extension abuse
34 * Revision 1.5 2004/06/06 16:41:44 bernie
35 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
37 * Revision 1.4 2004/06/03 11:27:09 bernie
38 * Add dual-license information.
40 * Revision 1.3 2004/06/02 21:35:24 aleph
41 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
43 * Revision 1.2 2004/05/23 18:21:53 bernie
44 * Trim CVS logs and cleanup header info.
48 #include <mware/formatwr.h>
49 #include <drv/kdebug.h>
55 #include <kern/proc.h>
57 #if defined(CONFIG_SER_TXTIMEOUT) || defined(CONFIG_SER_RXTIMEOUT)
58 #include <drv/timer.h>
62 /* Serial configuration parameters */
63 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
64 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
65 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
68 struct Serial ser_handles[SER_CNT];
72 * Inserisce il carattere c nel buffer di trasmissione.
73 * Questa funzione mette il processo chiamante in attesa
74 * quando il buffer e' pieno.
76 * \return EOF in caso di errore o timeout, altrimenti
77 * il carattere inviato.
79 int ser_putchar(int c, struct Serial *port)
81 if (fifo_isfull_locked(&port->txfifo))
83 #ifdef CONFIG_SER_TXTIMEOUT
84 time_t start_time = timer_gettick();
87 /* Attende finche' il buffer e' pieno... */
90 #ifdef CONFIG_KERN_SCHED
91 /* Give up timeslice to other processes. */
94 #ifdef CONFIG_SER_TXTIMEOUT
95 if (timer_gettick() - start_time >= port->txtimeout)
97 port->status |= SERRF_TXTIMEOUT;
100 #endif /* CONFIG_SER_TXTIMEOUT */
102 while (fifo_isfull_locked(&port->txfifo));
105 fifo_push_locked(&port->txfifo, (unsigned char)c);
107 /* (re)trigger tx interrupt */
108 port->hw->table->enabletxirq(port->hw);
110 /* Avoid returning signed estended char */
111 return (int)((unsigned char)c);
116 * Preleva un carattere dal buffer di ricezione.
117 * Questa funzione mette il processo chiamante in attesa
118 * quando il buffer e' vuoto. L'attesa ha un timeout
119 * di ser_rxtimeout millisecondi.
121 * \return EOF in caso di errore o timeout, altrimenti
122 * il carattere ricevuto.
124 int ser_getchar(struct Serial *port)
128 if (fifo_isempty_locked(&port->rxfifo))
130 #ifdef CONFIG_SER_RXTIMEOUT
131 time_t start_time = timer_gettick();
133 /* Wait while buffer is empty */
136 #ifdef CONFIG_KERN_SCHED
137 /* Give up timeslice to other processes. */
140 #ifdef CONFIG_SER_RXTIMEOUT
141 if (timer_gettick() - start_time >= port->rxtimeout)
143 port->status |= SERRF_RXTIMEOUT;
146 #endif /* CONFIG_SER_RXTIMEOUT */
148 while (fifo_isempty_locked(&port->rxfifo));
152 * Get a byte from the FIFO (avoiding sign-extension),
153 * re-enable RTS, then return result.
155 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
156 return port->status ? EOF : result;
161 * Preleva un carattere dal buffer di ricezione.
162 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
163 * immediatamente EOF.
165 int ser_getchar_nowait(struct Serial *port)
167 if (fifo_isempty_locked(&port->rxfifo))
170 /* NOTE: the double cast prevents unwanted sign extension */
171 return (int)(unsigned char)fifo_pop(&port->rxfifo);
176 * Read a line long at most as size and puts it
178 * \return number of chars read or EOF in case
181 int ser_gets(struct Serial *port, char *buf, int size)
183 return ser_gets_echo(port, buf, size, false);
188 * Read a line long at most as size and puts it
189 * in buf, with optional echo.
190 * \return number of chars read or EOF in case
193 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
200 if ((c = ser_getchar(port)) == EOF)
203 if (c == '\r' || c == '\n' || i >= size-1)
207 ser_print(port, "\r\n");
212 ser_putchar(c, port);
220 * Read at most size bytes and puts them
222 * \return number of bytes read or EOF in case
225 int ser_read(struct Serial *port, char *buf, size_t size)
232 if ((c = ser_getchar(port)) == EOF)
242 * Write a string to serial.
243 * \return 0 if OK, EOF in case of error.
245 int ser_print(struct Serial *port, const char *s)
249 if (ser_putchar(*s++, port) == EOF)
257 * \brief Write a buffer to serial.
259 * \return 0 if OK, EOF in case of error.
261 * \todo Optimize with fifo_pushblock()
263 int ser_write(struct Serial *port, const void *_buf, size_t len)
265 const char *buf = _buf;
269 if (ser_putchar(*buf++, port) == EOF)
279 int ser_printf(struct Serial *port, const char *format, ...)
284 ser_setstatus(port, 0);
285 va_start(ap, format);
286 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
292 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
293 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
295 port->rxtimeout = rxtimeout;
296 port->txtimeout = txtimeout;
298 #endif /* defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT) */
301 void ser_setbaudrate(struct Serial *port, unsigned long rate)
303 port->hw->table->setbaudrate(port->hw, rate);
307 void ser_setparity(struct Serial *port, int parity)
309 port->hw->table->setparity(port->hw, parity);
314 * Flush both the RX and TX buffers.
316 void ser_purge(struct Serial *ser)
318 fifo_flush(&ser->rxfifo);
319 fifo_flush(&ser->txfifo);
326 struct Serial *ser_open(unsigned int unit)
330 ASSERT(unit < countof(ser_handles));
332 port = &ser_handles[unit];
334 ASSERT(!port->is_open);
337 port->is_open = true;
339 /* Initialize circular buffer */
340 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
341 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
343 port->hw = ser_hw_getdesc(unit);
344 port->hw->table->init(port->hw, port);
346 /* Set default values */
347 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
348 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
350 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
357 * Clean up serial port, disabling the associated hardware.
359 void ser_close(struct Serial *port)
361 ASSERT(port->is_open);
363 port->is_open = false;
364 port->hw->table->cleanup(port->hw);