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.20 2005/01/22 04:20:01 bernie
20 *#* Revision 1.19 2005/01/11 18:08:16 aleph
21 *#* Add missing include
23 *#* Revision 1.18 2004/12/08 08:57:17 bernie
24 *#* Rename time_t to mtime_t.
26 *#* Revision 1.17 2004/11/16 21:54:56 bernie
27 *#* Changes for SC Monoboard support.
29 *#* Revision 1.16 2004/10/19 11:48:05 bernie
32 *#* Revision 1.15 2004/10/19 08:11:53 bernie
33 *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
35 *#* Revision 1.14 2004/10/03 18:43:18 bernie
36 *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
38 *#* Revision 1.13 2004/09/14 21:04:57 bernie
39 *#* Don't vanely call kdebug.h.
41 *#* Revision 1.12 2004/09/06 21:40:50 bernie
42 *#* Move buffer handling in chip-specific driver.
44 *#* Revision 1.11 2004/08/25 14:12:08 rasky
45 *#* Aggiornato il comment block dei log RCS
47 *#* Revision 1.10 2004/08/24 16:20:48 bernie
48 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
50 *#* Revision 1.9 2004/08/15 05:32:22 bernie
51 *#* ser_resync(): New function.
53 *#* Revision 1.8 2004/08/02 20:20:29 aleph
54 *#* Merge from project_ks
56 *#* Revision 1.7 2004/07/30 14:15:53 rasky
57 *#* Nuovo supporto unificato per detect della CPU
59 *#* Revision 1.6 2004/07/29 22:57:09 bernie
60 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
62 *#* Revision 1.5 2004/07/18 21:54:23 bernie
63 *#* Add ATmega8 support.
65 *#* Revision 1.4 2004/06/03 11:27:09 bernie
66 *#* Add dual-license information.
68 *#* Revision 1.3 2004/06/02 21:35:24 aleph
69 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
71 *#* Revision 1.2 2004/05/23 18:21:53 bernie
72 *#* Trim CVS logs and cleanup header info.
78 #include <mware/fifobuf.h>
81 #include <macros.h> /* BV() */
83 /*! \name Serial Error/status flags. */
86 typedef uint8_t serstatus_t;
89 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
90 #define SERRF_RXTIMEOUT BV(5) /*!< Receive timeout */
91 #define SERRF_TXTIMEOUT BV(6) /*!< Transmit timeout */
95 * These flags map directly to the AVR UART Status Register (USR).
97 #define SERRF_RXSROVERRUN BV(3) /*!< Rx shift register overrun */
98 #define SERRF_FRAMEERROR BV(4) /*!< Stop bit missing */
99 #define SERRF_PARITYERROR BV(7) /*!< Parity error */
100 #define SERRF_NOISEERROR 0 /*!< Unsupported */
102 typedef uint16_t serstatus_t;
104 /* Software errors */
105 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
106 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
107 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
111 * These flags map directly to the SCI Control Register.
113 #define SERRF_PARITYERROR BV(8) /*!< Parity error */
114 #define SERRF_FRAMEERROR BV(9) /*!< Stop bit missing */
115 #define SERRF_NOISEERROR BV(10) /*!< Noise error */
116 #define SERRF_RXSROVERRUN BV(11) /*!< Rx shift register overrun */
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 */
126 #error unknown architecture
130 /*! \name Masks to group TX/RX errors. */
133 ( SERRF_RXFIFOOVERRUN \
135 | SERRF_RXSROVERRUN \
136 | SERRF_PARITYERROR \
139 #define SERRF_TX (SERRF_TXTIMEOUT)
144 * \name Parity settings for ser_setparity().
146 * \note Values are AVR-specific for performance reasons.
147 * Other processors should either decode them or
148 * redefine these macros.
151 #define SER_PARITY_NONE 0
152 #define SER_PARITY_EVEN 2
153 #define SER_PARITY_ODD 3
157 * \name Serial hw numbers
163 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
167 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
171 // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
177 #if CONFIG_EMUL_UART1
181 #error unknown architecture
183 SER_CNT /*!< Number of serial ports */
188 struct SerialHardware;
190 /*! Human-readable serial error descriptions */
191 extern const char * const serial_errors[8];
193 /*! Serial handle structure */
196 /*! Physical port number */
204 * \name Transmit and receive FIFOs.
206 * Declared volatile because handled asinchronously by interrupts.
214 #if CONFIG_SER_RXTIMEOUT != -1
217 #if CONFIG_SER_TXTIMEOUT != -1
221 /*! Holds the flags defined above. Will be 0 when no errors have occurred. */
224 /*! Low-level interface to hardware. */
225 struct SerialHardware* hw;
229 /* Function prototypes */
230 extern int ser_putchar(int c, struct Serial *port);
231 extern int ser_getchar(struct Serial *port);
232 extern int ser_getchar_nowait(struct Serial *port);
234 extern int ser_write(struct Serial *port, const void *buf, size_t len);
235 extern int ser_read(struct Serial *port, void *buf, size_t size);
237 extern int ser_print(struct Serial *port, const char *s);
238 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
240 extern int ser_gets(struct Serial *port, char *buf, int size);
241 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
243 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
244 extern void ser_setparity(struct Serial *port, int parity);
245 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
246 extern void ser_resync(struct Serial *port, mtime_t delay);
247 extern void ser_purge(struct Serial *port);
248 extern void ser_drain(struct Serial *port);
250 extern struct Serial *ser_open(unsigned int unit);
251 extern void ser_close(struct Serial *port);
254 * \name Additional functions implemented as macros
258 #define ser_getstatus(h) ((h)->status)
259 #define ser_setstatus(h, x) ((h)->status = (x))
262 #endif /* DRV_SER_H */