hci_mem_pool.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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(HCI_MEM_POOL)
  42. #include "hci_mem_pool.h"
  43. #include <stdbool.h>
  44. #include <stdio.h>
  45. /**@brief RX buffer element instance structure.
  46. */
  47. typedef struct
  48. {
  49. uint8_t rx_buffer[HCI_RX_BUF_SIZE]; /**< RX buffer memory array. */
  50. uint32_t length; /**< Length of the RX buffer memory array. */
  51. } rx_buffer_elem_t;
  52. /**@brief RX buffer queue element instance structure.
  53. */
  54. typedef struct
  55. {
  56. rx_buffer_elem_t * p_buffer; /**< Pointer to RX buffer element. */
  57. uint32_t free_window_count; /**< Free space element count. */
  58. uint32_t free_available_count; /**< Free area element count. */
  59. uint32_t read_available_count; /**< Read area element count. */
  60. uint32_t write_index; /**< Write position index. */
  61. uint32_t read_index; /**< Read position index. */
  62. uint32_t free_index; /**< Free position index. */
  63. } rx_buffer_queue_t;
  64. static bool m_is_tx_allocated; /**< Boolean value to determine if the TX buffer is allocated. */
  65. static rx_buffer_elem_t m_rx_buffer_elem_queue[HCI_RX_BUF_QUEUE_SIZE]; /**< RX buffer element instances. */
  66. static rx_buffer_queue_t m_rx_buffer_queue; /**< RX buffer queue element instance. */
  67. uint32_t hci_mem_pool_open(void)
  68. {
  69. m_is_tx_allocated = false;
  70. m_rx_buffer_queue.p_buffer = m_rx_buffer_elem_queue;
  71. m_rx_buffer_queue.free_window_count = HCI_RX_BUF_QUEUE_SIZE;
  72. m_rx_buffer_queue.free_available_count = 0;
  73. m_rx_buffer_queue.read_available_count = 0;
  74. m_rx_buffer_queue.write_index = 0;
  75. m_rx_buffer_queue.read_index = 0;
  76. m_rx_buffer_queue.free_index = 0;
  77. return NRF_SUCCESS;
  78. }
  79. uint32_t hci_mem_pool_close(void)
  80. {
  81. return NRF_SUCCESS;
  82. }
  83. uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer)
  84. {
  85. static uint8_t tx_buffer[HCI_TX_BUF_SIZE];
  86. uint32_t err_code;
  87. if (pp_buffer == NULL)
  88. {
  89. return NRF_ERROR_NULL;
  90. }
  91. if (!m_is_tx_allocated)
  92. {
  93. m_is_tx_allocated = true;
  94. *pp_buffer = tx_buffer;
  95. err_code = NRF_SUCCESS;
  96. }
  97. else
  98. {
  99. err_code = NRF_ERROR_NO_MEM;
  100. }
  101. return err_code;
  102. }
  103. uint32_t hci_mem_pool_tx_free(void)
  104. {
  105. m_is_tx_allocated = false;
  106. return NRF_SUCCESS;
  107. }
  108. uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
  109. {
  110. uint32_t err_code;
  111. if (pp_buffer == NULL)
  112. {
  113. return NRF_ERROR_NULL;
  114. }
  115. *pp_buffer = NULL;
  116. if (m_rx_buffer_queue.free_window_count != 0)
  117. {
  118. if (length <= HCI_RX_BUF_SIZE)
  119. {
  120. --(m_rx_buffer_queue.free_window_count);
  121. ++(m_rx_buffer_queue.read_available_count);
  122. *pp_buffer =
  123. m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.write_index].rx_buffer;
  124. m_rx_buffer_queue.free_index |= (1u << m_rx_buffer_queue.write_index);
  125. // @note: Adjust the write_index making use of the fact that the buffer size is of
  126. // power of two and two's complement arithmetic. For details refer example to book
  127. // "Making embedded systems: Elicia White".
  128. m_rx_buffer_queue.write_index =
  129. (m_rx_buffer_queue.write_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
  130. err_code = NRF_SUCCESS;
  131. }
  132. else
  133. {
  134. err_code = NRF_ERROR_DATA_SIZE;
  135. }
  136. }
  137. else
  138. {
  139. err_code = NRF_ERROR_NO_MEM;
  140. }
  141. return err_code;
  142. }
  143. uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer)
  144. {
  145. uint32_t err_code;
  146. uint32_t consume_index;
  147. uint32_t start_index;
  148. if (m_rx_buffer_queue.free_available_count != 0)
  149. {
  150. // Find the buffer that has been freed -
  151. // Start at read_index minus free_available_count and then increment until read index.
  152. err_code = NRF_ERROR_INVALID_ADDR;
  153. consume_index = (m_rx_buffer_queue.read_index - m_rx_buffer_queue.free_available_count) &
  154. (HCI_RX_BUF_QUEUE_SIZE - 1u);
  155. start_index = consume_index;
  156. do
  157. {
  158. if (m_rx_buffer_queue.p_buffer[consume_index].rx_buffer == p_buffer)
  159. {
  160. m_rx_buffer_queue.free_index ^= (1u << consume_index);
  161. err_code = NRF_SUCCESS;
  162. break;
  163. }
  164. else
  165. {
  166. consume_index = (consume_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
  167. }
  168. }
  169. while (consume_index != m_rx_buffer_queue.read_index);
  170. while (!(m_rx_buffer_queue.free_index & (1 << start_index)) &&
  171. (m_rx_buffer_queue.free_available_count != 0))
  172. {
  173. --(m_rx_buffer_queue.free_available_count);
  174. ++(m_rx_buffer_queue.free_window_count);
  175. start_index = (consume_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
  176. }
  177. }
  178. else
  179. {
  180. err_code = NRF_ERROR_NO_MEM;
  181. }
  182. return err_code;
  183. }
  184. uint32_t hci_mem_pool_rx_data_size_set(uint32_t length)
  185. {
  186. // @note: Adjust the write_index making use of the fact that the buffer size is of power
  187. // of two and two's complement arithmetic. For details refer example to book
  188. // "Making embedded systems: Elicia White".
  189. const uint32_t index = (m_rx_buffer_queue.write_index - 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
  190. m_rx_buffer_queue.p_buffer[index].length = length;
  191. return NRF_SUCCESS;
  192. }
  193. uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length)
  194. {
  195. uint32_t err_code;
  196. if ((pp_buffer == NULL) || (p_length == NULL))
  197. {
  198. return NRF_ERROR_NULL;
  199. }
  200. if (m_rx_buffer_queue.read_available_count != 0)
  201. {
  202. --(m_rx_buffer_queue.read_available_count);
  203. ++(m_rx_buffer_queue.free_available_count);
  204. *pp_buffer =
  205. m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].rx_buffer;
  206. *p_length =
  207. m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].length;
  208. // @note: Adjust the write_index making use of the fact that the buffer size is of power
  209. // of two and two's complement arithmetic. For details refer example to book
  210. // "Making embedded systems: Elicia White".
  211. m_rx_buffer_queue.read_index =
  212. (m_rx_buffer_queue.read_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
  213. err_code = NRF_SUCCESS;
  214. }
  215. else
  216. {
  217. err_code = NRF_ERROR_NO_MEM;
  218. }
  219. return err_code;
  220. }
  221. #endif //NRF_MODULE_ENABLED(HCI_MEM_POOL)