hli_api.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include "esp_err.h"
  9. #include "esp_intr_alloc.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/semphr.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #if CONFIG_BTDM_CTRL_HLI
  17. /*** Queues ***/
  18. struct hli_queue_t
  19. {
  20. size_t elem_size;
  21. char* begin;
  22. char* end;
  23. const char* bufend;
  24. QueueHandle_t downstream;
  25. int flags;
  26. char buf[0];
  27. };
  28. /**
  29. * @brief Register a high level interrupt function
  30. *
  31. * @param handler interrupt handler function
  32. * @param arg argument to pass to the interrupt handler
  33. * @param intr_reg address of the peripheral register containing the interrupt status,
  34. * or value 0 to get the status from CPU INTERRUPT register
  35. * @param intr_mask mask of the interrupt, in the interrupt status register
  36. * @return
  37. * - ESP_OK on success
  38. * - ESP_ERR_NO_MEM if too many handlers are registered
  39. */
  40. esp_err_t hli_intr_register(intr_handler_t handler, void* arg, uint32_t intr_reg, uint32_t intr_mask);
  41. /**
  42. * @brief Mask all interrupts (including high level ones) on the current CPU
  43. *
  44. * @return uint32_t interrupt status, pass it to hli_intr_restore
  45. */
  46. uint32_t hli_intr_disable(void);
  47. /**
  48. * @brief Re-enable interrupts
  49. *
  50. * @param state value returned by hli_intr_disable
  51. */
  52. void hli_intr_restore(uint32_t state);
  53. /**
  54. * @brief Type of a hli queue
  55. */
  56. typedef struct hli_queue_t* hli_queue_handle_t;
  57. /**
  58. * @brief Initialize hli_queue module. Must be called once before using hli queue APIs.
  59. */
  60. void hli_queue_setup(void);
  61. /**
  62. * @brief Shutdown hli_queue module.
  63. */
  64. void hli_queue_shutdown(void);
  65. /**
  66. * @brief Create a hli queue, wrapping a FreeRTOS queue
  67. *
  68. * This queue can be used from high level interrupts,
  69. * but **ONLY ON THE CPU WHERE hli_queue_setup WAS CALLED**. Values sent to this
  70. * queue are automatically forwarded to "downstream" FreeRTOS queue using a level 3
  71. * software interrupt.
  72. *
  73. * @param nelem number of elements in the queue
  74. * @param elem_size size of one element; must match element size of a downstream queue
  75. * @param downstream FreeRTOS queue to send the values to
  76. * @return hli_queue_handle_t handle of the created queue, or NULL on failure
  77. */
  78. hli_queue_handle_t hli_queue_create(size_t nelem, size_t elem_size, QueueHandle_t downstream);
  79. /**
  80. * @brief Create a customer hli queue, wrapping a FreeRTOS queue
  81. *
  82. * This queue can be used from high level interrupts,
  83. * but **ONLY ON THE CPU WHERE hli_queue_setup WAS CALLED**. Values sent to this
  84. * queue are automatically forwarded to "downstream" FreeRTOS queue using a level 3
  85. * software interrupt.
  86. *
  87. * @param nelem number of elements in the queue
  88. * @param elem_size size of one element; must match element size of a downstream queue
  89. * @param downstream FreeRTOS queue to send the values to
  90. * @return hli_queue_handle_t handle of the created queue, or NULL on failure
  91. */
  92. hli_queue_handle_t hli_customer_queue_create(size_t nelem, size_t elem_size, QueueHandle_t downstream);
  93. /**
  94. * @brief Create a hli queue, wrapping a FreeRTOS semaphore
  95. *
  96. * See notes on hli_queue_create.
  97. *
  98. * @param max_count maximum semaphore count
  99. * @param downstream FreeRTOS semaphore to forward the calls to
  100. * @return hli_queue_handle_t handle of the created queue, or NULL on failure
  101. */
  102. hli_queue_handle_t hli_semaphore_create(size_t max_count, SemaphoreHandle_t downstream);
  103. /**
  104. * @brief Delete a hli queue
  105. *
  106. * Make sure noone is using the queue before deleting it.
  107. *
  108. * @param queue handle returned by hli_queue_create or hli_semaphore_create
  109. */
  110. void hli_queue_delete(hli_queue_handle_t queue);
  111. /**
  112. * @brief Get one element from a hli queue
  113. *
  114. * Usually not used, values get sent to a downstream FreeRTOS queue automatically.
  115. * However if downstream queue is NULL, this API can be used to get values from a hli queue.
  116. *
  117. * @param queue handle of a queue
  118. * @param out pointer where to store the element
  119. * @return true if the element was successfully read from the queue
  120. */
  121. bool hli_queue_get(hli_queue_handle_t queue, void* out);
  122. /**
  123. * @brief Put one element into a hli queue
  124. *
  125. * This puts copies an element into the queue and raises a software interrupt (level 3).
  126. * In the interrupt, the value is copied to a FreeRTOS "downstream" queue.
  127. *
  128. * Note that if the value does not fit into a downstream queue, no error is returned,
  129. * and the value is lost.
  130. *
  131. * @param queue handle of a queue
  132. * @param data pointer to the element to be sent
  133. * @return true if data was placed into the hli queue successfully
  134. */
  135. bool hli_queue_put(hli_queue_handle_t queue, const void* data);
  136. /**
  137. * @brief "Give" a semaphore wrapped by a hli queue
  138. *
  139. * @param queue handle returned by hli_semaphore_create
  140. * @return true if the event was sent to a hli queue successfully
  141. */
  142. bool hli_semaphore_give(hli_queue_handle_t queue);
  143. #endif /* CONFIG_BTDM_CTRL_HLI */
  144. #ifdef __cplusplus
  145. }
  146. #endif