1 /* $Id: serial.h,v 1.3 2003/04/29 20:28:05 fredette Exp $ */
2 
3 /* tme/generic/serial.h - header file for a generic serial support: */
4 
5 /*
6  * Copyright (c) 2003 Matt Fredette
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Matt Fredette.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _TME_GENERIC_SERIAL_H
37 #define _TME_GENERIC_SERIAL_H
38 
39 #include <tme/common.h>
40 _TME_RCSID("$Id: serial.h,v 1.3 2003/04/29 20:28:05 fredette Exp $");
41 
42 /* includes: */
43 #include <tme/element.h>
44 
45 /* macros: */
46 
47 /* parity types: */
48 #define TME_SERIAL_PARITY_NONE		(0)
49 #define TME_SERIAL_PARITY_ODD		(1)
50 #define TME_SERIAL_PARITY_EVEN		(2)
51 
52 /* config flags: */
53 #define TME_SERIAL_FLAGS_CHECK_PARITY	TME_BIT(0)
54 
55 /* serial port controls: */
56 #define TME_SERIAL_CTRL_DTR		TME_BIT(0)
57 #define TME_SERIAL_CTRL_RTS		TME_BIT(1)
58 #define TME_SERIAL_CTRL_DCD		TME_BIT(2)
59 #define TME_SERIAL_CTRL_CTS		TME_BIT(3)
60 #define TME_SERIAL_CTRL_BREAK		TME_BIT(4)
61 #define TME_SERIAL_CTRL_RI		TME_BIT(5)
62 #define TME_SERIAL_CTRL_OK_READ		TME_BIT(6)
63 
64 /* special data flags: */
65 #define TME_SERIAL_DATA_NORMAL		(0)
66 #define TME_SERIAL_DATA_BAD_FRAME	TME_BIT(0)
67 #define TME_SERIAL_DATA_BAD_PARITY	TME_BIT(1)
68 #define TME_SERIAL_DATA_OVERRUN		TME_BIT(2)
69 
70 /* copyin/copyout flags: */
71 #define TME_SERIAL_COPY_NORMAL		(0)
72 #define TME_SERIAL_COPY_FULL_IS_OVERRUN	TME_BIT(0)
73 #define TME_SERIAL_COPY_PEEK		TME_BIT(1)
74 
75 /* this evaluates to nonzero iff a buffer is empty: */
76 #define tme_serial_buffer_is_empty(b)	\
77   ((b)->tme_serial_buffer_head		\
78    == ((b)->tme_serial_buffer_tail))
79 
80 /* this evaluates to nonzero iff a buffer is full: */
81 #define tme_serial_buffer_is_full(b)	\
82   ((((b)->tme_serial_buffer_head + 1)	\
83     & ((b)->tme_serial_buffer_size - 1))\
84    == (b)->tme_serial_buffer_tail)
85 
86 /* types: */
87 typedef tme_uint8_t tme_serial_data_flags_t;
88 
89 /* a serial configuration: */
90 struct tme_serial_config {
91 
92   /* the baud rate: */
93   tme_uint32_t tme_serial_config_baud;
94 
95   /* the number of data bits per character: */
96   tme_uint8_t tme_serial_config_bits_data;
97 
98   /* the number of stop bits per character: */
99   tme_uint8_t tme_serial_config_bits_stop;
100 
101   /* the parity: */
102   tme_uint8_t tme_serial_config_parity;
103 
104   /* flags: */
105   tme_uint8_t tme_serial_config_flags;
106 };
107 
108 /* a serial connection: */
109 struct tme_serial_connection {
110 
111   /* the generic connection side: */
112   struct tme_connection tme_serial_connection;
113 
114   /* this is called when the serial configuration changes: */
115   int (*tme_serial_connection_config) _TME_P((struct tme_serial_connection *,
116 					      struct tme_serial_config *));
117 
118   /* this is called when control lines change: */
119   int (*tme_serial_connection_ctrl) _TME_P((struct tme_serial_connection *,
120 					    unsigned int));
121 
122   /* this is called to read data: */
123   int (*tme_serial_connection_read) _TME_P((struct tme_serial_connection *,
124 					    tme_uint8_t *, unsigned int,
125 					    tme_serial_data_flags_t *));
126 };
127 
128 /* a serial buffer.  it is assumed that the user provides locking: */
129 struct tme_serial_buffer {
130 
131   /* the buffer size.  this must always be a power of two: */
132   unsigned int tme_serial_buffer_size;
133 
134   /* our head and tail pointers: */
135   unsigned int tme_serial_buffer_head;
136   unsigned int tme_serial_buffer_tail;
137 
138   /* our buffer data: */
139   tme_uint8_t *tme_serial_buffer_data;
140 
141   /* our buffer data flags: */
142   tme_serial_data_flags_t *tme_serial_buffer_data_flags;
143 };
144 
145 /* prototypes: */
146 int tme_serial_buffer_init _TME_P((struct tme_serial_buffer *, unsigned int));
147 unsigned int tme_serial_buffer_copyin _TME_P((struct tme_serial_buffer *,
148 					      _tme_const tme_uint8_t *,
149 					      unsigned int,
150 					      tme_serial_data_flags_t,
151 					      int));
152 unsigned int tme_serial_buffer_copyout _TME_P((struct tme_serial_buffer *,
153 					       tme_uint8_t *,
154 					       unsigned int,
155 					       tme_serial_data_flags_t *,
156 					       int));
157 unsigned int tme_serial_buffer_space_busy _TME_P((_tme_const struct tme_serial_buffer *));
158 unsigned int tme_serial_buffer_space_free _TME_P((_tme_const struct tme_serial_buffer *));
159 #endif /* !_TME_GENERIC_SERIAL_H */
160