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.16 2004/10/19 11:48:05 bernie
20 *#* Revision 1.15 2004/10/19 08:11:53 bernie
21 *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
23 *#* Revision 1.14 2004/10/03 18:43:18 bernie
24 *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
26 *#* Revision 1.13 2004/09/14 21:04:57 bernie
27 *#* Don't vanely call kdebug.h.
29 *#* Revision 1.12 2004/09/06 21:40:50 bernie
30 *#* Move buffer handling in chip-specific driver.
32 *#* Revision 1.11 2004/08/25 14:12:08 rasky
33 *#* Aggiornato il comment block dei log RCS
35 *#* Revision 1.10 2004/08/24 16:20:48 bernie
36 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
38 *#* Revision 1.9 2004/08/15 05:32:22 bernie
39 *#* ser_resync(): New function.
41 *#* Revision 1.8 2004/08/02 20:20:29 aleph
42 *#* Merge from project_ks
44 *#* Revision 1.7 2004/07/30 14:15:53 rasky
45 *#* Nuovo supporto unificato per detect della CPU
47 *#* Revision 1.6 2004/07/29 22:57:09 bernie
48 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
50 *#* Revision 1.5 2004/07/18 21:54:23 bernie
51 *#* Add ATmega8 support.
53 *#* Revision 1.4 2004/06/03 11:27:09 bernie
54 *#* Add dual-license information.
56 *#* Revision 1.3 2004/06/02 21:35:24 aleph
57 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
59 *#* Revision 1.2 2004/05/23 18:21:53 bernie
60 *#* Trim CVS logs and cleanup header info.
66 #include <mware/fifobuf.h>
70 /*! \name Serial Error/status flags. */
73 typedef uint8_t serstatus_t;
76 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
77 #define SERRF_RXTIMEOUT BV(5) /*!< Receive timeout */
78 #define SERRF_TXTIMEOUT BV(6) /*!< Transmit timeout */
82 * These flags map directly to the AVR UART Status Register (USR).
84 #define SERRF_RXSROVERRUN BV(3) /*!< Rx shift register overrun */
85 #define SERRF_FRAMEERROR BV(4) /*!< Stop bit missing */
86 #define SERRF_PARITYERROR BV(7) /*!< Parity error */
87 #define SERRF_NOISEERROR 0 /*!< Unsupported */
89 typedef uint16_t serstatus_t;
92 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
93 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
94 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
98 * These flags map directly to the SCI Control Register.
100 #define SERRF_PARITYERROR BV(8) /*!< Parity error */
101 #define SERRF_FRAMEERROR BV(9) /*!< Stop bit missing */
102 #define SERRF_NOISEERROR BV(10) /*!< Noise error */
103 #define SERRF_RXSROVERRUN BV(11) /*!< Rx shift register overrun */
105 #error unknown architecture
109 /*! \name Masks to group TX/RX errors. */
111 #define SERRF_RX (SERRF_RXFIFOOVERRUN \
113 | SERRF_RXSROVERRUN \
114 | SERRF_PARITYERROR \
117 #define SERRF_TX (SERRF_TXTIMEOUT)
122 * \name Parity settings for ser_setparity().
124 * \note Values are AVR-specific for performance reasons.
125 * Other processors should either decode them or
126 * redefine these macros.
129 #define SER_PARITY_NONE 0
130 #define SER_PARITY_EVEN 2
131 #define SER_PARITY_ODD 3
135 * \name Serial hw numbers
141 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
145 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
152 #error unknown architecture
154 SER_CNT /*!< Number of serial ports */
159 struct SerialHardware;
161 /*! Human-readable serial error descriptions */
162 extern const char * const serial_errors[8];
164 /*! Serial handle structure */
167 /*! Physical port number */
175 * \name Transmit and receive FIFOs.
177 * Declared volatile because handled asinchronously by interrupts.
185 #if CONFIG_SER_RXTIMEOUT != -1
188 #if CONFIG_SER_TXTIMEOUT != -1
192 /*! Holds the flags defined above. Will be 0 when no errors have occurred. */
195 /*! Low-level interface to hardware. */
196 struct SerialHardware* hw;
200 /* Function prototypes */
201 extern int ser_putchar(int c, struct Serial *port);
202 extern int ser_getchar(struct Serial *port);
203 extern int ser_getchar_nowait(struct Serial *port);
205 extern int ser_write(struct Serial *port, const void *buf, size_t len);
206 extern int ser_read(struct Serial *port, void *buf, size_t size);
208 extern int ser_print(struct Serial *port, const char *s);
209 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
211 extern int ser_gets(struct Serial *port, char *buf, int size);
212 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
214 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
215 extern void ser_setparity(struct Serial *port, int parity);
216 extern void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout);
217 extern void ser_resync(struct Serial *port, time_t delay);
218 extern void ser_purge(struct Serial *port);
219 extern void ser_drain(struct Serial *port);
221 extern struct Serial *ser_open(unsigned int unit);
222 extern void ser_close(struct Serial *port);
225 * \name Additional functions implemented as macros
229 #define ser_getstatus(h) ((h)->status)
230 #define ser_setstatus(h, x) ((h)->status = (x))
233 #endif /* DRV_SER_H */