Add missing include
[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 devlib/README 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.19  2005/01/11 18:08:16  aleph
18  *#* Add missing include
19  *#*
20  *#* Revision 1.18  2004/12/08 08:57:17  bernie
21  *#* Rename time_t to mtime_t.
22  *#*
23  *#* Revision 1.17  2004/11/16 21:54:56  bernie
24  *#* Changes for SC Monoboard support.
25  *#*
26  *#* Revision 1.16  2004/10/19 11:48:05  bernie
27  *#* Reformat.
28  *#*
29  *#* Revision 1.15  2004/10/19 08:11:53  bernie
30  *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
31  *#*
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.
34  *#*
35  *#* Revision 1.13  2004/09/14 21:04:57  bernie
36  *#* Don't vanely call kdebug.h.
37  *#*
38  *#* Revision 1.12  2004/09/06 21:40:50  bernie
39  *#* Move buffer handling in chip-specific driver.
40  *#*
41  *#* Revision 1.11  2004/08/25 14:12:08  rasky
42  *#* Aggiornato il comment block dei log RCS
43  *#*
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()
46  *#*
47  *#* Revision 1.9  2004/08/15 05:32:22  bernie
48  *#* ser_resync(): New function.
49  *#*
50  *#* Revision 1.8  2004/08/02 20:20:29  aleph
51  *#* Merge from project_ks
52  *#*
53  *#* Revision 1.7  2004/07/30 14:15:53  rasky
54  *#* Nuovo supporto unificato per detect della CPU
55  *#*
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.
58  *#*
59  *#* Revision 1.5  2004/07/18 21:54:23  bernie
60  *#* Add ATmega8 support.
61  *#*
62  *#* Revision 1.4  2004/06/03 11:27:09  bernie
63  *#* Add dual-license information.
64  *#*
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
67  *#*
68  *#* Revision 1.2  2004/05/23 18:21:53  bernie
69  *#* Trim CVS logs and cleanup header info.
70  *#*
71  *#*/
72 #ifndef DRV_SER_H
73 #define DRV_SER_H
74
75 #include <mware/fifobuf.h>
76 #include <compiler.h>
77 #include <config.h>
78 #include <macros.h> /* BV() */
79
80 /*! \name Serial Error/status flags. */
81 /*\{*/
82 #if CPU_AVR
83         typedef uint8_t serstatus_t;
84
85         /* Software errors */
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 */
89
90         /*
91          * Hardware errors.
92          * These flags map directly to the AVR UART Status Register (USR).
93          */
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 */
98 #elif CPU_DSP56K
99         typedef uint16_t serstatus_t;
100
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 */
105
106         /*
107          * Hardware errors.
108          * These flags map directly to the SCI Control Register.
109          */
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 */
114 #elif defined(_EMUL)
115         typedef uint16_t serstatus_t;
116
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 */
121
122 #else
123         #error unknown architecture
124 #endif
125 /*\}*/
126
127 /*! \name Masks to group TX/RX errors. */
128 /*\{*/
129 #define SERRF_RX  (SERRF_RXFIFOOVERRUN \
130         | SERRF_RXTIMEOUT \
131         | SERRF_RXSROVERRUN \
132         | SERRF_PARITYERROR \
133         | SERRF_FRAMEERROR \
134         | SERRF_NOISEERROR)
135 #define SERRF_TX  (SERRF_TXTIMEOUT)
136 /*\}*/
137
138
139 /*!
140  * \name Parity settings for ser_setparity().
141  *
142  * \note Values are AVR-specific for performance reasons.
143  *       Other processors should either decode them or
144  *       redefine these macros.
145  */
146 /*\{*/
147 #define SER_PARITY_NONE  0
148 #define SER_PARITY_EVEN  2
149 #define SER_PARITY_ODD   3
150 /*\}*/
151
152 /*!
153  * \name Serial hw numbers
154  *
155  * \{
156  */
157 enum
158 {
159 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
160         SER_UART0,
161         SER_UART1,
162         SER_SPI,
163 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
164         SER_UART0,
165         SER_SPI,
166 #elif CPU_DSP56K
167         // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
168         SER_UART0,
169         SER_PUNTALI,
170         SER_BARCODE,
171 #elif defined(_EMUL)
172         SER_UART0,
173         #if CONFIG_EMUL_UART1
174                 SER_UART1,
175         #endif
176 #else
177         #error unknown architecture
178 #endif
179         SER_CNT  /*!< Number of serial ports */
180 };
181 /*\}*/
182
183
184 struct SerialHardware;
185
186 /*! Human-readable serial error descriptions */
187 extern const char * const serial_errors[8];
188
189 /*! Serial handle structure */
190 struct Serial
191 {
192         /*! Physical port number */
193         unsigned int unit;
194
195 #ifdef _DEBUG
196         bool is_open;
197 #endif
198
199         /*!
200          * \name Transmit and receive FIFOs.
201          *
202          * Declared volatile because handled asinchronously by interrupts.
203          *
204          * \{
205          */
206         FIFOBuffer txfifo;
207         FIFOBuffer rxfifo;
208         /* \} */
209
210 #if CONFIG_SER_RXTIMEOUT != -1
211         mtime_t rxtimeout;
212 #endif
213 #if CONFIG_SER_TXTIMEOUT != -1
214         mtime_t txtimeout;
215 #endif
216
217         /*! Holds the flags defined above.  Will be 0 when no errors have occurred. */
218         serstatus_t status;
219
220         /*! Low-level interface to hardware. */
221         struct SerialHardware* hw;
222 };
223
224
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);
229
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);
232
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);
235
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);
238
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);
245
246 extern struct Serial *ser_open(unsigned int unit);
247 extern void ser_close(struct Serial *port);
248
249 /*!
250  * \name Additional functions implemented as macros
251  *
252  * \{
253  */
254 #define ser_getstatus(h)    ((h)->status)
255 #define ser_setstatus(h, x) ((h)->status = (x))
256 /* \} */
257
258 #endif /* DRV_SER_H */