nrf_atfifo.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2011 - 2018, 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 <string.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include "app_util.h"
  44. #include "nrf_atfifo.h"
  45. #include "nrf_atfifo_internal.h"
  46. #if NRF_ATFIFO_CONFIG_LOG_ENABLED
  47. #define NRF_LOG_LEVEL NRF_ATFIFO_CONFIG_LOG_LEVEL
  48. #define NRF_LOG_INIT_FILTER_LEVEL NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL
  49. #define NRF_LOG_INFO_COLOR NRF_ATFIFO_CONFIG_INFO_COLOR
  50. #define NRF_LOG_DEBUG_COLOR NRF_ATFIFO_CONFIG_DEBUG_COLOR
  51. #else
  52. #define NRF_LOG_LEVEL 0
  53. #endif // NRF_ATFIFO_CONFIG_LOG_ENABLED
  54. #include "nrf_log.h"
  55. /* Unions testing */
  56. STATIC_ASSERT(sizeof(nrf_atfifo_postag_t) == sizeof(uint32_t));
  57. ret_code_t nrf_atfifo_init(nrf_atfifo_t * const p_fifo, void * p_buf, uint16_t buf_size, uint16_t item_size)
  58. {
  59. if (NULL == p_buf)
  60. {
  61. NRF_LOG_INST_ERROR(p_fifo->p_log, "Initialization failed. p_buf == NULL");
  62. return NRF_ERROR_NULL;
  63. }
  64. if (0 != (buf_size % item_size))
  65. {
  66. NRF_LOG_INST_ERROR(p_fifo->p_log, "Initialization failed. Buf_size not multiple of item_size");
  67. return NRF_ERROR_INVALID_LENGTH;
  68. }
  69. p_fifo->p_buf = p_buf;
  70. p_fifo->tail.tag = 0;
  71. p_fifo->head.tag = 0;
  72. p_fifo->buf_size = buf_size;
  73. p_fifo->item_size = item_size;
  74. NRF_LOG_INST_INFO(p_fifo->p_log, "Initialized.");
  75. return NRF_SUCCESS;
  76. }
  77. ret_code_t nrf_atfifo_clear(nrf_atfifo_t * const p_fifo)
  78. {
  79. bool released = nrf_atfifo_space_clear(p_fifo);
  80. NRF_LOG_INST_INFO(p_fifo->p_log, "Cleared result:%s", released ? "success" : "busy");
  81. return released ? NRF_SUCCESS : NRF_ERROR_BUSY;
  82. }
  83. ret_code_t nrf_atfifo_alloc_put(nrf_atfifo_t * const p_fifo, void const * p_var, size_t size, bool * const p_visible)
  84. {
  85. nrf_atfifo_item_put_t context;
  86. bool visible;
  87. void * p_data = nrf_atfifo_item_alloc(p_fifo, &context);
  88. if (NULL == p_data)
  89. {
  90. NRF_LOG_INST_WARNING(p_fifo->p_log, "Copying in element (0x%08X) failed - no space.", p_var);
  91. return NRF_ERROR_NO_MEM;
  92. }
  93. memcpy(p_data, p_var, size);
  94. visible = nrf_atfifo_item_put(p_fifo, &context);
  95. if (NULL != p_visible)
  96. {
  97. *p_visible = visible;
  98. }
  99. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Element (0x%08X) copied in.", p_var);
  100. return NRF_SUCCESS;
  101. }
  102. void * nrf_atfifo_item_alloc(nrf_atfifo_t * const p_fifo, nrf_atfifo_item_put_t * p_context)
  103. {
  104. if (nrf_atfifo_wspace_req(p_fifo, &(p_context->last_tail)))
  105. {
  106. void * p_item = ((uint8_t*)(p_fifo->p_buf)) + p_context->last_tail.pos.wr;
  107. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Allocated element (0x%08X).", p_item);
  108. return p_item;
  109. }
  110. NRF_LOG_INST_WARNING(p_fifo->p_log, "Allocation failed - no space.");
  111. return NULL;
  112. }
  113. bool nrf_atfifo_item_put(nrf_atfifo_t * const p_fifo, nrf_atfifo_item_put_t * p_context)
  114. {
  115. if ((p_context->last_tail.pos.wr) == (p_context->last_tail.pos.rd))
  116. {
  117. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Put (uninterrupted)");
  118. nrf_atfifo_wspace_close(p_fifo);
  119. return true;
  120. }
  121. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Put (interrupted!)");
  122. return false;
  123. }
  124. ret_code_t nrf_atfifo_get_free(nrf_atfifo_t * const p_fifo, void * const p_var, size_t size, bool * p_released)
  125. {
  126. nrf_atfifo_item_get_t context;
  127. bool released;
  128. void const * p_s = nrf_atfifo_item_get(p_fifo, &context);
  129. if (NULL == p_s)
  130. {
  131. NRF_LOG_INST_WARNING(p_fifo->p_log, "Copying out failed - no item in the FIFO.");
  132. return NRF_ERROR_NOT_FOUND;
  133. }
  134. memcpy(p_var, p_s, size);
  135. released = nrf_atfifo_item_free(p_fifo, &context);
  136. if (NULL != p_released)
  137. {
  138. *p_released = released;
  139. }
  140. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Element (0x%08X) copied out.", p_var);
  141. return NRF_SUCCESS;
  142. }
  143. void * nrf_atfifo_item_get(nrf_atfifo_t * const p_fifo, nrf_atfifo_item_get_t * p_context)
  144. {
  145. if (nrf_atfifo_rspace_req(p_fifo, &(p_context->last_head)))
  146. {
  147. void * p_item = ((uint8_t*)(p_fifo->p_buf)) + p_context->last_head.pos.rd;
  148. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Get element: 0x%08X", p_item);
  149. return p_item;
  150. }
  151. NRF_LOG_INST_WARNING(p_fifo->p_log, "Get failed - no item in the FIFO.");
  152. return NULL;
  153. }
  154. bool nrf_atfifo_item_free(nrf_atfifo_t * const p_fifo, nrf_atfifo_item_get_t * p_context)
  155. {
  156. if ((p_context->last_head.pos.wr) == (p_context->last_head.pos.rd))
  157. {
  158. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Free (uninterrupted)");
  159. nrf_atfifo_rspace_close(p_fifo);
  160. return true;
  161. }
  162. NRF_LOG_INST_DEBUG(p_fifo->p_log, "Free (interrupted)");
  163. return false;
  164. }