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 Bernie Innocenti <bernie@codewiz.org>
33 * \brief Buffered serial I/O driver
35 * The serial rx interrupt buffers incoming data in a software FIFO
36 * to decouple the higher level protocols from the line speed.
37 * Outgoing data is buffered as well for better performance.
38 * This driver is not optimized for best performance, but it
39 * has proved to be fast enough to handle transfer rates up to
40 * 38400bps on a 16MHz 80196.
42 * MODULE CONFIGURATION
44 * \li \c CONFIG_SER_HWHANDSHAKE - set to 1 to enable RTS/CTS handshake.
45 * Support is incomplete/untested.
46 * \li \c CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
50 * \author Bernie Innocenti <bernie@codewiz.org>
57 #include "cfg/cfg_ser.h"
58 #include "cfg/cfg_kern.h"
59 #include <cfg/debug.h>
61 #include <mware/formatwr.h>
63 #include <cpu/power.h> /* cpu_relax() */
65 #include <string.h> /* memset() */
68 * Sanity check for config parameters required by this module.
70 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
71 #error CONFIG_KERNEL must be set to either 0 or 1 in cfg_kern.h
73 #if !defined(CONFIG_SER_RXTIMEOUT)
74 #error CONFIG_SER_TXTIMEOUT missing in cfg_ser.h
76 #if !defined(CONFIG_SER_RXTIMEOUT)
77 #error CONFIG_SER_RXTIMEOUT missing in cfg_ser.h
79 #if !defined(CONFIG_SER_DEFBAUDRATE)
80 #error CONFIG_SER_DEFBAUDRATE missing in cfg_ser.h
84 struct Serial *ser_handles[SER_CNT];
87 * Insert \a c in tx FIFO buffer.
88 * \note This function will switch out the calling process
89 * if the tx buffer is full. If the buffer is full
90 * and \a port->txtimeout is 0 return EOF immediatly.
92 * \return EOF on error or timeout, \a c otherwise.
94 static int ser_putchar(int c, struct Serial *port)
96 if (fifo_isfull_locked(&port->txfifo))
98 #if CONFIG_SER_TXTIMEOUT != -1
99 /* If timeout == 0 we don't want to wait */
100 if (port->txtimeout == 0)
103 ticks_t start_time = timer_clock();
106 /* Wait while buffer is full... */
111 #if CONFIG_SER_TXTIMEOUT != -1
112 if (timer_clock() - start_time >= port->txtimeout)
114 ATOMIC(port->status |= SERRF_TXTIMEOUT);
117 #endif /* CONFIG_SER_TXTIMEOUT */
119 while (fifo_isfull_locked(&port->txfifo));
122 fifo_push_locked(&port->txfifo, (unsigned char)c);
124 /* (re)trigger tx interrupt */
125 port->hw->table->txStart(port->hw);
127 /* Avoid returning signed extended char */
128 return (int)((unsigned char)c);
133 * Fetch a character from the rx FIFO buffer.
134 * \note This function will switch out the calling process
135 * if the rx buffer is empty. If the buffer is empty
136 * and \a port->rxtimeout is 0 return EOF immediatly.
138 * \return EOF on error or timeout, \a c otherwise.
140 static int ser_getchar(struct Serial *port)
142 if (fifo_isempty_locked(&port->rxfifo))
144 #if CONFIG_SER_RXTIMEOUT != -1
145 /* If timeout == 0 we don't want to wait for chars */
146 if (port->rxtimeout == 0)
149 ticks_t start_time = timer_clock();
152 /* Wait while buffer is empty */
157 #if CONFIG_SER_RXTIMEOUT != -1
158 if (timer_clock() - start_time >= port->rxtimeout)
160 ATOMIC(port->status |= SERRF_RXTIMEOUT);
163 #endif /* CONFIG_SER_RXTIMEOUT */
165 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
169 * Get a byte from the FIFO (avoiding sign-extension),
170 * re-enable RTS, then return result.
172 if (ser_getstatus(port) & SERRF_RX)
174 return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
178 * Fetch a character from the rx FIFO buffer.
179 * If the buffer is empty, ser_getchar_nowait() returns
181 * \note Deprecated, use ser_getchar with rx_timeout set to 0.
183 int ser_getchar_nowait(struct Serial *fd)
185 if (fifo_isempty_locked(&fd->rxfifo))
188 /* NOTE: the double cast prevents unwanted sign extension */
189 return (int)(unsigned char)fifo_pop_locked(&fd->rxfifo);
195 * Read at most \a size bytes from \a port and put them in \a buf
197 * \return number of bytes actually read.
199 static size_t ser_read(struct KFile *fd, void *_buf, size_t size)
201 Serial *fds = SERIAL_CAST(fd);
204 char *buf = (char *)_buf;
209 if ((c = ser_getchar(fds)) == EOF)
218 * \brief Write a buffer to serial.
220 * \return 0 if OK, EOF in case of error.
222 * \todo Optimize with fifo_pushblock()
224 static size_t ser_write(struct KFile *fd, const void *_buf, size_t size)
226 Serial *fds = SERIAL_CAST(fd);
227 const char *buf = (const char *)_buf;
232 if (ser_putchar(*buf++, fds) == EOF)
240 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
241 void ser_settimeouts(struct Serial *fd, mtime_t rxtimeout, mtime_t txtimeout)
243 fd->rxtimeout = ms_to_ticks(rxtimeout);
244 fd->txtimeout = ms_to_ticks(txtimeout);
246 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
248 #if CONFIG_SER_RXTIMEOUT != -1
250 * Discard input to resynchronize with remote end.
252 * Discard incoming data until the port stops receiving
253 * characters for at least \a delay milliseconds.
255 * \note Serial errors are reset before and after executing the purge.
257 void ser_resync(struct Serial *fd, mtime_t delay)
259 mtime_t old_rxtimeout = ticks_to_ms(fd->rxtimeout);
261 ser_settimeouts(fd, delay, ticks_to_ms(fd->txtimeout));
264 ser_setstatus(fd, 0);
267 while (!(ser_getstatus(fd) & SERRF_RXTIMEOUT));
269 /* Restore port to an usable status */
270 ser_setstatus(fd, 0);
271 ser_settimeouts(fd, old_rxtimeout, ticks_to_ms(fd->txtimeout));
273 #endif /* CONFIG_SER_RXTIMEOUT */
276 void ser_setbaudrate(struct Serial *fd, unsigned long rate)
278 fd->hw->table->setBaudrate(fd->hw, rate);
282 void ser_setparity(struct Serial *fd, int parity)
284 fd->hw->table->setParity(fd->hw, parity);
287 static int ser_error(struct KFile *fd)
289 Serial *fds = SERIAL_CAST(fd);
290 return ser_getstatus(fds);
293 static void ser_clearerr(struct KFile *fd)
295 Serial *fds = SERIAL_CAST(fd);
296 ser_setstatus(fds, 0);
302 * Flush both the RX and TX buffers.
304 void ser_purge(struct Serial *fd)
313 void ser_purgeRx(struct Serial *fd)
315 fifo_flush_locked(&fd->rxfifo);
321 void ser_purgeTx(struct Serial *fd)
323 fifo_flush_locked(&fd->txfifo);
328 * Wait until all pending output is completely
329 * transmitted to the other end.
331 * \note The current implementation only checks the
332 * software transmission queue. Any hardware
335 static int ser_flush(struct KFile *fd)
337 Serial *fds = SERIAL_CAST(fd);
340 * Wait until the FIFO becomes empty, and then until the byte currently in
341 * the hardware register gets shifted out.
343 while (!fifo_isempty(&fds->txfifo)
344 || fds->hw->table->txSending(fds->hw))
351 * Initialize a serial port.
353 * \param fd KFile Serial struct interface.
354 * \param unit Serial unit to open. Possible values are architecture dependant.
356 static struct Serial *ser_open(struct Serial *fd, unsigned int unit)
358 ASSERT(unit < countof(ser_handles));
360 ser_handles[unit] = fd;
361 ASSERT(!fd->is_open);
362 DB(fd->is_open = true);
366 fd->hw = ser_hw_getdesc(unit);
368 /* Initialize circular buffers */
369 ASSERT(fd->hw->txbuffer);
370 ASSERT(fd->hw->rxbuffer);
371 fifo_init(&fd->txfifo, fd->hw->txbuffer, fd->hw->txbuffer_size);
372 fifo_init(&fd->rxfifo, fd->hw->rxbuffer, fd->hw->rxbuffer_size);
374 fd->hw->table->init(fd->hw, fd);
376 /* Set default values */
377 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
378 ser_settimeouts(fd, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
380 #if CONFIG_SER_DEFBAUDRATE
381 ser_setbaudrate(fd, CONFIG_SER_DEFBAUDRATE);
384 /* Clear error flags */
385 ser_setstatus(fd, 0);
392 * Clean up serial port, disabling the associated hardware.
394 static int ser_close(struct KFile *fd)
396 Serial *fds = SERIAL_CAST(fd);
399 ASSERT(port->is_open);
400 DB(port->is_open = false);
402 // Wait until we finish sending everything
405 port->hw->table->cleanup(port->hw);
409 * We purge the FIFO buffer only after the low-level cleanup, so that
410 * we are sure that there are no more interrupts.
417 * Reopen serial port.
419 static struct KFile *ser_reopen(struct KFile *fd)
421 Serial *fds = SERIAL_CAST(fd);
424 ser_open(fds, fds->unit);
429 * Init serial driver for \a unit.
431 void ser_init(struct Serial *fds, unsigned int unit)
433 memset(fds, 0, sizeof(*fds));
435 DB(fds->fd._type = KFT_SERIAL);
436 fds->fd.reopen = ser_reopen;
437 fds->fd.close = ser_close;
438 fds->fd.read = ser_read;
439 fds->fd.write = ser_write;
440 fds->fd.flush = ser_flush;
441 fds->fd.error = ser_error;
442 fds->fd.clearerr = ser_clearerr;
448 * Read data from SPI bus.
449 * Since we are master, we have to trigger slave by sending
450 * fake chars on the bus.
452 static size_t spimaster_read(struct KFile *fd, void *_buf, size_t size)
454 Serial *fd_spi = SERIAL_CAST(fd);
456 ser_flush(&fd_spi->fd);
460 uint8_t *buf = (uint8_t *)_buf;
466 * Send and receive chars 1 by 1, otherwise the rxfifo
469 ser_putchar(0, fd_spi);
471 if ((c = ser_getchar(fd_spi)) == EOF)
481 * Write data to SPI bus.
483 static size_t spimaster_write(struct KFile *fd, const void *buf, size_t size)
485 Serial *fd_spi = SERIAL_CAST(fd);
489 return ser_write(&fd_spi->fd, buf, size);
494 * Init SPI serial driver \a unit in master mode.
496 * This interface implements the SPI master protocol over a serial SPI
497 * driver. This is needed because normal serial driver send/receive data
498 * at the same time. SPI slaves like memories and other peripherals
499 * first receive and *then* send response back instead.
500 * To achieve this, when we are master and we are *sending*,
501 * we have to discard all incoming data. Then, when we want to
502 * receive, we must write fake data to SPI to trigger slave devices.
504 void spimaster_init(Serial *fds, unsigned int unit)
507 fds->fd.read = spimaster_read;
508 fds->fd.write = spimaster_write;