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