hli_api.h 5.4 KB

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