|
| 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 | +/** @} */ |
0 commit comments