hli_api.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include <string.h>
  14. #include "esp_log.h"
  15. #include "esp_heap_caps.h"
  16. #include "xtensa/core-macros.h"
  17. #include "soc/dport_reg.h"
  18. #include "hli_api.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/queue.h"
  21. #if CONFIG_BTDM_CTRL_HLI
  22. #define HLI_MAX_HANDLERS 4
  23. typedef struct {
  24. intr_handler_t handler;
  25. void* arg;
  26. uint32_t intr_reg;
  27. uint32_t intr_mask;
  28. } hli_handler_info_t;
  29. typedef struct {
  30. #define CUSTOMER_TYPE_REQUEST (0)
  31. #define CUSTOMER_TYPE_RELEASE (1)
  32. struct {
  33. uint32_t cb_type;
  34. union {
  35. int (* request)(uint32_t, uint32_t, uint32_t);
  36. int (* release)(uint32_t);
  37. } cb;
  38. } customer_cb;
  39. uint32_t arg0, arg1, arg2;
  40. } customer_swisr_t;
  41. static void IRAM_ATTR customer_swisr_handle(customer_swisr_t *cus_swisr)
  42. {
  43. if (cus_swisr->customer_cb.cb_type == CUSTOMER_TYPE_REQUEST) {
  44. if (cus_swisr->customer_cb.cb.request != NULL) {
  45. cus_swisr->customer_cb.cb.request(cus_swisr->arg0, cus_swisr->arg1, cus_swisr->arg2);
  46. }
  47. } else if(cus_swisr->customer_cb.cb_type == CUSTOMER_TYPE_RELEASE) {
  48. if (cus_swisr->customer_cb.cb.release != NULL) {
  49. cus_swisr->customer_cb.cb.release(cus_swisr->arg0);
  50. }
  51. }
  52. }
  53. static DRAM_ATTR hli_handler_info_t s_hli_handlers[HLI_MAX_HANDLERS];
  54. esp_err_t hli_intr_register(intr_handler_t handler, void* arg, uint32_t intr_reg, uint32_t intr_mask)
  55. {
  56. for (hli_handler_info_t* hip = s_hli_handlers;
  57. hip < s_hli_handlers + HLI_MAX_HANDLERS;
  58. ++hip) {
  59. if (hip->handler == NULL) {
  60. hip->arg = arg;
  61. hip->intr_reg = intr_reg;
  62. hip->intr_mask = intr_mask;
  63. hip->handler = handler; /* set last, indicates the entry as valid */
  64. return ESP_OK;
  65. }
  66. }
  67. return ESP_ERR_NO_MEM;
  68. }
  69. void IRAM_ATTR hli_c_handler(void)
  70. {
  71. bool handled = false;
  72. /* Iterate over registered interrupt handlers,
  73. * and check if the expected mask is present in the interrupt status register.
  74. */
  75. for (hli_handler_info_t* hip = s_hli_handlers;
  76. hip < s_hli_handlers + HLI_MAX_HANDLERS;
  77. ++hip) {
  78. if (hip->handler == NULL) {
  79. continue;
  80. }
  81. uint32_t reg = hip->intr_reg;
  82. uint32_t val;
  83. if (reg == 0) { /* special case for CPU internal interrupts */
  84. val = XTHAL_GET_INTERRUPT();
  85. } else {
  86. /* "reg" might not be in DPORT, but this will work in any case */
  87. val = DPORT_REG_READ(reg);
  88. }
  89. if ((val & hip->intr_mask) != 0) {
  90. handled = true;
  91. (*hip->handler)(hip->arg);
  92. }
  93. }
  94. if (!handled) {
  95. /* no handler found, it is OK in this case. */
  96. }
  97. }
  98. uint32_t IRAM_ATTR hli_intr_disable(void)
  99. {
  100. /* disable level 4 and below */
  101. return XTOS_SET_INTLEVEL(XCHAL_DEBUGLEVEL - 2);
  102. }
  103. void IRAM_ATTR hli_intr_restore(uint32_t state)
  104. {
  105. XTOS_RESTORE_JUST_INTLEVEL(state);
  106. }
  107. #define HLI_META_QUEUE_SIZE 16
  108. #define HLI_QUEUE_MAX_ELEM_SIZE 32
  109. #define HLI_QUEUE_SW_INT_NUM 29
  110. #define HLI_QUEUE_FLAG_SEMAPHORE BIT(0)
  111. #define HLI_QUEUE_FLAG_CUSTOMER BIT(1)
  112. static DRAM_ATTR struct hli_queue_t *s_meta_queue_ptr = NULL;
  113. static intr_handle_t ret_handle;
  114. static inline char* IRAM_ATTR wrap_ptr(hli_queue_handle_t queue, char *ptr)
  115. {
  116. return (ptr == queue->bufend) ? queue->buf : ptr;
  117. }
  118. static inline bool IRAM_ATTR queue_empty(hli_queue_handle_t queue)
  119. {
  120. return queue->begin == queue->end;
  121. }
  122. static inline bool IRAM_ATTR queue_full(hli_queue_handle_t queue)
  123. {
  124. return wrap_ptr(queue, queue->end + queue->elem_size) == queue->begin;
  125. }
  126. static void IRAM_ATTR queue_isr_handler(void* arg)
  127. {
  128. int do_yield = pdFALSE;
  129. XTHAL_SET_INTCLEAR(BIT(HLI_QUEUE_SW_INT_NUM));
  130. hli_queue_handle_t queue;
  131. while (hli_queue_get(s_meta_queue_ptr, &queue)) {
  132. static DRAM_ATTR char scratch[HLI_QUEUE_MAX_ELEM_SIZE];
  133. while (hli_queue_get(queue, scratch)) {
  134. int res = pdPASS;
  135. if ((queue->flags & HLI_QUEUE_FLAG_CUSTOMER) != 0) {
  136. customer_swisr_handle((customer_swisr_t *)scratch);
  137. } else if ((queue->flags & HLI_QUEUE_FLAG_SEMAPHORE) != 0) {
  138. res = xSemaphoreGiveFromISR((SemaphoreHandle_t) queue->downstream, &do_yield);
  139. } else {
  140. res = xQueueSendFromISR(queue->downstream, scratch, &do_yield);
  141. }
  142. if (res == pdFAIL) {
  143. /* Failed to send to downstream queue, it is OK in this case. */
  144. }
  145. }
  146. }
  147. if (do_yield) {
  148. portYIELD_FROM_ISR();
  149. }
  150. }
  151. /* Notify the level 3 handler that an element is added to the given hli queue.
  152. * Do this by placing the queue handle onto s_meta_queue, and raising a SW interrupt.
  153. *
  154. * This function must be called with HL interrupts disabled!
  155. */
  156. static void IRAM_ATTR queue_signal(hli_queue_handle_t queue)
  157. {
  158. /* See if the queue is already in s_meta_queue, before adding */
  159. bool found = false;
  160. const hli_queue_handle_t *end = (hli_queue_handle_t*) s_meta_queue_ptr->end;
  161. hli_queue_handle_t *item = (hli_queue_handle_t*) s_meta_queue_ptr->begin;
  162. for (;item != end; item = (hli_queue_handle_t*) wrap_ptr(s_meta_queue_ptr, (char*) (item + 1))) {
  163. if (*item == queue) {
  164. found = true;
  165. break;
  166. }
  167. }
  168. if (!found) {
  169. bool res = hli_queue_put(s_meta_queue_ptr, &queue);
  170. if (!res) {
  171. esp_rom_printf(DRAM_STR("Fatal error in queue_signal: s_meta_queue full\n"));
  172. abort();
  173. }
  174. XTHAL_SET_INTSET(BIT(HLI_QUEUE_SW_INT_NUM));
  175. }
  176. }
  177. static void queue_init(hli_queue_handle_t queue, size_t buf_size, size_t elem_size, QueueHandle_t downstream)
  178. {
  179. queue->elem_size = elem_size;
  180. queue->begin = queue->buf;
  181. queue->end = queue->buf;
  182. queue->bufend = queue->buf + buf_size;
  183. queue->downstream = downstream;
  184. queue->flags = 0;
  185. }
  186. void hli_queue_setup(void)
  187. {
  188. if (s_meta_queue_ptr == NULL) {
  189. s_meta_queue_ptr = hli_queue_create(HLI_META_QUEUE_SIZE, sizeof(void*), NULL);
  190. ESP_ERROR_CHECK(esp_intr_alloc(ETS_INTERNAL_SW1_INTR_SOURCE, ESP_INTR_FLAG_IRAM, queue_isr_handler, NULL, &ret_handle));
  191. xt_ints_on(BIT(HLI_QUEUE_SW_INT_NUM));
  192. }
  193. }
  194. void hli_queue_shutdown(void)
  195. {
  196. if (s_meta_queue_ptr != NULL) {
  197. hli_queue_delete(s_meta_queue_ptr);
  198. s_meta_queue_ptr = NULL;
  199. esp_intr_free(ret_handle);
  200. xt_ints_off(BIT(HLI_QUEUE_SW_INT_NUM));
  201. }
  202. }
  203. hli_queue_handle_t hli_queue_create(size_t nelem, size_t elem_size, QueueHandle_t downstream)
  204. {
  205. const size_t buf_elem = nelem + 1;
  206. if (elem_size > HLI_QUEUE_MAX_ELEM_SIZE) {
  207. return NULL;
  208. }
  209. size_t buf_size = buf_elem * elem_size;
  210. hli_queue_handle_t res = (hli_queue_handle_t) heap_caps_malloc(sizeof(struct hli_queue_t) + buf_size,
  211. MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  212. if (res == NULL) {
  213. return NULL;
  214. }
  215. queue_init(res, buf_size, elem_size, downstream);
  216. return res;
  217. }
  218. hli_queue_handle_t hli_customer_queue_create(size_t nelem, size_t elem_size, QueueHandle_t downstream)
  219. {
  220. hli_queue_handle_t res = hli_queue_create(nelem, elem_size, (QueueHandle_t) downstream);
  221. if (res == NULL) {
  222. return NULL;
  223. }
  224. res->flags |= HLI_QUEUE_FLAG_CUSTOMER;
  225. return res;
  226. }
  227. hli_queue_handle_t hli_semaphore_create(size_t max_count, SemaphoreHandle_t downstream)
  228. {
  229. const size_t elem_size = 1;
  230. hli_queue_handle_t res = hli_queue_create(max_count, elem_size, (QueueHandle_t) downstream);
  231. if (res == NULL) {
  232. return NULL;
  233. }
  234. res->flags |= HLI_QUEUE_FLAG_SEMAPHORE;
  235. return res;
  236. }
  237. void hli_queue_delete(hli_queue_handle_t queue)
  238. {
  239. free(queue);
  240. }
  241. bool IRAM_ATTR hli_queue_get(hli_queue_handle_t queue, void* out)
  242. {
  243. uint32_t int_state = hli_intr_disable();
  244. bool res = false;
  245. if (!queue_empty(queue)) {
  246. memcpy(out, queue->begin, queue->elem_size);
  247. queue->begin = wrap_ptr(queue, queue->begin + queue->elem_size);
  248. res = true;
  249. }
  250. hli_intr_restore(int_state);
  251. return res;
  252. }
  253. bool IRAM_ATTR hli_queue_put(hli_queue_handle_t queue, const void* data)
  254. {
  255. uint32_t int_state = hli_intr_disable();
  256. bool res = false;
  257. bool was_empty = queue_empty(queue);
  258. if (!queue_full(queue)) {
  259. memcpy(queue->end, data, queue->elem_size);
  260. queue->end = wrap_ptr(queue, queue->end + queue->elem_size);
  261. if (was_empty && queue != s_meta_queue_ptr) {
  262. queue_signal(queue);
  263. }
  264. res = true;
  265. }
  266. hli_intr_restore(int_state);
  267. return res;
  268. }
  269. bool IRAM_ATTR hli_semaphore_give(hli_queue_handle_t queue)
  270. {
  271. uint8_t data = 0;
  272. return hli_queue_put(queue, &data);
  273. }
  274. #endif /* CONFIG_BTDM_CTRL_HLI */