4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
34 * \brief Buffered serial I/O driver
36 * The serial rx interrupt buffers incoming data in a software FIFO
37 * to decouple the higher level protocols from the line speed.
38 * Outgoing data is buffered as well for better performance.
39 * This driver is not optimized for best performance, but it
40 * has proved to be fast enough to handle transfer rates up to
41 * 38400bps on a 16MHz 80196.
43 * MODULE CONFIGURATION
45 * \li \c CONFIG_SER_HWHANDSHAKE - set to 1 to enable RTS/CTS handshake.
46 * Support is incomplete/untested.
47 * \li \c CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
51 * \author Bernardo Innocenti <bernie@develer.com>
59 #include <mware/formatwr.h>
60 #include <cfg/debug.h>
61 #include <appconfig.h>
64 * Sanity check for config parameters required by this module.
66 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
67 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
69 #if !defined(CONFIG_SER_RXTIMEOUT)
70 #error CONFIG_SER_TXTIMEOUT missing in config.h
72 #if !defined(CONFIG_SER_RXTIMEOUT)
73 #error CONFIG_SER_RXTIMEOUT missing in config.h
75 #if !defined(CONFIG_SER_GETS) || ((CONFIG_SER_GETS != 0) && CONFIG_SER_GETS != 1)
76 #error CONFIG_SER_GETS must be set to either 0 or 1 in config.h
78 #if !defined(CONFIG_SER_DEFBAUDRATE)
79 #error CONFIG_SER_DEFBAUDRATE missing in config.h
81 #if !defined(CONFIG_PRINTF)
82 #error CONFIG_PRINTF missing in config.h
86 #include <kern/proc.h>
89 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
90 #include <drv/timer.h>
94 /* Serial configuration parameters */
95 #define SER_CTSDELAY 70 /**< CTS line retry interval (ms) */
96 #define SER_TXPOLLDELAY 2 /**< Transmit buffer full retry interval (ms) */
97 #define SER_RXPOLLDELAY 2 /**< Receive buffer empty retry interval (ms) */
100 struct Serial ser_handles[SER_CNT];
104 * Inserisce il carattere c nel buffer di trasmissione.
105 * Questa funzione mette il processo chiamante in attesa
106 * quando il buffer e' pieno.
108 * \return EOF in caso di errore o timeout, altrimenti
109 * il carattere inviato.
111 int ser_putchar(int c, struct Serial *port)
113 //ASSERT_VALID_FIFO(&port->txfifo);
114 if (fifo_isfull_locked(&port->txfifo))
116 #if CONFIG_SER_TXTIMEOUT != -1
117 ticks_t start_time = timer_clock();
120 /* Attende finche' il buffer e' pieno... */
124 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
125 /* Give up timeslice to other processes. */
128 #if CONFIG_SER_TXTIMEOUT != -1
129 if (timer_clock() - start_time >= port->txtimeout)
131 ATOMIC(port->status |= SERRF_TXTIMEOUT);
134 #endif /* CONFIG_SER_TXTIMEOUT */
136 while (fifo_isfull_locked(&port->txfifo));
139 fifo_push_locked(&port->txfifo, (unsigned char)c);
141 /* (re)trigger tx interrupt */
142 port->hw->table->txStart(port->hw);
144 /* Avoid returning signed extended char */
145 return (int)((unsigned char)c);
150 * Preleva un carattere dal buffer di ricezione.
151 * Questa funzione mette il processo chiamante in attesa
152 * quando il buffer e' vuoto. L'attesa ha un timeout
153 * di ser_rxtimeout millisecondi.
155 * \return EOF in caso di errore o timeout, altrimenti
156 * il carattere ricevuto.
158 int ser_getchar(struct Serial *port)
160 if (fifo_isempty_locked(&port->rxfifo))
162 #if CONFIG_SER_RXTIMEOUT != -1
163 ticks_t start_time = timer_clock();
165 /* Wait while buffer is empty */
169 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
170 /* Give up timeslice to other processes. */
173 #if CONFIG_SER_RXTIMEOUT != -1
174 if (timer_clock() - start_time >= port->rxtimeout)
176 ATOMIC(port->status |= SERRF_RXTIMEOUT);
179 #endif /* CONFIG_SER_RXTIMEOUT */
181 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
185 * Get a byte from the FIFO (avoiding sign-extension),
186 * re-enable RTS, then return result.
188 if (ser_getstatus(port) & SERRF_RX)
190 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
195 * Preleva un carattere dal buffer di ricezione.
196 * Se il buffer e' vuoto, ser_getchar_nowait() ritorna
197 * immediatamente EOF.
199 int ser_getchar_nowait(struct Serial *port)
201 if (fifo_isempty_locked(&port->rxfifo))
204 /* NOTE: the double cast prevents unwanted sign extension */
205 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
211 * Read a line long at most as size and put it
213 * \return number of chars read or EOF in case
216 int ser_gets(struct Serial *port, char *buf, int size)
218 return ser_gets_echo(port, buf, size, false);
223 * Read a line long at most as size and put it
224 * in buf, with optional echo.
226 * \return number of chars read, or EOF in case
229 int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo)
236 if ((c = ser_getchar(port)) == EOF)
243 if (c == '\r' || c == '\n' || i >= size-1)
247 ser_print(port, "\r\n");
252 ser_putchar(c, port);
257 #endif /* !CONFIG_SER_GETS */
261 * Read at most \a size bytes from \a port and put them in \a buf
263 * \return number of bytes actually read, or EOF in
266 int ser_read(struct Serial *port, void *buf, size_t size)
269 char *_buf = (char *)buf;
274 if ((c = ser_getchar(port)) == EOF)
284 * Write a string to serial.
285 * \return 0 if OK, EOF in case of error.
287 int ser_print(struct Serial *port, const char *s)
291 if (ser_putchar(*s++, port) == EOF)
299 * \brief Write a buffer to serial.
301 * \return 0 if OK, EOF in case of error.
303 * \todo Optimize with fifo_pushblock()
305 int ser_write(struct Serial *port, const void *_buf, size_t len)
307 const char *buf = (const char *)_buf;
311 if (ser_putchar(*buf++, port) == EOF)
322 int ser_printf(struct Serial *port, const char *format, ...)
327 va_start(ap, format);
328 len = _formatted_write(format, (void (*)(char, void *))ser_putchar, port, ap);
333 #endif /* CONFIG_PRINTF */
336 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
337 void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout)
339 port->rxtimeout = ms_to_ticks(rxtimeout);
340 port->txtimeout = ms_to_ticks(txtimeout);
342 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
344 #if CONFIG_SER_RXTIMEOUT != -1
346 * Discard input to resynchronize with remote end.
348 * Discard incoming data until the port stops receiving
349 * characters for at least \a delay milliseconds.
351 * \note Serial errors are reset before and after executing the purge.
353 void ser_resync(struct Serial *port, mtime_t delay)
355 mtime_t old_rxtimeout = ticks_to_ms(port->rxtimeout);
357 ser_settimeouts(port, delay, ticks_to_ms(port->txtimeout));
360 ser_setstatus(port, 0);
363 while (!(ser_getstatus(port) & SERRF_RXTIMEOUT));
365 /* Restore port to an usable status */
366 ser_setstatus(port, 0);
367 ser_settimeouts(port, old_rxtimeout, ticks_to_ms(port->txtimeout));
369 #endif /* CONFIG_SER_RXTIMEOUT */
372 void ser_setbaudrate(struct Serial *port, unsigned long rate)
374 port->hw->table->setBaudrate(port->hw, rate);
378 void ser_setparity(struct Serial *port, int parity)
380 port->hw->table->setParity(port->hw, parity);
385 * Flush both the RX and TX buffers.
387 void ser_purge(struct Serial *port)
389 fifo_flush_locked(&port->rxfifo);
390 fifo_flush_locked(&port->txfifo);
395 * Wait until all pending output is completely
396 * transmitted to the other end.
398 * \note The current implementation only checks the
399 * software transmission queue. Any hardware
402 void ser_drain(struct Serial *ser)
405 * Wait until the FIFO becomes empty, and then until the byte currently in
406 * the hardware register gets shifted out.
408 while (!fifo_isempty(&ser->txfifo)
409 || ser->hw->table->txSending(ser->hw))
411 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
412 /* Give up timeslice to other processes. */
421 * Initialize a serial port.
423 * \param unit Serial unit to open. Possible values are architecture dependant.
425 struct Serial *ser_open(unsigned int unit)
429 ASSERT(unit < countof(ser_handles));
430 port = &ser_handles[unit];
432 ASSERT(!port->is_open);
433 DB(port->is_open = true;)
437 port->hw = ser_hw_getdesc(unit);
439 /* Initialize circular buffers */
440 ASSERT(port->hw->txbuffer);
441 ASSERT(port->hw->rxbuffer);
442 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
443 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
445 port->hw->table->init(port->hw, port);
447 /* Set default values */
448 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
449 ser_settimeouts(port, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
451 #if CONFIG_SER_DEFBAUDRATE
452 ser_setbaudrate(port, CONFIG_SER_DEFBAUDRATE);
455 /* Clear error flags */
456 ser_setstatus(port, 0);
463 * Clean up serial port, disabling the associated hardware.
465 void ser_close(struct Serial *port)
467 ASSERT(port->is_open);
468 DB(port->is_open = false;)
470 // Wait until we finish sending everything
473 port->hw->table->cleanup(port->hw);
477 * We purge the FIFO buffer only after the low-level cleanup, so that
478 * we are sure that there are no more interrupts.