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.5 2004/06/06 16:41:44 bernie
32 * ser_putchar(): Use fifo_push_locked() to fix potential race on 8bit processors.
34 * Revision 1.4 2004/06/03 11:27:09 bernie
35 * Add dual-license information.
37 * Revision 1.3 2004/06/02 21:35:24 aleph
38 * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
40 * Revision 1.2 2004/05/23 18:21:53 bernie
41 * Trim CVS logs and cleanup header info.
45 #include <mware/formatwr.h>
46 #include <drv/kdebug.h>
52 #include <kern/proc.h>
54 #if defined(CONFIG_SER_TXTIMEOUT) || defined(CONFIG_SER_RXTIMEOUT)
55 #include <drv/timer.h>
59 /* Serial configuration parameters */
60 #define SER_CTSDELAY 70 /*!< CTS line retry interval (ms) */
61 #define SER_TXPOLLDELAY 2 /*!< Transmit buffer full retry interval (ms) */
62 #define SER_RXPOLLDELAY 2 /*!< Receive buffer empty retry interval (ms) */
65 struct Serial ser_handles[SER_CNT];
69 * Inserisce il carattere c nel buffer di trasmissione.
70 * Questa funzione mette il processo chiamante in attesa
71 * quando il buffer e' pieno.
73 * \return EOF in caso di errore o timeout, altrimenti
74 * il carattere inviato.
76 int ser_putchar(int c, struct Serial *port)
78 if (fifo_isfull_locked(&port->txfifo))
80 #ifdef CONFIG_SER_TXTIMEOUT
81 time_t start_time = timer_gettick();
84 /* Attende finche' il buffer e' pieno... */
87 #ifdef CONFIG_KERN_SCHED
88 /* Give up timeslice to other processes. */
91 #ifdef CONFIG_SER_TXTIMEOUT
92 if (timer_gettick() - start_time >= port->txtimeout)
94 port->status |= SERRF_TXTIMEOUT;
97 #endif /* CONFIG_SER_TXTIMEOUT */
99 while (fifo_isfull_locked(&port->txfifo));
102 fifo_push_locked(&port->txfifo, (unsigned char)c);
104 /* (re)trigger tx interrupt */
105 port->hw->table->enabletxirq(port->hw);
107 /* Avoid returning signed estended char */
108 return (int)((unsigned char)c);
113 * Preleva un carattere dal buffer di ricezione.
114 * Questa funzione mette il processo chiamante in attesa
115 * quando il buffer e' vuoto. L'attesa ha un timeout
116 * di ser_rxtimeout millisecondi.
118 * \return EOF in caso di errore o timeout, altrimenti
119 * il carattere ricevuto.
121 int ser_getchar(struct Serial *port)
125 if (fifo_isempty_locked(&port->rxfifo))
127 #ifdef CONFIG_SER_RXTIMEOUT
128 time_t start_time = timer_gettick();
130 /* Wait while buffer is empty */
133 #ifdef CONFIG_KERN_SCHED
134 /* Give up timeslice to other processes. */
137 #ifdef CONFIG_SER_RXTIMEOUT
138 if (timer_gettick() - start_time >= port->rxtimeout)
140 port->status |= SERRF_RXTIMEOUT;
143 #endif /* CONFIG_SER_RXTIMEOUT */
145 while (fifo_isempty_locked(&port->rxfifo));
149 * Get a byte from the FIFO (avoiding sign-extension),
150 * re-enable RTS, then return result.
152 result = (int)(unsigned char)fifo_pop(&port->rxfifo);
153 return port->status ? EOF : result;
158 * Preleva un carattere dal buffer di ricezione.
159 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
160 * immediatamente EOF.
162 int ser_getchar_nowait(struct Serial *port)
164 if (fifo_isempty_locked(&port->rxfifo))
167 /* NOTE: the double cast prevents unwanted sign extension */
168 return (int)(unsigned char)fifo_pop(&port->rxfifo);
173 * Read a line long at most as size and puts it
175 * \return number of chars read or EOF in case
178 int ser_gets(struct Serial *port, char *buf, int size)
180 return ser_gets_echo(port, buf, size, false);
185 * Read a line long at most as size and puts it
186 * in buf, with optional echo.
187 * \return number of chars read or EOF in case
190 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
197 if ((c = ser_getchar(port)) == EOF)
200 if (c == '\r' || c == '\n' || i >= size-1)
204 ser_print(port, "\r\n");
209 ser_putchar(c, port);
217 * Read at most size bytes and puts them
219 * \return number of bytes read or EOF in case
222 int ser_read(struct Serial *port, char *buf, size_t size)
229 if ((c = ser_getchar(port)) == EOF)
239 * Write a string to serial.
240 * \return 0 if OK, EOF in case of error.
242 int ser_print(struct Serial *port, const char *s)
246 if (ser_putchar(*s++, port) == EOF)
254 * \brief Write a buffer to serial.
256 * \return 0 if OK, EOF in case of error.
258 int ser_write(struct Serial *port, const void *buf, size_t len)
262 if (ser_putchar(*((const char *)buf)++, port) == EOF)
272 int ser_printf(struct Serial *port, const char *format, ...)
277 ser_setstatus(port, 0);
278 va_start(ap, format);
279 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
285 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
286 void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout)
288 port->rxtimeout = rxtimeout;
289 port->txtimeout = txtimeout;
291 #endif /* defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT) */
294 void ser_setbaudrate(struct Serial *port, unsigned long rate)
296 port->hw->table->setbaudrate(port->hw, rate);
300 void ser_setparity(struct Serial *port, int parity)
302 port->hw->table->setparity(port->hw, parity);
307 * Flush both the RX and TX buffers.
309 void ser_purge(struct Serial *ser)
311 fifo_flush(&ser->rxfifo);
312 fifo_flush(&ser->txfifo);
319 struct Serial *ser_open(unsigned int unit)
323 ASSERT(unit < countof(ser_handles));
325 port = &ser_handles[unit];
327 ASSERT(!port->is_open);
330 port->is_open = true;
332 /* Initialize circular buffer */
333 fifo_init(&port->rxfifo, port->rxbuffer, sizeof(port->rxbuffer));
334 fifo_init(&port->txfifo, port->txbuffer, sizeof(port->txbuffer));
336 port->hw = ser_hw_getdesc(unit);
337 port->hw->table->init(port->hw, port);
339 /* Set default values */
340 #if defined(CONFIG_SER_RXTIMEOUT) || defined(CONFIG_SER_TXTIMEOUT)
341 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
343 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
350 * Clean up serial port, disabling the associated hardware.
352 void ser_close(struct Serial *port)
354 ASSERT(port->is_open);
356 port->is_open = false;
357 port->hw->table->cleanup(port->hw);