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 Bernardo Innocenti <bernie@codewiz.org>
34 * \brief High level serial I/O API
37 * \author Bernardo Innocenti <bernie@develer.com>
42 *#* Revision 1.36 2007/01/29 11:30:30 batt
43 *#* Reimplement ser_clearstatus as a macro.
45 *#* Revision 1.35 2007/01/27 20:47:12 batt
48 *#* Revision 1.34 2006/11/20 15:07:40 batt
49 *#* Revert unneeded locked functions.
51 *#* Revision 1.33 2006/11/17 17:03:58 batt
52 *#* Implement ser_setstatus and ser_getstatus as functions to avoid race conditions.
54 *#* Revision 1.32 2006/11/17 16:01:12 batt
55 *#* Serial status MUST be volatile.
57 *#* Revision 1.31 2006/09/13 18:22:48 bernie
60 *#* Revision 1.30 2006/09/13 18:21:23 bernie
61 *#* Add configurable SPI pin mapping.
63 *#* Revision 1.29 2006/09/13 13:59:11 bernie
64 *#* Documentation fixes.
66 *#* Revision 1.28 2006/07/21 10:58:00 batt
67 *#* Use timer_clock() instead of obsolete timer_ticks().
69 *#* Revision 1.27 2006/07/19 12:56:26 bernie
70 *#* Convert to new Doxygen style.
72 *#* Revision 1.26 2006/05/18 00:39:13 bernie
73 *#* Add struct Serial friendly typedef.
75 *#* Revision 1.25 2006/02/17 22:28:00 bernie
76 *#* Rename ser_emul.c to ser_posix.c.
78 *#* Revision 1.24 2006/02/17 22:23:06 bernie
79 *#* Update POSIX serial emulator.
81 *#* Revision 1.23 2005/11/27 23:33:40 bernie
82 *#* Use appconfig.h instead of cfg/config.h.
84 *#* Revision 1.22 2005/11/04 16:20:02 bernie
85 *#* Fix reference to README.devlib in header.
87 *#* Revision 1.21 2005/04/11 19:10:27 bernie
88 *#* Include top-level headers from cfg/ subdir.
90 *#* Revision 1.20 2005/01/22 04:20:01 bernie
93 *#* Revision 1.19 2005/01/11 18:08:16 aleph
94 *#* Add missing include
96 *#* Revision 1.18 2004/12/08 08:57:17 bernie
97 *#* Rename time_t to mtime_t.
99 *#* Revision 1.17 2004/11/16 21:54:56 bernie
100 *#* Changes for SC Monoboard support.
102 *#* Revision 1.16 2004/10/19 11:48:05 bernie
105 *#* Revision 1.15 2004/10/19 08:11:53 bernie
106 *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
108 *#* Revision 1.14 2004/10/03 18:43:18 bernie
109 *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
111 *#* Revision 1.13 2004/09/14 21:04:57 bernie
112 *#* Don't vanely call kdebug.h.
114 *#* Revision 1.12 2004/09/06 21:40:50 bernie
115 *#* Move buffer handling in chip-specific driver.
117 *#* Revision 1.11 2004/08/25 14:12:08 rasky
118 *#* Aggiornato il comment block dei log RCS
120 *#* Revision 1.10 2004/08/24 16:20:48 bernie
121 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
123 *#* Revision 1.9 2004/08/15 05:32:22 bernie
124 *#* ser_resync(): New function.
126 *#* Revision 1.8 2004/08/02 20:20:29 aleph
127 *#* Merge from project_ks
129 *#* Revision 1.7 2004/07/30 14:15:53 rasky
130 *#* Nuovo supporto unificato per detect della CPU
132 *#* Revision 1.6 2004/07/29 22:57:09 bernie
133 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
135 *#* Revision 1.5 2004/07/18 21:54:23 bernie
136 *#* Add ATmega8 support.
138 *#* Revision 1.4 2004/06/03 11:27:09 bernie
139 *#* Add dual-license information.
141 *#* Revision 1.3 2004/06/02 21:35:24 aleph
142 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
144 *#* Revision 1.2 2004/05/23 18:21:53 bernie
145 *#* Trim CVS logs and cleanup header info.
152 #include <mware/fifobuf.h>
153 #include <cfg/compiler.h>
155 #include CPU_HEADER(ser)
157 #include <appconfig.h>
162 * \name Masks to group TX/RX errors.
166 ( SERRF_RXFIFOOVERRUN \
168 | SERRF_RXSROVERRUN \
169 | SERRF_PARITYERROR \
172 #define SERRF_TX (SERRF_TXTIMEOUT)
176 * \name LSB or MSB first data order for SPI driver.
179 #define SER_MSB_FIRST 0
180 #define SER_LSB_FIRST 1
184 * \name Parity settings for ser_setparity().
186 * \note Values are AVR-specific for performance reasons.
187 * Other processors should either decode them or
188 * redefine these macros.
191 #define SER_PARITY_NONE 0
192 #define SER_PARITY_EVEN 2
193 #define SER_PARITY_ODD 3
197 struct SerialHardware;
199 /** Human-readable serial error descriptions */
200 extern const char * const serial_errors[8];
202 /** Serial handle structure */
203 typedef struct Serial
205 /** Physical port number */
213 * \name Transmit and receive FIFOs.
215 * Declared volatile because handled asinchronously by interrupts.
223 #if CONFIG_SER_RXTIMEOUT != -1
226 #if CONFIG_SER_TXTIMEOUT != -1
230 /** Holds the flags defined above. Will be 0 when no errors have occurred. */
231 volatile serstatus_t status;
233 /** Low-level interface to hardware. */
234 struct SerialHardware* hw;
238 /* Function prototypes */
239 extern int ser_putchar(int c, struct Serial *port);
240 extern int ser_getchar(struct Serial *port);
241 extern int ser_getchar_nowait(struct Serial *port);
243 extern int ser_write(struct Serial *port, const void *buf, size_t len);
244 extern int ser_read(struct Serial *port, void *buf, size_t size);
246 extern int ser_print(struct Serial *port, const char *s);
247 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
249 extern int ser_gets(struct Serial *port, char *buf, int size);
250 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
252 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
253 extern void ser_setparity(struct Serial *port, int parity);
254 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
255 extern void ser_resync(struct Serial *port, mtime_t delay);
256 extern void ser_purge(struct Serial *port);
257 extern void ser_drain(struct Serial *port);
259 extern struct Serial *ser_open(unsigned int unit);
260 extern void ser_close(struct Serial *port);
263 * \name Additional functions implemented as macros
267 #define ser_getstatus(h) ((h)->status)
268 #define ser_setstatus(h, x) ((h)->status = (x))
269 #define ser_clearstatus(h) ser_setstatus(h, 0)
272 #endif /* DRV_SER_H */