Remove cvs log.
[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 /*#*
41  *#* $Log$
42  *#* Revision 1.36  2007/01/29 11:30:30  batt
43  *#* Reimplement ser_clearstatus as a macro.
44  *#*
45  *#* Revision 1.35  2007/01/27 20:47:12  batt
46  *#* Add clear status.
47  *#*
48  *#* Revision 1.34  2006/11/20 15:07:40  batt
49  *#* Revert unneeded locked functions.
50  *#*
51  *#* Revision 1.33  2006/11/17 17:03:58  batt
52  *#* Implement ser_setstatus and ser_getstatus as functions to avoid race conditions.
53  *#*
54  *#* Revision 1.32  2006/11/17 16:01:12  batt
55  *#* Serial status MUST be volatile.
56  *#*
57  *#* Revision 1.31  2006/09/13 18:22:48  bernie
58  *#* Typo.
59  *#*
60  *#* Revision 1.30  2006/09/13 18:21:23  bernie
61  *#* Add configurable SPI pin mapping.
62  *#*
63  *#* Revision 1.29  2006/09/13 13:59:11  bernie
64  *#* Documentation fixes.
65  *#*
66  *#* Revision 1.28  2006/07/21 10:58:00  batt
67  *#* Use timer_clock() instead of obsolete timer_ticks().
68  *#*
69  *#* Revision 1.27  2006/07/19 12:56:26  bernie
70  *#* Convert to new Doxygen style.
71  *#*
72  *#* Revision 1.26  2006/05/18 00:39:13  bernie
73  *#* Add struct Serial friendly typedef.
74  *#*
75  *#* Revision 1.25  2006/02/17 22:28:00  bernie
76  *#* Rename ser_emul.c to ser_posix.c.
77  *#*
78  *#* Revision 1.24  2006/02/17 22:23:06  bernie
79  *#* Update POSIX serial emulator.
80  *#*
81  *#* Revision 1.23  2005/11/27 23:33:40  bernie
82  *#* Use appconfig.h instead of cfg/config.h.
83  *#*
84  *#* Revision 1.22  2005/11/04 16:20:02  bernie
85  *#* Fix reference to README.devlib in header.
86  *#*
87  *#* Revision 1.21  2005/04/11 19:10:27  bernie
88  *#* Include top-level headers from cfg/ subdir.
89  *#*
90  *#* Revision 1.20  2005/01/22 04:20:01  bernie
91  *#* Reformat.
92  *#*
93  *#* Revision 1.19  2005/01/11 18:08:16  aleph
94  *#* Add missing include
95  *#*
96  *#* Revision 1.18  2004/12/08 08:57:17  bernie
97  *#* Rename time_t to mtime_t.
98  *#*
99  *#* Revision 1.17  2004/11/16 21:54:56  bernie
100  *#* Changes for SC Monoboard support.
101  *#*
102  *#* Revision 1.16  2004/10/19 11:48:05  bernie
103  *#* Reformat.
104  *#*
105  *#* Revision 1.15  2004/10/19 08:11:53  bernie
106  *#* SERRF_TX, SERRF_RX: New macros; Enhance documentation.
107  *#*
108  *#* Revision 1.14  2004/10/03 18:43:18  bernie
109  *#* Fix a nasty bug caused by confusion between old-style and new-style configuration macros.
110  *#*
111  *#* Revision 1.13  2004/09/14 21:04:57  bernie
112  *#* Don't vanely call kdebug.h.
113  *#*
114  *#* Revision 1.12  2004/09/06 21:40:50  bernie
115  *#* Move buffer handling in chip-specific driver.
116  *#*
117  *#* Revision 1.11  2004/08/25 14:12:08  rasky
118  *#* Aggiornato il comment block dei log RCS
119  *#*
120  *#* Revision 1.10  2004/08/24 16:20:48  bernie
121  *#* ser_read(): Make buffer argument void *#* for consistency with ANSI C and ser_write()
122  *#*
123  *#* Revision 1.9  2004/08/15 05:32:22  bernie
124  *#* ser_resync(): New function.
125  *#*
126  *#* Revision 1.8  2004/08/02 20:20:29  aleph
127  *#* Merge from project_ks
128  *#*
129  *#* Revision 1.7  2004/07/30 14:15:53  rasky
130  *#* Nuovo supporto unificato per detect della CPU
131  *#*
132  *#* Revision 1.6  2004/07/29 22:57:09  bernie
133  *#* ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
134  *#*
135  *#* Revision 1.5  2004/07/18 21:54:23  bernie
136  *#* Add ATmega8 support.
137  *#*
138  *#* Revision 1.4  2004/06/03 11:27:09  bernie
139  *#* Add dual-license information.
140  *#*
141  *#* Revision 1.3  2004/06/02 21:35:24  aleph
142  *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
143  *#*
144  *#* Revision 1.2  2004/05/23 18:21:53  bernie
145  *#* Trim CVS logs and cleanup header info.
146  *#*
147  *#*/
148 #ifndef DRV_SER_H
149 #define DRV_SER_H
150
151
152 #include <mware/fifobuf.h>
153 #include <cfg/compiler.h>
154
155 #include CPU_HEADER(ser)
156
157 #include <appconfig.h>
158
159
160
161 /**
162  * \name Masks to group TX/RX errors.
163  * \{
164  */
165 #define SERRF_RX \
166         ( SERRF_RXFIFOOVERRUN \
167         | SERRF_RXTIMEOUT \
168         | SERRF_RXSROVERRUN \
169         | SERRF_PARITYERROR \
170         | SERRF_FRAMEERROR \
171         | SERRF_NOISEERROR)
172 #define SERRF_TX  (SERRF_TXTIMEOUT)
173 /*\}*/
174
175 /**
176  * \name LSB or MSB first data order for SPI driver.
177  * \{
178  */
179 #define SER_MSB_FIRST 0
180 #define SER_LSB_FIRST 1
181 /*\}*/
182
183 /**
184  * \name Parity settings for ser_setparity().
185  *
186  * \note Values are AVR-specific for performance reasons.
187  *       Other processors should either decode them or
188  *       redefine these macros.
189  * \{
190  */
191 #define SER_PARITY_NONE  0
192 #define SER_PARITY_EVEN  2
193 #define SER_PARITY_ODD   3
194 /*\}*/
195
196
197 struct SerialHardware;
198
199 /** Human-readable serial error descriptions */
200 extern const char * const serial_errors[8];
201
202 /** Serial handle structure */
203 typedef struct Serial
204 {
205         /** Physical port number */
206         unsigned int unit;
207
208 #ifdef _DEBUG
209         bool is_open;
210 #endif
211
212         /**
213          * \name Transmit and receive FIFOs.
214          *
215          * Declared volatile because handled asinchronously by interrupts.
216          *
217          * \{
218          */
219         FIFOBuffer txfifo;
220         FIFOBuffer rxfifo;
221         /* \} */
222
223 #if CONFIG_SER_RXTIMEOUT != -1
224         ticks_t rxtimeout;
225 #endif
226 #if CONFIG_SER_TXTIMEOUT != -1
227         ticks_t txtimeout;
228 #endif
229
230         /** Holds the flags defined above.  Will be 0 when no errors have occurred. */
231         volatile serstatus_t status;
232
233         /** Low-level interface to hardware. */
234         struct SerialHardware* hw;
235 } Serial;
236
237
238 /* Function prototypes */
239 extern int ser_putchar(int c, struct Serial *port);
240 extern int ser_getchar(struct Serial *port);
241 extern int ser_getchar_nowait(struct Serial *port);
242
243 extern int ser_write(struct Serial *port, const void *buf, size_t len);
244 extern int ser_read(struct Serial *port, void *buf, size_t size);
245
246 extern int ser_print(struct Serial *port, const char *s);
247 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
248
249 extern int ser_gets(struct Serial *port, char *buf, int size);
250 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
251
252 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
253 extern void ser_setparity(struct Serial *port, int parity);
254 extern void ser_settimeouts(struct Serial *port, mtime_t rxtimeout, mtime_t txtimeout);
255 extern void ser_resync(struct Serial *port, mtime_t delay);
256 extern void ser_purge(struct Serial *port);
257 extern void ser_drain(struct Serial *port);
258
259 extern struct Serial *ser_open(unsigned int unit);
260 extern void ser_close(struct Serial *port);
261
262 /**
263  * \name Additional functions implemented as macros
264  *
265  * \{
266  */
267 #define ser_getstatus(h)    ((h)->status)
268 #define ser_setstatus(h, x) ((h)->status = (x))
269 #define ser_clearstatus(h)  ser_setstatus(h, 0)
270 /* \} */
271
272 #endif /* DRV_SER_H */