bt.c 46 KB

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