bt.c 44 KB

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