ChibiOS/HAL 9.0.0
hal_queues.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_queues.h
19 * @brief I/O Queues macros and structures.
20 *
21 * @addtogroup HAL_QUEUES
22 * @{
23 */
24
25#ifndef HAL_QUEUES_H
26#define HAL_QUEUES_H
27
28/*===========================================================================*/
29/* Driver constants. */
30/*===========================================================================*/
31
32/**
33 * @name Queue functions returned status value
34 * @{
35 */
36#define Q_OK MSG_OK /**< @brief Operation successful. */
37#define Q_TIMEOUT MSG_TIMEOUT /**< @brief Timeout condition. */
38#define Q_RESET MSG_RESET /**< @brief Queue has been reset. */
39#define Q_EMPTY MSG_TIMEOUT /**< @brief Queue empty. */
40#define Q_FULL MSG_TIMEOUT /**< @brief Queue full, */
41/** @} */
42
43/*===========================================================================*/
44/* Driver pre-compile time settings. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Derived constants and error checks. */
49/*===========================================================================*/
50
51/*===========================================================================*/
52/* Driver data structures and types. */
53/*===========================================================================*/
54
55/**
56 * @brief Type of a generic I/O queue structure.
57 */
58typedef struct io_queue io_queue_t;
59
60/**
61 * @brief Queue notification callback type.
62 *
63 * @param[in] qp the queue pointer
64 */
65typedef void (*qnotify_t)(io_queue_t *qp);
66
67/**
68 * @brief Generic I/O queue structure.
69 * @details This structure represents a generic Input or Output asymmetrical
70 * queue. The queue is asymmetrical because one end is meant to be
71 * accessed from a thread context, and thus can be blocking, the other
72 * end is accessible from interrupt handlers or from within a kernel
73 * lock zone and is non-blocking.
74 */
75struct io_queue {
76 threads_queue_t q_waiting; /**< @brief Queue of waiting threads. */
77 volatile size_t q_counter; /**< @brief Resources counter. */
78 uint8_t *q_buffer; /**< @brief Pointer to the queue buffer.*/
79 uint8_t *q_top; /**< @brief Pointer to the first
80 location after the buffer. */
81 uint8_t *q_wrptr; /**< @brief Write pointer. */
82 uint8_t *q_rdptr; /**< @brief Read pointer. */
83 qnotify_t q_notify; /**< @brief Data notification callback. */
84 void *q_link; /**< @brief Application defined field. */
85};
86
87/**
88 * @extends io_queue_t
89 *
90 * @brief Type of an input queue structure.
91 * @details This structure represents a generic asymmetrical input queue.
92 * Writing to the queue is non-blocking and can be performed from
93 * interrupt handlers or from within a kernel lock zone.
94 * Reading the queue can be a blocking operation and is supposed to
95 * be performed by a system thread.
96 */
98
99/**
100 * @extends io_queue_t
101 *
102 * @brief Type of an output queue structure.
103 * @details This structure represents a generic asymmetrical output queue.
104 * Reading from the queue is non-blocking and can be performed from
105 * interrupt handlers or from within a kernel lock zone.
106 * Writing the queue can be a blocking operation and is supposed to
107 * be performed by a system thread.
108 */
110
111/*===========================================================================*/
112/* Driver macros. */
113/*===========================================================================*/
114
115/**
116 * @name Macro Functions
117 * @{
118 */
119/**
120 * @brief Returns the queue's buffer size.
121 *
122 * @param[in] qp pointer to a @p io_queue_t structure
123 * @return The buffer size.
124 *
125 * @xclass
126 */
127#define qSizeX(qp) \
128 /*lint -save -e9033 [10.8] The cast is safe.*/ \
129 ((size_t)((qp)->q_top - (qp)->q_buffer)) \
130 /*lint -restore*/
131
132/**
133 * @brief Queue space.
134 * @details Returns the used space if used on an input queue or the empty
135 * space if used on an output queue.
136 *
137 * @param[in] qp pointer to a @p io_queue_t structure
138 * @return The buffer space.
139 *
140 * @iclass
141 */
142#define qSpaceI(qp) ((qp)->q_counter)
143
144/**
145 * @brief Returns the queue application-defined link.
146 * @note This function can be called in any context.
147 *
148 * @param[in] qp pointer to a @p io_queue_t structure
149 * @return The application-defined link.
150 *
151 * @special
152 */
153#define qGetLink(qp) ((qp)->q_link)
154
155/**
156 * @brief Sets the queue application-defined link.
157 * @note This function can be called in any context.
158 *
159 * @param[in] qp pointer to a @p io_queue_t structure
160 * @param[in] lk The application-defined link.
161 *
162 * @special
163 */
164#define qSetLink(qp, lk) ((qp)->q_link = lk)
165
166/**
167 * @brief Returns the filled space into an input queue.
168 *
169 * @param[in] iqp pointer to an @p input_queue_t structure
170 * @return The number of full bytes in the queue.
171 * @retval 0 if the queue is empty.
172 *
173 * @iclass
174 */
175#define iqGetFullI(iqp) qSpaceI(iqp)
176
177/**
178 * @brief Returns the empty space into an input queue.
179 *
180 * @param[in] iqp pointer to an @p input_queue_t structure
181 * @return The number of empty bytes in the queue.
182 * @retval 0 if the queue is full.
183 *
184 * @iclass
185 */
186#define iqGetEmptyI(iqp) (qSizeX(iqp) - qSpaceI(iqp))
187
188/**
189 * @brief Evaluates to @p true if the specified input queue is empty.
190 *
191 * @param[in] iqp pointer to an @p input_queue_t structure
192 * @return The queue status.
193 * @retval false if the queue is not empty.
194 * @retval true if the queue is empty.
195 *
196 * @iclass
197 */
198#define iqIsEmptyI(iqp) ((bool)(qSpaceI(iqp) == 0U))
199
200/**
201 * @brief Evaluates to @p true if the specified input queue is full.
202 *
203 * @param[in] iqp pointer to an @p input_queue_t structure
204 * @return The queue status.
205 * @retval false if the queue is not full.
206 * @retval true if the queue is full.
207 *
208 * @iclass
209 */
210#define iqIsFullI(iqp) \
211 /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
212 ((bool)(((iqp)->q_wrptr == (iqp)->q_rdptr) && ((iqp)->q_counter != 0U))) \
213 /*lint -restore*/
214
215/**
216 * @brief Input queue read.
217 * @details This function reads a byte value from an input queue. If the queue
218 * is empty then the calling thread is suspended until a byte arrives
219 * in the queue.
220 *
221 * @param[in] iqp pointer to an @p input_queue_t structure
222 * @return A byte value from the queue.
223 * @retval MSG_RESET if the queue has been reset.
224 *
225 * @api
226 */
227#define iqGet(iqp) iqGetTimeout(iqp, TIME_INFINITE)
228
229/**
230 * @brief Returns the filled space into an output queue.
231 *
232 * @param[in] oqp pointer to an @p output_queue_t structure
233 * @return The number of full bytes in the queue.
234 * @retval 0 if the queue is empty.
235 *
236 * @iclass
237 */
238#define oqGetFullI(oqp) (qSizeX(oqp) - qSpaceI(oqp))
239
240/**
241 * @brief Returns the empty space into an output queue.
242 *
243 * @param[in] oqp pointer to an @p output_queue_t structure
244 * @return The number of empty bytes in the queue.
245 * @retval 0 if the queue is full.
246 *
247 * @iclass
248 */
249#define oqGetEmptyI(oqp) qSpaceI(oqp)
250
251/**
252 * @brief Evaluates to @p true if the specified output queue is empty.
253 *
254 * @param[in] oqp pointer to an @p output_queue_t structure
255 * @return The queue status.
256 * @retval false if the queue is not empty.
257 * @retval true if the queue is empty.
258 *
259 * @iclass
260 */
261#define oqIsEmptyI(oqp) \
262 /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
263 ((bool)(((oqp)->q_wrptr == (oqp)->q_rdptr) && ((oqp)->q_counter != 0U))) \
264 /*lint -restore*/
265
266/**
267 * @brief Evaluates to @p true if the specified output queue is full.
268 *
269 * @param[in] oqp pointer to an @p output_queue_t structure
270 * @return The queue status.
271 * @retval false if the queue is not full.
272 * @retval true if the queue is full.
273 *
274 * @iclass
275 */
276#define oqIsFullI(oqp) ((bool)(qSpaceI(oqp) == 0U))
277
278/**
279 * @brief Output queue write.
280 * @details This function writes a byte value to an output queue. If the queue
281 * is full then the calling thread is suspended until there is space
282 * in the queue.
283 *
284 * @param[in] oqp pointer to an @p output_queue_t structure
285 * @param[in] b the byte value to be written in the queue
286 * @return The operation status.
287 * @retval MSG_OK if the operation succeeded.
288 * @retval MSG_RESET if the queue has been reset.
289 *
290 * @api
291 */
292#define oqPut(oqp, b) oqPutTimeout(oqp, b, TIME_INFINITE)
293/** @} */
294
295/*===========================================================================*/
296/* External declarations. */
297/*===========================================================================*/
298
299#ifdef __cplusplus
300extern "C" {
301#endif
302 void iqObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size,
303 qnotify_t infy, void *link);
304 void iqResetI(input_queue_t *iqp);
305 msg_t iqPutI(input_queue_t *iqp, uint8_t b);
308 size_t iqReadI(input_queue_t *iqp, uint8_t *bp, size_t n);
309 size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp,
310 size_t n, sysinterval_t timeout);
311
312 void oqObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size,
313 qnotify_t onfy, void *link);
314 void oqResetI(output_queue_t *oqp);
315 msg_t oqPutI(output_queue_t *oqp, uint8_t b);
316 msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout);
318 size_t oqWriteI(output_queue_t *oqp, const uint8_t *bp, size_t n);
319 size_t oqWriteTimeout(output_queue_t *oqp, const uint8_t *bp,
320 size_t n, sysinterval_t timeout);
321#ifdef __cplusplus
322}
323#endif
324
325#endif /* HAL_QUEUES_H */
326
327/** @} */
io_queue_t output_queue_t
Type of an output queue structure.
Definition hal_queues.h:109
msg_t iqGetI(input_queue_t *iqp)
Input queue non-blocking read.
Definition hal_queues.c:257
io_queue_t input_queue_t
Type of an input queue structure.
Definition hal_queues.h:97
void iqResetI(input_queue_t *iqp)
Resets an input queue.
Definition hal_queues.c:201
size_t oqWriteTimeout(output_queue_t *oqp, const uint8_t *bp, size_t n, sysinterval_t timeout)
Output queue write with timeout.
Definition hal_queues.c:651
msg_t oqPutI(output_queue_t *oqp, uint8_t b)
Output queue non-blocking write.
Definition hal_queues.c:490
void iqObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size, qnotify_t infy, void *link)
Initializes an input queue.
Definition hal_queues.c:177
msg_t iqGetTimeout(input_queue_t *iqp, sysinterval_t timeout)
Input queue read with timeout.
Definition hal_queues.c:302
msg_t iqPutI(input_queue_t *iqp, uint8_t b)
Input queue write.
Definition hal_queues.c:224
size_t oqWriteI(output_queue_t *oqp, const uint8_t *bp, size_t n)
Output queue non-blocking write.
Definition hal_queues.c:611
void oqResetI(output_queue_t *oqp)
Resets an output queue.
Definition hal_queues.c:467
void(* qnotify_t)(io_queue_t *qp)
Queue notification callback type.
Definition hal_queues.h:65
size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp, size_t n, sysinterval_t timeout)
Input queue read with timeout.
Definition hal_queues.c:386
msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout)
Output queue write with timeout.
Definition hal_queues.c:535
void oqObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size, qnotify_t onfy, void *link)
Initializes an output queue.
Definition hal_queues.c:443
msg_t oqGetI(output_queue_t *oqp)
Output queue read.
Definition hal_queues.c:576
struct io_queue io_queue_t
Type of a generic I/O queue structure.
Definition hal_queues.h:58
size_t iqReadI(input_queue_t *iqp, uint8_t *bp, size_t n)
Input queue non-blocking read.
Definition hal_queues.c:346
int32_t msg_t
Type of a message.
Definition osal.h:159
uint32_t sysinterval_t
Type of system time interval.
Definition osal.h:169
Generic I/O queue structure.
Definition hal_queues.h:75
qnotify_t q_notify
Data notification callback.
Definition hal_queues.h:83
uint8_t * q_buffer
Pointer to the queue buffer.
Definition hal_queues.h:78
uint8_t * q_wrptr
Write pointer.
Definition hal_queues.h:81
threads_queue_t q_waiting
Queue of waiting threads.
Definition hal_queues.h:76
uint8_t * q_rdptr
Read pointer.
Definition hal_queues.h:82
uint8_t * q_top
Pointer to the first location after the buffer.
Definition hal_queues.h:79
void * q_link
Application defined field.
Definition hal_queues.h:84
volatile size_t q_counter
Resources counter.
Definition hal_queues.h:77
Type of a thread queue.
Definition osal.h:238