app_fifo.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup app_fifo FIFO implementation
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief FIFO implementation.
  19. */
  20. #ifndef APP_FIFO_H__
  21. #define APP_FIFO_H__
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. /**@brief A FIFO instance structure.
  25. * @details Keeps track of which bytes to read and write next.
  26. * Also, it keeps the information about which memory is allocated for the buffer
  27. * and its size. This structure must be initialized by app_fifo_init() before use.
  28. */
  29. typedef struct
  30. {
  31. uint8_t * p_buf; /**< Pointer to FIFO buffer memory. */
  32. uint16_t buf_size_mask; /**< Read/write index mask. Also used for size checking. */
  33. volatile uint32_t read_pos; /**< Next read position in the FIFO buffer. */
  34. volatile uint32_t write_pos; /**< Next write position in the FIFO buffer. */
  35. } app_fifo_t;
  36. /**@brief Function for initializing the FIFO.
  37. *
  38. * @param[out] p_fifo FIFO object.
  39. * @param[in] p_buf FIFO buffer for storing data. The buffer size must be a power of two.
  40. * @param[in] buf_size Size of the FIFO buffer provided. This size must be a power of two.
  41. *
  42. * @retval NRF_SUCCESS If initialization was successful.
  43. * @retval NRF_ERROR_NULL If a NULL pointer is provided as buffer.
  44. * @retval NRF_ERROR_INVALID_LENGTH If size of buffer provided is not a power of two.
  45. */
  46. uint32_t app_fifo_init(app_fifo_t * p_fifo, uint8_t * p_buf, uint16_t buf_size);
  47. /**@brief Function for adding an element to the FIFO.
  48. *
  49. * @param[in] p_fifo Pointer to the FIFO.
  50. * @param[in] byte Data byte to add to the FIFO.
  51. *
  52. * @retval NRF_SUCCESS If an element has been successfully added to the FIFO.
  53. * @retval NRF_ERROR_NO_MEM If the FIFO is full.
  54. */
  55. uint32_t app_fifo_put(app_fifo_t * p_fifo, uint8_t byte);
  56. /**@brief Function for getting the next element from the FIFO.
  57. *
  58. * @param[in] p_fifo Pointer to the FIFO.
  59. * @param[out] p_byte Byte fetched from the FIFO.
  60. *
  61. * @retval NRF_SUCCESS If an element was returned.
  62. * @retval NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  63. */
  64. uint32_t app_fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte);
  65. /**@brief Function for looking at an element in the FIFO, without consuming it.
  66. *
  67. * @param[in] p_fifo Pointer to the FIFO.
  68. * @param[in] index Which element to look at. The lower the index, the earlier it was put.
  69. * @param[out] p_byte Byte fetched from the FIFO.
  70. *
  71. * @retval NRF_SUCCESS If an element was returned.
  72. * @retval NRF_ERROR_NOT_FOUND If there are no more elements in the queue, or the index was
  73. * too large.
  74. */
  75. uint32_t app_fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte);
  76. /**@brief Function for flushing the FIFO.
  77. *
  78. * @param[in] p_fifo Pointer to the FIFO.
  79. *
  80. * @retval NRF_SUCCESS If the FIFO was flushed successfully.
  81. */
  82. uint32_t app_fifo_flush(app_fifo_t * p_fifo);
  83. /**@brief Function for reading bytes from the FIFO.
  84. *
  85. * This function can also be used to get the number of bytes in the FIFO.
  86. *
  87. * @param[in] p_fifo Pointer to the FIFO. Must not be NULL.
  88. * @param[out] p_byte_array Memory pointer where the read bytes are fetched from the FIFO.
  89. * Can be NULL. If NULL, the number of bytes that can be read in the FIFO
  90. * are returned in the p_size parameter.
  91. * @param[inout] p_size Address to memory indicating the maximum number of bytes to be read.
  92. * The provided memory is overwritten with the actual number of bytes
  93. * read if the procedure was successful. This field must not be NULL.
  94. * If p_byte_array is set to NULL by the application, this parameter
  95. * returns the number of bytes in the FIFO.
  96. *
  97. * @retval NRF_SUCCESS If the procedure is successful. The actual number of bytes read might
  98. * be less than the requested maximum, depending on how many elements exist
  99. * in the FIFO. Even if less bytes are returned, the procedure is considered
  100. * successful.
  101. * @retval NRF_ERROR_NULL If a NULL parameter was passed for a parameter that must not
  102. * be NULL.
  103. * @retval NRF_ERROR_NOT_FOUND If the FIFO is empty.
  104. */
  105. uint32_t app_fifo_read(app_fifo_t * p_fifo, uint8_t * p_byte_array, uint32_t * p_size);
  106. /**@brief Function for writing bytes to the FIFO.
  107. *
  108. * This function can also be used to get the available size on the FIFO.
  109. *
  110. * @param[in] p_fifo Pointer to the FIFO. Must not be NULL.
  111. * @param[in] p_byte_array Memory pointer containing the bytes to be written to the FIFO.
  112. * Can be NULL. If NULL, this function returns the number of bytes
  113. * that can be written to the FIFO.
  114. * @param[inout] p_size Address to memory indicating the maximum number of bytes to be written.
  115. * The provided memory is overwritten with the number of bytes that were actually
  116. * written if the procedure is successful. This field must not be NULL.
  117. * If p_byte_array is set to NULL by the application, this parameter
  118. * returns the number of bytes available in the FIFO.
  119. *
  120. * @retval NRF_SUCCESS If the procedure is successful. The actual number of bytes written might
  121. * be less than the requested maximum, depending on how much room there is in
  122. * the FIFO. Even if less bytes are written, the procedure is considered
  123. * successful. If the write was partial, the application should use
  124. * subsequent calls to attempt writing the data again.
  125. * @retval NRF_ERROR_NULL If a NULL parameter was passed for a parameter that must not
  126. * be NULL.
  127. * @retval NRF_ERROR_NO_MEM If the FIFO is full.
  128. *
  129. */
  130. uint32_t app_fifo_write(app_fifo_t * p_fifo, uint8_t const * p_byte_array, uint32_t * p_size);
  131. #endif // APP_FIFO_H__
  132. /** @} */