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