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.23 2005/11/27 23:33:40 bernie
18 *#* Use appconfig.h instead of cfg/config.h.
20 *#* Revision 1.22 2005/11/04 16:20:02 bernie
21 *#* Fix reference to README.devlib in header.
23 *#* Revision 1.21 2005/04/11 19:10:27 bernie
24 *#* Include top-level headers from cfg/ subdir.
26 *#* Revision 1.20 2005/01/22 04:20:01 bernie
29 *#* Revision 1.19 2005/01/11 18:08:16 aleph
30 *#* Add missing include
32 *#* Revision 1.18 2004/12/08 08:57:17 bernie
33 *#* Rename time_t to mtime_t.
35 *#* Revision 1.17 2004/11/16 21:54:56 bernie
36 *#* Changes for SC Monoboard support.
38 *#* Revision 1.16 2004/10/19 11:48:05 bernie
41 *#* Revision 1.15 2004/10/19 08:11:53 bernie
42 *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
44 *#* Revision 1.14 2004/10/03 18:43:18 bernie
45 *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
47 *#* Revision 1.13 2004/09/14 21:04:57 bernie
48 *#* Don't vanely call kdebug.h.
50 *#* Revision 1.12 2004/09/06 21:40:50 bernie
51 *#* Move buffer handling in chip-specific driver.
53 *#* Revision 1.11 2004/08/25 14:12:08 rasky
54 *#* Aggiornato il comment block dei log RCS
56 *#* Revision 1.10 2004/08/24 16:20:48 bernie
57 *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
59 *#* Revision 1.9 2004/08/15 05:32:22 bernie
60 *#* ser_resync(): New function.
62 *#* Revision 1.8 2004/08/02 20:20:29 aleph
63 *#* Merge from project_ks
65 *#* Revision 1.7 2004/07/30 14:15:53 rasky
66 *#* Nuovo supporto unificato per detect della CPU
68 *#* Revision 1.6 2004/07/29 22:57:09 bernie
69 *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
71 *#* Revision 1.5 2004/07/18 21:54:23 bernie
72 *#* Add ATmega8 support.
74 *#* Revision 1.4 2004/06/03 11:27:09 bernie
75 *#* Add dual-license information.
77 *#* Revision 1.3 2004/06/02 21:35:24 aleph
78 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
80 *#* Revision 1.2 2004/05/23 18:21:53 bernie
81 *#* Trim CVS logs and cleanup header info.
87 #include <mware/fifobuf.h>
88 #include <cfg/compiler.h>
89 #include <cfg/macros.h> /* BV() */
90 #include <appconfig.h>
92 /*! \name Serial Error/status flags. */
95 typedef uint8_t serstatus_t;
98 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
99 #define SERRF_RXTIMEOUT BV(5) /*!< Receive timeout */
100 #define SERRF_TXTIMEOUT BV(6) /*!< Transmit timeout */
104 * These flags map directly to the AVR UART Status Register (USR).
106 #define SERRF_RXSROVERRUN BV(3) /*!< Rx shift register overrun */
107 #define SERRF_FRAMEERROR BV(4) /*!< Stop bit missing */
108 #define SERRF_PARITYERROR BV(7) /*!< Parity error */
109 #define SERRF_NOISEERROR 0 /*!< Unsupported */
111 typedef uint16_t serstatus_t;
113 /* Software errors */
114 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
115 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
116 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
120 * These flags map directly to the SCI Control Register.
122 #define SERRF_PARITYERROR BV(8) /*!< Parity error */
123 #define SERRF_FRAMEERROR BV(9) /*!< Stop bit missing */
124 #define SERRF_NOISEERROR BV(10) /*!< Noise error */
125 #define SERRF_RXSROVERRUN BV(11) /*!< Rx shift register overrun */
127 typedef uint16_t serstatus_t;
129 /* Software errors */
130 #define SERRF_RXFIFOOVERRUN BV(0) /*!< Rx FIFO buffer overrun */
131 #define SERRF_RXTIMEOUT BV(1) /*!< Receive timeout */
132 #define SERRF_TXTIMEOUT BV(2) /*!< Transmit timeout */
135 #error unknown architecture
139 /*! \name Masks to group TX/RX errors. */
142 ( SERRF_RXFIFOOVERRUN \
144 | SERRF_RXSROVERRUN \
145 | SERRF_PARITYERROR \
148 #define SERRF_TX (SERRF_TXTIMEOUT)
153 * \name Parity settings for ser_setparity().
155 * \note Values are AVR-specific for performance reasons.
156 * Other processors should either decode them or
157 * redefine these macros.
160 #define SER_PARITY_NONE 0
161 #define SER_PARITY_EVEN 2
162 #define SER_PARITY_ODD 3
166 * \name Serial hw numbers
172 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
176 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
180 // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
186 #if CONFIG_EMUL_UART1
190 #error unknown architecture
192 SER_CNT /*!< Number of serial ports */
197 struct SerialHardware;
199 /*! Human-readable serial error descriptions */
200 extern const char * const serial_errors[8];
202 /*! Serial handle structure */
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. */
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))
271 #endif /* DRV_SER_H */