4 * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
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.2 2004/05/23 18:21:53 bernie
32 * Trim CVS logs and cleanup header info.
36 #include <mware/formatwr.h>
37 #include <drv/kdebug.h>
43 #include <kern/proc.h>
45 #if defined(CONFIG_SER_TXTIMEOUT) || defined(CONFIG_SER_RXTIMEOUT)
46 #include <drv/timer.h>
50 /* Serial configuration parameters */
51 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
52 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
53 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
56 struct Serial ser_handles[SER_CNT];
60 * Inserisce il carattere c nel buffer di trasmissione.
61 * Questa funzione mette il processo chiamante in attesa
62 * quando il buffer e' pieno.
64 * \return EOF in caso di errore o timeout, altrimenti
65 * il carattere inviato.
67 int ser_putchar(int c, struct Serial *port)
69 if (fifo_isfull_locked(&port->txfifo))
71 #ifdef CONFIG_SER_TXTIMEOUT
72 time_t start_time = timer_gettick();
75 /* Attende finche' il buffer e' pieno... */
78 #ifdef CONFIG_KERN_SCHED
79 /* Give up timeslice to other processes. */
82 #ifdef CONFIG_SER_TXTIMEOUT
83 if (timer_gettick() - start_time >= port->txtimeout)
85 port->status |= SERRF_TXTIMEOUT;
88 #endif /* CONFIG_SER_TXTIMEOUT */
90 while (fifo_isfull_locked(&port->txfifo));
93 fifo_push(&port->txfifo, (unsigned char)c);
95 /* (re)trigger tx interrupt */
96 port->hw->table->enabletxirq(port->hw);
98 /* Avoid returning signed estended char */
99 return (int)((unsigned char)c);
104 * Preleva un carattere dal buffer di ricezione.
105 * Questa funzione mette il processo chiamante in attesa
106 * quando il buffer e' vuoto. L'attesa ha un timeout
107 * di ser_rxtimeout millisecondi.
109 * \return EOF in caso di errore o timeout, altrimenti
110 * il carattere ricevuto.
112 int ser_getchar(struct Serial *port)
116 if (fifo_isempty_locked(&port->rxfifo))
118 #ifdef CONFIG_SER_RXTIMEOUT
119 time_t start_time = timer_gettick();
121 /* Wait while buffer is empty */
124 #ifdef CONFIG_KERN_SCHED
125 /* Give up timeslice to other processes. */
128 #ifdef CONFIG_SER_RXTIMEOUT
129 if (timer_gettick() - start_time >= port->rxtimeout)
131 port->status |= SERRF_RXTIMEOUT;
134 #endif /* CONFIG_SER_RXTIMEOUT */
136 while (fifo_isempty_locked(&port->rxfifo));
140 * Get a byte from the FIFO (avoiding sign-extension),
141 * re-enable RTS, then return result.
143 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
144 return port->status ? EOF : result;
149 * Preleva un carattere dal buffer di ricezione.
150 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
151 * immediatamente EOF.
153 int ser_getchar_nowait(struct Serial *port)
155 if (fifo_isempty_locked(&port->rxfifo))
158 /* NOTE: the double cast prevents unwanted sign extension */
159 return (int)(unsigned char)fifo_pop(&port->rxfifo);
164 * Read a line long at most as size and puts it
166 * \return number of chars read or EOF in case
169 int ser_gets(struct Serial *port, char *buf, int size)
171 return ser_gets_echo(port, buf, size, false);
176 * Read a line long at most as size and puts it
177 * in buf, with optional echo.
178 * \return number of chars read or EOF in case
181 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
188 if ((c = ser_getchar(port)) == EOF)
191 if (c == '\r' || c == '\n' || i >= size-1)
195 ser_print(port, "\r\n");
200 ser_putchar(c, port);
208 * Read at most size bytes and puts them
210 * \return number of bytes read or EOF in case
213 int ser_read(struct Serial *port, char *buf, size_t size)
220 if ((c = ser_getchar(port)) == EOF)
230 * Write a string to serial.
231 * \return 0 if OK, EOF in case of error.
233 int ser_print(struct Serial *port, const char *s)
237 if (ser_putchar(*s++, port) == EOF)
245 * \brief Write a buffer to serial.
247 * \return 0 if OK, EOF in case of error.
249 int ser_write(struct Serial *port, const void *buf, size_t len)
253 if (ser_putchar(*((const char *)buf)++, port) == EOF)
263 int ser_printf(struct Serial *port, const char *format, ...)
268 ser_setstatus(port, 0);
269 va_start(ap, format);
270 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
276 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
277 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
279 port->rxtimeout = rxtimeout;
280 port->txtimeout = txtimeout;
282 #endif /* defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT) */
285 void ser_setbaudrate(struct Serial *port, unsigned long rate)
287 port->hw->table->setbaudrate(port->hw, rate);
291 void ser_setparity(struct Serial *port, int parity)
293 port->hw->table->setparity(port->hw, parity);
298 * Flush both the RX and TX buffers.
300 void ser_purge(struct Serial *ser)
302 fifo_flush(&ser->rxfifo);
303 fifo_flush(&ser->txfifo);
310 struct Serial *ser_open(unsigned int unit)
314 ASSERT(unit < countof(ser_handles));
316 port = &ser_handles[unit];
318 ASSERT(!port->is_open);
321 port->is_open = true;
323 /* Initialize circular buffer */
324 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
325 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
327 port->hw = ser_hw_getdesc(unit);
328 port->hw->table->init(port->hw, port);
330 /* Set default values */
331 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
332 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
334 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
341 * Clean up serial port, disabling the associated hardware.
343 void ser_close(struct Serial *port)
345 ASSERT(port->is_open);
347 port->is_open = false;
348 port->hw->table->cleanup(port->hw);