bt.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE 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 <stddef.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "sdkconfig.h"
  18. #include "esp_heap_caps.h"
  19. #include "esp_heap_caps_init.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "freertos/queue.h"
  23. #include "freertos/semphr.h"
  24. #include "freertos/portmacro.h"
  25. #include "esp_types.h"
  26. #include "esp_system.h"
  27. #include "esp_task.h"
  28. #include "riscv/interrupt.h"
  29. #include "esp_attr.h"
  30. #include "esp_phy_init.h"
  31. #include "esp_bt.h"
  32. #include "esp_err.h"
  33. #include "esp_log.h"
  34. #include "esp_pm.h"
  35. #include "esp_ipc.h"
  36. #include "driver/periph_ctrl.h"
  37. #include "soc/rtc.h"
  38. #include "soc/rtc_cntl_reg.h"
  39. #include "soc/soc_memory_layout.h"
  40. #include "esp_clk.h"
  41. #include "esp_coexist_internal.h"
  42. #include "esp32c3/rom/rom_layout.h"
  43. #include "esp_timer.h"
  44. #if CONFIG_BT_ENABLED
  45. /* Macro definition
  46. ************************************************************************
  47. */
  48. #define BTDM_LOG_TAG "BTDM_INIT"
  49. #define BTDM_INIT_PERIOD (5000) /* ms */
  50. /* Low Power Clock Selection */
  51. #define BTDM_LPCLK_SEL_XTAL (0)
  52. #define BTDM_LPCLK_SEL_XTAL32K (1)
  53. #define BTDM_LPCLK_SEL_RTC_SLOW (2)
  54. #define BTDM_LPCLK_SEL_8M (3)
  55. /* Sleep and wakeup interval control */
  56. #define BTDM_MIN_SLEEP_DURATION (24) // threshold of interval in half slots to allow to fall into modem sleep
  57. #define BTDM_MODEM_WAKE_UP_DELAY (8) // delay in half slots of modem wake up procedure, including re-enable PHY/RF
  58. #define BTDM_MODEM_SLEEP_IN_EFFECT (1)
  59. #define BT_DEBUG(...)
  60. #define BT_API_CALL_CHECK(info, api_call, ret) \
  61. do{\
  62. esp_err_t __err = (api_call);\
  63. if ((ret) != __err) {\
  64. BT_DEBUG("%s %d %s ret=0x%X\n", __FUNCTION__, __LINE__, (info), __err);\
  65. return __err;\
  66. }\
  67. } while(0)
  68. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  69. #define OSI_VERSION 0x00010006
  70. #define OSI_MAGIC_VALUE 0xFADEBEAD
  71. /* SPIRAM Configuration */
  72. #if CONFIG_SPIRAM_USE_MALLOC
  73. #define BTDM_MAX_QUEUE_NUM (5)
  74. #endif
  75. /* Types definition
  76. ************************************************************************
  77. */
  78. /* VHCI function interface */
  79. typedef struct vhci_host_callback {
  80. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  81. 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*/
  82. } vhci_host_callback_t;
  83. /* Dram region */
  84. typedef struct {
  85. esp_bt_mode_t mode;
  86. intptr_t start;
  87. intptr_t end;
  88. } btdm_dram_available_region_t;
  89. /* PSRAM configuration */
  90. #if CONFIG_SPIRAM_USE_MALLOC
  91. typedef struct {
  92. QueueHandle_t handle;
  93. void *storage;
  94. void *buffer;
  95. } btdm_queue_item_t;
  96. #endif
  97. typedef void (* osi_intr_handler)(void);
  98. static uint8_t own_bda[6];
  99. /* OSI function */
  100. struct osi_funcs_t {
  101. uint32_t _magic;
  102. uint32_t _version;
  103. void (*_interrupt_set)(int cpu_no, int intr_source, int interrupt_no, int interrpt_prio);
  104. void (*_interrupt_clear)(int interrupt_source, int interrupt_no);
  105. void (*_interrupt_handler_set)(int interrupt_no, intr_handler_t fn, void *arg);
  106. void (*_interrupt_disable)(void);
  107. void (*_interrupt_restore)(void);
  108. void (*_task_yield)(void);
  109. void (*_task_yield_from_isr)(void);
  110. void *(*_semphr_create)(uint32_t max, uint32_t init);
  111. void (*_semphr_delete)(void *semphr);
  112. int (*_semphr_take_from_isr)(void *semphr, void *hptw);
  113. int (*_semphr_give_from_isr)(void *semphr, void *hptw);
  114. int (*_semphr_take)(void *semphr, uint32_t block_time_ms);
  115. int (*_semphr_give)(void *semphr);
  116. void *(*_mutex_create)(void);
  117. void (*_mutex_delete)(void *mutex);
  118. int (*_mutex_lock)(void *mutex);
  119. int (*_mutex_unlock)(void *mutex);
  120. void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
  121. void (* _queue_delete)(void *queue);
  122. int (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
  123. int (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
  124. int (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
  125. int (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
  126. int (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  127. void (* _task_delete)(void *task_handle);
  128. bool (* _is_in_isr)(void);
  129. int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
  130. void *(* _malloc)(size_t size);
  131. void *(* _malloc_internal)(size_t size);
  132. void (* _free)(void *p);
  133. int (* _read_efuse_mac)(uint8_t mac[6]);
  134. void (* _srand)(unsigned int seed);
  135. int (* _rand)(void);
  136. uint32_t (* _btdm_lpcycles_2_hus)(uint32_t cycles, uint32_t *error_corr);
  137. uint32_t (* _btdm_hus_2_lpcycles)(uint32_t hus);
  138. bool (* _btdm_sleep_check_duration)(int32_t *slot_cnt);
  139. void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */
  140. void (* _btdm_sleep_enter_phase2)(void);
  141. void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */
  142. void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */
  143. void (* _btdm_sleep_exit_phase3)(void); /* called from task */
  144. void (* _coex_wifi_sleep_set)(bool sleep);
  145. int (* _coex_core_ble_conn_dyn_prio_get)(bool *low, bool *high);
  146. void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status);
  147. void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status);
  148. void (* _interrupt_on)(int intr_num);
  149. void (* _interrupt_off)(int intr_num);
  150. void (* _esp_hw_power_down)(void);
  151. void (* _esp_hw_power_up)(void);
  152. void (* _ets_backup_dma_copy)(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_rem);
  153. };
  154. /* External functions or values
  155. ************************************************************************
  156. */
  157. /* not for user call, so don't put to include file */
  158. /* OSI */
  159. extern int btdm_osi_funcs_register(void *osi_funcs);
  160. /* Initialise and De-initialise */
  161. extern int btdm_controller_init(esp_bt_controller_config_t *config_opts);
  162. extern void btdm_controller_deinit(void);
  163. extern int btdm_controller_enable(esp_bt_mode_t mode);
  164. extern void btdm_controller_disable(void);
  165. extern uint8_t btdm_controller_get_mode(void);
  166. extern const char *btdm_controller_get_compile_version(void);
  167. extern void btdm_rf_bb_init_phase2(void); // shall be called after PHY/RF is enabled
  168. /* Sleep */
  169. extern void btdm_controller_enable_sleep(bool enable);
  170. extern uint8_t btdm_controller_get_sleep_mode(void);
  171. extern bool btdm_power_state_active(void);
  172. extern void btdm_wakeup_request(void);
  173. extern void btdm_wakeup_request_start(void);
  174. extern void btdm_wakeup_request_end(void);
  175. /* Low Power Clock */
  176. extern bool btdm_lpclk_select_src(uint32_t sel);
  177. extern bool btdm_lpclk_set_div(uint32_t div);
  178. extern int btdm_hci_tl_io_event_post(int event);
  179. /* VHCI */
  180. extern bool API_vhci_host_check_send_available(void);
  181. extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
  182. extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback);
  183. /* TX power */
  184. extern int ble_txpwr_set(int power_type, int power_level);
  185. extern int ble_txpwr_get(int power_type);
  186. extern uint16_t l2c_ble_link_get_tx_buf_num(void);
  187. extern int coex_core_ble_conn_dyn_prio_get(bool *low, bool *high);
  188. extern bool btdm_deep_sleep_mem_init(void);
  189. extern void btdm_deep_sleep_mem_deinit(void);
  190. extern void btdm_ble_power_down_dma_copy(bool copy);
  191. extern uint8_t btdm_sleep_clock_sync(void);
  192. #if CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  193. extern void esp_mac_bb_power_down(void);
  194. extern void esp_mac_bb_power_up(void);
  195. extern void ets_backup_dma_copy(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem);
  196. #endif
  197. extern char _bss_start_btdm;
  198. extern char _bss_end_btdm;
  199. extern char _data_start_btdm;
  200. extern char _data_end_btdm;
  201. extern uint32_t _data_start_btdm_rom;
  202. extern uint32_t _data_end_btdm_rom;
  203. extern uint32_t _bt_bss_start;
  204. extern uint32_t _bt_bss_end;
  205. extern uint32_t _btdm_bss_start;
  206. extern uint32_t _btdm_bss_end;
  207. extern uint32_t _bt_data_start;
  208. extern uint32_t _bt_data_end;
  209. extern uint32_t _btdm_data_start;
  210. extern uint32_t _btdm_data_end;
  211. extern char _bt_tmp_bss_start;
  212. extern char _bt_tmp_bss_end;
  213. /* Local Function Declare
  214. *********************************************************************
  215. */
  216. #if CONFIG_SPIRAM_USE_MALLOC
  217. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue);
  218. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue);
  219. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  220. static void interrupt_set_wrapper(int cpu_no, int intr_source, int intr_num, int intr_prio);
  221. static void interrupt_clear_wrapper(int intr_source, int intr_num);
  222. static void interrupt_handler_set_wrapper(int n, intr_handler_t fn, void *arg);
  223. static void IRAM_ATTR interrupt_disable(void);
  224. static void IRAM_ATTR interrupt_restore(void);
  225. static void IRAM_ATTR task_yield_from_isr(void);
  226. static void *semphr_create_wrapper(uint32_t max, uint32_t init);
  227. static void semphr_delete_wrapper(void *semphr);
  228. static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
  229. static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
  230. static int semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
  231. static int semphr_give_wrapper(void *semphr);
  232. static void *mutex_create_wrapper(void);
  233. static void mutex_delete_wrapper(void *mutex);
  234. static int mutex_lock_wrapper(void *mutex);
  235. static int mutex_unlock_wrapper(void *mutex);
  236. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
  237. static void queue_delete_wrapper(void *queue);
  238. static int queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms);
  239. static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw);
  240. static int queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
  241. static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw);
  242. static int 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);
  243. static void task_delete_wrapper(void *task_handle);
  244. static bool IRAM_ATTR is_in_isr_wrapper(void);
  245. static void *malloc_internal_wrapper(size_t size);
  246. static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
  247. static void IRAM_ATTR srand_wrapper(unsigned int seed);
  248. static int IRAM_ATTR rand_wrapper(void);
  249. static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles, uint32_t *error_corr);
  250. static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t hus);
  251. static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *slot_cnt);
  252. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles);
  253. static void btdm_sleep_enter_phase2_wrapper(void);
  254. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void);
  255. static void btdm_sleep_exit_phase3_wrapper(void);
  256. static void coex_wifi_sleep_set_hook(bool sleep);
  257. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status);
  258. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status);
  259. static void interrupt_on_wrapper(int intr_num);
  260. static void interrupt_off_wrapper(int intr_num);
  261. static void btdm_hw_mac_power_up_wrapper(void);
  262. static void btdm_hw_mac_power_down_wrapper(void);
  263. static void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem);
  264. /* Local variable definition
  265. ***************************************************************************
  266. */
  267. /* OSI funcs */
  268. static const struct osi_funcs_t osi_funcs_ro = {
  269. ._magic = OSI_MAGIC_VALUE,
  270. ._version = OSI_VERSION,
  271. ._interrupt_set = interrupt_set_wrapper,
  272. ._interrupt_clear = interrupt_clear_wrapper,
  273. ._interrupt_handler_set = interrupt_handler_set_wrapper,
  274. ._interrupt_disable = interrupt_disable,
  275. ._interrupt_restore = interrupt_restore,
  276. ._task_yield = vPortYield,
  277. ._task_yield_from_isr = task_yield_from_isr,
  278. ._semphr_create = semphr_create_wrapper,
  279. ._semphr_delete = semphr_delete_wrapper,
  280. ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
  281. ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
  282. ._semphr_take = semphr_take_wrapper,
  283. ._semphr_give = semphr_give_wrapper,
  284. ._mutex_create = mutex_create_wrapper,
  285. ._mutex_delete = mutex_delete_wrapper,
  286. ._mutex_lock = mutex_lock_wrapper,
  287. ._mutex_unlock = mutex_unlock_wrapper,
  288. ._queue_create = queue_create_wrapper,
  289. ._queue_delete = queue_delete_wrapper,
  290. ._queue_send = queue_send_wrapper,
  291. ._queue_send_from_isr = queue_send_from_isr_wrapper,
  292. ._queue_recv = queue_recv_wrapper,
  293. ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
  294. ._task_create = task_create_wrapper,
  295. ._task_delete = task_delete_wrapper,
  296. ._is_in_isr = is_in_isr_wrapper,
  297. ._cause_sw_intr_to_core = NULL,
  298. ._malloc = malloc,
  299. ._malloc_internal = malloc_internal_wrapper,
  300. ._free = free,
  301. ._read_efuse_mac = read_mac_wrapper,
  302. ._srand = srand_wrapper,
  303. ._rand = rand_wrapper,
  304. ._btdm_lpcycles_2_hus = btdm_lpcycles_2_hus,
  305. ._btdm_hus_2_lpcycles = btdm_hus_2_lpcycles,
  306. ._btdm_sleep_check_duration = btdm_sleep_check_duration,
  307. ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper,
  308. ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper,
  309. ._btdm_sleep_exit_phase1 = btdm_sleep_exit_phase1_wrapper,
  310. ._btdm_sleep_exit_phase2 = NULL,
  311. ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper,
  312. //._coex_bt_request = coex_bt_request_wrapper,
  313. ._coex_wifi_sleep_set = coex_wifi_sleep_set_hook,
  314. ._coex_core_ble_conn_dyn_prio_get = coex_core_ble_conn_dyn_prio_get,
  315. ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
  316. ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
  317. //._coex_bt_release = coex_bt_release_wrapper,
  318. // ._coex_register_bt_cb = coex_register_bt_cb_wrapper,
  319. // ._coex_bb_reset_lock = coex_bb_reset_lock_wrapper,
  320. //._coex_bb_reset_unlock = coex_bb_reset_unlock_wrapper,
  321. ._interrupt_on = interrupt_on_wrapper,
  322. ._interrupt_off = interrupt_off_wrapper,
  323. ._esp_hw_power_down = btdm_hw_mac_power_down_wrapper,
  324. ._esp_hw_power_up = btdm_hw_mac_power_up_wrapper,
  325. ._ets_backup_dma_copy = btdm_backup_dma_copy_wrapper,
  326. };
  327. static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
  328. #if CONFIG_SPIRAM_USE_MALLOC
  329. static DRAM_ATTR btdm_queue_item_t btdm_queue_table[BTDM_MAX_QUEUE_NUM];
  330. static DRAM_ATTR SemaphoreHandle_t btdm_queue_table_mux = NULL;
  331. #endif /* #if CONFIG_SPIRAM_USE_MALLOC */
  332. /* Static variable declare */
  333. // timestamp when PHY/RF was switched on
  334. // static DRAM_ATTR int64_t s_time_phy_rf_just_enabled = 0;
  335. static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  336. static DRAM_ATTR portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
  337. // measured average low power clock period in micro seconds
  338. static DRAM_ATTR uint32_t btdm_lpcycle_us = 0;
  339. static DRAM_ATTR uint8_t btdm_lpcycle_us_frac = 0; // number of fractional bit for btdm_lpcycle_us
  340. #ifdef CONFIG_PM_ENABLE
  341. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock;
  342. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock; // pm_lock to prevent light sleep due to incompatibility currently
  343. static DRAM_ATTR QueueHandle_t s_pm_lock_sem = NULL;
  344. #endif
  345. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  346. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr;
  347. static void btdm_slp_tmr_callback(void *arg);
  348. #endif
  349. #ifdef CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  350. static bool sleep_backup_en = false;
  351. static bool sleep_backup_done = false;
  352. #endif
  353. static inline void btdm_check_and_init_bb(void)
  354. {
  355. // todo:
  356. // btdm_rf_bb_init_phase2();
  357. }
  358. void btdm_hw_mac_power_down_wrapper(void)
  359. {
  360. #if CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  361. esp_mac_bb_power_down();
  362. #endif
  363. }
  364. void btdm_hw_mac_power_up_wrapper(void)
  365. {
  366. #if CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  367. esp_mac_bb_power_up();
  368. #endif
  369. }
  370. void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem)
  371. {
  372. #if CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  373. ets_backup_dma_copy(reg, mem_addr, num, to_mem);
  374. #endif
  375. }
  376. #if CONFIG_SPIRAM_USE_MALLOC
  377. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue)
  378. {
  379. if (!btdm_queue_table_mux || !queue) {
  380. return NULL;
  381. }
  382. bool ret = false;
  383. btdm_queue_item_t *item;
  384. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  385. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  386. item = &btdm_queue_table[i];
  387. if (item->handle == NULL) {
  388. memcpy(item, queue, sizeof(btdm_queue_item_t));
  389. ret = true;
  390. break;
  391. }
  392. }
  393. xSemaphoreGive(btdm_queue_table_mux);
  394. return ret;
  395. }
  396. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue)
  397. {
  398. if (!btdm_queue_table_mux || !queue) {
  399. return false;
  400. }
  401. bool ret = false;
  402. btdm_queue_item_t *item;
  403. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  404. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  405. item = &btdm_queue_table[i];
  406. if (item->handle == queue->handle) {
  407. memcpy(queue, item, sizeof(btdm_queue_item_t));
  408. memset(item, 0, sizeof(btdm_queue_item_t));
  409. ret = true;
  410. break;
  411. }
  412. }
  413. xSemaphoreGive(btdm_queue_table_mux);
  414. return ret;
  415. }
  416. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  417. static void interrupt_set_wrapper(int cpu_no, int intr_source, int intr_num, int intr_prio)
  418. {
  419. intr_matrix_route(intr_source, intr_num);
  420. esprv_intc_int_set_priority(intr_num, intr_prio);
  421. //esprv_intc_int_enable_level(1 << intr_num);
  422. esprv_intc_int_set_type(intr_num, 0);
  423. }
  424. static void interrupt_clear_wrapper(int intr_source, int intr_num)
  425. {
  426. }
  427. static void interrupt_handler_set_wrapper(int n, intr_handler_t fn, void *arg)
  428. {
  429. intr_handler_set(n, fn, arg);
  430. }
  431. static void interrupt_on_wrapper(int intr_num)
  432. {
  433. esprv_intc_int_enable(1 << intr_num);
  434. }
  435. static void interrupt_off_wrapper(int intr_num)
  436. {
  437. esprv_intc_int_disable(1<<intr_num);
  438. }
  439. static void IRAM_ATTR interrupt_disable(void)
  440. {
  441. if (xPortInIsrContext()) {
  442. portENTER_CRITICAL_ISR(&global_int_mux);
  443. } else {
  444. portENTER_CRITICAL(&global_int_mux);
  445. }
  446. }
  447. static void IRAM_ATTR interrupt_restore(void)
  448. {
  449. if (xPortInIsrContext()) {
  450. portEXIT_CRITICAL_ISR(&global_int_mux);
  451. } else {
  452. portEXIT_CRITICAL(&global_int_mux);
  453. }
  454. }
  455. static void IRAM_ATTR task_yield_from_isr(void)
  456. {
  457. portYIELD_FROM_ISR();
  458. }
  459. static void *semphr_create_wrapper(uint32_t max, uint32_t init)
  460. {
  461. #if !CONFIG_SPIRAM_USE_MALLOC
  462. return (void *)xSemaphoreCreateCounting(max, init);
  463. #else
  464. StaticQueue_t *queue_buffer = NULL;
  465. QueueHandle_t handle = NULL;
  466. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  467. if (!queue_buffer) {
  468. goto error;
  469. }
  470. handle = xSemaphoreCreateCountingStatic(max, init, queue_buffer);
  471. if (!handle) {
  472. goto error;
  473. }
  474. btdm_queue_item_t item = {
  475. .handle = handle,
  476. .storage = NULL,
  477. .buffer = queue_buffer,
  478. };
  479. if (!btdm_queue_generic_register(&item)) {
  480. goto error;
  481. }
  482. return handle;
  483. error:
  484. if (handle) {
  485. vSemaphoreDelete(handle);
  486. }
  487. if (queue_buffer) {
  488. free(queue_buffer);
  489. }
  490. return NULL;
  491. #endif
  492. }
  493. static void semphr_delete_wrapper(void *semphr)
  494. {
  495. #if !CONFIG_SPIRAM_USE_MALLOC
  496. vSemaphoreDelete(semphr);
  497. #else
  498. btdm_queue_item_t item = {
  499. .handle = semphr,
  500. .storage = NULL,
  501. .buffer = NULL,
  502. };
  503. if (btdm_queue_generic_deregister(&item)) {
  504. vSemaphoreDelete(item.handle);
  505. free(item.buffer);
  506. }
  507. return;
  508. #endif
  509. }
  510. static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  511. {
  512. return (int)xSemaphoreTakeFromISR(semphr, hptw);
  513. }
  514. static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  515. {
  516. return (int)xSemaphoreGiveFromISR(semphr, hptw);
  517. }
  518. static int semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
  519. {
  520. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  521. return (int)xSemaphoreTake(semphr, portMAX_DELAY);
  522. } else {
  523. return (int)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
  524. }
  525. }
  526. static int semphr_give_wrapper(void *semphr)
  527. {
  528. return (int)xSemaphoreGive(semphr);
  529. }
  530. static void *mutex_create_wrapper(void)
  531. {
  532. #if CONFIG_SPIRAM_USE_MALLOC
  533. StaticQueue_t *queue_buffer = NULL;
  534. QueueHandle_t handle = NULL;
  535. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  536. if (!queue_buffer) {
  537. goto error;
  538. }
  539. handle = xSemaphoreCreateMutexStatic(queue_buffer);
  540. if (!handle) {
  541. goto error;
  542. }
  543. btdm_queue_item_t item = {
  544. .handle = handle,
  545. .storage = NULL,
  546. .buffer = queue_buffer,
  547. };
  548. if (!btdm_queue_generic_register(&item)) {
  549. goto error;
  550. }
  551. return handle;
  552. error:
  553. if (handle) {
  554. vSemaphoreDelete(handle);
  555. }
  556. if (queue_buffer) {
  557. free(queue_buffer);
  558. }
  559. return NULL;
  560. #else
  561. return (void *)xSemaphoreCreateMutex();
  562. #endif
  563. }
  564. static void mutex_delete_wrapper(void *mutex)
  565. {
  566. #if !CONFIG_SPIRAM_USE_MALLOC
  567. vSemaphoreDelete(mutex);
  568. #else
  569. btdm_queue_item_t item = {
  570. .handle = mutex,
  571. .storage = NULL,
  572. .buffer = NULL,
  573. };
  574. if (btdm_queue_generic_deregister(&item)) {
  575. vSemaphoreDelete(item.handle);
  576. free(item.buffer);
  577. }
  578. return;
  579. #endif
  580. }
  581. static int mutex_lock_wrapper(void *mutex)
  582. {
  583. return (int)xSemaphoreTake(mutex, portMAX_DELAY);
  584. }
  585. static int mutex_unlock_wrapper(void *mutex)
  586. {
  587. return (int)xSemaphoreGive(mutex);
  588. }
  589. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
  590. {
  591. #if CONFIG_SPIRAM_USE_MALLOC
  592. StaticQueue_t *queue_buffer = NULL;
  593. uint8_t *queue_storage = NULL;
  594. QueueHandle_t handle = NULL;
  595. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  596. if (!queue_buffer) {
  597. goto error;
  598. }
  599. queue_storage = heap_caps_malloc((queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  600. if (!queue_storage ) {
  601. goto error;
  602. }
  603. handle = xQueueCreateStatic(queue_len, item_size, queue_storage, queue_buffer);
  604. if (!handle) {
  605. goto error;
  606. }
  607. btdm_queue_item_t item = {
  608. .handle = handle,
  609. .storage = queue_storage,
  610. .buffer = queue_buffer,
  611. };
  612. if (!btdm_queue_generic_register(&item)) {
  613. goto error;
  614. }
  615. return handle;
  616. error:
  617. if (handle) {
  618. vQueueDelete(handle);
  619. }
  620. if (queue_storage) {
  621. free(queue_storage);
  622. }
  623. if (queue_buffer) {
  624. free(queue_buffer);
  625. }
  626. return NULL;
  627. #else
  628. return (void *)xQueueCreate(queue_len, item_size);
  629. #endif
  630. }
  631. static void queue_delete_wrapper(void *queue)
  632. {
  633. #if !CONFIG_SPIRAM_USE_MALLOC
  634. vQueueDelete(queue);
  635. #else
  636. btdm_queue_item_t item = {
  637. .handle = queue,
  638. .storage = NULL,
  639. .buffer = NULL,
  640. };
  641. if (btdm_queue_generic_deregister(&item)) {
  642. vQueueDelete(item.handle);
  643. free(item.storage);
  644. free(item.buffer);
  645. }
  646. return;
  647. #endif
  648. }
  649. static int queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms)
  650. {
  651. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  652. return (int)xQueueSend(queue, item, portMAX_DELAY);
  653. } else {
  654. return (int)xQueueSend(queue, item, block_time_ms / portTICK_PERIOD_MS);
  655. }
  656. }
  657. static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
  658. {
  659. return (int)xQueueSendFromISR(queue, item, hptw);
  660. }
  661. static int queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms)
  662. {
  663. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  664. return (int)xQueueReceive(queue, item, portMAX_DELAY);
  665. } else {
  666. return (int)xQueueReceive(queue, item, block_time_ms / portTICK_PERIOD_MS);
  667. }
  668. }
  669. static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw)
  670. {
  671. return (int)xQueueReceiveFromISR(queue, item, hptw);
  672. }
  673. static int 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)
  674. {
  675. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  676. }
  677. static void task_delete_wrapper(void *task_handle)
  678. {
  679. vTaskDelete(task_handle);
  680. }
  681. static bool IRAM_ATTR is_in_isr_wrapper(void)
  682. {
  683. return (bool)xPortInIsrContext();
  684. }
  685. static void *malloc_internal_wrapper(size_t size)
  686. {
  687. return heap_caps_malloc(size, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL|MALLOC_CAP_DMA);
  688. }
  689. static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
  690. {
  691. int ret = esp_read_mac(mac, ESP_MAC_BT);
  692. ESP_LOGI(BTDM_LOG_TAG, "Bluetooth MAC: 0x%02x:%02x:%02x:%02x:%02x:%02x\n",
  693. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  694. return ret;
  695. }
  696. static void IRAM_ATTR srand_wrapper(unsigned int seed)
  697. {
  698. /* empty function */
  699. }
  700. static int IRAM_ATTR rand_wrapper(void)
  701. {
  702. return (int)esp_random();
  703. }
  704. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  705. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles)
  706. {
  707. // The number of lp cycles should not lead to overflow. Thrs: 100s
  708. // clock measurement is conducted
  709. uint64_t us = (uint64_t)btdm_lpcycle_us * cycles;
  710. us = (us + (1 << (btdm_lpcycle_us_frac - 1))) >> btdm_lpcycle_us_frac;
  711. return (uint32_t)us;
  712. }
  713. #endif
  714. static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles, uint32_t *error_corr)
  715. {
  716. uint64_t local_error_corr = (error_corr == NULL) ? 0 : (uint64_t)(*error_corr);
  717. uint64_t res = (uint64_t)btdm_lpcycle_us * cycles * 2;
  718. local_error_corr += res;
  719. res = (local_error_corr >> btdm_lpcycle_us_frac);
  720. local_error_corr -= (res << btdm_lpcycle_us_frac);
  721. if (error_corr) {
  722. *error_corr = (uint32_t) local_error_corr;
  723. }
  724. return (uint32_t)res;
  725. }
  726. /*
  727. * @brief Converts a duration in half us into a number of low power clock cycles.
  728. */
  729. static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t hus)
  730. {
  731. // The number of sleep duration(us) should not lead to overflow. Thrs: 100s
  732. // Compute the sleep duration in us to low power clock cycles, with calibration result applied
  733. // clock measurement is conducted
  734. uint64_t cycles = ((uint64_t)(hus) << btdm_lpcycle_us_frac) / btdm_lpcycle_us;
  735. cycles >>= 1;
  736. return (uint32_t)cycles;
  737. }
  738. static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *half_slot_cnt)
  739. {
  740. if (*half_slot_cnt < BTDM_MIN_SLEEP_DURATION) {
  741. return false;
  742. }
  743. /* wake up in advance considering the delay in enabling PHY/RF */
  744. *half_slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY;
  745. return true;
  746. }
  747. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
  748. {
  749. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  750. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  751. uint32_t us_to_sleep = btdm_lpcycles_2_us(lpcycles);
  752. #define BTDM_MIN_TIMER_UNCERTAINTY_US (1800)
  753. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  754. // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift
  755. // and set the timer in advance
  756. uint32_t uncertainty = (us_to_sleep >> 11);
  757. if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) {
  758. uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US;
  759. }
  760. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) != ESP_OK) {
  761. ESP_LOGW(BTDM_LOG_TAG, "timer start failed");
  762. }
  763. #endif
  764. }
  765. static void btdm_sleep_enter_phase2_wrapper(void)
  766. {
  767. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  768. #if BTDM_MODEM_SLEEP_IN_EFFECT
  769. esp_phy_disable();
  770. #endif /* BTDM_MODEM_SLEEP_IN_EFFECT */
  771. #if (CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  772. if(sleep_backup_en) {
  773. btdm_ble_power_down_dma_copy(true);
  774. sleep_backup_done = true;
  775. }
  776. #endif
  777. #ifdef CONFIG_PM_ENABLE
  778. esp_pm_lock_release(s_pm_lock);
  779. semphr_give_wrapper(s_pm_lock_sem);
  780. #endif
  781. }
  782. }
  783. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void)
  784. {
  785. #ifdef CONFIG_PM_ENABLE
  786. if (semphr_take_from_isr_wrapper(s_pm_lock_sem, NULL) == pdTRUE) {
  787. esp_pm_lock_acquire(s_pm_lock);
  788. }
  789. #endif
  790. }
  791. static void btdm_sleep_exit_phase3_wrapper(void)
  792. {
  793. if(btdm_sleep_clock_sync()) {
  794. ESP_LOGE(BTDM_LOG_TAG, "sleep eco state err\n");
  795. assert(0);
  796. }
  797. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  798. #if BTDM_MODEM_SLEEP_IN_EFFECT
  799. esp_phy_enable();
  800. #endif /* BTDM_MODEM_SLEEP_IN_EFFECT */
  801. btdm_check_and_init_bb();
  802. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  803. esp_timer_stop(s_btdm_slp_tmr);
  804. #endif
  805. }
  806. }
  807. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  808. static void IRAM_ATTR btdm_slp_tmr_callback(void *arg)
  809. {
  810. #ifdef CONFIG_PM_ENABLE
  811. if (semphr_take_wrapper(s_pm_lock_sem, 0) == pdTRUE) {
  812. esp_pm_lock_acquire(s_pm_lock);
  813. }
  814. #endif
  815. #ifdef CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  816. if (sleep_backup_en && sleep_backup_done) {
  817. btdm_ble_power_down_dma_copy(false);
  818. sleep_backup_done = false;
  819. }
  820. #endif
  821. }
  822. #endif
  823. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
  824. {
  825. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  826. coex_schm_status_bit_set(type, status);
  827. #endif
  828. }
  829. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
  830. {
  831. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  832. coex_schm_status_bit_clear(type, status);
  833. #endif
  834. }
  835. bool esp_vhci_host_check_send_available(void)
  836. {
  837. return API_vhci_host_check_send_available();
  838. }
  839. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  840. {
  841. btdm_wakeup_request_start();
  842. if (!btdm_power_state_active()) {
  843. #if CONFIG_PM_ENABLE
  844. if (semphr_take_wrapper(s_pm_lock_sem, 0)) {
  845. esp_pm_lock_acquire(s_pm_lock);
  846. }
  847. #endif
  848. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  849. esp_timer_stop(s_btdm_slp_tmr);
  850. #ifdef CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  851. if (sleep_backup_en && sleep_backup_done) {
  852. btdm_ble_power_down_dma_copy(false);
  853. sleep_backup_done = false;
  854. }
  855. #endif // CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  856. #endif
  857. btdm_wakeup_request();
  858. }
  859. API_vhci_host_send_packet(data, len);
  860. btdm_wakeup_request_end();
  861. }
  862. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  863. {
  864. return API_vhci_host_register_callback((const vhci_host_callback_t *)callback) == 0 ? ESP_OK : ESP_FAIL;
  865. }
  866. static void btdm_controller_mem_init(void)
  867. {
  868. extern void btdm_controller_rom_data_init(void );
  869. btdm_controller_rom_data_init();
  870. }
  871. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  872. {
  873. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  874. return ESP_OK;
  875. }
  876. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  877. {
  878. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  879. return ESP_OK;
  880. }
  881. static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end)
  882. {
  883. int ret = heap_caps_add_region(start, end);
  884. /* heap_caps_add_region() returns ESP_ERR_INVALID_SIZE if the memory region is
  885. * is too small to fit a heap. This cannot be termed as a fatal error and hence
  886. * we replace it by ESP_OK
  887. */
  888. if (ret == ESP_ERR_INVALID_SIZE) {
  889. return ESP_OK;
  890. }
  891. return ret;
  892. }
  893. // release wifi and coex memory, free about 720 bytes,
  894. void esp_release_wifi_and_coex_mem(void)
  895. {
  896. ESP_ERROR_CHECK(try_heap_caps_add_region((intptr_t)ets_rom_layout_p->dram_start_coexist, (intptr_t)ets_rom_layout_p->dram_end_pp));
  897. ESP_ERROR_CHECK(try_heap_caps_add_region((intptr_t)ets_rom_layout_p->data_start_interface_coexist,(intptr_t)ets_rom_layout_p->bss_end_interface_pp));
  898. }
  899. esp_err_t esp_bluetooth_stop(void)
  900. {
  901. #ifdef CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  902. if (sleep_backup_en && sleep_backup_done) {
  903. btdm_hw_mac_power_up_wrapper();
  904. // le module power up
  905. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_BT_FORCE_PD);
  906. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_BT_FORCE_ISO);
  907. sleep_backup_done = false;
  908. }
  909. #endif
  910. return 0;
  911. }
  912. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
  913. {
  914. esp_err_t err;
  915. btdm_controller_mem_init();
  916. if (esp_register_shutdown_handler((shutdown_handler_t)esp_bluetooth_stop) != 0) {
  917. return ESP_ERR_INVALID_ARG;
  918. }
  919. osi_funcs_p = (struct osi_funcs_t *)malloc_internal_wrapper(sizeof(struct osi_funcs_t));
  920. if (osi_funcs_p == NULL) {
  921. return ESP_ERR_NO_MEM;
  922. }
  923. memcpy(osi_funcs_p, &osi_funcs_ro, sizeof(struct osi_funcs_t));
  924. if (btdm_osi_funcs_register(osi_funcs_p) != 0) {
  925. return ESP_ERR_INVALID_ARG;
  926. }
  927. #if (CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  928. bool try_alloc = btdm_deep_sleep_mem_init();
  929. if(try_alloc) {
  930. sleep_backup_en = true;
  931. }
  932. #endif
  933. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  934. return ESP_ERR_INVALID_STATE;
  935. }
  936. if (cfg == NULL) {
  937. return ESP_ERR_INVALID_ARG;
  938. }
  939. if (cfg->controller_task_prio != ESP_TASK_BT_CONTROLLER_PRIO
  940. || cfg->controller_task_stack_size < ESP_TASK_BT_CONTROLLER_STACK) {
  941. ESP_LOGE(BTDM_LOG_TAG, "Invalid controller task prioriy or stack size");
  942. return ESP_ERR_INVALID_ARG;
  943. }
  944. //overwrite some parameters
  945. cfg->magic = ESP_BT_CTRL_CONFIG_MAGIC_VAL;
  946. if (cfg->bluetooth_mode != ESP_BT_MODE_BLE) {
  947. ESP_LOGE(BTDM_LOG_TAG, "%s controller only support BLE only mode", __func__);
  948. return ESP_ERR_NOT_SUPPORTED;
  949. }
  950. if (cfg->bluetooth_mode & ESP_BT_MODE_BLE) {
  951. if ((cfg->ble_max_act <= 0) || (cfg->ble_max_act > BT_CTRL_BLE_MAX_ACT_LIMIT)) {
  952. ESP_LOGE(BTDM_LOG_TAG, "Invalid value of ble_max_act");
  953. return ESP_ERR_INVALID_ARG;
  954. }
  955. }
  956. if (cfg->sleep_mode == ESP_BT_SLEEP_MODE_1) {
  957. if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_NONE) {
  958. ESP_LOGE(BTDM_LOG_TAG, "SLEEP_MODE_1 enabled but sleep clock not configured");
  959. return ESP_ERR_INVALID_ARG;
  960. }
  961. }
  962. read_mac_wrapper(own_bda);
  963. ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version());
  964. #if CONFIG_SPIRAM_USE_MALLOC
  965. btdm_queue_table_mux = xSemaphoreCreateMutex();
  966. if (btdm_queue_table_mux == NULL) {
  967. return ESP_ERR_NO_MEM;
  968. }
  969. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  970. #endif
  971. if (cfg->sleep_mode == ESP_BT_SLEEP_MODE_1) {
  972. #ifdef CONFIG_PM_ENABLE
  973. if ((err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btLS", &s_light_sleep_pm_lock)) != ESP_OK) {
  974. goto error;
  975. }
  976. if ((err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock)) != ESP_OK) {
  977. goto error;
  978. }
  979. s_pm_lock_sem = semphr_create_wrapper(1, 0);
  980. if (s_pm_lock_sem == NULL) {
  981. err = ESP_ERR_NO_MEM;
  982. goto error;
  983. }
  984. #endif
  985. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  986. esp_timer_create_args_t create_args = {
  987. .callback = btdm_slp_tmr_callback,
  988. .arg = NULL,
  989. .name = "btSlp"
  990. };
  991. if ((err = esp_timer_create(&create_args, &s_btdm_slp_tmr)) != ESP_OK) {
  992. goto error;
  993. }
  994. #endif
  995. do {// todo: rewrite this block of code for chip
  996. #if CONFIG_IDF_ENV_FPGA
  997. // overwrite the sleep clock for FPGA
  998. cfg->sleep_clock = ESP_BT_SLEEP_CLOCK_FPGA_32K;
  999. ESP_LOGW(BTDM_LOG_TAG, "%s sleep clock overwrite on FPGA", __func__);
  1000. #endif
  1001. bool select_src_ret = false;
  1002. bool set_div_ret = false;
  1003. if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_MAIN_XTAL) {
  1004. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL);
  1005. set_div_ret = btdm_lpclk_set_div(rtc_clk_xtal_freq_get() * 2);
  1006. assert(select_src_ret && set_div_ret);
  1007. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1008. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  1009. } else if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_EXT_32K_XTAL) {
  1010. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K);
  1011. set_div_ret = btdm_lpclk_set_div(0);
  1012. assert(select_src_ret && set_div_ret);
  1013. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1014. btdm_lpcycle_us = esp_clk_slowclk_cal_get();
  1015. assert(btdm_lpcycle_us != 0);
  1016. } else if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_FPGA_32K) {
  1017. // on FPGA, the low power clock is hard-wired to a 32kHz(clock cycle 31.25us) oscillator
  1018. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1019. btdm_lpcycle_us = 125 << (btdm_lpcycle_us_frac - 2);
  1020. }
  1021. } while (0);
  1022. }
  1023. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  1024. coex_init();
  1025. #endif
  1026. periph_module_enable(PERIPH_BT_MODULE);
  1027. #ifdef CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  1028. btdm_hw_mac_power_up_wrapper();
  1029. #endif
  1030. // must do fpga_init and phy init before controller init
  1031. esp_phy_enable();
  1032. if (btdm_controller_init(cfg) != 0) {
  1033. err = ESP_ERR_NO_MEM;
  1034. goto error;
  1035. }
  1036. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1037. return ESP_OK;
  1038. error:
  1039. #ifdef CONFIG_PM_ENABLE
  1040. if (s_light_sleep_pm_lock != NULL) {
  1041. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1042. s_light_sleep_pm_lock = NULL;
  1043. }
  1044. if (s_pm_lock != NULL) {
  1045. esp_pm_lock_delete(s_pm_lock);
  1046. s_pm_lock = NULL;
  1047. }
  1048. if (s_pm_lock_sem) {
  1049. semphr_delete_wrapper(s_pm_lock_sem);
  1050. s_pm_lock_sem = NULL;
  1051. }
  1052. #endif
  1053. #if (defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  1054. if (s_btdm_slp_tmr != NULL) {
  1055. esp_timer_delete(s_btdm_slp_tmr);
  1056. s_btdm_slp_tmr = NULL;
  1057. }
  1058. #endif
  1059. #if (CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  1060. btdm_deep_sleep_mem_deinit();
  1061. sleep_backup_en = false;
  1062. #endif
  1063. esp_unregister_shutdown_handler((shutdown_handler_t)esp_bluetooth_stop);
  1064. return err;
  1065. }
  1066. esp_err_t esp_bt_controller_deinit(void)
  1067. {
  1068. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1069. return ESP_ERR_INVALID_STATE;
  1070. }
  1071. btdm_controller_deinit();
  1072. #if (CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  1073. btdm_deep_sleep_mem_deinit();
  1074. #endif
  1075. esp_unregister_shutdown_handler((shutdown_handler_t)esp_bluetooth_stop);
  1076. periph_module_disable(PERIPH_BT_MODULE);
  1077. esp_phy_disable();
  1078. #ifdef CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB
  1079. btdm_hw_mac_power_down_wrapper();
  1080. #endif
  1081. #ifdef CONFIG_PM_ENABLE
  1082. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1083. s_light_sleep_pm_lock = NULL;
  1084. esp_pm_lock_delete(s_pm_lock);
  1085. s_pm_lock = NULL;
  1086. semphr_delete_wrapper(s_pm_lock_sem);
  1087. s_pm_lock_sem = NULL;
  1088. #endif
  1089. #if ( defined CONFIG_PM_ENABLE) || (defined CONFIG_PM_POWER_DOWN_WIFI_BT_MAC_BB)
  1090. esp_timer_stop(s_btdm_slp_tmr);
  1091. esp_timer_delete(s_btdm_slp_tmr);
  1092. s_btdm_slp_tmr = NULL;
  1093. #endif
  1094. #if CONFIG_SPIRAM_USE_MALLOC
  1095. vSemaphoreDelete(btdm_queue_table_mux);
  1096. btdm_queue_table_mux = NULL;
  1097. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  1098. #endif
  1099. free(osi_funcs_p);
  1100. osi_funcs_p = NULL;
  1101. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  1102. btdm_lpcycle_us = 0;
  1103. return ESP_OK;
  1104. }
  1105. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  1106. {
  1107. int ret;
  1108. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1109. return ESP_ERR_INVALID_STATE;
  1110. }
  1111. //As the history reason, mode should be equal to the mode which set in esp_bt_controller_init()
  1112. if (mode != btdm_controller_get_mode()) {
  1113. ESP_LOGE(BTDM_LOG_TAG, "invalid mode %d, controller support mode is %d", mode, btdm_controller_get_mode());
  1114. return ESP_ERR_INVALID_ARG;
  1115. }
  1116. #ifdef CONFIG_PM_ENABLE
  1117. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  1118. esp_pm_lock_acquire(s_pm_lock);
  1119. #endif
  1120. // esp_phy_enable();
  1121. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  1122. coex_enable();
  1123. #endif
  1124. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1125. btdm_controller_enable_sleep(true);
  1126. }
  1127. // inititalize bluetooth baseband
  1128. btdm_check_and_init_bb();
  1129. ret = btdm_controller_enable(mode);
  1130. if (ret) {
  1131. // esp_phy_disable();
  1132. #ifdef CONFIG_PM_ENABLE
  1133. esp_pm_lock_release(s_light_sleep_pm_lock);
  1134. esp_pm_lock_release(s_pm_lock);
  1135. #endif
  1136. return ESP_ERR_INVALID_STATE;
  1137. }
  1138. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  1139. return ESP_OK;
  1140. }
  1141. esp_err_t esp_bt_controller_disable(void)
  1142. {
  1143. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1144. return ESP_ERR_INVALID_STATE;
  1145. }
  1146. // disable modem sleep and wake up from sleep mode
  1147. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1148. btdm_controller_enable_sleep(false);
  1149. if (!btdm_power_state_active()) {
  1150. btdm_wakeup_request();
  1151. }
  1152. while (!btdm_power_state_active()) {
  1153. esp_rom_delay_us(1000);
  1154. }
  1155. }
  1156. btdm_controller_disable();
  1157. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  1158. coex_disable();
  1159. #endif
  1160. // esp_phy_disable();
  1161. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1162. #ifdef CONFIG_PM_ENABLE
  1163. esp_pm_lock_release(s_light_sleep_pm_lock);
  1164. esp_pm_lock_release(s_pm_lock);
  1165. #endif
  1166. return ESP_OK;
  1167. }
  1168. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  1169. {
  1170. return btdm_controller_status;
  1171. }
  1172. uint8_t* esp_bt_get_mac(void)
  1173. {
  1174. return own_bda;
  1175. }
  1176. /* extra functions */
  1177. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  1178. {
  1179. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  1180. return ESP_OK;
  1181. }
  1182. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  1183. {
  1184. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return 0", __func__);
  1185. return 0;
  1186. }
  1187. esp_err_t esp_bt_sleep_enable (void)
  1188. {
  1189. esp_err_t status;
  1190. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1191. return ESP_ERR_INVALID_STATE;
  1192. }
  1193. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1194. btdm_controller_enable_sleep (true);
  1195. status = ESP_OK;
  1196. } else {
  1197. status = ESP_ERR_NOT_SUPPORTED;
  1198. }
  1199. return status;
  1200. }
  1201. esp_err_t esp_bt_sleep_disable (void)
  1202. {
  1203. esp_err_t status;
  1204. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1205. return ESP_ERR_INVALID_STATE;
  1206. }
  1207. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1208. btdm_controller_enable_sleep (false);
  1209. status = ESP_OK;
  1210. } else {
  1211. status = ESP_ERR_NOT_SUPPORTED;
  1212. }
  1213. return status;
  1214. }
  1215. bool esp_bt_controller_is_sleeping(void)
  1216. {
  1217. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1218. btdm_controller_get_sleep_mode() != ESP_BT_SLEEP_MODE_1) {
  1219. return false;
  1220. }
  1221. return !btdm_power_state_active();
  1222. }
  1223. void esp_bt_controller_wakeup_request(void)
  1224. {
  1225. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1226. btdm_controller_get_sleep_mode() != ESP_BT_SLEEP_MODE_1) {
  1227. return;
  1228. }
  1229. btdm_wakeup_request();
  1230. }
  1231. int IRAM_ATTR esp_bt_h4tl_eif_io_event_notify(int event)
  1232. {
  1233. return btdm_hci_tl_io_event_post(event);
  1234. }
  1235. void force_wifi_mode(int arg)
  1236. {
  1237. }
  1238. void unforce_wifi_mode(void)
  1239. {
  1240. }
  1241. void bt_bb_init_cmplx_reg(void)
  1242. {
  1243. }
  1244. uint16_t esp_bt_get_tx_buf_num(void)
  1245. {
  1246. return l2c_ble_link_get_tx_buf_num();
  1247. }
  1248. static void coex_wifi_sleep_set_hook(bool sleep)
  1249. {
  1250. }
  1251. #endif /* CONFIG_BT_ENABLED */