app_fifo.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Copyright (c) 2013 - 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. /**@file
  41. *
  42. * @defgroup app_fifo FIFO implementation
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief FIFO implementation.
  47. */
  48. #ifndef APP_FIFO_H__
  49. #define APP_FIFO_H__
  50. #include <stdint.h>
  51. #include <stdlib.h>
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**@brief A FIFO instance structure.
  56. * @details Keeps track of which bytes to read and write next.
  57. * Also, it keeps the information about which memory is allocated for the buffer
  58. * and its size. This structure must be initialized by app_fifo_init() before use.
  59. */
  60. typedef struct
  61. {
  62. uint8_t * p_buf; /**< Pointer to FIFO buffer memory. */
  63. uint16_t buf_size_mask; /**< Read/write index mask. Also used for size checking. */
  64. volatile uint32_t read_pos; /**< Next read position in the FIFO buffer. */
  65. volatile uint32_t write_pos; /**< Next write position in the FIFO buffer. */
  66. } app_fifo_t;
  67. /**@brief Function for initializing the FIFO.
  68. *
  69. * @param[out] p_fifo FIFO object.
  70. * @param[in] p_buf FIFO buffer for storing data. The buffer size must be a power of two.
  71. * @param[in] buf_size Size of the FIFO buffer provided. This size must be a power of two.
  72. *
  73. * @retval NRF_SUCCESS If initialization was successful.
  74. * @retval NRF_ERROR_NULL If a NULL pointer is provided as buffer.
  75. * @retval NRF_ERROR_INVALID_LENGTH If size of buffer provided is not a power of two.
  76. */
  77. uint32_t app_fifo_init(app_fifo_t * p_fifo, uint8_t * p_buf, uint16_t buf_size);
  78. /**@brief Function for adding an element to the FIFO.
  79. *
  80. * @param[in] p_fifo Pointer to the FIFO.
  81. * @param[in] byte Data byte to add to the FIFO.
  82. *
  83. * @retval NRF_SUCCESS If an element has been successfully added to the FIFO.
  84. * @retval NRF_ERROR_NO_MEM If the FIFO is full.
  85. */
  86. uint32_t app_fifo_put(app_fifo_t * p_fifo, uint8_t byte);
  87. /**@brief Function for getting the next element from the FIFO.
  88. *
  89. * @param[in] p_fifo Pointer to the FIFO.
  90. * @param[out] p_byte Byte fetched from the FIFO.
  91. *
  92. * @retval NRF_SUCCESS If an element was returned.
  93. * @retval NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  94. */
  95. uint32_t app_fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte);
  96. /**@brief Function for looking at an element in the FIFO, without consuming it.
  97. *
  98. * @param[in] p_fifo Pointer to the FIFO.
  99. * @param[in] index Which element to look at. The lower the index, the earlier it was put.
  100. * @param[out] p_byte Byte fetched from the FIFO.
  101. *
  102. * @retval NRF_SUCCESS If an element was returned.
  103. * @retval NRF_ERROR_NOT_FOUND If there are no more elements in the queue, or the index was
  104. * too large.
  105. */
  106. uint32_t app_fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte);
  107. /**@brief Function for flushing the FIFO.
  108. *
  109. * @param[in] p_fifo Pointer to the FIFO.
  110. *
  111. * @retval NRF_SUCCESS If the FIFO was flushed successfully.
  112. */
  113. uint32_t app_fifo_flush(app_fifo_t * p_fifo);
  114. /**@brief Function for reading bytes from the FIFO.
  115. *
  116. * This function can also be used to get the number of bytes in the FIFO.
  117. *
  118. * @param[in] p_fifo Pointer to the FIFO. Must not be NULL.
  119. * @param[out] p_byte_array Memory pointer where the read bytes are fetched from the FIFO.
  120. * Can be NULL. If NULL, the number of bytes that can be read in the FIFO
  121. * are returned in the p_size parameter.
  122. * @param[inout] p_size Address to memory indicating the maximum number of bytes to be read.
  123. * The provided memory is overwritten with the actual number of bytes
  124. * read if the procedure was successful. This field must not be NULL.
  125. * If p_byte_array is set to NULL by the application, this parameter
  126. * returns the number of bytes in the FIFO.
  127. *
  128. * @retval NRF_SUCCESS If the procedure is successful. The actual number of bytes read might
  129. * be less than the requested maximum, depending on how many elements exist
  130. * in the FIFO. Even if less bytes are returned, the procedure is considered
  131. * successful.
  132. * @retval NRF_ERROR_NULL If a NULL parameter was passed for a parameter that must not
  133. * be NULL.
  134. * @retval NRF_ERROR_NOT_FOUND If the FIFO is empty.
  135. */
  136. uint32_t app_fifo_read(app_fifo_t * p_fifo, uint8_t * p_byte_array, uint32_t * p_size);
  137. /**@brief Function for writing bytes to the FIFO.
  138. *
  139. * This function can also be used to get the available size on the FIFO.
  140. *
  141. * @param[in] p_fifo Pointer to the FIFO. Must not be NULL.
  142. * @param[in] p_byte_array Memory pointer containing the bytes to be written to the FIFO.
  143. * Can be NULL. If NULL, this function returns the number of bytes
  144. * that can be written to the FIFO.
  145. * @param[inout] p_size Address to memory indicating the maximum number of bytes to be written.
  146. * The provided memory is overwritten with the number of bytes that were actually
  147. * written if the procedure is successful. This field must not be NULL.
  148. * If p_byte_array is set to NULL by the application, this parameter
  149. * returns the number of bytes available in the FIFO.
  150. *
  151. * @retval NRF_SUCCESS If the procedure is successful. The actual number of bytes written might
  152. * be less than the requested maximum, depending on how much room there is in
  153. * the FIFO. Even if less bytes are written, the procedure is considered
  154. * successful. If the write was partial, the application should use
  155. * subsequent calls to attempt writing the data again.
  156. * @retval NRF_ERROR_NULL If a NULL parameter was passed for a parameter that must not
  157. * be NULL.
  158. * @retval NRF_ERROR_NO_MEM If the FIFO is full.
  159. *
  160. */
  161. uint32_t app_fifo_write(app_fifo_t * p_fifo, uint8_t const * p_byte_array, uint32_t * p_size);
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif // APP_FIFO_H__
  166. /** @} */