Reformat.
[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.20  2005/01/22 04:20:01  bernie
18  *#* Reformat.
19  *#*
20  *#* Revision 1.19  2005/01/11 18:08:16  aleph
21  *#* Add missing include
22  *#*
23  *#* Revision 1.18  2004/12/08 08:57:17  bernie
24  *#* Rename time_t to mtime_t.
25  *#*
26  *#* Revision 1.17  2004/11/16 21:54:56  bernie
27  *#* Changes for SC Monoboard support.
28  *#*
29  *#* Revision 1.16  2004/10/19 11:48:05  bernie
30  *#* Reformat.
31  *#*
32  *#* Revision 1.15  2004/10/19 08:11:53  bernie
33  *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
34  *#*
35  *#* Revision 1.14  2004/10/03 18:43:18  bernie
36  *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
37  *#*
38  *#* Revision 1.13  2004/09/14 21:04:57  bernie
39  *#* Don't vanely call kdebug.h.
40  *#*
41  *#* Revision 1.12  2004/09/06 21:40:50  bernie
42  *#* Move buffer handling in chip-specific driver.
43  *#*
44  *#* Revision 1.11  2004/08/25 14:12:08  rasky
45  *#* Aggiornato il comment block dei log RCS
46  *#*
47  *#* Revision 1.10  2004/08/24 16:20:48  bernie
48  *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
49  *#*
50  *#* Revision 1.9  2004/08/15 05:32:22  bernie
51  *#* ser_resync(): New function.
52  *#*
53  *#* Revision 1.8  2004/08/02 20:20:29  aleph
54  *#* Merge from project_ks
55  *#*
56  *#* Revision 1.7  2004/07/30 14:15:53  rasky
57  *#* Nuovo supporto unificato per detect della CPU
58  *#*
59  *#* Revision 1.6  2004/07/29 22:57:09  bernie
60  *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
61  *#*
62  *#* Revision 1.5  2004/07/18 21:54:23  bernie
63  *#* Add ATmega8 support.
64  *#*
65  *#* Revision 1.4  2004/06/03 11:27:09  bernie
66  *#* Add dual-license information.
67  *#*
68  *#* Revision 1.3  2004/06/02 21:35:24  aleph
69  *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
70  *#*
71  *#* Revision 1.2  2004/05/23 18:21:53  bernie
72  *#* Trim CVS logs and cleanup header info.
73  *#*
74  *#*/
75 #ifndef DRV_SER_H
76 #define DRV_SER_H
77
78 #include <mware/fifobuf.h>
79 #include <compiler.h>
80 #include <config.h>
81 #include <macros.h> /* BV() */
82
83 /*! \name Serial Error/status flags. */
84 /*\{*/
85 #if CPU_AVR
86         typedef uint8_t serstatus_t;
87
88         /* Software errors */
89         #define SERRF_RXFIFOOVERRUN  BV(0)  /*!< Rx FIFO buffer overrun */
90         #define SERRF_RXTIMEOUT      BV(5)  /*!< Receive timeout */
91         #define SERRF_TXTIMEOUT      BV(6)  /*!< Transmit timeout */
92
93         /*
94          * Hardware errors.
95          * These flags map directly to the AVR UART Status Register (USR).
96          */
97         #define SERRF_RXSROVERRUN    BV(3)  /*!< Rx shift register overrun */
98         #define SERRF_FRAMEERROR     BV(4)  /*!< Stop bit missing */
99         #define SERRF_PARITYERROR    BV(7)  /*!< Parity error */
100         #define SERRF_NOISEERROR     0      /*!< Unsupported */
101 #elif CPU_DSP56K
102         typedef uint16_t serstatus_t;
103
104         /* Software errors */
105         #define SERRF_RXFIFOOVERRUN  BV(0)  /*!< Rx FIFO buffer overrun */
106         #define SERRF_RXTIMEOUT      BV(1)  /*!< Receive timeout */
107         #define SERRF_TXTIMEOUT      BV(2)  /*!< Transmit timeout */
108
109         /*
110          * Hardware errors.
111          * These flags map directly to the SCI Control Register.
112          */
113         #define SERRF_PARITYERROR    BV(8)  /*!< Parity error */
114         #define SERRF_FRAMEERROR     BV(9)  /*!< Stop bit missing */
115         #define SERRF_NOISEERROR     BV(10) /*!< Noise error */
116         #define SERRF_RXSROVERRUN    BV(11) /*!< Rx shift register overrun */
117 #elif defined(_EMUL)
118         typedef uint16_t serstatus_t;
119
120         /* Software errors */
121         #define SERRF_RXFIFOOVERRUN  BV(0)  /*!< Rx FIFO buffer overrun */
122         #define SERRF_RXTIMEOUT      BV(1)  /*!< Receive timeout */
123         #define SERRF_TXTIMEOUT      BV(2)  /*!< Transmit timeout */
124
125 #else
126         #error unknown architecture
127 #endif
128 /*\}*/
129
130 /*! \name Masks to group TX/RX errors. */
131 /*\{*/
132 #define SERRF_RX \
133         ( SERRF_RXFIFOOVERRUN \
134         | SERRF_RXTIMEOUT \
135         | SERRF_RXSROVERRUN \
136         | SERRF_PARITYERROR \
137         | SERRF_FRAMEERROR \
138         | SERRF_NOISEERROR)
139 #define SERRF_TX  (SERRF_TXTIMEOUT)
140 /*\}*/
141
142
143 /*!
144  * \name Parity settings for ser_setparity().
145  *
146  * \note Values are AVR-specific for performance reasons.
147  *       Other processors should either decode them or
148  *       redefine these macros.
149  */
150 /*\{*/
151 #define SER_PARITY_NONE  0
152 #define SER_PARITY_EVEN  2
153 #define SER_PARITY_ODD   3
154 /*\}*/
155
156 /*!
157  * \name Serial hw numbers
158  *
159  * \{
160  */
161 enum
162 {
163 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
164         SER_UART0,
165         SER_UART1,
166         SER_SPI,
167 #elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8
168         SER_UART0,
169         SER_SPI,
170 #elif CPU_DSP56K
171         // \todo since we now support "fake" multiplexed serials, this should be moved to hw.h
172         SER_UART0,
173         SER_PUNTALI,
174         SER_BARCODE,
175 #elif defined(_EMUL)
176         SER_UART0,
177         #if CONFIG_EMUL_UART1
178                 SER_UART1,
179         #endif
180 #else
181         #error unknown architecture
182 #endif
183         SER_CNT  /*!< Number of serial ports */
184 };
185 /*\}*/
186
187
188 struct SerialHardware;
189
190 /*! Human-readable serial error descriptions */
191 extern const char * const serial_errors[8];
192
193 /*! Serial handle structure */
194 struct Serial
195 {
196         /*! Physical port number */
197         unsigned int unit;
198
199 #ifdef _DEBUG
200         bool is_open;
201 #endif
202
203         /*!
204          * \name Transmit and receive FIFOs.
205          *
206          * Declared volatile because handled asinchronously by interrupts.
207          *
208          * \{
209          */
210         FIFOBuffer txfifo;
211         FIFOBuffer rxfifo;
212         /* \} */
213
214 #if CONFIG_SER_RXTIMEOUT != -1
215         mtime_t rxtimeout;
216 #endif
217 #if CONFIG_SER_TXTIMEOUT != -1
218         mtime_t txtimeout;
219 #endif
220
221         /*! Holds the flags defined above.  Will be 0 when no errors have occurred. */
222         serstatus_t status;
223
224         /*! Low-level interface to hardware. */
225         struct SerialHardware* hw;
226 };
227
228
229 /* Function prototypes */
230 extern int ser_putchar(int c, struct Serial *port);
231 extern int ser_getchar(struct Serial *port);
232 extern int ser_getchar_nowait(struct Serial *port);
233
234 extern int ser_write(struct Serial *port, const void *buf, size_t len);
235 extern int ser_read(struct Serial *port, void *buf, size_t size);
236
237 extern int ser_print(struct Serial *port, const char *s);
238 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
239
240 extern int ser_gets(struct Serial *port, char *buf, int size);
241 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
242
243 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
244 extern void ser_setparity(struct Serial *port, int parity);
245 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
246 extern void ser_resync(struct Serial *port, mtime_t delay);
247 extern void ser_purge(struct Serial *port);
248 extern void ser_drain(struct Serial *port);
249
250 extern struct Serial *ser_open(unsigned int unit);
251 extern void ser_close(struct Serial *port);
252
253 /*!
254  * \name Additional functions implemented as macros
255  *
256  * \{
257  */
258 #define ser_getstatus(h)    ((h)->status)
259 #define ser_setstatus(h, x) ((h)->status = (x))
260 /* \} */
261
262 #endif /* DRV_SER_H */