ChibiOS/HAL 9.0.0
hal_channels.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file hal_channels.h
19 * @brief I/O channels access.
20 * @details This header defines an abstract interface useful to access generic
21 * I/O serial devices in a standardized way.
22 *
23 * @addtogroup IO_CHANNEL
24 * @details This module defines an abstract interface for I/O channels by
25 * extending the @p BaseSequentialStream interface.<br>
26 * Note that no code is present, I/O channels are just abstract
27 * interface like structures, you should look at the systems as
28 * to a set of abstract C++ classes (even if written in C).
29 * Specific device drivers can use/extend the interface and
30 * implement them.<br>
31 * This system has the advantage to make the access to channels
32 * independent from the implementation logic.
33 * @{
34 */
35
36#ifndef HAL_CHANNELS_H
37#define HAL_CHANNELS_H
38
39/**
40 * @name Default control operation codes.
41 * @{
42 */
43#define CHN_CTL_INVALID 0 /**< @brief Invalid operation code. */
44#define CHN_CTL_NOP 1 /**< @brief Does nothing. */
45#define CHN_CTL_TX_WAIT 2 /**< @brief Wait for TX completion. */
46/** @} */
47
48/**
49 * @brief @p BaseChannel specific methods.
50 */
51#define _base_channel_methods \
52 _base_sequential_stream_methods \
53 /* Channel put method with timeout specification.*/ \
54 msg_t (*putt)(void *instance, uint8_t b, sysinterval_t time); \
55 /* Channel get method with timeout specification.*/ \
56 msg_t (*gett)(void *instance, sysinterval_t time); \
57 /* Channel write method with timeout specification.*/ \
58 size_t (*writet)(void *instance, const uint8_t *bp, \
59 size_t n, sysinterval_t time); \
60 /* Channel read method with timeout specification.*/ \
61 size_t (*readt)(void *instance, uint8_t *bp, size_t n, \
62 sysinterval_t time); \
63 /* Channel control method.*/ \
64 msg_t (*ctl)(void *instance, unsigned int operation, void *arg);
65
66/**
67 * @brief @p BaseChannel specific data.
68 * @note It is empty because @p BaseChannel is only an interface without
69 * implementation.
70 */
71#define _base_channel_data \
72 _base_sequential_stream_data
73
74/**
75 * @extends BaseSequentialStreamVMT
76 *
77 * @brief @p BaseChannel virtual methods table.
78 */
82
83/**
84 * @extends BaseSequentialStream
85 *
86 * @brief Base channel class.
87 * @details This class represents a generic, byte-wide, I/O channel. This class
88 * introduces generic I/O primitives with timeout specification.
89 */
90typedef struct {
91 /** @brief Virtual Methods Table.*/
92 const struct BaseChannelVMT *vmt;
95
96/**
97 * @name Macro Functions (BaseChannel)
98 * @{
99 */
100/**
101 * @brief Channel blocking byte write with timeout.
102 * @details This function writes a byte value to a channel. If the channel
103 * is not ready to accept data then the calling thread is suspended.
104 *
105 * @param[in] ip pointer to a @p BaseChannel or derived class
106 * @param[in] b the byte value to be written to the channel
107 * @param[in] time the number of ticks before the operation timeouts,
108 * the following special values are allowed:
109 * - @a TIME_IMMEDIATE immediate timeout.
110 * - @a TIME_INFINITE no timeout.
111 * @return The operation status.
112 * @retval STM_OK if the operation succeeded.
113 * @retval STM_TIMEOUT if the specified time expired.
114 * @retval STM_RESET if the channel associated queue (if any) was reset.
115 *
116 * @api
117 */
118#define chnPutTimeout(ip, b, time) ((ip)->vmt->putt(ip, b, time))
119
120/**
121 * @brief Channel blocking byte read with timeout.
122 * @details This function reads a byte value from a channel. If the data
123 * is not available then the calling thread is suspended.
124 *
125 * @param[in] ip pointer to a @p BaseChannel or derived class
126 * @param[in] time the number of ticks before the operation timeouts,
127 * the following special values are allowed:
128 * - @a TIME_IMMEDIATE immediate timeout.
129 * - @a TIME_INFINITE no timeout.
130 * @return A byte value from the queue.
131 * @retval STM_TIMEOUT if the specified time expired.
132 * @retval STM_RESET if the channel associated queue (if any) has been
133 * reset.
134 *
135 * @api
136 */
137#define chnGetTimeout(ip, time) ((ip)->vmt->gett(ip, time))
138
139/**
140 * @brief Channel blocking write.
141 * @details The function writes data from a buffer to a channel. If the channel
142 * is not ready to accept data then the calling thread is suspended.
143 *
144 * @param[in] ip pointer to a @p BaseChannel or derived class
145 * @param[out] bp pointer to the data buffer
146 * @param[in] n the maximum amount of data to be transferred
147 *
148 * @return The number of bytes transferred.
149 *
150 * @api
151 */
152#define chnWrite(ip, bp, n) streamWrite(ip, bp, n)
153
154/**
155 * @brief Channel blocking write with timeout.
156 * @details The function writes data from a buffer to a channel. If the channel
157 * is not ready to accept data then the calling thread is suspended.
158 *
159 * @param[in] ip pointer to a @p BaseChannel or derived class
160 * @param[out] bp pointer to the data buffer
161 * @param[in] n the maximum amount of data to be transferred
162 * @param[in] time the number of ticks before the operation timeouts,
163 * the following special values are allowed:
164 * - @a TIME_IMMEDIATE immediate timeout.
165 * - @a TIME_INFINITE no timeout.
166 * @return The number of bytes transferred.
167 *
168 * @api
169 */
170#define chnWriteTimeout(ip, bp, n, time) ((ip)->vmt->writet(ip, bp, n, time))
171
172/**
173 * @brief Channel blocking read.
174 * @details The function reads data from a channel into a buffer. If the data
175 * is not available then the calling thread is suspended.
176 *
177 * @param[in] ip pointer to a @p BaseChannel or derived class
178 * @param[in] bp pointer to the data buffer
179 * @param[in] n the maximum amount of data to be transferred
180 *
181 * @return The number of bytes transferred.
182 *
183 * @api
184 */
185#define chnRead(ip, bp, n) streamRead(ip, bp, n)
186
187/**
188 * @brief Channel blocking read with timeout.
189 * @details The function reads data from a channel into a buffer. If the data
190 * is not available then the calling thread is suspended.
191 *
192 * @param[in] ip pointer to a @p BaseChannel or derived class
193 * @param[in] bp pointer to the data buffer
194 * @param[in] n the maximum amount of data to be transferred
195 * @param[in] time the number of ticks before the operation timeouts,
196 * the following special values are allowed:
197 * - @a TIME_IMMEDIATE immediate timeout.
198 * - @a TIME_INFINITE no timeout.
199 * @return The number of bytes transferred.
200 *
201 * @api
202 */
203#define chnReadTimeout(ip, bp, n, time) ((ip)->vmt->readt(ip, bp, n, time))
204
205/**
206 * @brief Control operation on a channel.
207 *
208 * @param[in] ip pointer to a @p BaseChannel or derived class
209 * @param[in] operation control operation code
210 * @param[in,out] arg operation argument
211 *
212 * @return The control operation status.
213 * @retval MSG_OK in case of success.
214 * @retval MSG_TIMEOUT in case of operation timeout.
215 * @retval MSG_RESET in case of operation reset.
216 *
217 * @api
218 */
219#define chnControl(ip, operation, arg) ((ip)->vmt->ctl(ip, operation, arg))
220/** @} */
221
222/**
223 * @name I/O status flags added to the event listener
224 * @{
225 */
226/** @brief No pending conditions.*/
227#define CHN_NO_ERROR (eventflags_t)0
228/** @brief Connection happened.*/
229#define CHN_CONNECTED (eventflags_t)1
230/** @brief Disconnection happened.*/
231#define CHN_DISCONNECTED (eventflags_t)2
232/** @brief Data available in the input queue.*/
233#define CHN_INPUT_AVAILABLE (eventflags_t)4
234/** @brief Output queue empty.*/
235#define CHN_OUTPUT_EMPTY (eventflags_t)8
236/** @brief Transmission end.*/
237#define CHN_TRANSMISSION_END (eventflags_t)16
238/** @brief Parity error.*/
239#define CHN_PARITY_ERROR (eventflags_t)32
240/** @brief Framing error.*/
241#define CHN_FRAMING_ERROR (eventflags_t)64
242/** @brief Line noise error.*/
243#define CHN_NOISE_ERROR (eventflags_t)128
244/** @brief Overflow error.*/
245#define CHN_OVERRUN_ERROR (eventflags_t)256
246/** @brief RX line idle.*/
247#define CHN_IDLE_DETECTED (eventflags_t)512
248/** @brief LIN Break.*/
249#define CHN_BREAK_DETECTED (eventflags_t)1024
250/**< @brief RX buffer full. */
251#define CHN_BUFFER_FULL_ERROR (eventflags_t)2048
252/** @} */
253
254/**
255 * @brief @p BaseAsynchronousChannel specific methods.
256 */
257#define _base_asynchronous_channel_methods \
258 _base_channel_methods \
259
260/**
261 * @brief @p BaseAsynchronousChannel specific data.
262 */
263#define _base_asynchronous_channel_data \
264 _base_channel_data \
265 /* I/O condition event source.*/ \
266 event_source_t event;
267
268/**
269 * @extends BaseChannelVMT
270 *
271 * @brief @p BaseAsynchronousChannel virtual methods table.
272 */
276
277/**
278 * @extends BaseChannel
279 *
280 * @brief Base asynchronous channel class.
281 * @details This class extends @p BaseChannel by adding event sources fields
282 * for asynchronous I/O for use in an event-driven environment.
283 */
284typedef struct {
285 /** @brief Virtual Methods Table.*/
289
290/**
291 * @name Macro Functions (BaseAsynchronousChannel)
292 * @{
293 */
294/**
295 * @brief Returns the I/O condition event source.
296 * @details The event source is broadcasted when an I/O condition happens.
297 *
298 * @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
299 * class
300 * @return A pointer to an @p EventSource object.
301 *
302 * @api
303 */
304#define chnGetEventSource(ip) (&((ip)->event))
305
306/**
307 * @brief Adds status flags to the listeners's flags mask.
308 * @details This function is usually called from the I/O ISRs in order to
309 * notify I/O conditions such as data events, errors, signal
310 * changes etc.
311 *
312 * @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
313 * class
314 * @param[in] flags condition flags to be added to the listener flags mask
315 *
316 * @iclass
317 */
318#define chnAddFlagsI(ip, flags) { \
319 osalEventBroadcastFlagsI(&(ip)->event, flags); \
320}
321/** @} */
322
323#endif /* HAL_CHANNELS_H */
324
325/** @} */
#define _base_asynchronous_channel_data
BaseAsynchronousChannel specific data.
#define _base_channel_data
BaseChannel specific data.
#define _base_asynchronous_channel_methods
BaseAsynchronousChannel specific methods.
#define _base_channel_methods
BaseChannel specific methods.
Base asynchronous channel class.
const struct BaseAsynchronousChannelVMT * vmt
Virtual Methods Table.
BaseAsynchronousChannel virtual methods table.
Base channel class.
const struct BaseChannelVMT * vmt
Virtual Methods Table.
BaseChannel virtual methods table.