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