hci_mem_pool.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 memory_pool Memory pool
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief Memory pool implementation
  19. *
  20. * Memory pool implementation, based on circular buffer data structure, which supports asynchronous
  21. * processing of RX data. The current default implementation supports 1 TX buffer and 4 RX buffers.
  22. * The memory managed by the pool is allocated from static storage instead of heap. The internal
  23. * design of the circular buffer implementing the RX memory layout is illustrated in the picture
  24. * below.
  25. *
  26. * @image html memory_pool.png "Circular buffer design"
  27. *
  28. * The expected call order for the RX APIs is as follows:
  29. * - hci_mem_pool_rx_produce
  30. * - hci_mem_pool_rx_data_size_set
  31. * - hci_mem_pool_rx_extract
  32. * - hci_mem_pool_rx_consume
  33. *
  34. * @warning If the above mentioned expected call order is violated the end result can be undefined.
  35. *
  36. * \par Component specific configuration options
  37. *
  38. * The following compile time configuration options are available to suit various implementations:
  39. * - TX_BUF_SIZE TX buffer size in bytes.
  40. * - RX_BUF_SIZE RX buffer size in bytes.
  41. * - RX_BUF_QUEUE_SIZE RX buffer element size.
  42. */
  43. #ifndef HCI_MEM_POOL_H__
  44. #define HCI_MEM_POOL_H__
  45. #include <stdint.h>
  46. #include "nrf_error.h"
  47. /**@brief Function for opening the module.
  48. *
  49. * @retval NRF_SUCCESS Operation success.
  50. */
  51. uint32_t hci_mem_pool_open(void);
  52. /**@brief Function for closing the module.
  53. *
  54. * @retval NRF_SUCCESS Operation success.
  55. */
  56. uint32_t hci_mem_pool_close(void);
  57. /**@brief Function for allocating requested amount of TX memory.
  58. *
  59. * @param[out] pp_buffer Pointer to the allocated memory.
  60. *
  61. * @retval NRF_SUCCESS Operation success. Memory was allocated.
  62. * @retval NRF_ERROR_NO_MEM Operation failure. No memory available for allocation.
  63. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  64. */
  65. uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer);
  66. /**@brief Function for freeing previously allocated TX memory.
  67. *
  68. * @note Memory management follows the FIFO principle meaning that free() order must match the
  69. * alloc(...) order, which is the reason for omitting exact memory block identifier as an
  70. * input parameter.
  71. *
  72. * @retval NRF_SUCCESS Operation success. Memory was freed.
  73. */
  74. uint32_t hci_mem_pool_tx_free(void);
  75. /**@brief Function for producing a free RX memory block for usage.
  76. *
  77. * @note Upon produce request amount being 0, NRF_SUCCESS is returned.
  78. *
  79. * @param[in] length Amount, in bytes, of free memory to be produced.
  80. * @param[out] pp_buffer Pointer to the allocated memory.
  81. *
  82. * @retval NRF_SUCCESS Operation success. Free RX memory block produced.
  83. * @retval NRF_ERROR_NO_MEM Operation failure. No suitable memory available for allocation.
  84. * @retval NRF_ERROR_DATA_SIZE Operation failure. Request size exceeds limit.
  85. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  86. */
  87. uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer);
  88. /**@brief Function for setting the length of the last produced RX memory block.
  89. *
  90. * @warning If call to this API is omitted the end result is that the following call to
  91. * mem_pool_rx_extract will return incorrect data in the p_length output parameter.
  92. *
  93. * @param[in] length Amount, in bytes, of actual memory used.
  94. *
  95. * @retval NRF_SUCCESS Operation success. Length was set.
  96. */
  97. uint32_t hci_mem_pool_rx_data_size_set(uint32_t length);
  98. /**@brief Function for extracting a packet, which has been filled with read data, for further
  99. * processing.
  100. *
  101. * @param[out] pp_buffer Pointer to the packet data.
  102. * @param[out] p_length Length of packet data in bytes.
  103. *
  104. * @retval NRF_SUCCESS Operation success.
  105. * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to extract.
  106. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  107. */
  108. uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length);
  109. /**@brief Function for freeing previously extracted packet, which has been filled with read data.
  110. *
  111. * @param[in] p_buffer Pointer to consumed buffer.
  112. *
  113. * @retval NRF_SUCCESS Operation success.
  114. * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to free.
  115. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Not a valid pointer.
  116. */
  117. uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer);
  118. #endif // HCI_MEM_POOL_H__
  119. /** @} */