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 devlib/README for information.
9 * \brief High level serial I/O API
12 * \author Bernardo Innocenti <bernie@develer.com>
17 *#* Revision 1.21 2005/04/11 19:10:27 bernie
18 *#* Include top-level headers from cfg/ subdir.
20 *#* Revision 1.20 2005/01/22 04:20:01 bernie
23 *#* Revision 1.19 2005/01/11 18:08:16 aleph
24 *#* Add missing include
26 *#* Revision 1.18 2004/12/08 08:57:17 bernie
27 *#* Rename time_t to mtime_t.
29 *#* Revision 1.17 2004/11/16 21:54:56 bernie
30 *#* Changes for SC Monoboard support.
32 *#* Revision 1.16 2004/10/19 11:48:05 bernie
35 *#* Revision 1.15 2004/10/19 08:11:53 bernie
36 *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
38 *#* Revision 1.14 2004/10/03 18:43:18 bernie
39 *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
41 *#* Revision 1.13 2004/09/14 21:04:57 bernie
42 *#* Don't vanely call kdebug.h.
44 *#* Revision 1.12 2004/09/06 21:40:50 bernie
45 *#* Move buffer handling in chip-specific driver.
47 *#* Revision 1.11 2004/08/25 14:12:08 rasky
48 *#* Aggiornato il comment block dei log RCS
50 *#* Revision 1.10 2004/08/24 16:20:48 bernie
51 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
53 *#* Revision 1.9 2004/08/15 05:32:22 bernie
54 *#* ser_resync(): New function.
56 *#* Revision 1.8 2004/08/02 20:20:29 aleph
57 *#* Merge from project_ks
59 *#* Revision 1.7 2004/07/30 14:15:53 rasky
60 *#* Nuovo supporto unificato per detect della CPU
62 *#* Revision 1.6 2004/07/29 22:57:09 bernie
63 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
65 *#* Revision 1.5 2004/07/18 21:54:23 bernie
66 *#* Add ATmega8 support.
68 *#* Revision 1.4 2004/06/03 11:27:09 bernie
69 *#* Add dual-license information.
71 *#* Revision 1.3 2004/06/02 21:35:24 aleph
72 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
74 *#* Revision 1.2 2004/05/23 18:21:53 bernie
75 *#* Trim CVS logs and cleanup header info.
81 #include <mware/fifobuf.h>
82 #include <cfg/compiler.h>
83 #include <cfg/config.h>
84 #include <cfg/macros.h> /* BV() */
86 /*! \name Serial Error/status flags. */
89 typedef uint8_t serstatus_t;
92 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
93 #define SERRF_RXTIMEOUT BV(5) /*!< Receive timeout */
94 #define SERRF_TXTIMEOUT BV(6) /*!< Transmit timeout */
98 * These flags map directly to the AVR UART Status Register (USR).
100 #define SERRF_RXSROVERRUN BV(3) /*!< Rx shift register overrun */
101 #define SERRF_FRAMEERROR BV(4) /*!< Stop bit missing */
102 #define SERRF_PARITYERROR BV(7) /*!< Parity error */
103 #define SERRF_NOISEERROR 0 /*!< Unsupported */
105 typedef uint16_t serstatus_t;
107 /* Software errors */
108 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
109 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
110 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
114 * These flags map directly to the SCI Control Register.
116 #define SERRF_PARITYERROR BV(8) /*!< Parity error */
117 #define SERRF_FRAMEERROR BV(9) /*!< Stop bit missing */
118 #define SERRF_NOISEERROR BV(10) /*!< Noise error */
119 #define SERRF_RXSROVERRUN BV(11) /*!< Rx shift register overrun */
121 typedef uint16_t serstatus_t;
123 /* Software errors */
124 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
125 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
126 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
129 #error unknown architecture
133 /*! \name Masks to group TX/RX errors. */
136 ( SERRF_RXFIFOOVERRUN \
138 | SERRF_RXSROVERRUN \
139 | SERRF_PARITYERROR \
142 #define SERRF_TX (SERRF_TXTIMEOUT)
147 * \name Parity settings for ser_setparity().
149 * \note Values are AVR-specific for performance reasons.
150 * Other processors should either decode them or
151 * redefine these macros.
154 #define SER_PARITY_NONE 0
155 #define SER_PARITY_EVEN 2
156 #define SER_PARITY_ODD 3
160 * \name Serial hw numbers
166 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
170 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
174 // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
180 #if CONFIG_EMUL_UART1
184 #error unknown architecture
186 SER_CNT /*!< Number of serial ports */
191 struct SerialHardware;
193 /*! Human-readable serial error descriptions */
194 extern const char * const serial_errors[8];
196 /*! Serial handle structure */
199 /*! Physical port number */
207 * \name Transmit and receive FIFOs.
209 * Declared volatile because handled asinchronously by interrupts.
217 #if CONFIG_SER_RXTIMEOUT != -1
220 #if CONFIG_SER_TXTIMEOUT != -1
224 /*! Holds the flags defined above. Will be 0 when no errors have occurred. */
227 /*! Low-level interface to hardware. */
228 struct SerialHardware* hw;
232 /* Function prototypes */
233 extern int ser_putchar(int c, struct Serial *port);
234 extern int ser_getchar(struct Serial *port);
235 extern int ser_getchar_nowait(struct Serial *port);
237 extern int ser_write(struct Serial *port, const void *buf, size_t len);
238 extern int ser_read(struct Serial *port, void *buf, size_t size);
240 extern int ser_print(struct Serial *port, const char *s);
241 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
243 extern int ser_gets(struct Serial *port, char *buf, int size);
244 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
246 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
247 extern void ser_setparity(struct Serial *port, int parity);
248 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
249 extern void ser_resync(struct Serial *port, mtime_t delay);
250 extern void ser_purge(struct Serial *port);
251 extern void ser_drain(struct Serial *port);
253 extern struct Serial *ser_open(unsigned int unit);
254 extern void ser_close(struct Serial *port);
257 * \name Additional functions implemented as macros
261 #define ser_getstatus(h) ((h)->status)
262 #define ser_setstatus(h, x) ((h)->status = (x))
265 #endif /* DRV_SER_H */