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