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