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