Use appconfig.h instead of cfg/config.h.
[bertos.git] / drv / ser.h
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \brief High level serial I/O API
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.23  2005/11/27 23:33:40  bernie
18  *#* Use appconfig.h instead of cfg/config.h.
19  *#*
20  *#* Revision 1.22  2005/11/04 16:20:02  bernie
21  *#* Fix reference to README.devlib in header.
22  *#*
23  *#* Revision 1.21  2005/04/11 19:10:27  bernie
24  *#* Include top-level headers from cfg/ subdir.
25  *#*
26  *#* Revision 1.20  2005/01/22 04:20:01  bernie
27  *#* Reformat.
28  *#*
29  *#* Revision 1.19  2005/01/11 18:08:16  aleph
30  *#* Add missing include
31  *#*
32  *#* Revision 1.18  2004/12/08 08:57:17  bernie
33  *#* Rename time_t to mtime_t.
34  *#*
35  *#* Revision 1.17  2004/11/16 21:54:56  bernie
36  *#* Changes for SC Monoboard support.
37  *#*
38  *#* Revision 1.16  2004/10/19 11:48:05  bernie
39  *#* Reformat.
40  *#*
41  *#* Revision 1.15  2004/10/19 08:11:53  bernie
42  *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
43  *#*
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.
46  *#*
47  *#* Revision 1.13  2004/09/14 21:04:57  bernie
48  *#* Don't vanely call kdebug.h.
49  *#*
50  *#* Revision 1.12  2004/09/06 21:40:50  bernie
51  *#* Move buffer handling in chip-specific driver.
52  *#*
53  *#* Revision 1.11  2004/08/25 14:12:08  rasky
54  *#* Aggiornato il comment block dei log RCS
55  *#*
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()
58  *#*
59  *#* Revision 1.9  2004/08/15 05:32:22  bernie
60  *#* ser_resync(): New function.
61  *#*
62  *#* Revision 1.8  2004/08/02 20:20:29  aleph
63  *#* Merge from project_ks
64  *#*
65  *#* Revision 1.7  2004/07/30 14:15:53  rasky
66  *#* Nuovo supporto unificato per detect della CPU
67  *#*
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.
70  *#*
71  *#* Revision 1.5  2004/07/18 21:54:23  bernie
72  *#* Add ATmega8 support.
73  *#*
74  *#* Revision 1.4  2004/06/03 11:27:09  bernie
75  *#* Add dual-license information.
76  *#*
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
79  *#*
80  *#* Revision 1.2  2004/05/23 18:21:53  bernie
81  *#* Trim CVS logs and cleanup header info.
82  *#*
83  *#*/
84 #ifndef DRV_SER_H
85 #define DRV_SER_H
86
87 #include <mware/fifobuf.h>
88 #include <cfg/compiler.h>
89 #include <cfg/macros.h> /* BV() */
90 #include <appconfig.h>
91
92 /*! \name Serial Error/status flags. */
93 /*\{*/
94 #if CPU_AVR
95         typedef uint8_t serstatus_t;
96
97         /* Software errors */
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 */
101
102         /*
103          * Hardware errors.
104          * These flags map directly to the AVR UART Status Register (USR).
105          */
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 */
110 #elif CPU_DSP56K
111         typedef uint16_t serstatus_t;
112
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 */
117
118         /*
119          * Hardware errors.
120          * These flags map directly to the SCI Control Register.
121          */
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 */
126 #elif defined(_EMUL)
127         typedef uint16_t serstatus_t;
128
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 */
133
134 #else
135         #error unknown architecture
136 #endif
137 /*\}*/
138
139 /*! \name Masks to group TX/RX errors. */
140 /*\{*/
141 #define SERRF_RX \
142         ( SERRF_RXFIFOOVERRUN \
143         | SERRF_RXTIMEOUT \
144         | SERRF_RXSROVERRUN \
145         | SERRF_PARITYERROR \
146         | SERRF_FRAMEERROR \
147         | SERRF_NOISEERROR)
148 #define SERRF_TX  (SERRF_TXTIMEOUT)
149 /*\}*/
150
151
152 /*!
153  * \name Parity settings for ser_setparity().
154  *
155  * \note Values are AVR-specific for performance reasons.
156  *       Other processors should either decode them or
157  *       redefine these macros.
158  */
159 /*\{*/
160 #define SER_PARITY_NONE  0
161 #define SER_PARITY_EVEN  2
162 #define SER_PARITY_ODD   3
163 /*\}*/
164
165 /*!
166  * \name Serial hw numbers
167  *
168  * \{
169  */
170 enum
171 {
172 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
173         SER_UART0,
174         SER_UART1,
175         SER_SPI,
176 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
177         SER_UART0,
178         SER_SPI,
179 #elif CPU_DSP56K
180         // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
181         SER_UART0,
182         SER_PUNTALI,
183         SER_BARCODE,
184 #elif defined(_EMUL)
185         SER_UART0,
186         #if CONFIG_EMUL_UART1
187                 SER_UART1,
188         #endif
189 #else
190         #error unknown architecture
191 #endif
192         SER_CNT  /*!< Number of serial ports */
193 };
194 /*\}*/
195
196
197 struct SerialHardware;
198
199 /*! Human-readable serial error descriptions */
200 extern const char * const serial_errors[8];
201
202 /*! Serial handle structure */
203 struct Serial
204 {
205         /*! Physical port number */
206         unsigned int unit;
207
208 #ifdef _DEBUG
209         bool is_open;
210 #endif
211
212         /*!
213          * \name Transmit and receive FIFOs.
214          *
215          * Declared volatile because handled asinchronously by interrupts.
216          *
217          * \{
218          */
219         FIFOBuffer txfifo;
220         FIFOBuffer rxfifo;
221         /* \} */
222
223 #if CONFIG_SER_RXTIMEOUT != -1
224         mtime_t rxtimeout;
225 #endif
226 #if CONFIG_SER_TXTIMEOUT != -1
227         mtime_t txtimeout;
228 #endif
229
230         /*! Holds the flags defined above.  Will be 0 when no errors have occurred. */
231         serstatus_t status;
232
233         /*! Low-level interface to hardware. */
234         struct SerialHardware* hw;
235 };
236
237
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);
242
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);
245
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);
248
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);
251
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);
258
259 extern struct Serial *ser_open(unsigned int unit);
260 extern void ser_close(struct Serial *port);
261
262 /*!
263  * \name Additional functions implemented as macros
264  *
265  * \{
266  */
267 #define ser_getstatus(h)    ((h)->status)
268 #define ser_setstatus(h, x) ((h)->status = (x))
269 /* \} */
270
271 #endif /* DRV_SER_H */