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>
58 #include <cfg/cfg_ser.h>
59 #include <cfg/cfg_kern.h>
60 #include <cfg/debug.h>
62 #include <mware/formatwr.h>
64 #include <string.h> /* memset */
67 * Sanity check for config parameters required by this module.
69 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
70 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
72 #if !defined(CONFIG_SER_RXTIMEOUT)
73 #error CONFIG_SER_TXTIMEOUT missing in config.h
75 #if !defined(CONFIG_SER_RXTIMEOUT)
76 #error CONFIG_SER_RXTIMEOUT missing in config.h
78 #if !defined(CONFIG_SER_DEFBAUDRATE)
79 #error CONFIG_SER_DEFBAUDRATE missing in config.h
83 #include <kern/proc.h>
86 #if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
87 #include <drv/timer.h>
91 struct Serial ser_handles[SER_CNT];
94 * Insert \a c in tx FIFO buffer.
95 * \note This function will switch out the calling process
96 * if the tx buffer is full. If the buffer is full
97 * and \a port->txtimeout is 0 return EOF immediatly.
99 * \return EOF on error or timeout, \a c otherwise.
101 static int ser_putchar(int c, struct Serial *port)
103 if (fifo_isfull_locked(&port->txfifo))
105 #if CONFIG_SER_TXTIMEOUT != -1
106 /* If timeout == 0 we don't want to wait */
107 if (port->txtimeout == 0)
110 ticks_t start_time = timer_clock();
113 /* Wait while buffer is full... */
117 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
118 /* Give up timeslice to other processes. */
121 #if CONFIG_SER_TXTIMEOUT != -1
122 if (timer_clock() - start_time >= port->txtimeout)
124 ATOMIC(port->status |= SERRF_TXTIMEOUT);
127 #endif /* CONFIG_SER_TXTIMEOUT */
129 while (fifo_isfull_locked(&port->txfifo));
132 fifo_push_locked(&port->txfifo, (unsigned char)c);
134 /* (re)trigger tx interrupt */
135 port->hw->table->txStart(port->hw);
137 /* Avoid returning signed extended char */
138 return (int)((unsigned char)c);
143 * Fetch a character from the rx FIFO buffer.
144 * \note This function will switch out the calling process
145 * if the rx buffer is empty. If the buffer is empty
146 * and \a port->rxtimeout is 0 return EOF immediatly.
148 * \return EOF on error or timeout, \a c otherwise.
150 static int ser_getchar(struct Serial *port)
152 if (fifo_isempty_locked(&port->rxfifo))
154 #if CONFIG_SER_RXTIMEOUT != -1
155 /* If timeout == 0 we don't want to wait for chars */
156 if (port->rxtimeout == 0)
159 ticks_t start_time = timer_clock();
161 /* Wait while buffer is empty */
165 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
166 /* Give up timeslice to other processes. */
169 #if CONFIG_SER_RXTIMEOUT != -1
170 if (timer_clock() - start_time >= port->rxtimeout)
172 ATOMIC(port->status |= SERRF_RXTIMEOUT);
175 #endif /* CONFIG_SER_RXTIMEOUT */
177 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
181 * Get a byte from the FIFO (avoiding sign-extension),
182 * re-enable RTS, then return result.
184 if (ser_getstatus(port) & SERRF_RX)
186 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
190 * Fetch a character from the rx FIFO buffer.
191 * If the buffer is empty, ser_getchar_nowait() returns
193 * \note Deprecated, use ser_getchar with rx_timeout set to 0.
195 int ser_getchar_nowait(struct KFileSerial *fd)
197 if (fifo_isempty_locked(&fd->ser->rxfifo))
200 /* NOTE: the double cast prevents unwanted sign extension */
201 return (int)(unsigned char)fifo_pop_locked(&fd->ser->rxfifo);
207 * Read at most \a size bytes from \a port and put them in \a buf
209 * \return number of bytes actually read.
211 static size_t ser_read(struct KFile *fd, void *_buf, size_t size)
213 KFileSerial *fds = KFILESERIAL(fd);
216 char *buf = (char *)_buf;
221 if ((c = ser_getchar(fds->ser)) == EOF)
230 * \brief Write a buffer to serial.
232 * \return 0 if OK, EOF in case of error.
234 * \todo Optimize with fifo_pushblock()
236 static size_t ser_write(struct KFile *fd, const void *_buf, size_t size)
238 KFileSerial *fds = KFILESERIAL(fd);
239 const char *buf = (const char *)_buf;
244 if (ser_putchar(*buf++, fds->ser) == EOF)
252 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
253 void ser_settimeouts(struct KFileSerial *fd, mtime_t rxtimeout, mtime_t txtimeout)
255 fd->ser->rxtimeout = ms_to_ticks(rxtimeout);
256 fd->ser->txtimeout = ms_to_ticks(txtimeout);
258 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
260 #if CONFIG_SER_RXTIMEOUT != -1
262 * Discard input to resynchronize with remote end.
264 * Discard incoming data until the port stops receiving
265 * characters for at least \a delay milliseconds.
267 * \note Serial errors are reset before and after executing the purge.
269 void ser_resync(struct KFileSerial *fd, mtime_t delay)
271 mtime_t old_rxtimeout = ticks_to_ms(fd->ser->rxtimeout);
273 ser_settimeouts(fd, delay, ticks_to_ms(fd->ser->txtimeout));
276 ser_setstatus(fd->ser, 0);
277 ser_getchar(fd->ser);
279 while (!(ser_getstatus(fd->ser) & SERRF_RXTIMEOUT));
281 /* Restore port to an usable status */
282 ser_setstatus(fd->ser, 0);
283 ser_settimeouts(fd, old_rxtimeout, ticks_to_ms(fd->ser->txtimeout));
285 #endif /* CONFIG_SER_RXTIMEOUT */
288 void ser_setbaudrate(struct KFileSerial *fd, unsigned long rate)
290 fd->ser->hw->table->setBaudrate(fd->ser->hw, rate);
294 void ser_setparity(struct KFileSerial *fd, int parity)
296 fd->ser->hw->table->setParity(fd->ser->hw, parity);
299 static int ser_error(struct KFile *fd)
301 KFileSerial *fds = KFILESERIAL(fd);
302 return ser_getstatus(fds->ser);
305 static void ser_clearerr(struct KFile *fd)
307 KFileSerial *fds = KFILESERIAL(fd);
308 ser_setstatus(fds->ser, 0);
314 * Flush both the RX and TX buffers.
316 void ser_purge(struct KFileSerial *fd)
325 void ser_purgeRx(struct KFileSerial *fd)
327 fifo_flush_locked(&fd->ser->rxfifo);
333 void ser_purgeTx(struct KFileSerial *fd)
335 fifo_flush_locked(&fd->ser->txfifo);
340 * Wait until all pending output is completely
341 * transmitted to the other end.
343 * \note The current implementation only checks the
344 * software transmission queue. Any hardware
347 static int ser_flush(struct KFile *fd)
349 KFileSerial *fds = KFILESERIAL(fd);
352 * Wait until the FIFO becomes empty, and then until the byte currently in
353 * the hardware register gets shifted out.
355 while (!fifo_isempty(&fds->ser->txfifo)
356 || fds->ser->hw->table->txSending(fds->ser->hw))
358 #if CONFIG_KERNEL && CONFIG_KERN_SCHED
359 /* Give up timeslice to other processes. */
369 * Initialize a serial port.
371 * \param fd KFile Serial struct interface.
372 * \param unit Serial unit to open. Possible values are architecture dependant.
374 static struct Serial *ser_open(struct KFileSerial *fd, unsigned int unit)
378 ASSERT(unit < countof(ser_handles));
379 port = &ser_handles[unit];
381 ASSERT(!port->is_open);
382 DB(port->is_open = true);
386 port->hw = ser_hw_getdesc(unit);
388 /* Initialize circular buffers */
389 ASSERT(port->hw->txbuffer);
390 ASSERT(port->hw->rxbuffer);
391 fifo_init(&port->txfifo, port->hw->txbuffer, port->hw->txbuffer_size);
392 fifo_init(&port->rxfifo, port->hw->rxbuffer, port->hw->rxbuffer_size);
394 port->hw->table->init(port->hw, port);
397 /* Set default values */
398 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
399 ser_settimeouts(fd, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
401 #if CONFIG_SER_DEFBAUDRATE
402 ser_setbaudrate(fd, CONFIG_SER_DEFBAUDRATE);
405 /* Clear error flags */
406 ser_setstatus(port, 0);
413 * Clean up serial port, disabling the associated hardware.
415 static int ser_close(struct KFile *fd)
417 KFileSerial *fds = KFILESERIAL(fd);
418 Serial *port = fds->ser;
420 ASSERT(port->is_open);
421 DB(port->is_open = false);
423 // Wait until we finish sending everything
426 port->hw->table->cleanup(port->hw);
430 * We purge the FIFO buffer only after the low-level cleanup, so that
431 * we are sure that there are no more interrupts.
438 * Reopen serial port.
440 static struct KFile *ser_reopen(struct KFile *fd)
442 KFileSerial *fds = KFILESERIAL(fd);
445 ser_open(fds, fds->ser->unit);
450 * Init serial driver for \a unit.
452 void ser_init(struct KFileSerial *fds, unsigned int unit)
454 memset(fds, 0, sizeof(*fds));
456 DB(fds->fd._type = KFT_SERIAL);
457 fds->fd.reopen = ser_reopen;
458 fds->fd.close = ser_close;
459 fds->fd.read = ser_read;
460 fds->fd.write = ser_write;
461 fds->fd.flush = ser_flush;
462 fds->fd.error = ser_error;
463 fds->fd.clearerr = ser_clearerr;
469 * Read data from SPI bus.
470 * Since we are master, we have to trigger slave by sending
471 * fake chars on the bus.
473 static size_t spimaster_read(struct KFile *fd, void *_buf, size_t size)
475 KFileSerial *fd_spi = KFILESERIAL(fd);
477 ser_flush(&fd_spi->fd);
481 uint8_t *buf = (uint8_t *)_buf;
487 * Send and receive chars 1 by 1, otherwise the rxfifo
490 ser_putchar(0, fd_spi->ser);
492 if ((c = ser_getchar(fd_spi->ser)) == EOF)
502 * Write data to SPI bus.
504 static size_t spimaster_write(struct KFile *fd, const void *buf, size_t size)
506 KFileSerial *fd_spi = KFILESERIAL(fd);
510 return ser_write(&fd_spi->fd, buf, size);
515 * Init SPI serial driver \a unit in master mode.
517 * This interface implements the SPI master protocol over a serial SPI
518 * driver. This is needed because normal serial driver send/receive data
519 * at the same time. SPI slaves like memories and other peripherals
520 * first receive and *then* send response back instead.
521 * To achieve this, when we are master and we are *sending*,
522 * we have to discard all incoming data. Then, when we want to
523 * receive, we must write fake data to SPI to trigger slave devices.
525 void spimaster_init(KFileSerial *fds, unsigned int unit)
528 fds->fd.read = spimaster_read;
529 fds->fd.write = spimaster_write;