app_fifo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "app_fifo.h"
  13. #include "sdk_common.h"
  14. #include "nordic_common.h"
  15. static __INLINE uint32_t fifo_length(app_fifo_t * p_fifo)
  16. {
  17. uint32_t tmp = p_fifo->read_pos;
  18. return p_fifo->write_pos - tmp;
  19. }
  20. #define FIFO_LENGTH fifo_length(p_fifo) /**< Macro for calculating the FIFO length. */
  21. /**@brief Put one byte to the FIFO. */
  22. static __INLINE void fifo_put(app_fifo_t * p_fifo, uint8_t byte)
  23. {
  24. p_fifo->p_buf[p_fifo->write_pos & p_fifo->buf_size_mask] = byte;
  25. p_fifo->write_pos++;
  26. }
  27. /**@brief Look at one byte in the FIFO. */
  28. static __INLINE void fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
  29. {
  30. *p_byte = p_fifo->p_buf[(p_fifo->read_pos + index) & p_fifo->buf_size_mask];
  31. }
  32. /**@brief Get one byte from the FIFO. */
  33. static __INLINE void fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
  34. {
  35. fifo_peek(p_fifo, 0, p_byte);
  36. p_fifo->read_pos++;
  37. }
  38. uint32_t app_fifo_init(app_fifo_t * p_fifo, uint8_t * p_buf, uint16_t buf_size)
  39. {
  40. // Check buffer for null pointer.
  41. if (p_buf == NULL)
  42. {
  43. return NRF_ERROR_NULL;
  44. }
  45. // Check that the buffer size is a power of two.
  46. if (!IS_POWER_OF_TWO(buf_size))
  47. {
  48. return NRF_ERROR_INVALID_LENGTH;
  49. }
  50. p_fifo->p_buf = p_buf;
  51. p_fifo->buf_size_mask = buf_size - 1;
  52. p_fifo->read_pos = 0;
  53. p_fifo->write_pos = 0;
  54. return NRF_SUCCESS;
  55. }
  56. uint32_t app_fifo_put(app_fifo_t * p_fifo, uint8_t byte)
  57. {
  58. if (FIFO_LENGTH <= p_fifo->buf_size_mask)
  59. {
  60. fifo_put(p_fifo, byte);
  61. return NRF_SUCCESS;
  62. }
  63. return NRF_ERROR_NO_MEM;
  64. }
  65. uint32_t app_fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
  66. {
  67. if (FIFO_LENGTH != 0)
  68. {
  69. fifo_get(p_fifo, p_byte);
  70. return NRF_SUCCESS;
  71. }
  72. return NRF_ERROR_NOT_FOUND;
  73. }
  74. uint32_t app_fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
  75. {
  76. if (FIFO_LENGTH > index)
  77. {
  78. fifo_peek(p_fifo, index, p_byte);
  79. return NRF_SUCCESS;
  80. }
  81. return NRF_ERROR_NOT_FOUND;
  82. }
  83. uint32_t app_fifo_flush(app_fifo_t * p_fifo)
  84. {
  85. p_fifo->read_pos = p_fifo->write_pos;
  86. return NRF_SUCCESS;
  87. }
  88. uint32_t app_fifo_read(app_fifo_t * p_fifo, uint8_t * p_byte_array, uint32_t * p_size)
  89. {
  90. VERIFY_PARAM_NOT_NULL(p_fifo);
  91. VERIFY_PARAM_NOT_NULL(p_size);
  92. const uint32_t byte_count = fifo_length(p_fifo);
  93. const uint32_t requested_len = (*p_size);
  94. uint32_t index = 0;
  95. uint32_t read_size = MIN(requested_len, byte_count);
  96. (*p_size) = byte_count;
  97. // Check if the FIFO is empty.
  98. if (byte_count == 0)
  99. {
  100. return NRF_ERROR_NOT_FOUND;
  101. }
  102. // Check if application has requested only the size.
  103. if (p_byte_array == NULL)
  104. {
  105. return NRF_SUCCESS;
  106. }
  107. // Fetch bytes from the FIFO.
  108. while (index < read_size)
  109. {
  110. fifo_get(p_fifo, &p_byte_array[index++]);
  111. }
  112. (*p_size) = read_size;
  113. return NRF_SUCCESS;
  114. }
  115. uint32_t app_fifo_write(app_fifo_t * p_fifo, uint8_t const * p_byte_array, uint32_t * p_size)
  116. {
  117. VERIFY_PARAM_NOT_NULL(p_fifo);
  118. VERIFY_PARAM_NOT_NULL(p_size);
  119. const uint32_t available_count = p_fifo->buf_size_mask - fifo_length(p_fifo) + 1;
  120. const uint32_t requested_len = (*p_size);
  121. uint32_t index = 0;
  122. uint32_t write_size = MIN(requested_len, available_count);
  123. (*p_size) = available_count;
  124. // Check if the FIFO is FULL.
  125. if (available_count == 0)
  126. {
  127. return NRF_ERROR_NO_MEM;
  128. }
  129. // Check if application has requested only the size.
  130. if (p_byte_array == NULL)
  131. {
  132. return NRF_SUCCESS;
  133. }
  134. //Fetch bytes from the FIFO.
  135. while (index < write_size)
  136. {
  137. fifo_put(p_fifo, p_byte_array[index++]);
  138. }
  139. (*p_size) = write_size;
  140. return NRF_SUCCESS;
  141. }