Skip to content

Commit 3bc8892

Browse files
lemreyMirkoCovizzi
andcommitted
lib: bm_fifo: a FIFO queue implementation
A simple FIFO queue implementation using a circular buffer, that is ISR-safe. Signed-off-by: Emanuele Di Santo <[email protected]> Co-Authored-by: Mirko Covizzi <[email protected]>
1 parent 281ea45 commit 3bc8892

File tree

11 files changed

+835
-0
lines changed

11 files changed

+835
-0
lines changed

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
/lib/ble_qwr/ @nrfconnect/ncs-bm
4545
/lib/ble_racp/ @nrfconnect/ncs-bm
4646
/lib/bm_buttons/ @nrfconnect/ncs-bm
47+
/lib/bm_fifo/ @nrfconnect/ncs-bm
4748
/lib/bm_timer/ @nrfconnect/ncs-bm
4849
/lib/boot_banner/ @nrfconnect/ncs-bm
4950
/lib/event_scheduler/ @nrfconnect/ncs-bm
@@ -76,6 +77,7 @@
7677
# Tests
7778
/tests/lib/ble_qwr/ @nrfconnect/ncs-bm
7879
/tests/lib/ble_racp/ @nrfconnect/ncs-bm
80+
/tests/lib/bm_fifo/ @nrfconnect/ncs-bm
7981

8082
# Zephyr module
8183
/zephyr/ @nrfconnect/ncs-co-build-system

