4 * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See README.devlib for information.
9 * \brief High level serial I/O API
12 * \author Bernardo Innocenti <bernie@develer.com>
17 *#* Revision 1.25 2006/02/17 22:28:00 bernie
18 *#* Rename ser_emul.c to ser_posix.c.
20 *#* Revision 1.24 2006/02/17 22:23:06 bernie
21 *#* Update POSIX serial emulator.
23 *#* Revision 1.23 2005/11/27 23:33:40 bernie
24 *#* Use appconfig.h instead of cfg/config.h.
26 *#* Revision 1.22 2005/11/04 16:20:02 bernie
27 *#* Fix reference to README.devlib in header.
29 *#* Revision 1.21 2005/04/11 19:10:27 bernie
30 *#* Include top-level headers from cfg/ subdir.
32 *#* Revision 1.20 2005/01/22 04:20:01 bernie
35 *#* Revision 1.19 2005/01/11 18:08:16 aleph
36 *#* Add missing include
38 *#* Revision 1.18 2004/12/08 08:57:17 bernie
39 *#* Rename time_t to mtime_t.
41 *#* Revision 1.17 2004/11/16 21:54:56 bernie
42 *#* Changes for SC Monoboard support.
44 *#* Revision 1.16 2004/10/19 11:48:05 bernie
47 *#* Revision 1.15 2004/10/19 08:11:53 bernie
48 *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
50 *#* Revision 1.14 2004/10/03 18:43:18 bernie
51 *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
53 *#* Revision 1.13 2004/09/14 21:04:57 bernie
54 *#* Don't vanely call kdebug.h.
56 *#* Revision 1.12 2004/09/06 21:40:50 bernie
57 *#* Move buffer handling in chip-specific driver.
59 *#* Revision 1.11 2004/08/25 14:12:08 rasky
60 *#* Aggiornato il comment block dei log RCS
62 *#* Revision 1.10 2004/08/24 16:20:48 bernie
63 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
65 *#* Revision 1.9 2004/08/15 05:32:22 bernie
66 *#* ser_resync(): New function.
68 *#* Revision 1.8 2004/08/02 20:20:29 aleph
69 *#* Merge from project_ks
71 *#* Revision 1.7 2004/07/30 14:15:53 rasky
72 *#* Nuovo supporto unificato per detect della CPU
74 *#* Revision 1.6 2004/07/29 22:57:09 bernie
75 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
77 *#* Revision 1.5 2004/07/18 21:54:23 bernie
78 *#* Add ATmega8 support.
80 *#* Revision 1.4 2004/06/03 11:27:09 bernie
81 *#* Add dual-license information.
83 *#* Revision 1.3 2004/06/02 21:35:24 aleph
84 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
86 *#* Revision 1.2 2004/05/23 18:21:53 bernie
87 *#* Trim CVS logs and cleanup header info.
93 #include <mware/fifobuf.h>
94 #include <cfg/compiler.h>
95 #include <cfg/macros.h> /* BV() */
97 #include <appconfig.h>
99 /*! \name Serial Error/status flags. */
102 typedef uint8_t serstatus_t;
104 /* Software errors */
105 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
106 #define SERRF_RXTIMEOUT BV(5) /*!< Receive timeout */
107 #define SERRF_TXTIMEOUT BV(6) /*!< Transmit timeout */
111 * These flags map directly to the AVR UART Status Register (USR).
113 #define SERRF_RXSROVERRUN BV(3) /*!< Rx shift register overrun */
114 #define SERRF_FRAMEERROR BV(4) /*!< Stop bit missing */
115 #define SERRF_PARITYERROR BV(7) /*!< Parity error */
116 #define SERRF_NOISEERROR 0 /*!< Unsupported */
118 typedef uint16_t serstatus_t;
120 /* Software errors */
121 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
122 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
123 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
127 * These flags map directly to the SCI Control Register.
129 #define SERRF_PARITYERROR BV(8) /*!< Parity error */
130 #define SERRF_FRAMEERROR BV(9) /*!< Stop bit missing */
131 #define SERRF_NOISEERROR BV(10) /*!< Noise error */
132 #define SERRF_RXSROVERRUN BV(11) /*!< Rx shift register overrun */
134 typedef uint16_t serstatus_t;
136 /* Software errors */
137 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
138 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
139 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
141 /* Hardware errors */
142 #define SERRF_RXSROVERRUN 0 /*!< Unsupported in emulated serial port. */
143 #define SERRF_FRAMEERROR 0 /*!< Unsupported in emulated serial port. */
144 #define SERRF_PARITYERROR 0 /*!< Unsupported in emulated serial port. */
145 #define SERRF_NOISEERROR 0 /*!< Unsupported in emulated serial port. */
148 #error unknown architecture
152 /** Mask to group all RX errors. */
154 ( SERRF_RXFIFOOVERRUN \
156 | SERRF_RXSROVERRUN \
157 | SERRF_PARITYERROR \
161 /** Mask to group all TX errors. */
162 #define SERRF_TX (SERRF_TXTIMEOUT)
166 * \name Parity settings for ser_setparity().
168 * \note Values are AVR-specific for performance reasons.
169 * Other processors should either decode them or
170 * redefine these macros.
173 #define SER_PARITY_NONE 0
174 #define SER_PARITY_EVEN 2
175 #define SER_PARITY_ODD 3
179 * \name Serial hw numbers
185 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
189 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
193 // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
201 #error unknown architecture
203 SER_CNT /*!< Number of serial ports */
208 struct SerialHardware;
210 /*! Human-readable serial error descriptions */
211 extern const char * const serial_errors[8];
213 /*! Serial handle structure */
216 /*! Physical port number */
224 * \name Transmit and receive FIFOs.
226 * Declared volatile because handled asinchronously by interrupts.
234 #if CONFIG_SER_RXTIMEOUT != -1
237 #if CONFIG_SER_TXTIMEOUT != -1
241 /*! Holds the flags defined above. Will be 0 when no errors have occurred. */
244 /*! Low-level interface to hardware. */
245 struct SerialHardware* hw;
249 /* Function prototypes */
250 extern int ser_putchar(int c, struct Serial *port);
251 extern int ser_getchar(struct Serial *port);
252 extern int ser_getchar_nowait(struct Serial *port);
254 extern int ser_write(struct Serial *port, const void *buf, size_t len);
255 extern int ser_read(struct Serial *port, void *buf, size_t size);
257 extern int ser_print(struct Serial *port, const char *s);
258 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
260 extern int ser_gets(struct Serial *port, char *buf, int size);
261 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
263 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
264 extern void ser_setparity(struct Serial *port, int parity);
265 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
266 extern void ser_resync(struct Serial *port, mtime_t delay);
267 extern void ser_purge(struct Serial *port);
268 extern void ser_drain(struct Serial *port);
270 extern struct Serial *ser_open(unsigned int unit);
271 extern void ser_close(struct Serial *port);
274 * \name Additional functions implemented as macros
278 #define ser_getstatus(h) ((h)->status)
279 #define ser_setstatus(h, x) ((h)->status = (x))
282 #endif /* DRV_SER_H */