4 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
5 * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
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.1 2004/05/23 18:10:11 bernie
32 * Import drv/ modules.
34 * Revision 1.5 2004/05/14 12:47:26 rasky
35 * Importato nuovo supporto seriale per AVR da Stefano
37 * Revision 1.4 2004/04/29 16:40:23 rasky
38 * Durante i busy loop della seriale, chiama la proc_switch() per cambiare processo
40 * Revision 1.3 2004/04/21 17:38:24 rasky
43 * Revision 1.16 2004/04/03 18:30:49 aleph
44 * Move timeout defines in config, private define in .c
46 * Revision 1.15 2004/03/29 17:01:02 aleph
47 * Add function to set serial parity, fix it when ser_open is used
49 * Revision 1.14 2004/03/29 16:19:33 aleph
50 * Add ser_cleanup function; Various code improvements
52 * Revision 1.13 2004/03/24 15:48:53 bernie
53 * Remove Copyright messages from Doxygen output
56 #include <mware/formatwr.h>
57 #include <drv/kdebug.h>
63 #include <kern/proc.h>
65 #if defined(CONFIG_SER_TXTIMEOUT) || defined(CONFIG_SER_RXTIMEOUT)
66 #include <drv/timer.h>
70 /* Serial configuration parameters */
71 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
72 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
73 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
76 struct Serial ser_handles[SER_CNT];
80 * Inserisce il carattere c nel buffer di trasmissione.
81 * Questa funzione mette il processo chiamante in attesa
82 * quando il buffer e' pieno.
84 * \return EOF in caso di errore o timeout, altrimenti
85 * il carattere inviato.
87 int ser_putchar(int c, struct Serial *port)
89 if (fifo_isfull_locked(&port->txfifo))
91 #ifdef CONFIG_SER_TXTIMEOUT
92 time_t start_time = timer_gettick();
95 /* Attende finche' il buffer e' pieno... */
98 #ifdef CONFIG_KERN_SCHED
99 /* Give up timeslice to other processes. */
102 #ifdef CONFIG_SER_TXTIMEOUT
103 if (timer_gettick() - start_time >= port->txtimeout)
105 port->status |= SERRF_TXTIMEOUT;
108 #endif /* CONFIG_SER_TXTIMEOUT */
110 while (fifo_isfull_locked(&port->txfifo));
113 fifo_push(&port->txfifo, (unsigned char)c);
115 /* (re)trigger tx interrupt */
116 port->hw->table->enabletxirq(port->hw);
118 /* Avoid returning signed estended char */
119 return (int)((unsigned char)c);
124 * Preleva un carattere dal buffer di ricezione.
125 * Questa funzione mette il processo chiamante in attesa
126 * quando il buffer e' vuoto. L'attesa ha un timeout
127 * di ser_rxtimeout millisecondi.
129 * \return EOF in caso di errore o timeout, altrimenti
130 * il carattere ricevuto.
132 int ser_getchar(struct Serial *port)
136 if (fifo_isempty_locked(&port->rxfifo))
138 #ifdef CONFIG_SER_RXTIMEOUT
139 time_t start_time = timer_gettick();
141 /* Wait while buffer is empty */
144 #ifdef CONFIG_KERN_SCHED
145 /* Give up timeslice to other processes. */
148 #ifdef CONFIG_SER_RXTIMEOUT
149 if (timer_gettick() - start_time >= port->rxtimeout)
151 port->status |= SERRF_RXTIMEOUT;
154 #endif /* CONFIG_SER_RXTIMEOUT */
156 while (fifo_isempty_locked(&port->rxfifo));
160 * Get a byte from the FIFO (avoiding sign-extension),
161 * re-enable RTS, then return result.
163 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
164 return port->status ? EOF : result;
169 * Preleva un carattere dal buffer di ricezione.
170 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
171 * immediatamente EOF.
173 int ser_getchar_nowait(struct Serial *port)
175 if (fifo_isempty_locked(&port->rxfifo))
178 /* NOTE: the double cast prevents unwanted sign extension */
179 return (int)(unsigned char)fifo_pop(&port->rxfifo);
184 * Read a line long at most as size and puts it
186 * \return number of chars read or EOF in case
189 int ser_gets(struct Serial *port, char *buf, int size)
191 return ser_gets_echo(port, buf, size, false);
196 * Read a line long at most as size and puts it
197 * in buf, with optional echo.
198 * \return number of chars read or EOF in case
201 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
208 if ((c = ser_getchar(port)) == EOF)
211 if (c == '\r' || c == '\n' || i >= size-1)
215 ser_print(port, "\r\n");
220 ser_putchar(c, port);
228 * Read at most size bytes and puts them
230 * \return number of bytes read or EOF in case
233 int ser_read(struct Serial *port, char *buf, size_t size)
240 if ((c = ser_getchar(port)) == EOF)
250 * Write a string to serial.
251 * \return 0 if OK, EOF in case of error.
253 int ser_print(struct Serial *port, const char *s)
257 if (ser_putchar(*s++, port) == EOF)
265 * \brief Write a buffer to serial.
267 * \return 0 if OK, EOF in case of error.
269 int ser_write(struct Serial *port, const void *buf, size_t len)
273 if (ser_putchar(*((const char *)buf)++, port) == EOF)
283 int ser_printf(struct Serial *port, const char *format, ...)
288 ser_setstatus(port, 0);
289 va_start(ap, format);
290 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
296 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
297 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
299 port->rxtimeout = rxtimeout;
300 port->txtimeout = txtimeout;
302 #endif /* defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT) */
305 void ser_setbaudrate(struct Serial *port, unsigned long rate)
307 port->hw->table->setbaudrate(port->hw, rate);
311 void ser_setparity(struct Serial *port, int parity)
313 port->hw->table->setparity(port->hw, parity);
318 * Flush both the RX and TX buffers.
320 void ser_purge(struct Serial *ser)
322 fifo_flush(&ser->rxfifo);
323 fifo_flush(&ser->txfifo);
330 struct Serial *ser_open(unsigned int unit)
334 ASSERT(unit < countof(ser_handles));
336 port = &ser_handles[unit];
338 ASSERT(!port->is_open);
341 port->is_open = true;
343 /* Initialize circular buffer */
344 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
345 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
347 port->hw = ser_hw_getdesc(unit);
348 port->hw->table->init(port->hw, port);
350 /* Set default values */
351 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
352 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
354 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
361 * Clean up serial port, disabling the associated hardware.
363 void ser_close(struct Serial *port)
365 ASSERT(port->is_open);
367 port->is_open = false;
368 port->hw->table->cleanup(port->hw);