ChibiOS  0.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  */
58 typedef struct io_queue io_queue_t;
59 
60 /**
61  * @brief Queue notification callback type.
62  *
63  * @param[in] qp the queue pointer
64  */
65 typedef 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  */
75 struct 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 Returns the filled space into an input queue.
157  *
158  * @param[in] iqp pointer to an @p input_queue_t structure
159  * @return The number of full bytes in the queue.
160  * @retval 0 if the queue is empty.
161  *
162  * @iclass
163  */
164 #define iqGetFullI(iqp) qSpaceI(iqp)
165 
166 /**
167  * @brief Returns the empty space into an input queue.
168  *
169  * @param[in] iqp pointer to an @p input_queue_t structure
170  * @return The number of empty bytes in the queue.
171  * @retval 0 if the queue is full.
172  *
173  * @iclass
174  */
175 #define iqGetEmptyI(iqp) (qSizeX(iqp) - qSpaceI(iqp))
176 
177 /**
178  * @brief Evaluates to @p true if the specified input queue is empty.
179  *
180  * @param[in] iqp pointer to an @p input_queue_t structure
181  * @return The queue status.
182  * @retval false if the queue is not empty.
183  * @retval true if the queue is empty.
184  *
185  * @iclass
186  */
187 #define iqIsEmptyI(iqp) ((bool)(qSpaceI(iqp) == 0U))
188 
189 /**
190  * @brief Evaluates to @p true if the specified input queue is full.
191  *
192  * @param[in] iqp pointer to an @p input_queue_t structure
193  * @return The queue status.
194  * @retval false if the queue is not full.
195  * @retval true if the queue is full.
196  *
197  * @iclass
198  */
199 #define iqIsFullI(iqp) \
200  /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
201  ((bool)(((iqp)->q_wrptr == (iqp)->q_rdptr) && ((iqp)->q_counter != 0U))) \
202  /*lint -restore*/
203 
204 /**
205  * @brief Input queue read.
206  * @details This function reads a byte value from an input queue. If the queue
207  * is empty then the calling thread is suspended until a byte arrives
208  * in the queue.
209  *
210  * @param[in] iqp pointer to an @p input_queue_t structure
211  * @return A byte value from the queue.
212  * @retval MSG_RESET if the queue has been reset.
213  *
214  * @api
215  */
216 #define iqGet(iqp) iqGetTimeout(iqp, TIME_INFINITE)
217 
218 /**
219  * @brief Returns the filled space into an output queue.
220  *
221  * @param[in] oqp pointer to an @p output_queue_t structure
222  * @return The number of full bytes in the queue.
223  * @retval 0 if the queue is empty.
224  *
225  * @iclass
226  */
227 #define oqGetFullI(oqp) (qSizeX(oqp) - qSpaceI(oqp))
228 
229 /**
230  * @brief Returns the empty space into an output queue.
231  *
232  * @param[in] oqp pointer to an @p output_queue_t structure
233  * @return The number of empty bytes in the queue.
234  * @retval 0 if the queue is full.
235  *
236  * @iclass
237  */
238 #define oqGetEmptyI(oqp) qSpaceI(oqp)
239 
240 /**
241  * @brief Evaluates to @p true if the specified output queue is empty.
242  *
243  * @param[in] oqp pointer to an @p output_queue_t structure
244  * @return The queue status.
245  * @retval false if the queue is not empty.
246  * @retval true if the queue is empty.
247  *
248  * @iclass
249  */
250 #define oqIsEmptyI(oqp) \
251  /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
252  ((bool)(((oqp)->q_wrptr == (oqp)->q_rdptr) && ((oqp)->q_counter != 0U))) \
253  /*lint -restore*/
254 
255 /**
256  * @brief Evaluates to @p true if the specified output queue is full.
257  *
258  * @param[in] oqp pointer to an @p output_queue_t structure
259  * @return The queue status.
260  * @retval false if the queue is not full.
261  * @retval true if the queue is full.
262  *
263  * @iclass
264  */
265 #define oqIsFullI(oqp) ((bool)(qSpaceI(oqp) == 0U))
266 
267 /**
268  * @brief Output queue write.
269  * @details This function writes a byte value to an output queue. If the queue
270  * is full then the calling thread is suspended until there is space
271  * in the queue.
272  *
273  * @param[in] oqp pointer to an @p output_queue_t structure
274  * @param[in] b the byte value to be written in the queue
275  * @return The operation status.
276  * @retval MSG_OK if the operation succeeded.
277  * @retval MSG_RESET if the queue has been reset.
278  *
279  * @api
280  */
281 #define oqPut(oqp, b) oqPutTimeout(oqp, b, TIME_INFINITE)
282 /** @} */
283 
284 /*===========================================================================*/
285 /* External declarations. */
286 /*===========================================================================*/
287  /** @} */
288 
289 #ifdef __cplusplus
290 extern "C" {
291 #endif
292  void iqObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size,
293  qnotify_t infy, void *link);
294  void iqResetI(input_queue_t *iqp);
295  msg_t iqPutI(input_queue_t *iqp, uint8_t b);
296  msg_t iqGetI(input_queue_t *iqp);
298  size_t iqReadI(input_queue_t *iqp, uint8_t *bp, size_t n);
299  size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp,
300  size_t n, sysinterval_t timeout);
301 
302  void oqObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size,
303  qnotify_t onfy, void *link);
304  void oqResetI(output_queue_t *oqp);
305  msg_t oqPutI(output_queue_t *oqp, uint8_t b);
306  msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout);
308  size_t oqWriteI(output_queue_t *oqp, const uint8_t *bp, size_t n);
309  size_t oqWriteTimeout(output_queue_t *oqp, const uint8_t *bp,
310  size_t n, sysinterval_t timeout);
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 #endif /* HAL_QUEUES_H */
316 
317 /** @} */
Generic I/O queue structure.
Definition: hal_queues.h:75
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:304
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
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:446
size_t iqReadI(input_queue_t *iqp, uint8_t *bp, size_t n)
Input queue non-blocking read.
Definition: hal_queues.c:348
uint8_t * q_wrptr
Write pointer.
Definition: hal_queues.h:81
void iqResetI(input_queue_t *iqp)
Resets an input queue.
Definition: hal_queues.c:201
void oqResetI(output_queue_t *oqp)
Resets an output queue.
Definition: hal_queues.c:470
uint8_t * q_top
Pointer to the first location after the buffer.
Definition: hal_queues.h:79
threads_queue_t q_waiting
Queue of waiting threads.
Definition: hal_queues.h:76
io_queue_t input_queue_t
Type of an input queue structure.
Definition: hal_queues.h:97
void(* qnotify_t)(io_queue_t *qp)
Queue notification callback type.
Definition: hal_queues.h:65
Type of a thread queue.
Definition: osal.h:232
void * q_link
Application defined field.
Definition: hal_queues.h:84
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:658
uint8_t * q_buffer
Pointer to the queue buffer.
Definition: hal_queues.h:78
msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout)
Output queue write with timeout.
Definition: hal_queues.c:540
msg_t oqGetI(output_queue_t *oqp)
Output queue read.
Definition: hal_queues.c:581
msg_t oqPutI(output_queue_t *oqp, uint8_t b)
Output queue non-blocking write.
Definition: hal_queues.c:494
msg_t iqPutI(input_queue_t *iqp, uint8_t b)
Input queue write.
Definition: hal_queues.c:224
volatile size_t q_counter
Resources counter.
Definition: hal_queues.h:77
uint8_t * q_rdptr
Read pointer.
Definition: hal_queues.h:82
qnotify_t q_notify
Data notification callback.
Definition: hal_queues.h:83
msg_t iqGetI(input_queue_t *iqp)
Input queue non-blocking read.
Definition: hal_queues.c:258
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:389
size_t oqWriteI(output_queue_t *oqp, const uint8_t *bp, size_t n)
Output queue non-blocking write.
Definition: hal_queues.c:617
io_queue_t output_queue_t
Type of an output queue structure.
Definition: hal_queues.h:109
int32_t msg_t
Definition: chtypes.h:51