include/bm_fifo.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
/** @file
8+
*
9+
* @defgroup bm_fifo NCS Bare Metal FIFO library
10+
* @{
11+
*
12+
* @brief A simple FIFO queue implementation using a circular buffer, that is ISR-safe.
13+
*/
14+
15+
#ifndef BM_FIFO_H__
16+
#define BM_FIFO_H__
17+
18+
#include <stdbool.h>
19+
#include <stddef.h>
20+
#include <stdint.h>
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
struct bm_fifo {
27+
/**
28+
* @brief FIFO buffer.
29+
*/
30+
void *buf;
31+
/**
32+
* @brief FIFO maximum capacity (number of items).
33+
*/
34+
uint32_t capacity;
35+
/**
36+
* @brief FIFO item size.
37+
*/
38+
size_t item_size;
39+
/**
40+
* @brief Number of items in the queue.
41+
*/
42+
uint32_t count;
43+
/**
44+
* @brief FIFO head.
45+
*/
46+
uint32_t head;
47+
/**
48+
* @brief FIFO tail.
49+
*/
50+
uint32_t tail;
51+
};
52+
53+
/**
54+
* @brief Statically define a FIFO.
55+
*
56+
* Avoids the @ref bm_fifo_init() call.
57+
*/
58+
#define BM_FIFO_INIT(_name, _capacity, _item_size) \
59+
static uint8_t _name_buf[(_item_size) * (_capacity)]; \
60+
static struct bm_fifo _name = { \
61+
.buf = _name_buf, \
62+
.item_size = _item_size, \
63+
.capacity = _capacity, \
64+
}
65+
66+
/**
67+
* @brief Initialize a queue.
68+
*
69+
* @param fifo FIFO queue.
70+
* @param buf Queue buffer.
71+
* @param capacity Buffer capacity, in number of items.
72+
* @param item_size Size of a queue element, in bytes.
73+
*
74+
* @retval NRF_SUCCESS on success.
75+
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL.
76+
* @retval NRF_ERROR_INVALID_PARAM If @p capacity or @p item_size are 0.
77+
*/
78+
uint32_t bm_fifo_init(struct bm_fifo *fifo, void *buf, size_t capacity, size_t item_size);
79+
80+
/**
81+
* @brief Check whether the queue is full.
82+
*
83+
* @param fifo FIFO queue.
84+
*
85+
* @retval true If queue is full.
86+
* @retval false If queue is not full.
87+
*/
88+
bool bm_fifo_is_full(const struct bm_fifo *fifo);
89+
90+
/**
91+
* @brief Check whether the queue is empty.
92+
*
93+
* @param fifo FIFO queue.
94+
*
95+
* @retval true If queue is empty.
96+
* @retval false If queue is not empty.
97+
*/
98+
bool bm_fifo_is_empty(const struct bm_fifo *fifo);
99+
100+
/**
101+
* @brief Queue an element.
102+
*
103+
* The element is copied into the queue's own buffer.
104+
* Interrupts are disabled during the copy.
105+
*
106+
* @param fifo FIFO queue.
107+
* @param buf Buffer pointing to the element.
108+
*
109+
* @retval NRF_SUCCESS on success.
110+
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL.
111+
* @retval NRF_ERROR_NO_MEM If there are no buffers available in the queue.
112+
*/
113+
uint32_t bm_fifo_enqueue(struct bm_fifo *fifo, void *buf);
114+
115+
/**
116+
* @brief Dequeue an element.
117+
*
118+
* Dequeue an element from the queue's head.
119+
*
120+
* @param fifo FIFO queue.
121+
* @param[out] buf Buffer to copy the element into.
122+
*
123+
* @retval NRF_SUCCESS on success.
124+
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL.
125+
* @retval NRF_ERROR_NOT_FOUND If the queue is empty.
126+
*/
127+
uint32_t bm_fifo_dequeue(struct bm_fifo *fifo, void *buf);
128+
129+
/**
130+
* @brief Peek at the queue.
131+
*
132+
* Peek at the queue's head.
133+
*
134+
* @param fifo FIFO queue.
135+
* @param[out] buf Buffer to copy the element into.
136+
*
137+
* @retval NRF_SUCCESS on success.
138+
* @retval NRF_ERROR_NULL If @p fifo or @p buf are @c NULL.
139+
* @retval NRF_ERROR_NOT_FOUND If the queue is empty.
140+
*/
141+
uint32_t bm_fifo_peek(const struct bm_fifo *fifo, void *buf);
142+
143+
/**
144+
* @brief Dequeue one element and discard it.
145+
*
146+
* Dequeue an element and discard it.
147+
*
148+
* @param fifo FIFO queue.
149+
*
150+
* @retval NRF_SUCCESS on success.
151+
* @retval NRF_ERROR_NULL If @p fifo is @c NULL.
152+
* @retval NRF_ERROR_NOT_FOUND If the queue is empty.
153+
*/
154+
uint32_t bm_fifo_discard(struct bm_fifo *fifo);
155+
156+
/**
157+
* @brief Clear the queue, discarding all elements.
158+
*
159+
* @param fifo FIFO queue.
160+
*
161+
* @retval NRF_SUCCESS on success.
162+
* @retval NRF_ERROR_NULL If @p fifo is @c NULL.
163+
*/
164+
uint32_t bm_fifo_clear(struct bm_fifo *fifo);
165+
166+
#ifdef __cplusplus
167+
}
168+
#endif
169+
170+
#endif /* BM_FIFO_H__ */
171+
172+
/** @} */

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_subdirectory_ifdef(CONFIG_BLE_RACP ble_racp)
1111
add_subdirectory_ifdef(CONFIG_EVENT_SCHEDULER event_scheduler)
1212
add_subdirectory_ifdef(CONFIG_BM_BUTTONS bm_buttons)
1313
add_subdirectory_ifdef(CONFIG_BM_TIMER bm_timer)
14+
add_subdirectory_ifdef(CONFIG_BM_FIFO bm_fifo)
1415
add_subdirectory_ifdef(CONFIG_BLE_QWR ble_qwr)
1516
add_subdirectory_ifdef(CONFIG_SENSORSIM sensorsim)
1617
add_subdirectory_ifdef(CONFIG_NCS_BARE_METAL_BOOT_BANNER boot_banner)

lib/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rsource "ble_racp/Kconfig"
1212
rsource "event_scheduler/Kconfig"
1313
rsource "bm_buttons/Kconfig"
1414
rsource "bm_timer/Kconfig"
15+
rsource "bm_fifo/Kconfig"
1516
rsource "ble_qwr/Kconfig"
1617
rsource "sensorsim/Kconfig"
1718
rsource "boot_banner/Kconfig"

lib/bm_fifo/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
zephyr_library()
7+
zephyr_library_sources(bm_fifo.c)

lib/bm_fifo/Kconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
menuconfig BM_FIFO
7+
bool "FIFO queue library"
8+
help
9+
A simple FIFO queue using a circular buffer.
10+
11+
if BM_FIFO
12+
13+
module=BM_FIFO
14+
module-dep=LOG
15+
module-str=FIFO library
16+
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
17+
18+
endif

0 commit comments

Comments
 (0)