nrf_ringbuf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * Copyright (c) 2017 - 2019, 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. #include "nrf_ringbuf.h"
  41. #include "app_util_platform.h"
  42. #include "nrf_assert.h"
  43. #define WR_OFFSET 0
  44. #define RD_OFFSET 1
  45. void nrf_ringbuf_init(nrf_ringbuf_t const * p_ringbuf)
  46. {
  47. p_ringbuf->p_cb->wr_idx = 0;
  48. p_ringbuf->p_cb->rd_idx = 0;
  49. p_ringbuf->p_cb->tmp_rd_idx = 0;
  50. p_ringbuf->p_cb->tmp_wr_idx = 0;
  51. p_ringbuf->p_cb->rd_flag = 0;
  52. p_ringbuf->p_cb->wr_flag = 0;
  53. }
  54. ret_code_t nrf_ringbuf_alloc(nrf_ringbuf_t const * p_ringbuf, uint8_t * * pp_data, size_t * p_length, bool start)
  55. {
  56. ASSERT(pp_data);
  57. ASSERT(p_length);
  58. if (start)
  59. {
  60. if (nrf_atomic_flag_set_fetch(&p_ringbuf->p_cb->wr_flag))
  61. {
  62. return NRF_ERROR_BUSY;
  63. }
  64. }
  65. if (p_ringbuf->p_cb->tmp_wr_idx - p_ringbuf->p_cb->rd_idx == p_ringbuf->bufsize_mask + 1)
  66. {
  67. *p_length = 0;
  68. if (start)
  69. {
  70. UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&p_ringbuf->p_cb->wr_flag));
  71. }
  72. return NRF_SUCCESS;
  73. }
  74. uint32_t wr_idx = p_ringbuf->p_cb->tmp_wr_idx & p_ringbuf->bufsize_mask;
  75. uint32_t rd_idx = p_ringbuf->p_cb->rd_idx & p_ringbuf->bufsize_mask;
  76. uint32_t available = (wr_idx >= rd_idx) ? p_ringbuf->bufsize_mask + 1 - wr_idx :
  77. p_ringbuf->p_cb->rd_idx - (p_ringbuf->p_cb->tmp_wr_idx - (p_ringbuf->bufsize_mask + 1));
  78. *p_length = *p_length < available ? *p_length : available;
  79. *pp_data = &p_ringbuf->p_buffer[wr_idx];
  80. p_ringbuf->p_cb->tmp_wr_idx += *p_length;
  81. return NRF_SUCCESS;
  82. }
  83. ret_code_t nrf_ringbuf_put(nrf_ringbuf_t const * p_ringbuf, size_t length)
  84. {
  85. uint32_t available = p_ringbuf->bufsize_mask + 1 -
  86. (p_ringbuf->p_cb->wr_idx - p_ringbuf->p_cb->rd_idx);
  87. if (length > available)
  88. {
  89. return NRF_ERROR_NO_MEM;
  90. }
  91. p_ringbuf->p_cb->wr_idx += length;
  92. p_ringbuf->p_cb->tmp_wr_idx = p_ringbuf->p_cb->wr_idx;
  93. if (nrf_atomic_flag_clear_fetch(&p_ringbuf->p_cb->wr_flag) == 0)
  94. {
  95. /* Flag was already cleared. Suggests misuse. */
  96. return NRF_ERROR_INVALID_STATE;
  97. }
  98. return NRF_SUCCESS;
  99. }
  100. ret_code_t nrf_ringbuf_cpy_put(nrf_ringbuf_t const * p_ringbuf,
  101. uint8_t const * p_data,
  102. size_t * p_length)
  103. {
  104. ASSERT(p_data);
  105. ASSERT(p_length);
  106. if (nrf_atomic_flag_set_fetch(&p_ringbuf->p_cb->wr_flag))
  107. {
  108. return NRF_ERROR_BUSY;
  109. }
  110. uint32_t available = p_ringbuf->bufsize_mask + 1 -
  111. (p_ringbuf->p_cb->wr_idx - p_ringbuf->p_cb->rd_idx);
  112. *p_length = available > *p_length ? *p_length : available;
  113. size_t length = *p_length;
  114. uint32_t masked_wr_idx = (p_ringbuf->p_cb->wr_idx & p_ringbuf->bufsize_mask);
  115. uint32_t trail = p_ringbuf->bufsize_mask + 1 - masked_wr_idx;
  116. if (length > trail)
  117. {
  118. memcpy(&p_ringbuf->p_buffer[masked_wr_idx], p_data, trail);
  119. length -= trail;
  120. masked_wr_idx = 0;
  121. p_data += trail;
  122. }
  123. memcpy(&p_ringbuf->p_buffer[masked_wr_idx], p_data, length);
  124. p_ringbuf->p_cb->wr_idx += *p_length;
  125. UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&p_ringbuf->p_cb->wr_flag));
  126. return NRF_SUCCESS;
  127. }
  128. ret_code_t nrf_ringbuf_get(nrf_ringbuf_t const * p_ringbuf, uint8_t * * pp_data, size_t * p_length, bool start)
  129. {
  130. ASSERT(pp_data);
  131. ASSERT(p_length);
  132. if (start)
  133. {
  134. if (nrf_atomic_flag_set_fetch(&p_ringbuf->p_cb->rd_flag))
  135. {
  136. return NRF_ERROR_BUSY;
  137. }
  138. }
  139. uint32_t available = p_ringbuf->p_cb->wr_idx - p_ringbuf->p_cb->tmp_rd_idx;
  140. if (available == 0)
  141. {
  142. *p_length = 0;
  143. if (start)
  144. {
  145. UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&p_ringbuf->p_cb->rd_flag));
  146. }
  147. return NRF_SUCCESS;
  148. }
  149. uint32_t masked_tmp_rd_idx = p_ringbuf->p_cb->tmp_rd_idx & p_ringbuf->bufsize_mask;
  150. uint32_t masked_wr_idx = p_ringbuf->p_cb->wr_idx & p_ringbuf->bufsize_mask;
  151. if ((masked_wr_idx > masked_tmp_rd_idx) && (available < *p_length))
  152. {
  153. *p_length = available;
  154. }
  155. else if (masked_wr_idx <= masked_tmp_rd_idx)
  156. {
  157. uint32_t trail = p_ringbuf->bufsize_mask + 1 - masked_tmp_rd_idx;
  158. if (*p_length > trail)
  159. {
  160. *p_length = trail;
  161. }
  162. }
  163. *pp_data = &p_ringbuf->p_buffer[masked_tmp_rd_idx];
  164. p_ringbuf->p_cb->tmp_rd_idx += *p_length;
  165. return NRF_SUCCESS;
  166. }
  167. ret_code_t nrf_ringbuf_cpy_get(nrf_ringbuf_t const * p_ringbuf,
  168. uint8_t * p_data,
  169. size_t * p_length)
  170. {
  171. ASSERT(p_data);
  172. ASSERT(p_length);
  173. if (nrf_atomic_flag_set_fetch(&p_ringbuf->p_cb->rd_flag))
  174. {
  175. return NRF_ERROR_BUSY;
  176. }
  177. uint32_t available = p_ringbuf->p_cb->wr_idx - p_ringbuf->p_cb->rd_idx;
  178. *p_length = available > *p_length ? *p_length : available;
  179. size_t length = *p_length;
  180. uint32_t masked_rd_idx = (p_ringbuf->p_cb->rd_idx & p_ringbuf->bufsize_mask);
  181. uint32_t masked_wr_idx = (p_ringbuf->p_cb->wr_idx & p_ringbuf->bufsize_mask);
  182. uint32_t trail = (masked_wr_idx > masked_rd_idx) ? masked_wr_idx - masked_rd_idx :
  183. p_ringbuf->bufsize_mask + 1 - masked_rd_idx;
  184. if (length > trail)
  185. {
  186. memcpy(p_data, &p_ringbuf->p_buffer[masked_rd_idx], trail);
  187. length -= trail;
  188. masked_rd_idx = 0;
  189. p_data += trail;
  190. }
  191. memcpy(p_data, &p_ringbuf->p_buffer[masked_rd_idx], length);
  192. p_ringbuf->p_cb->rd_idx += *p_length;
  193. UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&p_ringbuf->p_cb->rd_flag));
  194. return NRF_SUCCESS;
  195. }
  196. ret_code_t nrf_ringbuf_free(nrf_ringbuf_t const * p_ringbuf, size_t length)
  197. {
  198. uint32_t available = (p_ringbuf->p_cb->wr_idx - p_ringbuf->p_cb->rd_idx);
  199. if (length > available)
  200. {
  201. return NRF_ERROR_NO_MEM;
  202. }
  203. p_ringbuf->p_cb->rd_idx += length;
  204. p_ringbuf->p_cb->tmp_rd_idx = p_ringbuf->p_cb->rd_idx;
  205. UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&p_ringbuf->p_cb->rd_flag));
  206. return NRF_SUCCESS;
  207. }