ChibiOS/HAL 9.0.0
hal_buffers.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_buffers.h
19 * @brief I/O Buffers macros and structures.
20 *
21 * @addtogroup HAL_BUFFERS
22 * @{
23 */
24
25#ifndef HAL_BUFFERS_H
26#define HAL_BUFFERS_H
27
28/*===========================================================================*/
29/* Driver constants. */
30/*===========================================================================*/
31
32/*===========================================================================*/
33/* Driver pre-compile time settings. */
34/*===========================================================================*/
35
36/**
37 * @brief Maximum size of blocks copied in critical sections.
38 * @note Increasing this value increases performance at expense of
39 * IRQ servicing efficiency.
40 * @note It must be a power of two.
41 */
42#if !defined(BUFFERS_CHUNKS_SIZE) || defined(__DOXYGEN__)
43#define BUFFERS_CHUNKS_SIZE 64
44#endif
45
46/*===========================================================================*/
47/* Derived constants and error checks. */
48/*===========================================================================*/
49
50/*lint -save -e9027 [10.1] It is meant to be this way, not an error.*/
51#if (BUFFERS_CHUNKS_SIZE & (BUFFERS_CHUNKS_SIZE - 1)) != 0
52/*lint -restore*/
53#error "BUFFERS_CHUNKS_SIZE must be a power of two"
54#endif
55
56/*===========================================================================*/
57/* Driver data structures and types. */
58/*===========================================================================*/
59
60/**
61 * @brief Type of a generic queue of buffers.
62 */
64
65/**
66 * @brief Double buffer notification callback type.
67 *
68 * @param[in] bqp the buffers queue pointer
69 */
70typedef void (*bqnotify_t)(io_buffers_queue_t *bqp);
71
72/**
73 * @brief Structure of a generic buffers queue.
74 */
76 /**
77 * @brief Queue of waiting threads.
78 */
80 /**
81 * @brief Queue suspended state flag.
82 */
84 /**
85 * @brief Active buffers counter.
86 */
87 volatile size_t bcounter;
88 /**
89 * @brief Buffer write pointer.
90 */
91 uint8_t *bwrptr;
92 /**
93 * @brief Buffer read pointer.
94 */
95 uint8_t *brdptr;
96 /**
97 * @brief Pointer to the buffers boundary.
98 */
99 uint8_t *btop;
100 /**
101 * @brief Size of buffers.
102 * @note The buffer size must be not lower than <tt>sizeof(size_t) + 2</tt>
103 * because the first bytes are used to store the used size of the
104 * buffer.
105 */
106 size_t bsize;
107 /**
108 * @brief Number of buffers.
109 */
110 size_t bn;
111 /**
112 * @brief Queue of buffer objects.
113 */
114 uint8_t *buffers;
115 /**
116 * @brief Pointer for R/W sequential access.
117 * @note It is @p NULL if a new buffer must be fetched from the queue.
118 */
119 uint8_t *ptr;
120 /**
121 * @brief Boundary for R/W sequential access.
122 */
123 uint8_t *top;
124 /**
125 * @brief Data notification callback.
126 */
128 /**
129 * @brief Application defined field.
130 */
131 void *link;
132};
133
134/**
135 * @brief Type of an input buffers queue.
136 */
138
139/**
140 * @brief Type of an output buffers queue.
141 */
143
144/*===========================================================================*/
145/* Driver macros. */
146/*===========================================================================*/
147
148/**
149 * @brief Computes the size of a buffers queue buffer size.
150 *
151 * @param[in] n number of buffers in the queue
152 * @param[in] size size of the buffers
153 */
154#define BQ_BUFFER_SIZE(n, size) \
155 (((size_t)(size) + sizeof (size_t)) * (size_t)(n))
156
157/**
158 * @name Macro Functions
159 * @{
160 */
161/**
162 * @brief Returns the queue's number of buffers.
163 *
164 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
165 * @return The number of buffers.
166 *
167 * @xclass
168 */
169#define bqSizeX(bqp) ((bqp)->bn)
170
171/**
172 * @brief Return the ready buffers number.
173 * @details Returns the number of filled buffers if used on an input queue
174 * or the number of empty buffers if used on an output queue.
175 *
176 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
177 * @return The number of ready buffers.
178 *
179 * @iclass
180 */
181#define bqSpaceI(bqp) ((bqp)->bcounter)
182
183/**
184 * @brief Returns the queue application-defined link.
185 *
186 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
187 * @return The application-defined link.
188 *
189 * @special
190 */
191#define bqGetLinkX(bqp) ((bqp)->link)
192
193/**
194 * @brief Sets the queue application-defined link.
195 *
196 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
197 * @param[in] lk The application-defined link.
198 *
199 * @special
200 */
201#define bqSetLinkX(bqp, lk) ((bqp)->link = lk)
202
203/**
204 * @brief Return the suspended state of the queue.
205 *
206 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
207 * @return The suspended state.
208 * @retval false if blocking access to the queue is enabled.
209 * @retval true if blocking access to the queue is suspended.
210 *
211 * @xclass
212 */
213#define bqIsSuspendedX(bqp) ((bqp)->suspended)
214
215/**
216 * @brief Puts the queue in suspended state.
217 * @details When the queue is put in suspended state all waiting threads are
218 * woken with message @p MSG_RESET and subsequent attempt at waiting
219 * on the queue will result in an immediate return with @p MSG_RESET
220 * message.
221 * @note The content of the queue is not altered, queues can be accessed
222 * is suspended state until a blocking operation is met then a
223 * @p MSG_RESET occurs.
224 *
225 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
226 *
227 * @iclass
228 */
229#define bqSuspendI(bqp) { \
230 (bqp)->suspended = true; \
231 osalThreadDequeueAllI(&(bqp)->waiting, MSG_RESET); \
232}
233
234/**
235 * @brief Resumes normal queue operations.
236 *
237 * @param[in] bqp pointer to an @p io_buffers_queue_t structure
238 *
239 * @xclass
240 */
241#define bqResumeX(bqp) { \
242 (bqp)->suspended = false; \
243}
244
245/**
246 * @brief Evaluates to @p true if the specified input buffers queue is empty.
247 *
248 * @param[in] ibqp pointer to an @p input_buffers_queue_t structure
249 * @return The queue status.
250 * @retval false if the queue is not empty.
251 * @retval true if the queue is empty.
252 *
253 * @iclass
254 */
255#define ibqIsEmptyI(ibqp) ((bool)(bqSpaceI(ibqp) == 0U))
256
257/**
258 * @brief Evaluates to @p true if the specified input buffers queue is full.
259 *
260 * @param[in] ibqp pointer to an @p input_buffers_queue_t structure
261 * @return The queue status.
262 * @retval false if the queue is not full.
263 * @retval true if the queue is full.
264 *
265 * @iclass
266 */
267#define ibqIsFullI(ibqp) \
268 /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
269 ((bool)(((ibqp)->bwrptr == (ibqp)->brdptr) && ((ibqp)->bcounter != 0U))) \
270 /*lint -restore*/
271
272/**
273 * @brief Evaluates to @p true if the specified output buffers queue is empty.
274 *
275 * @param[in] obqp pointer to an @p output_buffers_queue_t structure
276 * @return The queue status.
277 * @retval false if the queue is not empty.
278 * @retval true if the queue is empty.
279 *
280 * @iclass
281 */
282#define obqIsEmptyI(obqp) \
283 /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
284 ((bool)(((obqp)->bwrptr == (obqp)->brdptr) && ((obqp)->bcounter != 0U))) \
285 /*lint -restore*/
286
287/**
288 * @brief Evaluates to @p true if the specified output buffers queue is full.
289 *
290 * @param[in] obqp pointer to an @p output_buffers_queue_t structure
291 * @return The queue status.
292 * @retval false if the queue is not full.
293 * @retval true if the queue is full.
294 *
295 * @iclass
296 */
297#define obqIsFullI(obqp) ((bool)(bqSpaceI(obqp) == 0U))
298/** @} */
299
300/*===========================================================================*/
301/* External declarations. */
302/*===========================================================================*/
303
304#ifdef __cplusplus
305extern "C" {
306#endif
307 void ibqObjectInit(input_buffers_queue_t *ibqp, bool suspended, uint8_t *bp,
308 size_t size, size_t n, bqnotify_t infy, void *link);
311 void ibqPostFullBufferI(input_buffers_queue_t *ibqp, size_t size);
313 sysinterval_t timeout);
315 sysinterval_t timeout);
319 size_t ibqReadTimeout(input_buffers_queue_t *ibqp, uint8_t *bp,
320 size_t n, sysinterval_t timeout);
321 void obqObjectInit(output_buffers_queue_t *obqp, bool suspended, uint8_t *bp,
322 size_t size, size_t n, bqnotify_t onfy, void *link);
325 size_t *sizep);
328 sysinterval_t timeout);
330 sysinterval_t timeout);
331 void obqPostFullBuffer(output_buffers_queue_t *obqp, size_t size);
332 void obqPostFullBufferS(output_buffers_queue_t *obqp, size_t size);
334 sysinterval_t timeout);
335 size_t obqWriteTimeout(output_buffers_queue_t *obqp, const uint8_t *bp,
336 size_t n, sysinterval_t timeout);
339#ifdef __cplusplus
340}
341#endif
342
343#endif /* HAL_BUFFERS_H */
344
345/** @} */
uint8_t * ibqGetEmptyBufferI(input_buffers_queue_t *ibqp)
Gets the next empty buffer from the queue.
io_buffers_queue_t input_buffers_queue_t
Type of an input buffers queue.
void obqPostFullBuffer(output_buffers_queue_t *obqp, size_t size)
Posts a new filled buffer to the queue.
void obqFlush(output_buffers_queue_t *obqp)
Flushes the current, partially filled, buffer to the queue.
void ibqPostFullBufferI(input_buffers_queue_t *ibqp, size_t size)
Posts a new filled buffer to the queue.
uint8_t * obqGetFullBufferI(output_buffers_queue_t *obqp, size_t *sizep)
Gets the next filled buffer from the queue.
io_buffers_queue_t output_buffers_queue_t
Type of an output buffers queue.
bool obqTryFlushI(output_buffers_queue_t *obqp)
Flushes the current, partially filled, buffer to the queue.
msg_t ibqGetFullBufferTimeout(input_buffers_queue_t *ibqp, sysinterval_t timeout)
Gets the next filled buffer from the queue.
msg_t obqGetEmptyBufferTimeoutS(output_buffers_queue_t *obqp, sysinterval_t timeout)
Gets the next empty buffer from the queue.
void obqObjectInit(output_buffers_queue_t *obqp, bool suspended, uint8_t *bp, size_t size, size_t n, bqnotify_t onfy, void *link)
Initializes an output buffers queue object.
size_t obqWriteTimeout(output_buffers_queue_t *obqp, const uint8_t *bp, size_t n, sysinterval_t timeout)
Output queue write with timeout.
void obqReleaseEmptyBufferI(output_buffers_queue_t *obqp)
Releases the next filled buffer back in the queue.
struct io_buffers_queue io_buffers_queue_t
Type of a generic queue of buffers.
Definition hal_buffers.h:63
void obqPostFullBufferS(output_buffers_queue_t *obqp, size_t size)
Posts a new filled buffer to the queue.
void(* bqnotify_t)(io_buffers_queue_t *bqp)
Double buffer notification callback type.
Definition hal_buffers.h:70
void ibqObjectInit(input_buffers_queue_t *ibqp, bool suspended, uint8_t *bp, size_t size, size_t n, bqnotify_t infy, void *link)
Initializes an input buffers queue object.
Definition hal_buffers.c:76
void ibqReleaseEmptyBufferS(input_buffers_queue_t *ibqp)
Releases the buffer back in the queue.
msg_t ibqGetTimeout(input_buffers_queue_t *ibqp, sysinterval_t timeout)
Input queue read with timeout.
size_t ibqReadTimeout(input_buffers_queue_t *ibqp, uint8_t *bp, size_t n, sysinterval_t timeout)
Input queue read with timeout.
msg_t ibqGetFullBufferTimeoutS(input_buffers_queue_t *ibqp, sysinterval_t timeout)
Gets the next filled buffer from the queue.
void ibqResetI(input_buffers_queue_t *ibqp)
Resets an input buffers queue.
msg_t obqGetEmptyBufferTimeout(output_buffers_queue_t *obqp, sysinterval_t timeout)
Gets the next empty buffer from the queue.
void obqResetI(output_buffers_queue_t *obqp)
Resets an output buffers queue.
void ibqReleaseEmptyBuffer(input_buffers_queue_t *ibqp)
Releases the buffer back in the queue.
msg_t obqPutTimeout(output_buffers_queue_t *obqp, uint8_t b, sysinterval_t timeout)
Output queue write with timeout.
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
Structure of a generic buffers queue.
Definition hal_buffers.h:75
uint8_t * brdptr
Buffer read pointer.
Definition hal_buffers.h:95
bqnotify_t notify
Data notification callback.
size_t bn
Number of buffers.
uint8_t * buffers
Queue of buffer objects.
uint8_t * btop
Pointer to the buffers boundary.
Definition hal_buffers.h:99
bool suspended
Queue suspended state flag.
Definition hal_buffers.h:83
size_t bsize
Size of buffers.
void * link
Application defined field.
uint8_t * bwrptr
Buffer write pointer.
Definition hal_buffers.h:91
volatile size_t bcounter
Active buffers counter.
Definition hal_buffers.h:87
threads_queue_t waiting
Queue of waiting threads.
Definition hal_buffers.h:79
uint8_t * top
Boundary for R/W sequential access.
uint8_t * ptr
Pointer for R/W sequential access.
Type of a thread queue.
Definition osal.h:238