app_fifo.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Copyright (c) 2013 - 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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_FIFO)
  42. #include "app_fifo.h"
  43. static __INLINE uint32_t fifo_length(app_fifo_t * p_fifo)
  44. {
  45. uint32_t tmp = p_fifo->read_pos;
  46. return p_fifo->write_pos - tmp;
  47. }
  48. #define FIFO_LENGTH() fifo_length(p_fifo) /**< Macro for calculating the FIFO length. */
  49. /**@brief Put one byte to the FIFO. */
  50. static __INLINE void fifo_put(app_fifo_t * p_fifo, uint8_t byte)
  51. {
  52. p_fifo->p_buf[p_fifo->write_pos & p_fifo->buf_size_mask] = byte;
  53. p_fifo->write_pos++;
  54. }
  55. /**@brief Look at one byte in the FIFO. */
  56. static __INLINE void fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
  57. {
  58. *p_byte = p_fifo->p_buf[(p_fifo->read_pos + index) & p_fifo->buf_size_mask];
  59. }
  60. /**@brief Get one byte from the FIFO. */
  61. static __INLINE void fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
  62. {
  63. fifo_peek(p_fifo, 0, p_byte);
  64. p_fifo->read_pos++;
  65. }
  66. uint32_t app_fifo_init(app_fifo_t * p_fifo, uint8_t * p_buf, uint16_t buf_size)
  67. {
  68. // Check buffer for null pointer.
  69. if (p_buf == NULL)
  70. {
  71. return NRF_ERROR_NULL;
  72. }
  73. // Check that the buffer size is a power of two.
  74. if (!IS_POWER_OF_TWO(buf_size))
  75. {
  76. return NRF_ERROR_INVALID_LENGTH;
  77. }
  78. p_fifo->p_buf = p_buf;
  79. p_fifo->buf_size_mask = buf_size - 1;
  80. p_fifo->read_pos = 0;
  81. p_fifo->write_pos = 0;
  82. return NRF_SUCCESS;
  83. }
  84. uint32_t app_fifo_put(app_fifo_t * p_fifo, uint8_t byte)
  85. {
  86. if (FIFO_LENGTH() <= p_fifo->buf_size_mask)
  87. {
  88. fifo_put(p_fifo, byte);
  89. return NRF_SUCCESS;
  90. }
  91. return NRF_ERROR_NO_MEM;
  92. }
  93. uint32_t app_fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
  94. {
  95. if (FIFO_LENGTH() != 0)
  96. {
  97. fifo_get(p_fifo, p_byte);
  98. return NRF_SUCCESS;
  99. }
  100. return NRF_ERROR_NOT_FOUND;
  101. }
  102. uint32_t app_fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
  103. {
  104. if (FIFO_LENGTH() > index)
  105. {
  106. fifo_peek(p_fifo, index, p_byte);
  107. return NRF_SUCCESS;
  108. }
  109. return NRF_ERROR_NOT_FOUND;
  110. }
  111. uint32_t app_fifo_flush(app_fifo_t * p_fifo)
  112. {
  113. p_fifo->read_pos = p_fifo->write_pos;
  114. return NRF_SUCCESS;
  115. }
  116. uint32_t app_fifo_read(app_fifo_t * p_fifo, uint8_t * p_byte_array, uint32_t * p_size)
  117. {
  118. VERIFY_PARAM_NOT_NULL(p_fifo);
  119. VERIFY_PARAM_NOT_NULL(p_size);
  120. const uint32_t byte_count = fifo_length(p_fifo);
  121. const uint32_t requested_len = (*p_size);
  122. uint32_t index = 0;
  123. uint32_t read_size = MIN(requested_len, byte_count);
  124. (*p_size) = byte_count;
  125. // Check if the FIFO is empty.
  126. if (byte_count == 0)
  127. {
  128. return NRF_ERROR_NOT_FOUND;
  129. }
  130. // Check if application has requested only the size.
  131. if (p_byte_array == NULL)
  132. {
  133. return NRF_SUCCESS;
  134. }
  135. // Fetch bytes from the FIFO.
  136. while (index < read_size)
  137. {
  138. fifo_get(p_fifo, &p_byte_array[index++]);
  139. }
  140. (*p_size) = read_size;
  141. return NRF_SUCCESS;
  142. }
  143. uint32_t app_fifo_write(app_fifo_t * p_fifo, uint8_t const * p_byte_array, uint32_t * p_size)
  144. {
  145. VERIFY_PARAM_NOT_NULL(p_fifo);
  146. VERIFY_PARAM_NOT_NULL(p_size);
  147. const uint32_t available_count = p_fifo->buf_size_mask - fifo_length(p_fifo) + 1;
  148. const uint32_t requested_len = (*p_size);
  149. uint32_t index = 0;
  150. uint32_t write_size = MIN(requested_len, available_count);
  151. (*p_size) = available_count;
  152. // Check if the FIFO is FULL.
  153. if (available_count == 0)
  154. {
  155. return NRF_ERROR_NO_MEM;
  156. }
  157. // Check if application has requested only the size.
  158. if (p_byte_array == NULL)
  159. {
  160. return NRF_SUCCESS;
  161. }
  162. //Fetch bytes from the FIFO.
  163. while (index < write_size)
  164. {
  165. fifo_put(p_fifo, p_byte_array[index++]);
  166. }
  167. (*p_size) = write_size;
  168. return NRF_SUCCESS;
  169. }
  170. #endif //NRF_MODULE_ENABLED(APP_FIFO)