Nuovo supporto unificato per detect della CPU
[bertos.git] / drv / ser.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief High level serial I/O API
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  */
14
15 /*
16  * $Log$
17  * Revision 1.7  2004/07/30 14:15:53  rasky
18  * Nuovo supporto unificato per detect della CPU
19  *
20  * Revision 1.6  2004/07/29 22:57:09  bernie
21  * ser_drain(): New function; Make Serial::is_open a debug-only feature; Switch to new-style CONFIG_* macros.
22  *
23  * Revision 1.5  2004/07/18 21:54:23  bernie
24  * Add ATmega8 support.
25  *
26  * Revision 1.4  2004/06/03 11:27:09  bernie
27  * Add dual-license information.
28  *
29  * Revision 1.3  2004/06/02 21:35:24  aleph
30  * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
31  *
32  * Revision 1.2  2004/05/23 18:21:53  bernie
33  * Trim CVS logs and cleanup header info.
34  *
35  */
36 #ifndef DRV_SER_H
37 #define DRV_SER_H
38
39 #include <mware/fifobuf.h>
40 #include <drv/kdebug.h>
41 #include <compiler.h>
42 #include <config.h>
43
44 /*!
45  * \name Serial Error/status flags
46  *
47  * Some of these flags map directly to the flags
48  * in AVR UART Status Register(USR).
49  * \todo  flags of DSP56k aren't mapped to these flags. Luckily
50  *        these flags doesn't collide with the DSP56k ones,
51  *        which are from 0x0100 to 0x8000
52  */
53 /*\{*/
54 #if CPU_AVR
55         typedef uint8_t serstatus_t;
56
57         /* Software errors */
58         #define SERRF_RXFIFOOVERRUN  BV(0)  /*!< Rx FIFO buffer overrun */
59         #define SERRF_RXTIMEOUT      BV(5)  /*!< Receive timeout */
60         #define SERRF_TXTIMEOUT      BV(6)  /*!< Transmit timeout */
61
62         /* Hardware errors */
63         #define SERRF_RXSROVERRUN    BV(3)  /*!< Rx shift register overrun */
64         #define SERRF_FRAMEERROR     BV(4)  /*!< Stop bit missing */
65         #define SERRF_PARITYERROR    BV(7)  /*!< Parity error */
66 #elif CPU_DSP56K
67         typedef uint16_t serstatus_t;
68
69         /* Software errors */
70         #define SERRF_RXFIFOOVERRUN  BV(0)  /*!< Rx FIFO buffer overrun */
71         #define SERRF_RXTIMEOUT      BV(1)  /*!< Receive timeout */
72         #define SERRF_TXTIMEOUT      BV(2)  /*!< Transmit timeout */
73
74         /* Hardware errors */
75         #define SERRF_PARITYERROR    BV(8)  /*!< Parity error */
76         #define SERRF_FRAMEERROR     BV(9)  /*!< Stop bit missing */
77         #define SERRF_NOISEERROR     BV(10) /*!< Noise error */
78         #define SERRF_RXSROVERRUN    BV(11) /*!< Rx shift register overrun */
79 #else
80         #error unknown architecture
81 #endif
82 /*\}*/
83
84 /*! \name Parity settings for ser_setparity() */
85 /*\{*/
86 #define SER_PARITY_NONE  0
87 #define SER_PARITY_EVEN  2
88 #define SER_PARITY_ODD   3
89 /*\}*/
90
91 /*!
92  * \name Serial hw numbers
93  *
94  * \{
95  */
96 enum
97 {
98 #if defined(CPU_AVR_ATMEGA64) || defined(CPU_AVR_ATMEGA128)
99         SER_UART0,
100         SER_UART1,
101         SER_SPI,
102 #elif defined(CPU_AVR_ATMEGA103) || defined(CPU_AVR_ATMEGA8)
103         SER_UART0,
104         SER_SPI,
105 #elif CPU_DSP56K
106         SER_UART0,
107         SER_UART1,
108 #else
109         #error unknown architecture
110 #endif
111
112         SER_CNT       /**< Number of serial ports */
113 };
114 /* @} */
115
116
117 struct SerialHardware;
118
119 /*! Human-readable serial error descriptions */
120 extern const char * const serial_errors[8];
121
122 /*! Serial handle structure */
123 struct Serial
124 {
125         /*! Physical port number */
126         unsigned int unit;
127
128         DB(bool is_open;)
129
130         /*!
131          * \name FIFO transmit and receive buffers.
132          *
133          * Declared volatile because handled asinchronously by interrupts.
134          *
135          * \{
136          */
137         FIFOBuffer txfifo;
138         FIFOBuffer rxfifo;
139         unsigned char txbuffer[CONFIG_SER_TXBUFSIZE];
140         unsigned char rxbuffer[CONFIG_SER_RXBUFSIZE];
141         /* \} */
142
143 #if CONFIG_SER_RXTIMEOUT != -1
144         time_t rxtimeout;
145 #endif
146 #if CONFIG_SER_TXTIMEOUT != -1
147         time_t txtimeout;
148 #endif
149
150         /*! Holds the flags defined above.  Will be 0 when no errors have occurred. */
151         serstatus_t status;
152
153         /*! Low-level interface to hardware. */
154         struct SerialHardware* hw;
155 };
156
157
158 /* Function prototypes */
159 extern int ser_putchar(int c, struct Serial *port);
160 extern int ser_getchar(struct Serial *port);
161 extern int ser_getchar_nowait(struct Serial *port);
162
163 extern int ser_write(struct Serial *port, const void *buf, size_t len);
164 extern int ser_read(struct Serial *port, char *buf, size_t size);
165
166 extern int ser_print(struct Serial *port, const char *s);
167 extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3);
168
169 extern int ser_gets(struct Serial *port, char *buf, int size);
170 extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo);
171
172 extern void ser_setbaudrate(struct Serial *port, unsigned long rate);
173 extern void ser_setparity(struct Serial *port, int parity);
174 extern void ser_settimeouts(struct Serial *port, time_t rxtimeout, time_t txtimeout);
175 extern void ser_purge(struct Serial *port);
176 extern void ser_drain(struct Serial *port);
177
178 extern struct Serial *ser_open(unsigned int unit);
179 extern void ser_close(struct Serial *port);
180
181 /*!
182  * \name Additional functions implemented as macros
183  *
184  * \{
185  */
186 #define ser_getstatus(h)    ((h)->status)
187 #define ser_setstatus(h, x) ((h)->status = (x))
188 /* \} */
189
190 #endif /* DRV_SER_H */