esp_coex_adapter.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <pthread.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/queue.h"
  15. #include "freertos/semphr.h"
  16. #include "freertos/portmacro.h"
  17. #include "esp_heap_caps.h"
  18. #include "esp_timer.h"
  19. #include "private/esp_coexist_adapter.h"
  20. #include "esp32/rom/ets_sys.h"
  21. #define TAG "esp_coex_adapter"
  22. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  23. typedef struct {
  24. QueueHandle_t handle; /**< FreeRTOS queue handler */
  25. void *storage; /**< storage for FreeRTOS queue */
  26. } modem_static_queue_t;
  27. bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void)
  28. {
  29. #ifdef CONFIG_IDF_ENV_FPGA
  30. return false;
  31. #else
  32. return true;
  33. #endif
  34. }
  35. void * esp_coex_common_spin_lock_create_wrapper(void)
  36. {
  37. portMUX_TYPE tmp = portMUX_INITIALIZER_UNLOCKED;
  38. void *mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  39. if (mux) {
  40. memcpy(mux,&tmp,sizeof(portMUX_TYPE));
  41. return mux;
  42. }
  43. return NULL;
  44. }
  45. uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux)
  46. {
  47. if (xPortInIsrContext()) {
  48. portENTER_CRITICAL_ISR(wifi_int_mux);
  49. } else {
  50. portENTER_CRITICAL(wifi_int_mux);
  51. }
  52. return 0;
  53. }
  54. void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp)
  55. {
  56. if (xPortInIsrContext()) {
  57. portEXIT_CRITICAL_ISR(wifi_int_mux);
  58. } else {
  59. portEXIT_CRITICAL(wifi_int_mux);
  60. }
  61. }
  62. void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void)
  63. {
  64. portYIELD_FROM_ISR();
  65. }
  66. void * esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init)
  67. {
  68. return (void *)xSemaphoreCreateCounting(max, init);
  69. }
  70. void esp_coex_common_semphr_delete_wrapper(void *semphr)
  71. {
  72. vSemaphoreDelete(semphr);
  73. }
  74. int32_t esp_coex_common_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
  75. {
  76. if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
  77. return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
  78. } else {
  79. return (int32_t)xSemaphoreTake(semphr, block_time_tick);
  80. }
  81. }
  82. int32_t esp_coex_common_semphr_give_wrapper(void *semphr)
  83. {
  84. return (int32_t)xSemaphoreGive(semphr);
  85. }
  86. void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer)
  87. {
  88. ets_timer_disarm(timer);
  89. }
  90. void esp_coex_common_timer_done_wrapper(void *ptimer)
  91. {
  92. ets_timer_done(ptimer);
  93. }
  94. void esp_coex_common_timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
  95. {
  96. ets_timer_setfn(ptimer, pfunction, parg);
  97. }
  98. void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repeat)
  99. {
  100. ets_timer_arm_us(ptimer, us, repeat);
  101. }
  102. void * IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size)
  103. {
  104. return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  105. }
  106. /* static wrapper */
  107. static int IRAM_ATTR esp_coex_is_in_isr_wrapper(void)
  108. {
  109. return !xPortCanYield();
  110. }
  111. static void *esp_coex_internal_semphr_create_wrapper(uint32_t max, uint32_t init)
  112. {
  113. modem_static_queue_t *semphr = heap_caps_calloc(1, sizeof(modem_static_queue_t), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  114. if (!semphr) {
  115. return NULL;
  116. }
  117. #ifdef CONFIG_SPIRAM_USE_MALLOC
  118. semphr->storage = heap_caps_calloc(1, sizeof(StaticSemaphore_t), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  119. if (!semphr->storage) {
  120. goto _error;
  121. }
  122. semphr->handle = xSemaphoreCreateCountingStatic(max, init, semphr->storage);
  123. if (!semphr->handle) {
  124. goto _error;
  125. }
  126. return (void *)semphr;
  127. _error:
  128. if (semphr) {
  129. if (semphr->storage) {
  130. free(semphr->storage);
  131. }
  132. free(semphr);
  133. }
  134. return NULL;
  135. #else
  136. semphr->handle = xSemaphoreCreateCounting(max, init);
  137. return (void *)semphr;
  138. #endif
  139. }
  140. static void esp_coex_internal_semphr_delete_wrapper(void *semphr)
  141. {
  142. modem_static_queue_t *semphr_item = (modem_static_queue_t *)semphr;
  143. if (semphr_item) {
  144. if (semphr_item->handle) {
  145. vSemaphoreDelete(semphr_item->handle);
  146. }
  147. #ifdef CONFIG_SPIRAM_USE_MALLOC
  148. if (semphr_item->storage) {
  149. free(semphr_item->storage);
  150. }
  151. #endif
  152. free(semphr_item);
  153. }
  154. }
  155. static int32_t IRAM_ATTR esp_coex_internal_semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  156. {
  157. return (int32_t)xSemaphoreTakeFromISR(((modem_static_queue_t *)semphr)->handle, hptw);
  158. }
  159. static int32_t IRAM_ATTR esp_coex_internal_semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  160. {
  161. return (int32_t)xSemaphoreGiveFromISR(((modem_static_queue_t *)semphr)->handle, hptw);
  162. }
  163. static int32_t esp_coex_internal_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
  164. {
  165. if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
  166. return (int32_t)xSemaphoreTake(((modem_static_queue_t *)semphr)->handle, portMAX_DELAY);
  167. } else {
  168. return (int32_t)xSemaphoreTake(((modem_static_queue_t *)semphr)->handle, block_time_tick);
  169. }
  170. }
  171. static int32_t esp_coex_internal_semphr_give_wrapper(void *semphr)
  172. {
  173. return (int32_t)xSemaphoreGive(((modem_static_queue_t *)semphr)->handle);
  174. }
  175. coex_adapter_funcs_t g_coex_adapter_funcs = {
  176. ._version = COEX_ADAPTER_VERSION,
  177. ._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
  178. ._spin_lock_delete = free,
  179. ._int_disable = esp_coex_common_int_disable_wrapper,
  180. ._int_enable = esp_coex_common_int_restore_wrapper,
  181. ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
  182. ._semphr_create = esp_coex_internal_semphr_create_wrapper,
  183. ._semphr_delete = esp_coex_internal_semphr_delete_wrapper,
  184. ._semphr_take_from_isr = esp_coex_internal_semphr_take_from_isr_wrapper,
  185. ._semphr_give_from_isr = esp_coex_internal_semphr_give_from_isr_wrapper,
  186. ._semphr_take = esp_coex_internal_semphr_take_wrapper,
  187. ._semphr_give = esp_coex_internal_semphr_give_wrapper,
  188. ._is_in_isr = esp_coex_is_in_isr_wrapper,
  189. ._malloc_internal = esp_coex_common_malloc_internal_wrapper,
  190. ._free = free,
  191. ._esp_timer_get_time = esp_timer_get_time,
  192. ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
  193. ._timer_done = esp_coex_common_timer_done_wrapper,
  194. ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
  195. ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
  196. ._magic = COEX_ADAPTER_MAGIC,
  197. };