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