nrf_fifo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright (c) 2015 - 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 "iot_defines.h"
  41. #include "iot_errors.h"
  42. #include "nrf_fifo.h"
  43. #include "mem_manager.h"
  44. #include "nrf.h"
  45. static __INLINE uint32_t fifo_inc(nrf_fifo_t * p_fifo, uint32_t pos)
  46. {
  47. return (pos + 1) % p_fifo->nmemb;
  48. }
  49. static __INLINE bool fifo_full(nrf_fifo_t * p_fifo)
  50. {
  51. return fifo_inc(p_fifo, p_fifo->write_pos) == p_fifo->read_pos;
  52. }
  53. static __INLINE bool fifo_empty(nrf_fifo_t * p_fifo)
  54. {
  55. return p_fifo->read_pos == p_fifo->write_pos;
  56. }
  57. static __INLINE void fifo_enq(nrf_fifo_t * p_fifo, void * p_ctx)
  58. {
  59. p_fifo->pp_elements[p_fifo->write_pos] = p_ctx;
  60. __DSB();
  61. p_fifo->write_pos = fifo_inc(p_fifo, p_fifo->write_pos);
  62. }
  63. static __INLINE void fifo_deq(nrf_fifo_t * p_fifo, void ** pp_ctx)
  64. {
  65. *pp_ctx = p_fifo->pp_elements[p_fifo->read_pos];
  66. __DSB();
  67. p_fifo->read_pos = fifo_inc(p_fifo, p_fifo->read_pos);
  68. }
  69. uint32_t nrf_fifo_init(nrf_fifo_t * p_fifo, uint32_t nmemb, fifo_wait_fn wait_fn, fifo_flush_fn flush_fn)
  70. {
  71. uint32_t err_code = NRF_SUCCESS;
  72. uint32_t nmemb_actual = nmemb + 1; // Required to allow detection of empty and full state
  73. p_fifo->pp_elements = nrf_malloc(nmemb_actual * sizeof(void *));
  74. if (p_fifo->pp_elements == NULL)
  75. {
  76. err_code = NRF_ERROR_NO_MEM;
  77. }
  78. else
  79. {
  80. p_fifo->nmemb = nmemb_actual;
  81. p_fifo->wait = wait_fn;
  82. p_fifo->flush = flush_fn;
  83. p_fifo->read_pos = 0;
  84. p_fifo->write_pos = 0;
  85. }
  86. return err_code;
  87. }
  88. void nrf_fifo_deinit(nrf_fifo_t * p_fifo)
  89. {
  90. if (p_fifo->flush != NULL)
  91. {
  92. void * p_data;
  93. uint32_t err_code = nrf_fifo_deq(p_fifo, &p_data, false);
  94. while (err_code == NRF_SUCCESS)
  95. {
  96. p_fifo->flush(p_data);
  97. err_code = nrf_fifo_deq(p_fifo, &p_data, false);
  98. }
  99. }
  100. nrf_free(p_fifo->pp_elements);
  101. p_fifo->nmemb = 0;
  102. p_fifo->read_pos = 0;
  103. p_fifo->write_pos = 0;
  104. p_fifo->wait = NULL;
  105. p_fifo->flush = NULL;
  106. }
  107. uint32_t nrf_fifo_enq(nrf_fifo_t * p_fifo, void * p_ctx, bool wait)
  108. {
  109. uint32_t err_code = NRF_SUCCESS;
  110. if (fifo_full(p_fifo) == true)
  111. {
  112. if (wait == false || p_fifo->wait == NULL)
  113. {
  114. err_code = SOCKET_WOULD_BLOCK;
  115. }
  116. else
  117. {
  118. while (fifo_full(p_fifo) == true && err_code == NRF_SUCCESS)
  119. {
  120. err_code = p_fifo->wait();
  121. }
  122. }
  123. }
  124. else
  125. {
  126. fifo_enq(p_fifo, p_ctx);
  127. }
  128. return err_code;
  129. }
  130. uint32_t nrf_fifo_deq(nrf_fifo_t * p_fifo, void ** pp_ctx, bool wait)
  131. {
  132. uint32_t err_code = NRF_SUCCESS;
  133. if (fifo_empty(p_fifo) == true)
  134. {
  135. if (wait == false || p_fifo->wait == NULL)
  136. {
  137. err_code = SOCKET_WOULD_BLOCK;
  138. }
  139. else
  140. {
  141. while (fifo_empty(p_fifo) == true && err_code == NRF_SUCCESS)
  142. {
  143. err_code = p_fifo->wait();
  144. }
  145. }
  146. }
  147. if (err_code == NRF_SUCCESS)
  148. {
  149. fifo_deq(p_fifo, pp_ctx);
  150. }
  151. return err_code;
  152. }
  153. bool nrf_fifo_empty(nrf_fifo_t * p_fifo)
  154. {
  155. return fifo_empty(p_fifo);
  156. }
  157. bool nrf_fifo_full(nrf_fifo_t * p_fifo)
  158. {
  159. return fifo_full(p_fifo);
  160. }