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