VSF Documented
esp_ringbuf.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
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/*
19 * Clean-room re-implementation of ESP-IDF public API "freertos/ringbuf.h".
20 *
21 * Authored from the ESP-IDF v5.x public API reference only. No ESP-IDF /
22 * FreeRTOS source code or data is copied.
23 *
24 * All three ring buffer types are fully implemented: BYTEBUF (byte stream,
25 * zero-copy), NOSPLIT (item-oriented with dummy padding at wrap), and
26 * ALLOWSPLIT (item-oriented with split items at wrap). Blocking support
27 * (ticks_to_wait) is provided when VSF_USE_KERNEL is enabled; without the
28 * kernel the port operates poll-only.
29 *
30 * Because VSF's ESP-IDF port does not include a full FreeRTOS layer, this
31 * header exposes a minimal subset of the FreeRTOS value types that the
32 * ring buffer API signature references. The guards allow code that does
33 * include real FreeRTOS headers to compile side-by-side.
34 */
35
36#ifndef __VSF_ESPIDF_ESP_RINGBUF_H__
37#define __VSF_ESPIDF_ESP_RINGBUF_H__
38
39#include <stdint.h>
40#include <stdbool.h>
41#include <stddef.h>
42
43#include "esp_err.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49/*============================ TYPES =========================================*/
50
51#ifndef __VSF_ESPIDF_FREERTOS_TYPES_DEFINED__
52#define __VSF_ESPIDF_FREERTOS_TYPES_DEFINED__
53
54// for vsf_systimer_tick_t
55# include "kernel/vsf_kernel.h"
56
57typedef long BaseType_t;
58typedef unsigned long UBaseType_t;
60# ifndef pdTRUE
61# define pdTRUE ((BaseType_t)1)
62# endif
63# ifndef pdFALSE
64# define pdFALSE ((BaseType_t)0)
65# endif
66# ifndef pdPASS
67# define pdPASS pdTRUE
68# endif
69# ifndef pdFAIL
70# define pdFAIL pdFALSE
71# endif
72# ifndef portMAX_DELAY
73# define portMAX_DELAY ((TickType_t)-1)
74# endif
75#endif // __VSF_ESPIDF_FREERTOS_TYPES_DEFINED__
76
77#ifndef __VSF_FREERTOS_QUEUESET_TYPES_DEFINED__
78#define __VSF_FREERTOS_QUEUESET_TYPES_DEFINED__
79typedef void * QueueSetHandle_t;
81#endif
82
83typedef enum {
88
91
92/* Placeholder large enough for the internal control block on all
93 * supported platforms. Use xRingbufferCreateStatic() to initialise. */
94typedef struct {
97
98/*============================ PROTOTYPES ====================================*/
99
100/* Allocate a ring buffer of the requested byte capacity. Returns NULL on
101 * allocation failure. The returned handle must be released via
102 * vRingbufferDelete(). */
104
105/* Release a previously created ring buffer. Passing NULL is a no-op. */
107
108/* Enqueue `data_size` bytes (or a single item for NOSPLIT/ALLOWSPLIT).
109 * Returns pdTRUE on success, pdFALSE if there is insufficient space or the
110 * item exceeds xRingbufferGetMaxItemSize(). When VSF_USE_KERNEL is enabled
111 * the caller blocks up to ticks_to_wait when the buffer is full; without
112 * the kernel the port operates poll-only (ticks_to_wait is ignored). */
114 size_t data_size, TickType_t ticks_to_wait);
115
116/* Receive the next item (NOSPLIT/ALLOWSPLIT) or all available contiguous
117 * bytes (BYTEBUF). Returns a zero-copy pointer into the ring buffer; the
118 * caller must release it with vRingbufferReturnItem(). On success sets
119 * *item_size to the payload length. Returns NULL if no data is available.
120 * When VSF_USE_KERNEL is enabled the caller blocks up to ticks_to_wait
121 * when the buffer is empty. */
122void * xRingbufferReceive(RingbufHandle_t handle, size_t *item_size,
123 TickType_t ticks_to_wait);
124
125/* Same as xRingbufferReceive() but for BYTEBUF caps the returned byte
126 * count at wanted_size. */
128 TickType_t ticks_to_wait, size_t wanted_size);
129
130/* Release a pointer previously returned by xRingbufferReceive() or
131 * xRingbufferReceiveSplit(). For NOSPLIT/ALLOWSPLIT buffers this marks
132 * the item as free within the ring; for BYTEBUF it advances the free
133 * pointer to the read pointer. */
134void vRingbufferReturnItem(RingbufHandle_t handle, void *item);
135
136/* Retrieve a possibly-split item from an allow-split ring buffer. Only
137 * valid for RINGBUF_TYPE_ALLOWSPLIT handles. On success, *ppvHeadItem
138 * and *ppvTailItem point into the ring buffer (zero-copy) and
139 * *pxHeadItemSize / *pxTailItemSize are set accordingly. If the item is
140 * not split, *ppvTailItem is NULL. Each non-NULL pointer must be returned
141 * individually via vRingbufferReturnItem(). */
143 void **ppvHeadItem,
144 void **ppvTailItem,
145 size_t *pxHeadItemSize,
146 size_t *pxTailItemSize,
147 TickType_t ticks_to_wait);
148
149/* Bytes currently free for writing. */
151
152/* Bytes currently queued for reading. */
154
155/* Maximum item size that can currently be sent. For BYTEBUF this equals
156 * the free byte count; for NOSPLIT/ALLOWSPLIT it accounts for per-item
157 * header overhead and contiguous space constraints. */
159
160/* Convenience constructor for a NOSPLIT ring buffer with space for
161 * xItemNum items of xItemSize bytes each. */
162RingbufHandle_t xRingbufferCreateNoSplit(size_t xItemSize, size_t xItemNum);
163
164/* ISR-compatible send. Non-blocking; if the buffer is full, returns
165 * pdFALSE immediately. If a task was woken, *pxHigherPriorityTaskWoken
166 * is set to pdTRUE (may be NULL). */
168 size_t data_size,
169 BaseType_t *pxHigherPriorityTaskWoken);
170
171/* ISR-compatible receive. Not valid for ALLOWSPLIT buffers. */
173 BaseType_t *pxHigherPriorityTaskWoken);
174
175/* ISR-compatible receive-up-to. Byte buffers only. */
177 size_t *item_size, size_t wanted_size,
178 BaseType_t *pxHigherPriorityTaskWoken);
179
180/* ISR-compatible split receive. Allow-split buffers only. */
182 void **ppvHeadItem,
183 void **ppvTailItem,
184 size_t *pxHeadItemSize,
185 size_t *pxTailItemSize,
186 BaseType_t *pxHigherPriorityTaskWoken);
187
188/* ISR-compatible return item. */
189void vRingbufferReturnItemFromISR(RingbufHandle_t handle, void *item,
190 BaseType_t *pxHigherPriorityTaskWoken);
191
192/* Acquire space in a NOSPLIT buffer without writing data. Only valid
193 * for RINGBUF_TYPE_NOSPLIT. Call xRingbufferSendComplete to finalize. */
195 size_t xItemSize, TickType_t ticks_to_wait);
196
197/* Complete an acquire previously made with xRingbufferSendAcquire.
198 * Marks the item as written, making it available to readers. */
200
201/* Reset a ring buffer to its initial empty state. Returns ESP_OK on
202 * success, ESP_ERR_INVALID_STATE if there are outstanding reads/writes. */
204
205/* Read back the internal cursor positions (all in bytes from pucHead).
206 * Any pointer argument may be NULL to skip that field. */
208 UBaseType_t *uxFree, UBaseType_t *uxRead,
209 UBaseType_t *uxWrite, UBaseType_t *uxAcquire,
210 UBaseType_t *uxItemsWaiting);
211
212/* Print a human-readable snapshot of the ring buffer state to stdout.
213 * Intended for debugging; uses printf. */
215
216/* Allocate a ring buffer with explicit memory caps. The caps bitmask
217 * is resolved via the vsf_espidf_cfg_t::caps_to_heap callback when
218 * set; otherwise the default heap is used. */
221 UBaseType_t uxMemoryCaps);
222
223/* Create a ring buffer using caller-supplied storage. The control
224 * block (StaticRingbuffer_t) and data buffer must outlive the handle. */
227 uint8_t *pucRingbufferStorage,
228 StaticRingbuffer_t *pxStaticRingbuffer);
229
230/* Retrieve the original storage pointers from a statically-allocated
231 * ring buffer. Returns pdFALSE if the buffer was not created via
232 * xRingbufferCreateStatic. */
234 uint8_t **ppucRingbufferStorage,
235 StaticRingbuffer_t **ppxStaticRingbuffer);
236
237/* Delete a ring buffer created with xRingbufferCreateWithCaps(). Uses
238 * the caps-resolved heap for freeing; otherwise identical to
239 * vRingbufferDelete(). */
241
242/* Add a ring buffer to a QueueSet so that data arrival can be detected
243 * via xQueueSelectFromSet(). Only one QueueSet per ring buffer. Rejected
244 * if the ring buffer already belongs to a set or has data available. */
246 QueueSetHandle_t xQueueSet);
247
248/* Remove a ring buffer from its current QueueSet. Rejected if the set
249 * argument does not match or data is currently available to read. */
251 QueueSetHandle_t xQueueSet);
252
253/* Identify whether a QueueSet member (returned by xQueueSelectFromSet)
254 * corresponds to a given ring buffer handle. */
255static inline BaseType_t xRingbufferCanRead(RingbufHandle_t xRingbuffer,
257{
258 return (xMember == (QueueSetMemberHandle_t)xRingbuffer) ? pdTRUE : pdFALSE;
259}
260
261#ifdef __cplusplus
262}
263#endif
264
265#endif // __VSF_ESPIDF_ESP_RINGBUF_H__
uint64_t vsf_systimer_tick_t
Definition cortex_a_generic.h:70
int esp_err_t
Definition esp_err.h:41
void xRingbufferPrintInfo(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:1310
RingbufHandle_t xRingbufferCreateStatic(size_t buffer_size, RingbufferType_t type, uint8_t *pucRingbufferStorage, StaticRingbuffer_t *pxStaticRingbuffer)
Definition esp_ringbuf_port.c:1376
struct __vsf_espidf_ringbuf * RingbufHandle_t
Definition esp_ringbuf.h:90
size_t xRingbufferGetMaxItemSize(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:900
void vRingbufferReturnItem(RingbufHandle_t handle, void *item)
Definition esp_ringbuf_port.c:846
void vRingbufferDelete(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:612
void * QueueSetHandle_t
Definition esp_ringbuf.h:79
RingbufHandle_t xRingbufferCreateWithCaps(size_t buffer_size, RingbufferType_t type, UBaseType_t uxMemoryCaps)
Definition esp_ringbuf_port.c:1414
BaseType_t xRingbufferReceiveSplit(RingbufHandle_t handle, void **ppvHeadItem, void **ppvTailItem, size_t *pxHeadItemSize, size_t *pxTailItemSize, TickType_t ticks_to_wait)
Definition esp_ringbuf_port.c:908
long BaseType_t
Definition esp_ringbuf.h:57
BaseType_t xRingbufferGetStaticBuffer(RingbufHandle_t handle, uint8_t **ppucRingbufferStorage, StaticRingbuffer_t **ppxStaticRingbuffer)
Definition esp_ringbuf_port.c:1397
void * xRingbufferReceiveUpTo(RingbufHandle_t handle, size_t *item_size, TickType_t ticks_to_wait, size_t wanted_size)
Definition esp_ringbuf_port.c:803
BaseType_t xRingbufferReceiveSplitFromISR(RingbufHandle_t handle, void **ppvHeadItem, void **ppvTailItem, size_t *pxHeadItemSize, size_t *pxTailItemSize, BaseType_t *pxHigherPriorityTaskWoken)
Definition esp_ringbuf_port.c:1103
void vRingbufferGetInfo(RingbufHandle_t handle, UBaseType_t *uxFree, UBaseType_t *uxRead, UBaseType_t *uxWrite, UBaseType_t *uxAcquire, UBaseType_t *uxItemsWaiting)
Definition esp_ringbuf_port.c:1280
BaseType_t xRingbufferSendAcquire(RingbufHandle_t handle, void **ppvItem, size_t xItemSize, TickType_t ticks_to_wait)
Definition esp_ringbuf_port.c:1169
unsigned long UBaseType_t
Definition esp_ringbuf.h:58
BaseType_t xRingbufferSendComplete(RingbufHandle_t handle, void *pvItem)
Definition esp_ringbuf_port.c:1222
void * QueueSetMemberHandle_t
Definition esp_ringbuf.h:80
BaseType_t xRingbufferSend(RingbufHandle_t handle, const void *data, size_t data_size, TickType_t ticks_to_wait)
Definition esp_ringbuf_port.c:644
RingbufferType_t
Definition esp_ringbuf.h:83
@ RINGBUF_TYPE_NOSPLIT
Definition esp_ringbuf.h:84
@ RINGBUF_TYPE_ALLOWSPLIT
Definition esp_ringbuf.h:85
@ RINGBUF_TYPE_BYTEBUF
Definition esp_ringbuf.h:86
BaseType_t xRingbufferAddToQueueSetRead(RingbufHandle_t handle, QueueSetHandle_t xQueueSet)
Definition esp_ringbuf_port.c:1476
#define pdFALSE
Definition esp_ringbuf.h:64
BaseType_t xRingbufferRemoveFromQueueSetRead(RingbufHandle_t handle, QueueSetHandle_t xQueueSet)
Definition esp_ringbuf_port.c:1498
void * xRingbufferReceiveFromISR(RingbufHandle_t handle, size_t *item_size, BaseType_t *pxHigherPriorityTaskWoken)
Definition esp_ringbuf_port.c:1041
RingbufHandle_t xRingbufferCreate(size_t buffer_size, RingbufferType_t type)
Definition esp_ringbuf_port.c:587
esp_err_t vRingbufferReset(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:1253
void * xRingbufferReceiveUpToFromISR(RingbufHandle_t handle, size_t *item_size, size_t wanted_size, BaseType_t *pxHigherPriorityTaskWoken)
Definition esp_ringbuf_port.c:1078
RingbufHandle_t xRingbufferCreateNoSplit(size_t xItemSize, size_t xItemNum)
Definition esp_ringbuf_port.c:978
void vRingbufferDeleteWithCaps(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:1467
size_t xRingbufferGetCurFilledSize(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:886
vsf_systimer_tick_t TickType_t
Definition esp_ringbuf.h:59
BaseType_t xRingbufferSendFromISR(RingbufHandle_t handle, const void *data, size_t data_size, BaseType_t *pxHigherPriorityTaskWoken)
Definition esp_ringbuf_port.c:987
#define pdTRUE
Definition esp_ringbuf.h:61
void * xRingbufferReceive(RingbufHandle_t handle, size_t *item_size, TickType_t ticks_to_wait)
Definition esp_ringbuf_port.c:737
size_t xRingbufferGetCurFreeSize(RingbufHandle_t handle)
Definition esp_ringbuf_port.c:865
void vRingbufferReturnItemFromISR(RingbufHandle_t handle, void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition esp_ringbuf_port.c:1144
struct ieee80211_ext_chansw_ie data
Definition ieee80211.h:80
uint32_t TickType_t
Definition rtos_al.h:59
uint32_t UBaseType_t
Definition rtos_al.h:60
unsigned long long uint64_t
Definition stdint.h:11
unsigned char uint8_t
Definition stdint.h:5
Definition esp_ringbuf_port.c:117
Definition esp_ringbuf.h:94
vk_av_control_type_t type
Definition vsf_audio.h:170
struct @898::@908 _
uintptr_t uint_fast32_t item_size
Definition vsf_pool.h:477
Generated from commit: vsfteam/vsf@c3767bf