Convert clearstatus macro in function.
[bertos.git] / drv / ser.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief High level serial I/O API
35  *
36  * \version $Id$
37  * \author Bernardo Innocenti <bernie@develer.com>
38  */
39
40 #ifndef DRV_SER_H
41 #define DRV_SER_H
42
43
44 #include <mware/fifobuf.h>
45 #include <cfg/compiler.h>
46
47 #include CPU_HEADER(ser)
48
49 #include <appconfig.h>
50
51
52
53 /**
54  * \name Masks to group TX/RX errors.
55  * \{
56  */
57 #define SERRF_RX \
58         ( SERRF_RXFIFOOVERRUN \
59         | SERRF_RXTIMEOUT \
60         | SERRF_RXSROVERRUN \
61         | SERRF_PARITYERROR \
62         | SERRF_FRAMEERROR \
63         | SERRF_NOISEERROR)
64 #define SERRF_TX  (SERRF_TXTIMEOUT)
65 /*\}*/
66
67 /**
68  * \name LSB or MSB first data order for SPI driver.
69  * \{
70  */
71 #define SER_MSB_FIRST 0
72 #define SER_LSB_FIRST 1
73 /*\}*/
74
75 /**
76  * \name Parity settings for ser_setparity().
77  *
78  * \note Values are AVR-specific for performance reasons.
79  *       Other processors should either decode them or
80  *       redefine these macros.
81  * \{
82  */
83 #define SER_PARITY_NONE  0
84 #define SER_PARITY_EVEN  2
85 #define SER_PARITY_ODD   3
86 /*\}*/
87
88
89 struct SerialHardware;
90
91 /** Human-readable serial error descriptions */
92 extern const char * const serial_errors[8];
93
94 /** Serial handle structure */
95 typedef struct Serial
96 {
97         /** Physical port number */
98         unsigned int unit;
99
100 #ifdef _DEBUG
101         bool is_open;
102 #endif
103
104         /**
105          * \name Transmit and receive FIFOs.
106          *
107          * Declared volatile because handled asinchronously by interrupts.
108          *
109          * \{
110          */
111         FIFOBuffer txfifo;
112         FIFOBuffer rxfifo;
113         /* \} */
114
115 #if CONFIG_SER_RXTIMEOUT != -1
116         ticks_t rxtimeout;
117 #endif
118 #if CONFIG_SER_TXTIMEOUT != -1
119         ticks_t txtimeout;
120 #endif
121
122         /** Holds the flags defined above.  Will be 0 when no errors have occurred. */
123         volatile serstatus_t status;
124
125         /** Low-level interface to hardware. */
126         struct SerialHardware* hw;
127 } Serial;
128
129
130 /* Function prototypes */
131 extern int ser_putchar(int c, struct Serial *port);
132 extern int ser_getchar(struct Serial *port);
133 extern int ser_getchar_nowait(struct Serial *port);
134
135 extern int ser_write(struct Serial *port, const void *buf, size_t len);
136 extern int ser_read(struct Serial *port, void *buf, size_t size);
137
138 extern int ser_print(struct Serial *port, const char *s);
139 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
140
141 extern int ser_gets(struct Serial *port, char *buf, int size);
142 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
143 extern void ser_clearstatus(struct Serial *port);
144
145 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
146 extern void ser_setparity(struct Serial *port, int parity);
147 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
148 extern void ser_resync(struct Serial *port, mtime_t delay);
149 extern void ser_purge(struct Serial *port);
150 extern void ser_drain(struct Serial *port);
151
152 extern struct Serial *ser_open(unsigned int unit);
153 extern void ser_close(struct Serial *port);
154
155 /**
156  * \name Additional functions implemented as macros
157  *
158  * \{
159  */
160 #define ser_getstatus(h)    ((h)->status)
161 #define ser_setstatus(h, x) ((h)->status = (x))
162 /* \} */
163
164 #endif /* DRV_SER_H */