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