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