bt.c 48 KB

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