Spacing fix
[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 #include <kern/kfile.h>
44 #include <mware/fifobuf.h>
45 #include <cfg/compiler.h>
46
47 #if OS_HOSTED
48         typedef uint16_t serstatus_t;
49
50         /* Software errors */
51         #define SERRF_RXFIFOOVERRUN  BV(0)  /**< Rx FIFO buffer overrun */
52         #define SERRF_RXTIMEOUT      BV(1)  /**< Receive timeout */
53         #define SERRF_TXTIMEOUT      BV(2)  /**< Transmit timeout */
54
55         /* Hardware errors */
56         #define SERRF_RXSROVERRUN    0      /**< Unsupported in emulated serial port. */
57         #define SERRF_FRAMEERROR     0      /**< Unsupported in emulated serial port. */
58         #define SERRF_PARITYERROR    0      /**< Unsupported in emulated serial port. */
59         #define SERRF_NOISEERROR     0      /**< Unsupported in emulated serial port. */
60
61         enum
62         {
63                 SER_UART0,
64                 SER_UART1,
65
66                 SER_CNT  /**< Number of serial ports */
67         };
68
69 #else
70         #include CPU_HEADER(ser)
71 #endif
72
73 #include <appconfig.h>
74
75
76
77 /**
78  * \name Masks to group TX/RX errors.
79  * \{
80  */
81 #define SERRF_RX \
82         ( SERRF_RXFIFOOVERRUN \
83         | SERRF_RXTIMEOUT \
84         | SERRF_RXSROVERRUN \
85         | SERRF_PARITYERROR \
86         | SERRF_FRAMEERROR \
87         | SERRF_NOISEERROR)
88 #define SERRF_TX  (SERRF_TXTIMEOUT)
89 /*\}*/
90
91 /**
92  * \name LSB or MSB first data order for SPI driver.
93  * \{
94  */
95 #define SER_MSB_FIRST 0
96 #define SER_LSB_FIRST 1
97 /*\}*/
98
99 /**
100  * \name Parity settings for ser_setparity().
101  *
102  * \note Values are AVR-specific for performance reasons.
103  *       Other processors should either decode them or
104  *       redefine these macros.
105  * \{
106  */
107 #define SER_PARITY_NONE  0
108 #define SER_PARITY_EVEN  2
109 #define SER_PARITY_ODD   3
110 /*\}*/
111
112
113 struct SerialHardware;
114
115 /** Human-readable serial error descriptions */
116 extern const char * const serial_errors[8];
117
118 /** Serial handle structure */
119 typedef struct Serial
120 {
121         /** Physical port number */
122         unsigned int unit;
123
124 #ifdef _DEBUG
125         bool is_open;
126 #endif
127
128         /**
129          * \name Transmit and receive FIFOs.
130          *
131          * Declared volatile because handled asinchronously by interrupts.
132          *
133          * \{
134          */
135         FIFOBuffer txfifo;
136         FIFOBuffer rxfifo;
137         /* \} */
138
139 #if CONFIG_SER_RXTIMEOUT != -1
140         ticks_t rxtimeout;
141 #endif
142 #if CONFIG_SER_TXTIMEOUT != -1
143         ticks_t txtimeout;
144 #endif
145
146         /** Holds the flags defined above.  Will be 0 when no errors have occurred. */
147         volatile serstatus_t status;
148
149         /** Low-level interface to hardware. */
150         struct SerialHardware* hw;
151 } Serial;
152
153 typedef struct KFileSerial
154 {
155         KFile fd;
156         Serial *ser;
157 } KFileSerial;
158
159 INLINE KFileSerial * KFILESERIAL(KFile *fd)
160 {
161         ASSERT(fd->_type == KFT_SERIAL);
162         return (KFileSerial *)fd;
163 }
164
165 /* Function prototypes */
166 //extern int ser_getchar_nowait(struct Serial *port);
167
168 void ser_setbaudrate(struct KFileSerial *fd, unsigned long rate);
169 void ser_setparity(struct KFileSerial *fd, int parity);
170 void ser_settimeouts(struct KFileSerial *fd, mtime_t rxtimeout, mtime_t txtimeout);
171 void ser_resync(struct KFileSerial *fd, mtime_t delay);
172
173 void ser_purgeRx(struct KFileSerial *fd);
174 void ser_purgeTx(struct KFileSerial *fd);
175 void ser_purge(struct KFileSerial *fd);
176 void ser_init(struct KFileSerial *fds, unsigned int unit);
177 void spimaster_init(KFileSerial *fds, unsigned int unit);
178
179
180 /**
181  * \name Additional functions implemented as macros
182  *
183  * \{
184  */
185 #define ser_getstatus(h)    ((h)->status)
186 #define ser_setstatus(h, x) ((h)->status = (x))
187 /* \} */
188
189 #endif /* DRV_SER_H */