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 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
34 * \brief High level serial I/O API.
37 * \author Bernie Innocenti <bernie@codewiz.org>
39 * $WIZ$ module_name = "ser"
40 * $WIZ$ module_depends = "kfile", "timer"
41 * $WIZ$ module_configuration = "bertos/cfg/cfg_ser.h"
42 * $WIZ$ module_hw = "bertos/hw/hw_ser.h"
43 * $WIZ$ module_supports = "not atmega103 and not atmega168 "
44 * $WIZ$ module_supports += "and not atmega32 and not atmega8"
50 #include <kern/kfile.h>
51 #include <struct/fifobuf.h>
52 #include <cfg/compiler.h>
55 #include <cfg/macros.h> /* BV() */
57 typedef uint16_t serstatus_t;
60 #define SERRF_RXFIFOOVERRUN BV(0) /**< Rx FIFO buffer overrun */
61 #define SERRF_RXTIMEOUT BV(1) /**< Receive timeout */
62 #define SERRF_TXTIMEOUT BV(2) /**< Transmit timeout */
65 #define SERRF_RXSROVERRUN 0 /**< Unsupported in emulated serial port. */
66 #define SERRF_FRAMEERROR 0 /**< Unsupported in emulated serial port. */
67 #define SERRF_PARITYERROR 0 /**< Unsupported in emulated serial port. */
68 #define SERRF_NOISEERROR 0 /**< Unsupported in emulated serial port. */
75 SER_CNT /**< Number of serial ports */
79 #include CPU_HEADER(ser)
82 #include "cfg/cfg_ser.h"
87 * \name Masks to group TX/RX errors.
91 ( SERRF_RXFIFOOVERRUN \
97 #define SERRF_TX (SERRF_TXTIMEOUT)
101 * \name LSB or MSB first data order for SPI driver.
103 * $WIZ$ ser_order_bit = "SER_MSB_FIRST", "SER_LSB_FIRST"
105 #define SER_MSB_FIRST 0
106 #define SER_LSB_FIRST 1
109 * \name Parity settings for ser_setparity().
111 * \note Values are AVR-specific for performance reasons.
112 * Other processors should either decode them or
113 * redefine these macros.
116 #define SER_PARITY_NONE 0
117 #define SER_PARITY_EVEN 2
118 #define SER_PARITY_ODD 3
123 * \def CONFIG_SER_STROBE
125 * This is a debug facility that can be used to
126 * monitor SER interrupt activity on an external pin.
128 * To use strobes, redefine the macros SER_STROBE_ON,
129 * SER_STROBE_OFF and SER_STROBE_INIT and set
130 * CONFIG_SER_STROBE to 1.
132 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
133 #define SER_STROBE_ON do {/*nop*/} while(0)
134 #define SER_STROBE_OFF do {/*nop*/} while(0)
135 #define SER_STROBE_INIT do {/*nop*/} while(0)
138 struct SerialHardware;
140 /** Human-readable serial error descriptions */
141 extern const char * const serial_errors[8];
143 /** Serial handle structure */
144 typedef struct Serial
146 /** Serial have a KFile struct implementation **/
149 /** Physical port number */
157 * \name Transmit and receive FIFOs.
159 * Declared volatile because handled asinchronously by interrupts.
167 #if CONFIG_SER_RXTIMEOUT != -1
170 #if CONFIG_SER_TXTIMEOUT != -1
174 /** Holds the flags defined above. Will be 0 when no errors have occurred. */
175 volatile serstatus_t status;
177 /** Low-level interface to hardware. */
178 struct SerialHardware* hw;
185 #define KFT_SERIAL MAKE_ID('S', 'E', 'R', 'L')
188 INLINE Serial * SERIAL_CAST(KFile *fd)
190 ASSERT(fd->_type == KFT_SERIAL);
194 /* Function prototypes */
195 //extern int ser_getchar_nowait(struct Serial *port);
197 void ser_setbaudrate(struct Serial *fd, unsigned long rate);
198 void ser_setparity(struct Serial *fd, int parity);
199 void ser_settimeouts(struct Serial *fd, mtime_t rxtimeout, mtime_t txtimeout);
200 void ser_resync(struct Serial *fd, mtime_t delay);
201 int ser_getchar_nowait(struct Serial *fd);
203 void ser_purgeRx(struct Serial *fd);
204 void ser_purgeTx(struct Serial *fd);
205 void ser_purge(struct Serial *fd);
206 void ser_init(struct Serial *fds, unsigned int unit);
207 void spimaster_init(Serial *fds, unsigned int unit);
211 * \name Additional functions implemented as macros
215 #define ser_getstatus(h) ((h)->status)
216 #define ser_setstatus(h, x) ((h)->status = (x))
219 #endif /* DRV_SER_H */