bt.c 48 KB

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