nrf_ringbuf.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_RINGBUF_H
  41. #define NRF_RINGBUF_H
  42. /**
  43. * @defgroup nrf_ringbuf Ring buffer
  44. * @{
  45. * @ingroup app_common
  46. * @brief Functions for controlling the ring buffer.
  47. */
  48. #include <stdint.h>
  49. #include "nrf_atomic.h"
  50. #include "sdk_errors.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**
  55. * @brief Ring buffer instance control block.
  56. * */
  57. typedef struct
  58. {
  59. nrf_atomic_flag_t wr_flag; //!< Protection flag.
  60. nrf_atomic_flag_t rd_flag; //!< Protection flag.
  61. uint32_t wr_idx; //!< Write index (updated when putting).
  62. uint32_t tmp_wr_idx; //!< Temporary write index (updated when allocating).
  63. uint32_t rd_idx; //!< Read index (updated when freeing).
  64. uint32_t tmp_rd_idx; //!< Temporary read index (updated when getting).
  65. } nrf_ringbuf_cb_t;
  66. /**
  67. * @brief Ring buffer instance structure.
  68. * */
  69. typedef struct
  70. {
  71. uint8_t * p_buffer; //!< Pointer to the memory used by the ring buffer.
  72. uint32_t bufsize_mask; //!< Buffer size mask (buffer size must be a power of 2).
  73. nrf_ringbuf_cb_t * p_cb; //!< Pointer to the instance control block.
  74. } nrf_ringbuf_t;
  75. /**
  76. * @brief Macro for defining a ring buffer instance.
  77. *
  78. * @param _name Instance name.
  79. * @param _size Size of the ring buffer (must be a power of 2).
  80. * */
  81. #define NRF_RINGBUF_DEF(_name, _size) \
  82. STATIC_ASSERT(IS_POWER_OF_TWO(_size)); \
  83. static uint8_t CONCAT_2(_name,_buf)[_size]; \
  84. static nrf_ringbuf_cb_t CONCAT_2(_name,_cb); \
  85. static const nrf_ringbuf_t _name = { \
  86. .p_buffer = CONCAT_2(_name,_buf), \
  87. .bufsize_mask = _size - 1, \
  88. .p_cb = &CONCAT_2(_name,_cb), \
  89. }
  90. /**
  91. * @brief Function for initializing a ring buffer instance.
  92. *
  93. * @param p_ringbuf Pointer to the ring buffer instance.
  94. *
  95. * */
  96. void nrf_ringbuf_init(nrf_ringbuf_t const * p_ringbuf);
  97. /**
  98. * @brief Function for allocating memory from a ring buffer.
  99. *
  100. * This function attempts to allocate the amount of memory requested by the user, or a smaller amount if
  101. * the requested amount is not available. If a start flag is set, then exclusive access to allocation
  102. * is established. @ref nrf_ringbuf_put frees access to allocation. If a start flag is not set, then
  103. * exclusive access check is omitted.
  104. *
  105. * @param[in] p_ringbuf Pointer to the ring buffer instance.
  106. * @param[in] pp_data Pointer to the pointer to the allocated buffer.
  107. * @param[in, out] p_length Pointer to length. Length is set to the requested amount and filled
  108. * by the function with actually allocated amount.
  109. * @param[in] start Set to true if exclusive access should be controlled.
  110. *
  111. * @retval NRF_SUCCESS Successful allocation (can be smaller amount than requested).
  112. * NRF_ERROR_BUSY Ring buffer allocation process (alloc-put) is ongoing.
  113. *
  114. * */
  115. ret_code_t nrf_ringbuf_alloc(nrf_ringbuf_t const * p_ringbuf, uint8_t * * pp_data, size_t * p_length, bool start);
  116. /**
  117. * @brief Function for commiting data to a ring buffer.
  118. *
  119. * When an allocated buffer (see @ref nrf_ringbuf_alloc) has been filled with data, it must be committed
  120. * to make it available for @ref nrf_ringbuf_get and @ref nrf_ringbuf_cpy_get. This function commits
  121. * the data (can be smaller amount than allocated).
  122. *
  123. * @param[in] p_ringbuf Pointer to the ring buffer instance.
  124. * @param[in] length Amount of bytes to put.
  125. * @return NRF_SUCCESS on successful put or error.
  126. * */
  127. ret_code_t nrf_ringbuf_put(nrf_ringbuf_t const * p_ringbuf, size_t length);
  128. /**
  129. * @brief Function for copying data directly into the ring buffer.
  130. *
  131. * This function copies a user buffer to the ring buffer.
  132. *
  133. * @param[in] p_ringbuf Pointer to the ring buffer instance.
  134. * @param[in] p_data Pointer to the input buffer.
  135. * @param[in, out] p_length Amount of bytes to copy. Amount of bytes copied.
  136. * @return NRF_SUCCESS on successful put or error.
  137. * */
  138. ret_code_t nrf_ringbuf_cpy_put(nrf_ringbuf_t const * p_ringbuf,
  139. uint8_t const* p_data,
  140. size_t * p_length);
  141. /**
  142. * Function for getting data from the ring buffer.
  143. *
  144. * This function attempts to get the requested amount of data from the ring buffer. If a start flag is set, then
  145. * exclusive access to getting data from the ring buffer is established. @ref nrf_ringbuf_free frees
  146. * access to getting data from the ring buffer. If a start flag is not set, then
  147. * exclusive access check is omitted.
  148. *
  149. * @param[in] p_ringbuf Pointer to the ring buffer instance.
  150. * @param[in] pp_data Pointer to the pointer to the buffer with data.
  151. * @param[in, out] p_length Pointer to length. Length is set to the requested amount and filled
  152. * by the function with the actual amount.
  153. * @param[in] start Set to true if exclusive access should be controlled.
  154. *
  155. * @retval NRF_SUCCESS Successful getting (can be smaller amount than requested).
  156. * NRF_ERROR_BUSY Ring buffer getting process (get-free) is ongoing.
  157. */
  158. ret_code_t nrf_ringbuf_get(nrf_ringbuf_t const * p_ringbuf, uint8_t * * pp_data, size_t * p_length, bool start);
  159. /**
  160. * @brief Function for freeing a buffer back to the ring buffer.
  161. *
  162. * When a buffer with data taken from the ring buffer (see @ref nrf_ringbuf_get) has been processed, it
  163. * must be freed to make it available for further use. This function frees the buffer (can be smaller
  164. * amount than get).
  165. *
  166. * @param[in] p_ringbuf Pointer to the ring buffer instance.
  167. * @param[in] length Amount of bytes to free.
  168. * @return NRF_SUCCESS on successful put or error.
  169. * */
  170. ret_code_t nrf_ringbuf_free(nrf_ringbuf_t const * p_ringbuf, size_t length);
  171. /**
  172. * @brief Function for copying data directly out of the ring buffer.
  173. *
  174. * This function copies available data from the ring buffer to a user buffer.
  175. *
  176. * @param[in] p_ringbuf Pointer to the ring buffer instance.
  177. * @param[in] p_data Pointer to the input buffer.
  178. * @param[in, out] p_length Amount of bytes to copy. Amount of bytes copied.
  179. * @return NRF_SUCCESS on successful put or error.
  180. * */
  181. ret_code_t nrf_ringbuf_cpy_get(nrf_ringbuf_t const * p_ringbuf,
  182. uint8_t * p_data,
  183. size_t * p_length);
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif //NRF_RINGBUF_H
  188. /** @} */