bt.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 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 "sdkconfig.h"
  11. #include "esp_heap_caps.h"
  12. #include "esp_heap_caps_init.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/queue.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/xtensa_api.h"
  18. #include "freertos/portmacro.h"
  19. #include "xtensa/core-macros.h"
  20. #include "esp_types.h"
  21. #include "esp_system.h"
  22. #include "esp_task.h"
  23. #include "esp_intr_alloc.h"
  24. #include "esp_attr.h"
  25. #include "esp_phy_init.h"
  26. #include "esp_bt.h"
  27. #include "esp_err.h"
  28. #include "esp_log.h"
  29. #include "esp_pm.h"
  30. #include "driver/periph_ctrl.h"
  31. #include "soc/rtc.h"
  32. #include "soc/soc_memory_layout.h"
  33. #include "soc/dport_reg.h"
  34. #include "esp32/clk.h"
  35. #include "esp_coexist_internal.h"
  36. #if !CONFIG_FREERTOS_UNICORE
  37. #include "esp_ipc.h"
  38. #endif
  39. #include "esp_rom_sys.h"
  40. #include "hli_api.h"
  41. #if CONFIG_BT_ENABLED
  42. /* Macro definition
  43. ************************************************************************
  44. */
  45. #define UNUSED(x) (void)(x)
  46. #define BTDM_LOG_TAG "BTDM_INIT"
  47. #define BTDM_INIT_PERIOD (5000) /* ms */
  48. /* Bluetooth system and controller config */
  49. #define BTDM_CFG_BT_DATA_RELEASE (1<<0)
  50. #define BTDM_CFG_HCI_UART (1<<1)
  51. #define BTDM_CFG_CONTROLLER_RUN_APP_CPU (1<<2)
  52. #define BTDM_CFG_SCAN_DUPLICATE_OPTIONS (1<<3)
  53. #define BTDM_CFG_SEND_ADV_RESERVED_SIZE (1<<4)
  54. #define BTDM_CFG_BLE_FULL_SCAN_SUPPORTED (1<<5)
  55. /* Sleep mode */
  56. #define BTDM_MODEM_SLEEP_MODE_NONE (0)
  57. #define BTDM_MODEM_SLEEP_MODE_ORIG (1)
  58. #define BTDM_MODEM_SLEEP_MODE_EVED (2) // sleep mode for BLE controller, used only for internal test.
  59. /* Low Power Clock Selection */
  60. #define BTDM_LPCLK_SEL_XTAL (0)
  61. #define BTDM_LPCLK_SEL_XTAL32K (1)
  62. #define BTDM_LPCLK_SEL_RTC_SLOW (2)
  63. #define BTDM_LPCLK_SEL_8M (3)
  64. /* Sleep and wakeup interval control */
  65. #define BTDM_MIN_SLEEP_DURATION (12) // threshold of interval in slots to allow to fall into modem sleep
  66. #define BTDM_MODEM_WAKE_UP_DELAY (4) // delay in slots of modem wake up procedure, including re-enable PHY/RF
  67. #define BT_DEBUG(...)
  68. #define BT_API_CALL_CHECK(info, api_call, ret) \
  69. do{\
  70. esp_err_t __err = (api_call);\
  71. if ((ret) != __err) {\
  72. BT_DEBUG("%s %d %s ret=0x%X\n", __FUNCTION__, __LINE__, (info), __err);\
  73. return __err;\
  74. }\
  75. } while(0)
  76. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  77. #define OSI_VERSION 0x00010003
  78. #define OSI_MAGIC_VALUE 0xFADEBEAD
  79. /* SPIRAM Configuration */
  80. #if CONFIG_SPIRAM_USE_MALLOC
  81. #define BTDM_MAX_QUEUE_NUM (6)
  82. #endif
  83. /* Types definition
  84. ************************************************************************
  85. */
  86. /* VHCI function interface */
  87. typedef struct vhci_host_callback {
  88. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  89. int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
  90. } vhci_host_callback_t;
  91. /* Dram region */
  92. typedef struct {
  93. esp_bt_mode_t mode;
  94. intptr_t start;
  95. intptr_t end;
  96. } btdm_dram_available_region_t;
  97. /* PSRAM configuration */
  98. #if CONFIG_SPIRAM_USE_MALLOC
  99. typedef struct {
  100. QueueHandle_t handle;
  101. void *storage;
  102. void *buffer;
  103. } btdm_queue_item_t;
  104. #endif
  105. /* OSI function */
  106. struct osi_funcs_t {
  107. uint32_t _version;
  108. xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
  109. void (*_ints_on)(unsigned int mask);
  110. void (*_interrupt_disable)(void);
  111. void (*_interrupt_restore)(void);
  112. void (*_task_yield)(void);
  113. void (*_task_yield_from_isr)(void);
  114. void *(*_semphr_create)(uint32_t max, uint32_t init);
  115. void (*_semphr_delete)(void *semphr);
  116. int32_t (*_semphr_take_from_isr)(void *semphr, void *hptw);
  117. int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
  118. int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
  119. int32_t (*_semphr_give)(void *semphr);
  120. void *(*_mutex_create)(void);
  121. void (*_mutex_delete)(void *mutex);
  122. int32_t (*_mutex_lock)(void *mutex);
  123. int32_t (*_mutex_unlock)(void *mutex);
  124. void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
  125. void (* _queue_delete)(void *queue);
  126. int32_t (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
  127. int32_t (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
  128. int32_t (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
  129. int32_t (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
  130. int32_t (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  131. void (* _task_delete)(void *task_handle);
  132. bool (* _is_in_isr)(void);
  133. int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
  134. void *(* _malloc)(uint32_t size);
  135. void *(* _malloc_internal)(uint32_t size);
  136. void (* _free)(void *p);
  137. int32_t (* _read_efuse_mac)(uint8_t mac[6]);
  138. void (* _srand)(unsigned int seed);
  139. int (* _rand)(void);
  140. uint32_t (* _btdm_lpcycles_2_us)(uint32_t cycles);
  141. uint32_t (* _btdm_us_2_lpcycles)(uint32_t us);
  142. bool (* _btdm_sleep_check_duration)(uint32_t *slot_cnt);
  143. void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */
  144. void (* _btdm_sleep_enter_phase2)(void);
  145. void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */
  146. void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */
  147. void (* _btdm_sleep_exit_phase3)(void); /* called from task */
  148. bool (* _coex_bt_wakeup_request)(void);
  149. void (* _coex_bt_wakeup_request_end)(void);
  150. int (* _coex_bt_request)(uint32_t event, uint32_t latency, uint32_t duration);
  151. int (* _coex_bt_release)(uint32_t event);
  152. int (* _coex_register_bt_cb)(coex_func_cb_t cb);
  153. uint32_t (* _coex_bb_reset_lock)(void);
  154. void (* _coex_bb_reset_unlock)(uint32_t restore);
  155. int (* _coex_schm_register_btdm_callback)(void *callback);
  156. void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status);
  157. void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status);
  158. uint32_t (* _coex_schm_interval_get)(void);
  159. uint8_t (* _coex_schm_curr_period_get)(void);
  160. void *(* _coex_schm_curr_phase_get)(void);
  161. int (* _coex_wifi_channel_get)(uint8_t *primary, uint8_t *secondary);
  162. int (* _coex_register_wifi_channel_change_callback)(void *cb);
  163. xt_handler (*_set_isr_l3)(int n, xt_handler f, void *arg);
  164. void (*_interrupt_l3_disable)(void);
  165. void (*_interrupt_l3_restore)(void);
  166. void *(* _customer_queue_create)(uint32_t queue_len, uint32_t item_size);
  167. uint32_t _magic;
  168. };
  169. typedef void (*workitem_handler_t)(void* arg);
  170. /* External functions or values
  171. ************************************************************************
  172. */
  173. /* not for user call, so don't put to include file */
  174. /* OSI */
  175. extern int btdm_osi_funcs_register(void *osi_funcs);
  176. /* Initialise and De-initialise */
  177. extern int btdm_controller_init(uint32_t config_mask, esp_bt_controller_config_t *config_opts);
  178. extern void btdm_controller_deinit(void);
  179. extern int btdm_controller_enable(esp_bt_mode_t mode);
  180. extern void btdm_controller_disable(void);
  181. extern uint8_t btdm_controller_get_mode(void);
  182. extern const char *btdm_controller_get_compile_version(void);
  183. extern void btdm_rf_bb_init_phase2(void); // shall be called after PHY/RF is enabled
  184. extern int btdm_dispatch_work_to_controller(workitem_handler_t callback, void *arg, bool blocking);
  185. /* Sleep */
  186. extern void btdm_controller_enable_sleep(bool enable);
  187. extern void btdm_controller_set_sleep_mode(uint8_t mode);
  188. extern uint8_t btdm_controller_get_sleep_mode(void);
  189. extern bool btdm_power_state_active(void);
  190. extern void btdm_wakeup_request(void);
  191. extern void btdm_in_wakeup_requesting_set(bool in_wakeup_requesting);
  192. /* Low Power Clock */
  193. extern bool btdm_lpclk_select_src(uint32_t sel);
  194. extern bool btdm_lpclk_set_div(uint32_t div);
  195. /* VHCI */
  196. extern bool API_vhci_host_check_send_available(void);
  197. extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
  198. extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback);
  199. /* TX power */
  200. extern int ble_txpwr_set(int power_type, int power_level);
  201. extern int ble_txpwr_get(int power_type);
  202. extern int bredr_txpwr_set(int min_power_level, int max_power_level);
  203. extern int bredr_txpwr_get(int *min_power_level, int *max_power_level);
  204. extern void bredr_sco_datapath_set(uint8_t data_path);
  205. extern void btdm_controller_scan_duplicate_list_clear(void);
  206. /* Coexistence */
  207. extern int coex_bt_request(uint32_t event, uint32_t latency, uint32_t duration);
  208. extern int coex_bt_release(uint32_t event);
  209. extern int coex_register_bt_cb(coex_func_cb_t cb);
  210. extern uint32_t coex_bb_reset_lock(void);
  211. extern void coex_bb_reset_unlock(uint32_t restore);
  212. extern int coex_schm_register_btdm_callback(void *callback);
  213. extern void coex_schm_status_bit_clear(uint32_t type, uint32_t status);
  214. extern void coex_schm_status_bit_set(uint32_t type, uint32_t status);
  215. extern uint32_t coex_schm_interval_get(void);
  216. extern uint8_t coex_schm_curr_period_get(void);
  217. extern void * coex_schm_curr_phase_get(void);
  218. extern int coex_wifi_channel_get(uint8_t *primary, uint8_t *secondary);
  219. extern int coex_register_wifi_channel_change_callback(void *cb);
  220. /* Shutdown */
  221. extern void esp_bt_controller_shutdown(void);
  222. extern char _bss_start_btdm;
  223. extern char _bss_end_btdm;
  224. extern char _data_start_btdm;
  225. extern char _data_end_btdm;
  226. extern uint32_t _data_start_btdm_rom;
  227. extern uint32_t _data_end_btdm_rom;
  228. extern uint32_t _bt_bss_start;
  229. extern uint32_t _bt_bss_end;
  230. extern uint32_t _nimble_bss_start;
  231. extern uint32_t _nimble_bss_end;
  232. extern uint32_t _btdm_bss_start;
  233. extern uint32_t _btdm_bss_end;
  234. extern uint32_t _bt_data_start;
  235. extern uint32_t _bt_data_end;
  236. extern uint32_t _nimble_data_start;
  237. extern uint32_t _nimble_data_end;
  238. extern uint32_t _btdm_data_start;
  239. extern uint32_t _btdm_data_end;
  240. /* Local Function Declare
  241. *********************************************************************
  242. */
  243. #if CONFIG_SPIRAM_USE_MALLOC
  244. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue);
  245. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue);
  246. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  247. #if CONFIG_BTDM_CTRL_HLI
  248. static xt_handler set_isr_hlevel_wrapper(int n, xt_handler f, void *arg);
  249. static void IRAM_ATTR interrupt_hlevel_disable(void);
  250. static void IRAM_ATTR interrupt_hlevel_restore(void);
  251. #endif /* CONFIG_BTDM_CTRL_HLI */
  252. static void IRAM_ATTR task_yield(void);
  253. static void IRAM_ATTR task_yield_from_isr(void);
  254. static void *semphr_create_wrapper(uint32_t max, uint32_t init);
  255. static void semphr_delete_wrapper(void *semphr);
  256. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
  257. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
  258. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
  259. static int32_t semphr_give_wrapper(void *semphr);
  260. static void *mutex_create_wrapper(void);
  261. static void mutex_delete_wrapper(void *mutex);
  262. static int32_t mutex_lock_wrapper(void *mutex);
  263. static int32_t mutex_unlock_wrapper(void *mutex);
  264. #if CONFIG_BTDM_CTRL_HLI
  265. static void *queue_create_hlevel_wrapper(uint32_t queue_len, uint32_t item_size);
  266. static void queue_delete_hlevel_wrapper(void *queue);
  267. static int32_t IRAM_ATTR queue_send_hlevel_wrapper(void *queue, void *item, uint32_t block_time_ms);
  268. static int32_t IRAM_ATTR queue_send_from_isr_hlevel_wrapper(void *queue, void *item, void *hptw);
  269. static int32_t IRAM_ATTR queue_recv_hlevel_wrapper(void *queue, void *item, uint32_t block_time_ms);
  270. static int32_t IRAM_ATTR queue_recv_from_isr_hlevel_wrapper(void *queue, void *item, void *hptw);
  271. #else
  272. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
  273. static void queue_delete_wrapper(void *queue);
  274. static int32_t IRAM_ATTR queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms);
  275. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw);
  276. static int32_t IRAM_ATTR queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
  277. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw);
  278. #endif /* CONFIG_BTDM_CTRL_HLI */
  279. static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  280. static void task_delete_wrapper(void *task_handle);
  281. static bool IRAM_ATTR is_in_isr_wrapper(void);
  282. static void IRAM_ATTR cause_sw_intr(void *arg);
  283. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no);
  284. static void *malloc_internal_wrapper(size_t size);
  285. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
  286. static void IRAM_ATTR srand_wrapper(unsigned int seed);
  287. static int IRAM_ATTR rand_wrapper(void);
  288. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles);
  289. static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us);
  290. static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt);
  291. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles);
  292. static void btdm_sleep_enter_phase2_wrapper(void);
  293. static void btdm_sleep_exit_phase3_wrapper(void);
  294. static bool coex_bt_wakeup_request(void);
  295. static void coex_bt_wakeup_request_end(void);
  296. static int coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration);
  297. static int coex_bt_release_wrapper(uint32_t event);
  298. static int coex_register_bt_cb_wrapper(coex_func_cb_t cb);
  299. static uint32_t coex_bb_reset_lock_wrapper(void);
  300. static void coex_bb_reset_unlock_wrapper(uint32_t restore);
  301. static int coex_schm_register_btdm_callback_wrapper(void *callback);
  302. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status);
  303. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status);
  304. static uint32_t coex_schm_interval_get_wrapper(void);
  305. static uint8_t coex_schm_curr_period_get_wrapper(void);
  306. static void * coex_schm_curr_phase_get_wrapper(void);
  307. static int coex_wifi_channel_get_wrapper(uint8_t *primary, uint8_t *secondary);
  308. static int coex_register_wifi_channel_change_callback_wrapper(void *cb);
  309. #if CONFIG_BTDM_CTRL_HLI
  310. static void *customer_queue_create_hlevel_wrapper(uint32_t queue_len, uint32_t item_size);
  311. #endif /* CONFIG_BTDM_CTRL_HLI */
  312. static void IRAM_ATTR interrupt_l3_disable(void);
  313. static void IRAM_ATTR interrupt_l3_restore(void);
  314. /* Local variable definition
  315. ***************************************************************************
  316. */
  317. /* OSI funcs */
  318. static const struct osi_funcs_t osi_funcs_ro = {
  319. ._version = OSI_VERSION,
  320. #if CONFIG_BTDM_CTRL_HLI
  321. ._set_isr = set_isr_hlevel_wrapper,
  322. ._ints_on = xt_ints_on,
  323. ._interrupt_disable = interrupt_hlevel_disable,
  324. ._interrupt_restore = interrupt_hlevel_restore,
  325. #else
  326. ._set_isr = xt_set_interrupt_handler,
  327. ._ints_on = xt_ints_on,
  328. ._interrupt_disable = interrupt_l3_disable,
  329. ._interrupt_restore = interrupt_l3_restore,
  330. #endif /* CONFIG_BTDM_CTRL_HLI */
  331. ._task_yield = task_yield,
  332. ._task_yield_from_isr = task_yield_from_isr,
  333. ._semphr_create = semphr_create_wrapper,
  334. ._semphr_delete = semphr_delete_wrapper,
  335. ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
  336. ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
  337. ._semphr_take = semphr_take_wrapper,
  338. ._semphr_give = semphr_give_wrapper,
  339. ._mutex_create = mutex_create_wrapper,
  340. ._mutex_delete = mutex_delete_wrapper,
  341. ._mutex_lock = mutex_lock_wrapper,
  342. ._mutex_unlock = mutex_unlock_wrapper,
  343. #if CONFIG_BTDM_CTRL_HLI
  344. ._queue_create = queue_create_hlevel_wrapper,
  345. ._queue_delete = queue_delete_hlevel_wrapper,
  346. ._queue_send = queue_send_hlevel_wrapper,
  347. ._queue_send_from_isr = queue_send_from_isr_hlevel_wrapper,
  348. ._queue_recv = queue_recv_hlevel_wrapper,
  349. ._queue_recv_from_isr = queue_recv_from_isr_hlevel_wrapper,
  350. #else
  351. ._queue_create = queue_create_wrapper,
  352. ._queue_delete = queue_delete_wrapper,
  353. ._queue_send = queue_send_wrapper,
  354. ._queue_send_from_isr = queue_send_from_isr_wrapper,
  355. ._queue_recv = queue_recv_wrapper,
  356. ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
  357. #endif /* CONFIG_BTDM_CTRL_HLI */
  358. ._task_create = task_create_wrapper,
  359. ._task_delete = task_delete_wrapper,
  360. ._is_in_isr = is_in_isr_wrapper,
  361. ._cause_sw_intr_to_core = cause_sw_intr_to_core_wrapper,
  362. ._malloc = malloc,
  363. ._malloc_internal = malloc_internal_wrapper,
  364. ._free = free,
  365. ._read_efuse_mac = read_mac_wrapper,
  366. ._srand = srand_wrapper,
  367. ._rand = rand_wrapper,
  368. ._btdm_lpcycles_2_us = btdm_lpcycles_2_us,
  369. ._btdm_us_2_lpcycles = btdm_us_2_lpcycles,
  370. ._btdm_sleep_check_duration = btdm_sleep_check_duration,
  371. ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper,
  372. ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper,
  373. ._btdm_sleep_exit_phase1 = NULL,
  374. ._btdm_sleep_exit_phase2 = NULL,
  375. ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper,
  376. ._coex_bt_wakeup_request = coex_bt_wakeup_request,
  377. ._coex_bt_wakeup_request_end = coex_bt_wakeup_request_end,
  378. ._coex_bt_request = coex_bt_request_wrapper,
  379. ._coex_bt_release = coex_bt_release_wrapper,
  380. ._coex_register_bt_cb = coex_register_bt_cb_wrapper,
  381. ._coex_bb_reset_lock = coex_bb_reset_lock_wrapper,
  382. ._coex_bb_reset_unlock = coex_bb_reset_unlock_wrapper,
  383. ._coex_schm_register_btdm_callback = coex_schm_register_btdm_callback_wrapper,
  384. ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
  385. ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
  386. ._coex_schm_interval_get = coex_schm_interval_get_wrapper,
  387. ._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper,
  388. ._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper,
  389. ._coex_wifi_channel_get = coex_wifi_channel_get_wrapper,
  390. ._coex_register_wifi_channel_change_callback = coex_register_wifi_channel_change_callback_wrapper,
  391. ._set_isr_l3 = xt_set_interrupt_handler,
  392. ._interrupt_l3_disable = interrupt_l3_disable,
  393. ._interrupt_l3_restore = interrupt_l3_restore,
  394. #if CONFIG_BTDM_CTRL_HLI
  395. ._customer_queue_create = customer_queue_create_hlevel_wrapper,
  396. #else
  397. ._customer_queue_create = NULL,
  398. #endif /* CONFIG_BTDM_CTRL_HLI */
  399. ._magic = OSI_MAGIC_VALUE,
  400. };
  401. /* the mode column will be modified by release function to indicate the available region */
  402. static btdm_dram_available_region_t btdm_dram_available_region[] = {
  403. //following is .data
  404. {ESP_BT_MODE_BTDM, SOC_MEM_BT_DATA_START, SOC_MEM_BT_DATA_END },
  405. //following is memory which HW will use
  406. {ESP_BT_MODE_BTDM, SOC_MEM_BT_EM_BTDM0_START, SOC_MEM_BT_EM_BTDM0_END },
  407. {ESP_BT_MODE_BLE, SOC_MEM_BT_EM_BLE_START, SOC_MEM_BT_EM_BLE_END },
  408. {ESP_BT_MODE_BTDM, SOC_MEM_BT_EM_BTDM1_START, SOC_MEM_BT_EM_BTDM1_END },
  409. {ESP_BT_MODE_CLASSIC_BT, SOC_MEM_BT_EM_BREDR_START, SOC_MEM_BT_EM_BREDR_REAL_END},
  410. //following is .bss
  411. {ESP_BT_MODE_BTDM, SOC_MEM_BT_BSS_START, SOC_MEM_BT_BSS_END },
  412. {ESP_BT_MODE_BTDM, SOC_MEM_BT_MISC_START, SOC_MEM_BT_MISC_END },
  413. };
  414. /* Reserve the full memory region used by Bluetooth Controller,
  415. * some may be released later at runtime. */
  416. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_EM_START, SOC_MEM_BT_EM_BREDR_REAL_END, rom_bt_em);
  417. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_BSS_START, SOC_MEM_BT_BSS_END, rom_bt_bss);
  418. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_MISC_START, SOC_MEM_BT_MISC_END, rom_bt_misc);
  419. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_DATA_START, SOC_MEM_BT_DATA_END, rom_bt_data);
  420. static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
  421. #if CONFIG_SPIRAM_USE_MALLOC
  422. static DRAM_ATTR btdm_queue_item_t btdm_queue_table[BTDM_MAX_QUEUE_NUM];
  423. static DRAM_ATTR SemaphoreHandle_t btdm_queue_table_mux = NULL;
  424. #endif /* #if CONFIG_SPIRAM_USE_MALLOC */
  425. /* Static variable declare */
  426. // timestamp when PHY/RF was switched on
  427. static DRAM_ATTR int64_t s_time_phy_rf_just_enabled = 0;
  428. static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  429. static DRAM_ATTR portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
  430. // measured average low power clock period in micro seconds
  431. static DRAM_ATTR uint32_t btdm_lpcycle_us = 0;
  432. static DRAM_ATTR uint8_t btdm_lpcycle_us_frac = 0; // number of fractional bit for btdm_lpcycle_us
  433. #if CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_ORIG
  434. // used low power clock
  435. static DRAM_ATTR uint8_t btdm_lpclk_sel;
  436. #endif /* #ifdef CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_ORIG */
  437. static DRAM_ATTR QueueHandle_t s_wakeup_req_sem = NULL;
  438. #ifdef CONFIG_PM_ENABLE
  439. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr;
  440. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock;
  441. static bool s_pm_lock_acquired = true;
  442. static DRAM_ATTR bool s_btdm_allow_light_sleep;
  443. // pm_lock to prevent light sleep when using main crystal as Bluetooth low power clock
  444. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock;
  445. static void btdm_slp_tmr_callback(void *arg);
  446. #endif /* #ifdef CONFIG_PM_ENABLE */
  447. static inline void esp_bt_power_domain_on(void)
  448. {
  449. // Bluetooth module power up
  450. esp_wifi_bt_power_domain_on();
  451. }
  452. static inline void esp_bt_power_domain_off(void)
  453. {
  454. // Bluetooth module power down
  455. esp_wifi_bt_power_domain_off();
  456. }
  457. static inline void btdm_check_and_init_bb(void)
  458. {
  459. /* init BT-BB if PHY/RF has been switched off since last BT-BB init */
  460. int64_t latest_ts = esp_phy_rf_get_on_ts();
  461. if (latest_ts != s_time_phy_rf_just_enabled ||
  462. s_time_phy_rf_just_enabled == 0) {
  463. btdm_rf_bb_init_phase2();
  464. s_time_phy_rf_just_enabled = latest_ts;
  465. }
  466. }
  467. #if CONFIG_SPIRAM_USE_MALLOC
  468. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue)
  469. {
  470. if (!btdm_queue_table_mux || !queue) {
  471. return NULL;
  472. }
  473. bool ret = false;
  474. btdm_queue_item_t *item;
  475. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  476. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  477. item = &btdm_queue_table[i];
  478. if (item->handle == NULL) {
  479. memcpy(item, queue, sizeof(btdm_queue_item_t));
  480. ret = true;
  481. break;
  482. }
  483. }
  484. xSemaphoreGive(btdm_queue_table_mux);
  485. return ret;
  486. }
  487. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue)
  488. {
  489. if (!btdm_queue_table_mux || !queue) {
  490. return false;
  491. }
  492. bool ret = false;
  493. btdm_queue_item_t *item;
  494. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  495. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  496. item = &btdm_queue_table[i];
  497. if (item->handle == queue->handle) {
  498. memcpy(queue, item, sizeof(btdm_queue_item_t));
  499. memset(item, 0, sizeof(btdm_queue_item_t));
  500. ret = true;
  501. break;
  502. }
  503. }
  504. xSemaphoreGive(btdm_queue_table_mux);
  505. return ret;
  506. }
  507. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  508. #if CONFIG_BTDM_CTRL_HLI
  509. struct interrupt_hlevel_cb{
  510. uint32_t status;
  511. uint8_t nested;
  512. };
  513. static DRAM_ATTR struct interrupt_hlevel_cb hli_cb = {
  514. .status = 0,
  515. .nested = 0,
  516. };
  517. static xt_handler set_isr_hlevel_wrapper(int mask, xt_handler f, void *arg)
  518. {
  519. esp_err_t err = hli_intr_register((intr_handler_t) f, arg, DPORT_PRO_INTR_STATUS_0_REG, mask);
  520. if (err == ESP_OK) {
  521. return f;
  522. } else {
  523. return 0;
  524. }
  525. }
  526. static void IRAM_ATTR interrupt_hlevel_disable(void)
  527. {
  528. assert(xPortGetCoreID() == CONFIG_BTDM_CTRL_PINNED_TO_CORE);
  529. assert(hli_cb.nested != ~0);
  530. uint32_t status = hli_intr_disable();
  531. if (hli_cb.nested++ == 0) {
  532. hli_cb.status = status;
  533. }
  534. }
  535. static void IRAM_ATTR interrupt_hlevel_restore(void)
  536. {
  537. assert(xPortGetCoreID() == CONFIG_BTDM_CTRL_PINNED_TO_CORE);
  538. assert(hli_cb.nested > 0);
  539. if (--hli_cb.nested == 0) {
  540. hli_intr_restore(hli_cb.status);
  541. }
  542. }
  543. #endif /* CONFIG_BTDM_CTRL_HLI */
  544. static void IRAM_ATTR interrupt_l3_disable(void)
  545. {
  546. if (xPortInIsrContext()) {
  547. portENTER_CRITICAL_ISR(&global_int_mux);
  548. } else {
  549. portENTER_CRITICAL(&global_int_mux);
  550. }
  551. }
  552. static void IRAM_ATTR interrupt_l3_restore(void)
  553. {
  554. if (xPortInIsrContext()) {
  555. portEXIT_CRITICAL_ISR(&global_int_mux);
  556. } else {
  557. portEXIT_CRITICAL(&global_int_mux);
  558. }
  559. }
  560. static void IRAM_ATTR task_yield(void)
  561. {
  562. vPortYield();
  563. }
  564. static void IRAM_ATTR task_yield_from_isr(void)
  565. {
  566. portYIELD_FROM_ISR();
  567. }
  568. static void *semphr_create_wrapper(uint32_t max, uint32_t init)
  569. {
  570. void *handle = NULL;
  571. #if !CONFIG_SPIRAM_USE_MALLOC
  572. handle = (void *)xSemaphoreCreateCounting(max, init);
  573. #else
  574. StaticQueue_t *queue_buffer = NULL;
  575. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  576. if (!queue_buffer) {
  577. goto error;
  578. }
  579. handle = (void *)xSemaphoreCreateCountingStatic(max, init, queue_buffer);
  580. if (!handle) {
  581. goto error;
  582. }
  583. btdm_queue_item_t item = {
  584. .handle = handle,
  585. .storage = NULL,
  586. .buffer = queue_buffer,
  587. };
  588. if (!btdm_queue_generic_register(&item)) {
  589. goto error;
  590. }
  591. #endif
  592. #if CONFIG_BTDM_CTRL_HLI
  593. SemaphoreHandle_t downstream_semaphore = handle;
  594. assert(downstream_semaphore);
  595. hli_queue_handle_t s_semaphore = hli_semaphore_create(max, downstream_semaphore);
  596. assert(downstream_semaphore);
  597. return s_semaphore;
  598. #else
  599. return handle;
  600. #endif /* CONFIG_BTDM_CTRL_HLI */
  601. #if CONFIG_SPIRAM_USE_MALLOC
  602. error:
  603. if (handle) {
  604. vSemaphoreDelete(handle);
  605. }
  606. if (queue_buffer) {
  607. free(queue_buffer);
  608. }
  609. return NULL;
  610. #endif
  611. }
  612. static void semphr_delete_wrapper(void *semphr)
  613. {
  614. void *handle = NULL;
  615. #if CONFIG_BTDM_CTRL_HLI
  616. if (((hli_queue_handle_t)semphr)->downstream != NULL) {
  617. handle = ((hli_queue_handle_t)semphr)->downstream;
  618. }
  619. hli_queue_delete(semphr);
  620. #else
  621. handle = semphr;
  622. #endif /* CONFIG_BTDM_CTRL_HLI */
  623. #if !CONFIG_SPIRAM_USE_MALLOC
  624. vSemaphoreDelete(handle);
  625. #else
  626. btdm_queue_item_t item = {
  627. .handle = handle,
  628. .storage = NULL,
  629. .buffer = NULL,
  630. };
  631. if (btdm_queue_generic_deregister(&item)) {
  632. vSemaphoreDelete(item.handle);
  633. free(item.buffer);
  634. }
  635. #endif
  636. }
  637. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  638. {
  639. #if CONFIG_BTDM_CTRL_HLI
  640. return (int32_t)xSemaphoreTakeFromISR(((hli_queue_handle_t)semphr)->downstream, hptw);
  641. #else
  642. return (int32_t)xSemaphoreTakeFromISR(semphr, hptw);
  643. #endif /* CONFIG_BTDM_CTRL_HLI */
  644. }
  645. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  646. {
  647. #if CONFIG_BTDM_CTRL_HLI
  648. UNUSED(hptw);
  649. assert(xPortGetCoreID() == CONFIG_BTDM_CTRL_PINNED_TO_CORE);
  650. return hli_semaphore_give(semphr);
  651. #else
  652. return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
  653. #endif /* CONFIG_BTDM_CTRL_HLI */
  654. }
  655. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
  656. {
  657. bool ret;
  658. #if CONFIG_BTDM_CTRL_HLI
  659. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  660. ret = xSemaphoreTake(((hli_queue_handle_t)semphr)->downstream, portMAX_DELAY);
  661. } else {
  662. ret = xSemaphoreTake(((hli_queue_handle_t)semphr)->downstream, block_time_ms / portTICK_PERIOD_MS);
  663. }
  664. #else
  665. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  666. ret = xSemaphoreTake(semphr, portMAX_DELAY);
  667. } else {
  668. ret = xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
  669. }
  670. #endif /* CONFIG_BTDM_CTRL_HLI */
  671. return (int32_t)ret;
  672. }
  673. static int32_t semphr_give_wrapper(void *semphr)
  674. {
  675. #if CONFIG_BTDM_CTRL_HLI
  676. return (int32_t)xSemaphoreGive(((hli_queue_handle_t)semphr)->downstream);
  677. #else
  678. return (int32_t)xSemaphoreGive(semphr);
  679. #endif /* CONFIG_BTDM_CTRL_HLI */
  680. }
  681. static void *mutex_create_wrapper(void)
  682. {
  683. #if CONFIG_SPIRAM_USE_MALLOC
  684. StaticQueue_t *queue_buffer = NULL;
  685. QueueHandle_t handle = NULL;
  686. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  687. if (!queue_buffer) {
  688. goto error;
  689. }
  690. handle = xSemaphoreCreateMutexStatic(queue_buffer);
  691. if (!handle) {
  692. goto error;
  693. }
  694. btdm_queue_item_t item = {
  695. .handle = handle,
  696. .storage = NULL,
  697. .buffer = queue_buffer,
  698. };
  699. if (!btdm_queue_generic_register(&item)) {
  700. goto error;
  701. }
  702. return handle;
  703. error:
  704. if (handle) {
  705. vSemaphoreDelete(handle);
  706. }
  707. if (queue_buffer) {
  708. free(queue_buffer);
  709. }
  710. return NULL;
  711. #else
  712. return (void *)xSemaphoreCreateMutex();
  713. #endif
  714. }
  715. static void mutex_delete_wrapper(void *mutex)
  716. {
  717. #if !CONFIG_SPIRAM_USE_MALLOC
  718. vSemaphoreDelete(mutex);
  719. #else
  720. btdm_queue_item_t item = {
  721. .handle = mutex,
  722. .storage = NULL,
  723. .buffer = NULL,
  724. };
  725. if (btdm_queue_generic_deregister(&item)) {
  726. vSemaphoreDelete(item.handle);
  727. free(item.buffer);
  728. }
  729. return;
  730. #endif
  731. }
  732. static int32_t mutex_lock_wrapper(void *mutex)
  733. {
  734. return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
  735. }
  736. static int32_t mutex_unlock_wrapper(void *mutex)
  737. {
  738. return (int32_t)xSemaphoreGive(mutex);
  739. }
  740. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
  741. {
  742. #if CONFIG_SPIRAM_USE_MALLOC
  743. StaticQueue_t *queue_buffer = NULL;
  744. uint8_t *queue_storage = NULL;
  745. QueueHandle_t handle = NULL;
  746. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  747. if (!queue_buffer) {
  748. goto error;
  749. }
  750. queue_storage = heap_caps_malloc((queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  751. if (!queue_storage ) {
  752. goto error;
  753. }
  754. handle = xQueueCreateStatic(queue_len, item_size, queue_storage, queue_buffer);
  755. if (!handle) {
  756. goto error;
  757. }
  758. btdm_queue_item_t item = {
  759. .handle = handle,
  760. .storage = queue_storage,
  761. .buffer = queue_buffer,
  762. };
  763. if (!btdm_queue_generic_register(&item)) {
  764. goto error;
  765. }
  766. return handle;
  767. error:
  768. if (handle) {
  769. vQueueDelete(handle);
  770. }
  771. if (queue_storage) {
  772. free(queue_storage);
  773. }
  774. if (queue_buffer) {
  775. free(queue_buffer);
  776. }
  777. return NULL;
  778. #else
  779. return (void *)xQueueCreate(queue_len, item_size);
  780. #endif
  781. }
  782. static void queue_delete_wrapper(void *queue)
  783. {
  784. #if !CONFIG_SPIRAM_USE_MALLOC
  785. vQueueDelete(queue);
  786. #else
  787. btdm_queue_item_t item = {
  788. .handle = queue,
  789. .storage = NULL,
  790. .buffer = NULL,
  791. };
  792. if (btdm_queue_generic_deregister(&item)) {
  793. vQueueDelete(item.handle);
  794. free(item.storage);
  795. free(item.buffer);
  796. }
  797. return;
  798. #endif
  799. }
  800. #if CONFIG_BTDM_CTRL_HLI
  801. static void *queue_create_hlevel_wrapper(uint32_t queue_len, uint32_t item_size)
  802. {
  803. QueueHandle_t downstream_queue = queue_create_wrapper(queue_len, item_size);
  804. assert(downstream_queue);
  805. hli_queue_handle_t queue = hli_queue_create(queue_len, item_size, downstream_queue);
  806. assert(queue);
  807. return queue;
  808. }
  809. static void *customer_queue_create_hlevel_wrapper(uint32_t queue_len, uint32_t item_size)
  810. {
  811. QueueHandle_t downstream_queue = queue_create_wrapper(queue_len, item_size);
  812. assert(downstream_queue);
  813. hli_queue_handle_t queue = hli_customer_queue_create(queue_len, item_size, downstream_queue);
  814. assert(queue);
  815. return queue;
  816. }
  817. static void queue_delete_hlevel_wrapper(void *queue)
  818. {
  819. if (((hli_queue_handle_t)queue)->downstream != NULL) {
  820. queue_delete_wrapper(((hli_queue_handle_t)queue)->downstream);
  821. }
  822. hli_queue_delete(queue);
  823. }
  824. static int32_t queue_send_hlevel_wrapper(void *queue, void *item, uint32_t block_time_ms)
  825. {
  826. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  827. return (int32_t)xQueueSend(((hli_queue_handle_t)queue)->downstream, item, portMAX_DELAY);
  828. } else {
  829. return (int32_t)xQueueSend(((hli_queue_handle_t)queue)->downstream, item, block_time_ms / portTICK_PERIOD_MS);
  830. }
  831. }
  832. /**
  833. * Queue send from isr
  834. * @param queue The queue which will send to
  835. * @param item The message which will be send
  836. * @param hptw need do task yield or not
  837. * @return send success or not
  838. * There is an issue here: When the queue is full, it may reture true but it send fail to the queue, sometimes.
  839. * But in Bluetooth controller's isr, We don't care about the return value.
  840. * It only required tp send success when the queue is empty all the time.
  841. * So, this function meets the requirement.
  842. */
  843. static int32_t IRAM_ATTR queue_send_from_isr_hlevel_wrapper(void *queue, void *item, void *hptw)
  844. {
  845. UNUSED(hptw);
  846. assert(xPortGetCoreID() == CONFIG_BTDM_CTRL_PINNED_TO_CORE);
  847. return hli_queue_put(queue, item);
  848. }
  849. static int32_t queue_recv_hlevel_wrapper(void *queue, void *item, uint32_t block_time_ms)
  850. {
  851. bool ret;
  852. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  853. ret = xQueueReceive(((hli_queue_handle_t)queue)->downstream, item, portMAX_DELAY);
  854. } else {
  855. ret = xQueueReceive(((hli_queue_handle_t)queue)->downstream, item, block_time_ms / portTICK_PERIOD_MS);
  856. }
  857. return (int32_t)ret;
  858. }
  859. static int32_t IRAM_ATTR queue_recv_from_isr_hlevel_wrapper(void *queue, void *item, void *hptw)
  860. {
  861. return (int32_t)xQueueReceiveFromISR(((hli_queue_handle_t)queue)->downstream, item, hptw);
  862. }
  863. #else
  864. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms)
  865. {
  866. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  867. return (int32_t)xQueueSend(queue, item, portMAX_DELAY);
  868. } else {
  869. return (int32_t)xQueueSend(queue, item, block_time_ms / portTICK_PERIOD_MS);
  870. }
  871. }
  872. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
  873. {
  874. return (int32_t)xQueueSendFromISR(queue, item, hptw);
  875. }
  876. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms)
  877. {
  878. bool ret;
  879. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  880. ret = xQueueReceive(queue, item, portMAX_DELAY);
  881. } else {
  882. ret = xQueueReceive(queue, item, block_time_ms / portTICK_PERIOD_MS);
  883. }
  884. return (int32_t)ret;
  885. }
  886. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw)
  887. {
  888. return (int32_t)xQueueReceiveFromISR(queue, item, hptw);
  889. }
  890. #endif /* CONFIG_BTDM_CTRL_HLI */
  891. static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id)
  892. {
  893. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  894. }
  895. static void task_delete_wrapper(void *task_handle)
  896. {
  897. vTaskDelete(task_handle);
  898. }
  899. static bool IRAM_ATTR is_in_isr_wrapper(void)
  900. {
  901. return !xPortCanYield();
  902. }
  903. static void IRAM_ATTR cause_sw_intr(void *arg)
  904. {
  905. /* just convert void * to int, because the width is the same */
  906. uint32_t intr_no = (uint32_t)arg;
  907. XTHAL_SET_INTSET((1<<intr_no));
  908. }
  909. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no)
  910. {
  911. esp_err_t err = ESP_OK;
  912. #if CONFIG_FREERTOS_UNICORE
  913. cause_sw_intr((void *)intr_no);
  914. #else /* CONFIG_FREERTOS_UNICORE */
  915. if (xPortGetCoreID() == core_id) {
  916. cause_sw_intr((void *)intr_no);
  917. } else {
  918. err = esp_ipc_call(core_id, cause_sw_intr, (void *)intr_no);
  919. }
  920. #endif /* !CONFIG_FREERTOS_UNICORE */
  921. return err;
  922. }
  923. static void *malloc_internal_wrapper(size_t size)
  924. {
  925. return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  926. }
  927. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
  928. {
  929. return esp_read_mac(mac, ESP_MAC_BT);
  930. }
  931. static void IRAM_ATTR srand_wrapper(unsigned int seed)
  932. {
  933. /* empty function */
  934. }
  935. static int IRAM_ATTR rand_wrapper(void)
  936. {
  937. return (int)esp_random();
  938. }
  939. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles)
  940. {
  941. // The number of lp cycles should not lead to overflow. Thrs: 100s
  942. // clock measurement is conducted
  943. uint64_t us = (uint64_t)btdm_lpcycle_us * cycles;
  944. us = (us + (1 << (btdm_lpcycle_us_frac - 1))) >> btdm_lpcycle_us_frac;
  945. return (uint32_t)us;
  946. }
  947. /*
  948. * @brief Converts a duration in slots into a number of low power clock cycles.
  949. */
  950. static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us)
  951. {
  952. // The number of sleep duration(us) should not lead to overflow. Thrs: 100s
  953. // Compute the sleep duration in us to low power clock cycles, with calibration result applied
  954. // clock measurement is conducted
  955. uint64_t cycles = ((uint64_t)(us) << btdm_lpcycle_us_frac) / btdm_lpcycle_us;
  956. return (uint32_t)cycles;
  957. }
  958. static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt)
  959. {
  960. if (*slot_cnt < BTDM_MIN_SLEEP_DURATION) {
  961. return false;
  962. }
  963. /* wake up in advance considering the delay in enabling PHY/RF */
  964. *slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY;
  965. return true;
  966. }
  967. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
  968. {
  969. #ifdef CONFIG_PM_ENABLE
  970. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  971. uint32_t us_to_sleep = btdm_lpcycles_2_us(lpcycles);
  972. #define BTDM_MIN_TIMER_UNCERTAINTY_US (500)
  973. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  974. // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift
  975. // and set the timer in advance
  976. uint32_t uncertainty = (us_to_sleep >> 11);
  977. if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) {
  978. uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US;
  979. }
  980. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) != ESP_OK) {
  981. ESP_LOGW(BTDM_LOG_TAG, "timer start failed");
  982. }
  983. #endif
  984. }
  985. static void btdm_sleep_enter_phase2_wrapper(void)
  986. {
  987. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  988. esp_phy_disable();
  989. #ifdef CONFIG_PM_ENABLE
  990. if (s_pm_lock_acquired) {
  991. esp_pm_lock_release(s_pm_lock);
  992. s_pm_lock_acquired = false;
  993. }
  994. #endif
  995. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  996. esp_phy_disable();
  997. // pause bluetooth baseband
  998. periph_module_disable(PERIPH_BT_BASEBAND_MODULE);
  999. }
  1000. }
  1001. static void btdm_sleep_exit_phase3_wrapper(void)
  1002. {
  1003. #ifdef CONFIG_PM_ENABLE
  1004. if (!s_pm_lock_acquired) {
  1005. s_pm_lock_acquired = true;
  1006. esp_pm_lock_acquire(s_pm_lock);
  1007. }
  1008. #endif
  1009. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1010. esp_phy_enable();
  1011. btdm_check_and_init_bb();
  1012. #ifdef CONFIG_PM_ENABLE
  1013. esp_timer_stop(s_btdm_slp_tmr);
  1014. #endif
  1015. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1016. // resume bluetooth baseband
  1017. periph_module_enable(PERIPH_BT_BASEBAND_MODULE);
  1018. esp_phy_enable();
  1019. }
  1020. }
  1021. #ifdef CONFIG_PM_ENABLE
  1022. static void btdm_slp_tmr_customer_callback(void * arg)
  1023. {
  1024. (void)(arg);
  1025. if (!s_pm_lock_acquired) {
  1026. s_pm_lock_acquired = true;
  1027. esp_pm_lock_acquire(s_pm_lock);
  1028. }
  1029. }
  1030. static void IRAM_ATTR btdm_slp_tmr_callback(void *arg)
  1031. {
  1032. (void)(arg);
  1033. btdm_dispatch_work_to_controller(btdm_slp_tmr_customer_callback, NULL, true);
  1034. }
  1035. #endif
  1036. #define BTDM_ASYNC_WAKEUP_REQ_HCI 0
  1037. #define BTDM_ASYNC_WAKEUP_REQ_COEX 1
  1038. #define BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA 2
  1039. #define BTDM_ASYNC_WAKEUP_REQMAX 3
  1040. static void btdm_wakeup_request_callback(void * arg)
  1041. {
  1042. (void)(arg);
  1043. #if CONFIG_PM_ENABLE
  1044. if (!s_pm_lock_acquired) {
  1045. s_pm_lock_acquired = true;
  1046. esp_pm_lock_acquire(s_pm_lock);
  1047. }
  1048. esp_timer_stop(s_btdm_slp_tmr);
  1049. #endif
  1050. btdm_wakeup_request();
  1051. semphr_give_wrapper(s_wakeup_req_sem);
  1052. }
  1053. static bool async_wakeup_request(int event)
  1054. {
  1055. bool do_wakeup_request = false;
  1056. switch (event) {
  1057. case BTDM_ASYNC_WAKEUP_REQ_HCI:
  1058. btdm_in_wakeup_requesting_set(true);
  1059. // NO break
  1060. case BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA:
  1061. if (!btdm_power_state_active()) {
  1062. do_wakeup_request = true;
  1063. btdm_dispatch_work_to_controller(btdm_wakeup_request_callback, NULL, true);
  1064. semphr_take_wrapper(s_wakeup_req_sem, OSI_FUNCS_TIME_BLOCKING);
  1065. }
  1066. break;
  1067. case BTDM_ASYNC_WAKEUP_REQ_COEX:
  1068. if (!btdm_power_state_active()) {
  1069. do_wakeup_request = true;
  1070. #if CONFIG_PM_ENABLE
  1071. if (!s_pm_lock_acquired) {
  1072. s_pm_lock_acquired = true;
  1073. esp_pm_lock_acquire(s_pm_lock);
  1074. }
  1075. esp_timer_stop(s_btdm_slp_tmr);
  1076. #endif
  1077. btdm_wakeup_request();
  1078. }
  1079. break;
  1080. default:
  1081. return false;
  1082. }
  1083. return do_wakeup_request;
  1084. }
  1085. static void async_wakeup_request_end(int event)
  1086. {
  1087. bool request_lock = false;
  1088. switch (event) {
  1089. case BTDM_ASYNC_WAKEUP_REQ_HCI:
  1090. request_lock = true;
  1091. break;
  1092. case BTDM_ASYNC_WAKEUP_REQ_COEX:
  1093. case BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA:
  1094. request_lock = false;
  1095. break;
  1096. default:
  1097. return;
  1098. }
  1099. if (request_lock) {
  1100. btdm_in_wakeup_requesting_set(false);
  1101. }
  1102. return;
  1103. }
  1104. static bool coex_bt_wakeup_request(void)
  1105. {
  1106. return async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_COEX);
  1107. }
  1108. static void coex_bt_wakeup_request_end(void)
  1109. {
  1110. async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_COEX);
  1111. return;
  1112. }
  1113. static int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration)
  1114. {
  1115. #if CONFIG_SW_COEXIST_ENABLE
  1116. return coex_bt_request(event, latency, duration);
  1117. #else
  1118. return 0;
  1119. #endif
  1120. }
  1121. static int IRAM_ATTR coex_bt_release_wrapper(uint32_t event)
  1122. {
  1123. #if CONFIG_SW_COEXIST_ENABLE
  1124. return coex_bt_release(event);
  1125. #else
  1126. return 0;
  1127. #endif
  1128. }
  1129. static int coex_register_bt_cb_wrapper(coex_func_cb_t cb)
  1130. {
  1131. #if CONFIG_SW_COEXIST_ENABLE
  1132. return coex_register_bt_cb(cb);
  1133. #else
  1134. return 0;
  1135. #endif
  1136. }
  1137. static uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void)
  1138. {
  1139. #if CONFIG_SW_COEXIST_ENABLE
  1140. return coex_bb_reset_lock();
  1141. #else
  1142. return 0;
  1143. #endif
  1144. }
  1145. static void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore)
  1146. {
  1147. #if CONFIG_SW_COEXIST_ENABLE
  1148. coex_bb_reset_unlock(restore);
  1149. #endif
  1150. }
  1151. static int coex_schm_register_btdm_callback_wrapper(void *callback)
  1152. {
  1153. #if CONFIG_SW_COEXIST_ENABLE
  1154. return coex_schm_register_btdm_callback(callback);
  1155. #else
  1156. return 0;
  1157. #endif
  1158. }
  1159. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
  1160. {
  1161. #if CONFIG_SW_COEXIST_ENABLE
  1162. coex_schm_status_bit_clear(type, status);
  1163. #endif
  1164. }
  1165. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
  1166. {
  1167. #if CONFIG_SW_COEXIST_ENABLE
  1168. coex_schm_status_bit_set(type, status);
  1169. #endif
  1170. }
  1171. static uint32_t coex_schm_interval_get_wrapper(void)
  1172. {
  1173. #if CONFIG_SW_COEXIST_ENABLE
  1174. return coex_schm_interval_get();
  1175. #else
  1176. return 0;
  1177. #endif
  1178. }
  1179. static uint8_t coex_schm_curr_period_get_wrapper(void)
  1180. {
  1181. #if CONFIG_SW_COEXIST_ENABLE
  1182. return coex_schm_curr_period_get();
  1183. #else
  1184. return 1;
  1185. #endif
  1186. }
  1187. static void * coex_schm_curr_phase_get_wrapper(void)
  1188. {
  1189. #if CONFIG_SW_COEXIST_ENABLE
  1190. return coex_schm_curr_phase_get();
  1191. #else
  1192. return NULL;
  1193. #endif
  1194. }
  1195. static int coex_wifi_channel_get_wrapper(uint8_t *primary, uint8_t *secondary)
  1196. {
  1197. #if CONFIG_SW_COEXIST_ENABLE
  1198. return coex_wifi_channel_get(primary, secondary);
  1199. #else
  1200. return -1;
  1201. #endif
  1202. }
  1203. static int coex_register_wifi_channel_change_callback_wrapper(void *cb)
  1204. {
  1205. #if CONFIG_SW_COEXIST_ENABLE
  1206. return coex_register_wifi_channel_change_callback(cb);
  1207. #else
  1208. return -1;
  1209. #endif
  1210. }
  1211. bool esp_vhci_host_check_send_available(void)
  1212. {
  1213. return API_vhci_host_check_send_available();
  1214. }
  1215. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  1216. {
  1217. async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_HCI);
  1218. API_vhci_host_send_packet(data, len);
  1219. async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_HCI);
  1220. }
  1221. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  1222. {
  1223. return API_vhci_host_register_callback((const vhci_host_callback_t *)callback) == 0 ? ESP_OK : ESP_FAIL;
  1224. }
  1225. static uint32_t btdm_config_mask_load(void)
  1226. {
  1227. uint32_t mask = 0x0;
  1228. #if CONFIG_BTDM_CTRL_HCI_MODE_UART_H4
  1229. mask |= BTDM_CFG_HCI_UART;
  1230. #endif
  1231. #if CONFIG_BTDM_CTRL_PINNED_TO_CORE == 1
  1232. mask |= BTDM_CFG_CONTROLLER_RUN_APP_CPU;
  1233. #endif
  1234. #if CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED
  1235. mask |= BTDM_CFG_BLE_FULL_SCAN_SUPPORTED;
  1236. #endif /* CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED */
  1237. mask |= BTDM_CFG_SCAN_DUPLICATE_OPTIONS;
  1238. mask |= BTDM_CFG_SEND_ADV_RESERVED_SIZE;
  1239. return mask;
  1240. }
  1241. static void btdm_controller_mem_init(void)
  1242. {
  1243. /* initialise .data section */
  1244. memcpy(&_data_start_btdm, (void *)_data_start_btdm_rom, &_data_end_btdm - &_data_start_btdm);
  1245. ESP_LOGD(BTDM_LOG_TAG, ".data initialise [0x%08x] <== [0x%08x]", (uint32_t)&_data_start_btdm, _data_start_btdm_rom);
  1246. //initial em, .bss section
  1247. for (int i = 1; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  1248. if (btdm_dram_available_region[i].mode != ESP_BT_MODE_IDLE) {
  1249. memset((void *)btdm_dram_available_region[i].start, 0x0, btdm_dram_available_region[i].end - btdm_dram_available_region[i].start);
  1250. ESP_LOGD(BTDM_LOG_TAG, ".bss initialise [0x%08x] - [0x%08x]", btdm_dram_available_region[i].start, btdm_dram_available_region[i].end);
  1251. }
  1252. }
  1253. }
  1254. static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end)
  1255. {
  1256. int ret = heap_caps_add_region(start, end);
  1257. /* heap_caps_add_region() returns ESP_ERR_INVALID_SIZE if the memory region is
  1258. * is too small to fit a heap. This cannot be termed as a fatal error and hence
  1259. * we replace it by ESP_OK
  1260. */
  1261. if (ret == ESP_ERR_INVALID_SIZE) {
  1262. return ESP_OK;
  1263. }
  1264. return ret;
  1265. }
  1266. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  1267. {
  1268. bool update = true;
  1269. intptr_t mem_start=(intptr_t) NULL, mem_end=(intptr_t) NULL;
  1270. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  1271. return ESP_ERR_INVALID_STATE;
  1272. }
  1273. //already released
  1274. if (!(mode & btdm_dram_available_region[0].mode)) {
  1275. return ESP_ERR_INVALID_STATE;
  1276. }
  1277. for (int i = 0; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  1278. //skip the share mode, idle mode and other mode
  1279. if (btdm_dram_available_region[i].mode == ESP_BT_MODE_IDLE
  1280. || (mode & btdm_dram_available_region[i].mode) != btdm_dram_available_region[i].mode) {
  1281. //clear the bit of the mode which will be released
  1282. btdm_dram_available_region[i].mode &= ~mode;
  1283. continue;
  1284. } else {
  1285. //clear the bit of the mode which will be released
  1286. btdm_dram_available_region[i].mode &= ~mode;
  1287. }
  1288. if (update) {
  1289. mem_start = btdm_dram_available_region[i].start;
  1290. mem_end = btdm_dram_available_region[i].end;
  1291. update = false;
  1292. }
  1293. if (i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t) - 1) {
  1294. mem_end = btdm_dram_available_region[i].end;
  1295. if (btdm_dram_available_region[i+1].mode != ESP_BT_MODE_IDLE
  1296. && (mode & btdm_dram_available_region[i+1].mode) == btdm_dram_available_region[i+1].mode
  1297. && mem_end == btdm_dram_available_region[i+1].start) {
  1298. continue;
  1299. } else {
  1300. ESP_LOGD(BTDM_LOG_TAG, "Release DRAM [0x%08x] - [0x%08x]", mem_start, mem_end);
  1301. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1302. update = true;
  1303. }
  1304. } else {
  1305. mem_end = btdm_dram_available_region[i].end;
  1306. ESP_LOGD(BTDM_LOG_TAG, "Release DRAM [0x%08x] - [0x%08x]", mem_start, mem_end);
  1307. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1308. update = true;
  1309. }
  1310. }
  1311. if (mode == ESP_BT_MODE_BTDM) {
  1312. mem_start = (intptr_t)&_btdm_bss_start;
  1313. mem_end = (intptr_t)&_btdm_bss_end;
  1314. if (mem_start != mem_end) {
  1315. ESP_LOGD(BTDM_LOG_TAG, "Release BTDM BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  1316. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1317. }
  1318. mem_start = (intptr_t)&_btdm_data_start;
  1319. mem_end = (intptr_t)&_btdm_data_end;
  1320. if (mem_start != mem_end) {
  1321. ESP_LOGD(BTDM_LOG_TAG, "Release BTDM Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  1322. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1323. }
  1324. }
  1325. return ESP_OK;
  1326. }
  1327. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  1328. {
  1329. int ret;
  1330. intptr_t mem_start, mem_end;
  1331. ret = esp_bt_controller_mem_release(mode);
  1332. if (ret != ESP_OK) {
  1333. return ret;
  1334. }
  1335. if (mode == ESP_BT_MODE_BTDM) {
  1336. mem_start = (intptr_t)&_bt_bss_start;
  1337. mem_end = (intptr_t)&_bt_bss_end;
  1338. if (mem_start != mem_end) {
  1339. ESP_LOGD(BTDM_LOG_TAG, "Release BT BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  1340. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1341. }
  1342. mem_start = (intptr_t)&_bt_data_start;
  1343. mem_end = (intptr_t)&_bt_data_end;
  1344. if (mem_start != mem_end) {
  1345. ESP_LOGD(BTDM_LOG_TAG, "Release BT Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  1346. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1347. }
  1348. mem_start = (intptr_t)&_nimble_bss_start;
  1349. mem_end = (intptr_t)&_nimble_bss_end;
  1350. if (mem_start != mem_end) {
  1351. ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  1352. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1353. }
  1354. mem_start = (intptr_t)&_nimble_data_start;
  1355. mem_end = (intptr_t)&_nimble_data_end;
  1356. if (mem_start != mem_end) {
  1357. ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  1358. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1359. }
  1360. }
  1361. return ESP_OK;
  1362. }
  1363. #if CONFIG_BTDM_CTRL_HLI
  1364. static void hli_queue_setup_cb(void* arg)
  1365. {
  1366. hli_queue_setup();
  1367. }
  1368. static void hli_queue_setup_pinned_to_core(int core_id)
  1369. {
  1370. #if CONFIG_FREERTOS_UNICORE
  1371. hli_queue_setup_cb(NULL);
  1372. #else /* CONFIG_FREERTOS_UNICORE */
  1373. if (xPortGetCoreID() == core_id) {
  1374. hli_queue_setup_cb(NULL);
  1375. } else {
  1376. esp_ipc_call(core_id, hli_queue_setup_cb, NULL);
  1377. }
  1378. #endif /* !CONFIG_FREERTOS_UNICORE */
  1379. }
  1380. #endif /* CONFIG_BTDM_CTRL_HLI */
  1381. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
  1382. {
  1383. esp_err_t err;
  1384. uint32_t btdm_cfg_mask = 0;
  1385. #if CONFIG_BTDM_CTRL_HLI
  1386. hli_queue_setup_pinned_to_core(CONFIG_BTDM_CTRL_PINNED_TO_CORE);
  1387. #endif /* CONFIG_BTDM_CTRL_HLI */
  1388. //if all the bt available memory was already released, cannot initialize bluetooth controller
  1389. if (btdm_dram_available_region[0].mode == ESP_BT_MODE_IDLE) {
  1390. return ESP_ERR_INVALID_STATE;
  1391. }
  1392. osi_funcs_p = (struct osi_funcs_t *)malloc_internal_wrapper(sizeof(struct osi_funcs_t));
  1393. if (osi_funcs_p == NULL) {
  1394. return ESP_ERR_NO_MEM;
  1395. }
  1396. memcpy(osi_funcs_p, &osi_funcs_ro, sizeof(struct osi_funcs_t));
  1397. if (btdm_osi_funcs_register(osi_funcs_p) != 0) {
  1398. return ESP_ERR_INVALID_ARG;
  1399. }
  1400. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  1401. return ESP_ERR_INVALID_STATE;
  1402. }
  1403. if (cfg == NULL) {
  1404. return ESP_ERR_INVALID_ARG;
  1405. }
  1406. if (cfg->controller_task_prio != ESP_TASK_BT_CONTROLLER_PRIO
  1407. || cfg->controller_task_stack_size < ESP_TASK_BT_CONTROLLER_STACK) {
  1408. return ESP_ERR_INVALID_ARG;
  1409. }
  1410. //overwrite some parameters
  1411. cfg->bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF;
  1412. cfg->magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL;
  1413. if (((cfg->mode & ESP_BT_MODE_BLE) && (cfg->ble_max_conn <= 0 || cfg->ble_max_conn > BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT))
  1414. || ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_acl_conn <= 0 || cfg->bt_max_acl_conn > BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT))
  1415. || ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_sync_conn > BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT))) {
  1416. return ESP_ERR_INVALID_ARG;
  1417. }
  1418. ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version());
  1419. #if CONFIG_SPIRAM_USE_MALLOC
  1420. btdm_queue_table_mux = xSemaphoreCreateMutex();
  1421. if (btdm_queue_table_mux == NULL) {
  1422. return ESP_ERR_NO_MEM;
  1423. }
  1424. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  1425. #endif
  1426. s_wakeup_req_sem = semphr_create_wrapper(1, 0);
  1427. if (s_wakeup_req_sem == NULL) {
  1428. err = ESP_ERR_NO_MEM;
  1429. goto error;
  1430. }
  1431. esp_bt_power_domain_on();
  1432. btdm_controller_mem_init();
  1433. periph_module_enable(PERIPH_BT_MODULE);
  1434. #ifdef CONFIG_PM_ENABLE
  1435. s_btdm_allow_light_sleep = false;
  1436. #endif
  1437. // set default sleep clock cycle and its fractional bits
  1438. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1439. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  1440. #if CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_ORIG
  1441. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  1442. #if CONFIG_BTDM_CTRL_LPCLK_SEL_EXT_32K_XTAL
  1443. // check whether or not EXT_CRYS is working
  1444. if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_32K_XTAL) {
  1445. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL32K; // External 32kHz XTAL
  1446. #ifdef CONFIG_PM_ENABLE
  1447. s_btdm_allow_light_sleep = true;
  1448. #endif
  1449. } else {
  1450. ESP_LOGW(BTDM_LOG_TAG, "32.768kHz XTAL not detected, fall back to main XTAL as Bluetooth sleep clock\n"
  1451. "light sleep mode will not be able to apply when bluetooth is enabled");
  1452. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  1453. }
  1454. #else
  1455. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  1456. #endif
  1457. bool select_src_ret __attribute__((unused));
  1458. bool set_div_ret __attribute__((unused));
  1459. if (btdm_lpclk_sel == BTDM_LPCLK_SEL_XTAL) {
  1460. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL);
  1461. set_div_ret = btdm_lpclk_set_div(rtc_clk_xtal_freq_get() * 2 - 1);
  1462. assert(select_src_ret && set_div_ret);
  1463. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1464. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  1465. } else { // btdm_lpclk_sel == BTDM_LPCLK_SEL_XTAL32K
  1466. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K);
  1467. set_div_ret = btdm_lpclk_set_div(0);
  1468. assert(select_src_ret && set_div_ret);
  1469. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1470. btdm_lpcycle_us = (RTC_CLK_CAL_FRACT > 15) ? (1000000 << (RTC_CLK_CAL_FRACT - 15)) :
  1471. (1000000 >> (15 - RTC_CLK_CAL_FRACT));
  1472. assert(btdm_lpcycle_us != 0);
  1473. }
  1474. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_ORIG);
  1475. #elif CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_EVED
  1476. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_EVED);
  1477. #else
  1478. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE);
  1479. #endif
  1480. #ifdef CONFIG_PM_ENABLE
  1481. if (!s_btdm_allow_light_sleep) {
  1482. if ((err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btLS", &s_light_sleep_pm_lock)) != ESP_OK) {
  1483. goto error;
  1484. }
  1485. }
  1486. if ((err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock)) != ESP_OK) {
  1487. goto error;
  1488. }
  1489. esp_timer_create_args_t create_args = {
  1490. .callback = btdm_slp_tmr_callback,
  1491. .arg = NULL,
  1492. .name = "btSlp"
  1493. };
  1494. if ((err = esp_timer_create(&create_args, &s_btdm_slp_tmr)) != ESP_OK) {
  1495. goto error;
  1496. }
  1497. s_pm_lock_acquired = true;
  1498. #endif
  1499. #if CONFIG_SW_COEXIST_ENABLE
  1500. coex_init();
  1501. #endif
  1502. btdm_cfg_mask = btdm_config_mask_load();
  1503. if (btdm_controller_init(btdm_cfg_mask, cfg) != 0) {
  1504. err = ESP_ERR_NO_MEM;
  1505. goto error;
  1506. }
  1507. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1508. return ESP_OK;
  1509. error:
  1510. #ifdef CONFIG_PM_ENABLE
  1511. if (!s_btdm_allow_light_sleep) {
  1512. if (s_light_sleep_pm_lock != NULL) {
  1513. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1514. s_light_sleep_pm_lock = NULL;
  1515. }
  1516. }
  1517. if (s_pm_lock != NULL) {
  1518. esp_pm_lock_delete(s_pm_lock);
  1519. s_pm_lock = NULL;
  1520. }
  1521. if (s_btdm_slp_tmr != NULL) {
  1522. esp_timer_delete(s_btdm_slp_tmr);
  1523. s_btdm_slp_tmr = NULL;
  1524. }
  1525. #endif
  1526. if (s_wakeup_req_sem) {
  1527. semphr_delete_wrapper(s_wakeup_req_sem);
  1528. s_wakeup_req_sem = NULL;
  1529. }
  1530. return err;
  1531. }
  1532. esp_err_t esp_bt_controller_deinit(void)
  1533. {
  1534. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1535. return ESP_ERR_INVALID_STATE;
  1536. }
  1537. btdm_controller_deinit();
  1538. periph_module_disable(PERIPH_BT_MODULE);
  1539. #ifdef CONFIG_PM_ENABLE
  1540. if (!s_btdm_allow_light_sleep) {
  1541. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1542. s_light_sleep_pm_lock = NULL;
  1543. }
  1544. if (s_pm_lock != NULL) {
  1545. esp_pm_lock_delete(s_pm_lock);
  1546. s_pm_lock = NULL;
  1547. }
  1548. if (s_btdm_slp_tmr != NULL) {
  1549. esp_timer_stop(s_btdm_slp_tmr);
  1550. esp_timer_delete(s_btdm_slp_tmr);
  1551. s_btdm_slp_tmr = NULL;
  1552. }
  1553. s_pm_lock_acquired = false;
  1554. #endif
  1555. semphr_delete_wrapper(s_wakeup_req_sem);
  1556. s_wakeup_req_sem = NULL;
  1557. #if CONFIG_SPIRAM_USE_MALLOC
  1558. vSemaphoreDelete(btdm_queue_table_mux);
  1559. btdm_queue_table_mux = NULL;
  1560. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  1561. #endif
  1562. free(osi_funcs_p);
  1563. osi_funcs_p = NULL;
  1564. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  1565. btdm_lpcycle_us = 0;
  1566. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE);
  1567. esp_bt_power_domain_off();
  1568. return ESP_OK;
  1569. }
  1570. static void bt_controller_shutdown(void* arg)
  1571. {
  1572. esp_bt_controller_shutdown();
  1573. }
  1574. static void bt_shutdown(void)
  1575. {
  1576. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1577. return;
  1578. }
  1579. #if !CONFIG_FREERTOS_UNICORE
  1580. esp_ipc_call_blocking(CONFIG_BTDM_CTRL_PINNED_TO_CORE, bt_controller_shutdown, NULL);
  1581. #else
  1582. bt_controller_shutdown(NULL);
  1583. #endif
  1584. esp_phy_disable();
  1585. return;
  1586. }
  1587. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  1588. {
  1589. int ret;
  1590. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1591. return ESP_ERR_INVALID_STATE;
  1592. }
  1593. //As the history reason, mode should be equal to the mode which set in esp_bt_controller_init()
  1594. if (mode != btdm_controller_get_mode()) {
  1595. return ESP_ERR_INVALID_ARG;
  1596. }
  1597. #ifdef CONFIG_PM_ENABLE
  1598. if (!s_btdm_allow_light_sleep) {
  1599. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  1600. }
  1601. esp_pm_lock_acquire(s_pm_lock);
  1602. #endif
  1603. esp_phy_enable();
  1604. #if CONFIG_SW_COEXIST_ENABLE
  1605. coex_enable();
  1606. #endif
  1607. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1608. btdm_controller_enable_sleep(true);
  1609. }
  1610. // inititalize bluetooth baseband
  1611. btdm_check_and_init_bb();
  1612. ret = btdm_controller_enable(mode);
  1613. if (ret != 0) {
  1614. #if CONFIG_SW_COEXIST_ENABLE
  1615. coex_disable();
  1616. #endif
  1617. esp_phy_disable();
  1618. #ifdef CONFIG_PM_ENABLE
  1619. if (!s_btdm_allow_light_sleep) {
  1620. esp_pm_lock_release(s_light_sleep_pm_lock);
  1621. }
  1622. esp_pm_lock_release(s_pm_lock);
  1623. #endif
  1624. return ESP_ERR_INVALID_STATE;
  1625. }
  1626. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  1627. ret = esp_register_shutdown_handler(bt_shutdown);
  1628. if (ret != ESP_OK) {
  1629. ESP_LOGW(BTDM_LOG_TAG, "Register shutdown handler failed, ret = 0x%x", ret);
  1630. }
  1631. return ESP_OK;
  1632. }
  1633. esp_err_t esp_bt_controller_disable(void)
  1634. {
  1635. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1636. return ESP_ERR_INVALID_STATE;
  1637. }
  1638. // disable modem sleep and wake up from sleep mode
  1639. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1640. btdm_controller_enable_sleep(false);
  1641. async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA);
  1642. while (!btdm_power_state_active()) {
  1643. esp_rom_delay_us(1000);
  1644. }
  1645. }
  1646. btdm_controller_disable();
  1647. #if CONFIG_SW_COEXIST_ENABLE
  1648. coex_disable();
  1649. #endif
  1650. esp_phy_disable();
  1651. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1652. esp_unregister_shutdown_handler(bt_shutdown);
  1653. #ifdef CONFIG_PM_ENABLE
  1654. if (!s_btdm_allow_light_sleep) {
  1655. esp_pm_lock_release(s_light_sleep_pm_lock);
  1656. }
  1657. esp_pm_lock_release(s_pm_lock);
  1658. #endif
  1659. return ESP_OK;
  1660. }
  1661. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  1662. {
  1663. return btdm_controller_status;
  1664. }
  1665. /* extra functions */
  1666. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  1667. {
  1668. if (ble_txpwr_set(power_type, power_level) != 0) {
  1669. return ESP_ERR_INVALID_ARG;
  1670. }
  1671. return ESP_OK;
  1672. }
  1673. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  1674. {
  1675. return (esp_power_level_t)ble_txpwr_get(power_type);
  1676. }
  1677. esp_err_t esp_bredr_tx_power_set(esp_power_level_t min_power_level, esp_power_level_t max_power_level)
  1678. {
  1679. esp_err_t err;
  1680. int ret;
  1681. ret = bredr_txpwr_set(min_power_level, max_power_level);
  1682. if (ret == 0) {
  1683. err = ESP_OK;
  1684. } else if (ret == -1) {
  1685. err = ESP_ERR_INVALID_ARG;
  1686. } else {
  1687. err = ESP_ERR_INVALID_STATE;
  1688. }
  1689. return err;
  1690. }
  1691. esp_err_t esp_bredr_tx_power_get(esp_power_level_t *min_power_level, esp_power_level_t *max_power_level)
  1692. {
  1693. if (bredr_txpwr_get((int *)min_power_level, (int *)max_power_level) != 0) {
  1694. return ESP_ERR_INVALID_ARG;
  1695. }
  1696. return ESP_OK;
  1697. }
  1698. esp_err_t esp_bt_sleep_enable (void)
  1699. {
  1700. esp_err_t status;
  1701. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1702. return ESP_ERR_INVALID_STATE;
  1703. }
  1704. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG ||
  1705. btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1706. btdm_controller_enable_sleep (true);
  1707. status = ESP_OK;
  1708. } else {
  1709. status = ESP_ERR_NOT_SUPPORTED;
  1710. }
  1711. return status;
  1712. }
  1713. esp_err_t esp_bt_sleep_disable (void)
  1714. {
  1715. esp_err_t status;
  1716. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1717. return ESP_ERR_INVALID_STATE;
  1718. }
  1719. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG ||
  1720. btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1721. btdm_controller_enable_sleep (false);
  1722. status = ESP_OK;
  1723. } else {
  1724. status = ESP_ERR_NOT_SUPPORTED;
  1725. }
  1726. return status;
  1727. }
  1728. esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path)
  1729. {
  1730. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1731. return ESP_ERR_INVALID_STATE;
  1732. }
  1733. bredr_sco_datapath_set(data_path);
  1734. return ESP_OK;
  1735. }
  1736. esp_err_t esp_ble_scan_dupilcate_list_flush(void)
  1737. {
  1738. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1739. return ESP_ERR_INVALID_STATE;
  1740. }
  1741. btdm_controller_scan_duplicate_list_clear();
  1742. return ESP_OK;
  1743. }
  1744. /**
  1745. * This function re-write controller's function,
  1746. * As coredump can not show paramerters in function which is in a .a file.
  1747. *
  1748. * After coredump fixing this issue, just delete this function.
  1749. */
  1750. void IRAM_ATTR r_assert(const char *condition, int param0, int param1, const char *file, int line)
  1751. {
  1752. __asm__ __volatile__("ill\n");
  1753. }
  1754. #endif /* CONFIG_BT_ENABLED */