-
Notifications
You must be signed in to change notification settings - Fork 14
lib: bm_fifo: a FIFO queue implementation #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2025 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
*/ | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
struct bm_fifo { | ||
/** | ||
* @brief FIFO buffer. | ||
*/ | ||
void *buf; | ||
/** | ||
* @brief FIFO maximum capacity (number of items). | ||
*/ | ||
uint32_t capacity; | ||
/** | ||
* @brief FIFO item size. | ||
*/ | ||
size_t item_size; | ||
/** | ||
* @brief Number of items in the queue. | ||
*/ | ||
uint32_t count; | ||
/** | ||
* @brief FIFO head. | ||
*/ | ||
int head; | ||
/** | ||
* @brief FIFO tail. | ||
*/ | ||
int tail; | ||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** | ||
* @brief Statically define a FIFO. | ||
* | ||
* Avoids the @ref bm_fifo_init() call. | ||
*/ | ||
#define BM_FIFO_INIT(_name, _capacity, _item_size) \ | ||
static uint8_t _name##_buf[(_item_size) * (_capacity)]; \ | ||
static struct bm_fifo _name = { \ | ||
.buf = _name##_buf, \ | ||
.item_size = _item_size, \ | ||
.capacity = _capacity, \ | ||
} | ||
|
||
/** | ||
* @brief Initialize a queue. | ||
* | ||
* @param fifo FIFO queue. | ||
* @param buf Queue buffer. | ||
* @param capacity Buffer capacity, in number of items. | ||
* @param item_size Size of a queue element, in bytes. | ||
* | ||
* @retval NRF_SUCCESS on success. | ||
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL. | ||
* @retval NRF_ERROR_INVALID_PARAM If @p capacity or @p item_size are 0. | ||
*/ | ||
uint32_t bm_fifo_init(struct bm_fifo *fifo, void *buf, size_t capacity, size_t item_size); | ||
|
||
/** | ||
* @brief Check whether the queue is full. | ||
* | ||
* @param fifo FIFO queue. | ||
* | ||
* @return true Queue is full. | ||
* @return false Queue is not full. | ||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
bool bm_fifo_is_full(const struct bm_fifo *fifo); | ||
|
||
/** | ||
* @brief Check whether the queue is empty. | ||
* | ||
* @param fifo FIFO queue. | ||
* | ||
* @return true Queue is empty. | ||
* @return false Queue is not empty. | ||
*/ | ||
bool bm_fifo_is_empty(const struct bm_fifo *fifo); | ||
|
||
/** | ||
* @brief Queue an element. | ||
* | ||
* The element is copied into the queue's own buffer. | ||
* Interrupts are disabled during the copy. | ||
* | ||
* @param fifo FIFO queue. | ||
* @param buf Buffer pointing to the element. | ||
* | ||
* @retval NRF_SUCCESS on success. | ||
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL. | ||
* @retval NRF_ERROR_NO_MEM If there are no buffers available in the queue. | ||
*/ | ||
uint32_t bm_fifo_enqueue(struct bm_fifo *fifo, void *buf); | ||
|
||
/** | ||
* @brief Dequeue an element. | ||
* | ||
* Dequeue an element from the queue's head. | ||
* | ||
* @param fifo FIFO queue. | ||
* @param buf Buffer to copy the element into. | ||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @retval NRF_SUCCESS on success. | ||
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL. | ||
* @retval NRF_ERROR_NOT_FOUND If the queue is empty. | ||
*/ | ||
uint32_t bm_fifo_dequeue(struct bm_fifo *fifo, void *buf); | ||
|
||
/** | ||
* @brief Peek at the queue. | ||
* | ||
* Peek at the queue's head. | ||
* | ||
* @param fifo FIFO queue. | ||
* @param buf Buffer to copy the element into. | ||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @retval NRF_SUCCESS on success. | ||
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL. | ||
* @retval NRF_ERROR_NOT_FOUND If the queue is empty. | ||
*/ | ||
uint32_t bm_fifo_peek(const struct bm_fifo *fifo, void *buf); | ||
|
||
/** | ||
* @brief Dequeue one element and discard it. | ||
* | ||
* Dequeue an element and discard it. | ||
* | ||
* @param fifo FIFO queue. | ||
* | ||
* @retval NRF_SUCCESS on success. | ||
* @retval NRF_ERROR_NULL If @p fifo is @c NULL. | ||
* @retval NRF_ERROR_NOT_FOUND If the queue is empty. | ||
*/ | ||
uint32_t bm_fifo_discard(struct bm_fifo *fifo); | ||
|
||
/** | ||
* @brief Clear the queue, discarding all elements. | ||
* | ||
* @param fifo FIFO queue. | ||
* | ||
* @retval NRF_SUCCESS on success. | ||
* @retval NRF_ERROR_NULL If @p fifo is @c NULL. | ||
*/ | ||
uint32_t bm_fifo_clear(struct bm_fifo *fifo); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
zephyr_library() | ||
zephyr_library_sources(bm_fifo.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
menuconfig BM_FIFO | ||
bool "FIFO queue library" | ||
help | ||
A simple FIFO queue using a circular buffer. | ||
|
||
if BM_FIFO | ||
|
||
module=BM_FIFO | ||
module-dep=LOG | ||
module-str=FIFO library | ||
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" | ||
|
||
endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/* | ||
* Copyright (c) 2025 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
*/ | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <zephyr/sys/__assert.h> /* __ASSERT */ | ||
#include <s115/nrf_error.h> | ||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* We don't want to include this in unit tests. */ | ||
#if CONFIG_HAS_NRFX | ||
#include <nrfx.h> | ||
#else | ||
#define NRFX_CRITICAL_SECTION_ENTER(...) | ||
#define NRFX_CRITICAL_SECTION_EXIT(...) | ||
#endif | ||
|
||
#include <bm_fifo.h> | ||
|
||
bool bm_fifo_is_full(const struct bm_fifo *fifo) | ||
{ | ||
__ASSERT_NO_MSG(fifo); | ||
return (fifo->count == fifo->capacity); | ||
} | ||
|
||
bool bm_fifo_is_empty(const struct bm_fifo *fifo) | ||
{ | ||
__ASSERT_NO_MSG(fifo); | ||
return (fifo->count == 0); | ||
} | ||
|
||
uint32_t bm_fifo_init(struct bm_fifo *fifo, void *buf, size_t capacity, size_t item_size) | ||
{ | ||
if (!fifo || !buf) { | ||
return NRF_ERROR_NULL; | ||
} | ||
if (!item_size || !capacity) { | ||
return NRF_ERROR_INVALID_PARAM; | ||
} | ||
|
||
MirkoCovizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memset(fifo, 0x00, sizeof(*fifo)); | ||
|
||
fifo->buf = buf; | ||
fifo->item_size = item_size; | ||
fifo->capacity = capacity; | ||
|
||
return NRF_SUCCESS; | ||
} | ||
|
||
uint32_t bm_fifo_enqueue(struct bm_fifo *fifo, void *buf) | ||
{ | ||
uint32_t err; | ||
void *item; | ||
|
||
if (!fifo || !buf) { | ||
return NRF_ERROR_NULL; | ||
} | ||
|
||
NRFX_CRITICAL_SECTION_ENTER(); | ||
|
||
if (bm_fifo_is_full(fifo)) { | ||
err = NRF_ERROR_NO_MEM; | ||
goto out; | ||
} | ||
|
||
fifo->count++; | ||
__ASSERT(fifo->count <= fifo->capacity, "Queue overflow"); | ||
|
||
item = (uint8_t *)fifo->buf + (fifo->tail * fifo->item_size); | ||
fifo->tail = (fifo->tail + 1) % fifo->capacity; | ||
memcpy(item, buf, fifo->item_size); | ||
|
||
err = NRF_SUCCESS; | ||
|
||
out: | ||
NRFX_CRITICAL_SECTION_EXIT(); | ||
return err; | ||
} | ||
|
||
uint32_t bm_fifo_dequeue(struct bm_fifo *fifo, void *buf) | ||
{ | ||
uint32_t err; | ||
void *item; | ||
|
||
if (!fifo || !buf) { | ||
return NRF_ERROR_NULL; | ||
} | ||
|
||
NRFX_CRITICAL_SECTION_ENTER(); | ||
|
||
if (bm_fifo_is_empty(fifo)) { | ||
err = NRF_ERROR_NOT_FOUND; | ||
goto out; | ||
} | ||
|
||
fifo->count--; | ||
__ASSERT(fifo->count <= fifo->capacity, "Queue underflow"); | ||
|
||
item = (uint8_t *)fifo->buf + (fifo->head * fifo->item_size); | ||
fifo->head = (fifo->head + 1) % fifo->capacity; | ||
memcpy(buf, item, fifo->item_size); | ||
|
||
err = NRF_SUCCESS; | ||
|
||
out: | ||
NRFX_CRITICAL_SECTION_EXIT(); | ||
return err; | ||
} | ||
|
||
uint32_t bm_fifo_peek(const struct bm_fifo *fifo, void *buf) | ||
{ | ||
uint32_t err; | ||
void *item; | ||
|
||
if (!fifo || !buf) { | ||
return NRF_ERROR_NULL; | ||
} | ||
|
||
NRFX_CRITICAL_SECTION_ENTER(); | ||
|
||
if (bm_fifo_is_empty(fifo)) { | ||
err = NRF_ERROR_NOT_FOUND; | ||
goto out; | ||
} | ||
|
||
item = (uint8_t *)fifo->buf + (fifo->head * fifo->item_size); | ||
memcpy(buf, item, fifo->item_size); | ||
|
||
err = NRF_SUCCESS; | ||
|
||
out: | ||
NRFX_CRITICAL_SECTION_EXIT(); | ||
return err; | ||
} | ||
|
||
uint32_t bm_fifo_discard(struct bm_fifo *fifo) | ||
{ | ||
uint32_t err; | ||
|
||
if (!fifo) { | ||
return NRF_ERROR_NULL; | ||
} | ||
|
||
NRFX_CRITICAL_SECTION_ENTER(); | ||
|
||
if (bm_fifo_is_empty(fifo)) { | ||
err = NRF_ERROR_NOT_FOUND; | ||
goto out; | ||
} | ||
|
||
fifo->count--; | ||
__ASSERT(fifo->count <= fifo->capacity, "Queue underflow"); | ||
|
||
fifo->head = (fifo->head + 1) % fifo->capacity; | ||
|
||
err = NRF_SUCCESS; | ||
|
||
out: | ||
NRFX_CRITICAL_SECTION_EXIT(); | ||
return err; | ||
} | ||
|
||
uint32_t bm_fifo_clear(struct bm_fifo *fifo) | ||
{ | ||
if (!fifo) { | ||
return NRF_ERROR_NULL; | ||
} | ||
|
||
NRFX_CRITICAL_SECTION_ENTER(); | ||
|
||
fifo->head = 0; | ||
fifo->tail = 0; | ||
fifo->count = 0; | ||
|
||
NRFX_CRITICAL_SECTION_EXIT(); | ||
|
||
return NRF_SUCCESS; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.