bt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 <esp_random.h>
  11. #include <esp_mac.h>
  12. #include "sdkconfig.h"
  13. #include "os/os.h"
  14. #include "sysinit/sysinit.h"
  15. #include "nimble/nimble_port.h"
  16. #include "nimble/nimble_port_freertos.h"
  17. #ifdef ESP_PLATFORM
  18. #include "esp_log.h"
  19. #endif
  20. #if CONFIG_SW_COEXIST_ENABLE
  21. #include "esp_coexist_internal.h"
  22. #endif
  23. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  24. #include "transport/uart/ble_hci_uart.h"
  25. #else
  26. #include "transport/ram/ble_hci_ram.h"
  27. #endif
  28. #include "nimble/ble_hci_trans.h"
  29. #include "nimble/nimble_npl_os.h"
  30. #include "esp_bt.h"
  31. #include "esp_intr_alloc.h"
  32. #include "esp_sleep.h"
  33. #include "esp_pm.h"
  34. #include "esp_phy_init.h"
  35. #include "soc/system_reg.h"
  36. #include "hal/hal_uart.h"
  37. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  38. #include "hci/hci_hal.h"
  39. #endif
  40. #include "freertos/FreeRTOS.h"
  41. #include "freertos/task.h"
  42. #include "esp_private/periph_ctrl.h"
  43. #include "nimble/hci_common.h"
  44. /* Macro definition
  45. ************************************************************************
  46. */
  47. #define NIMBLE_PORT_LOG_TAG "BLE_INIT"
  48. #define OSI_COEX_VERSION 0x00010006
  49. #define OSI_COEX_MAGIC_VALUE 0xFADEBEAD
  50. /* Types definition
  51. ************************************************************************
  52. */
  53. struct osi_coex_funcs_t {
  54. uint32_t _magic;
  55. uint32_t _version;
  56. void (* _coex_wifi_sleep_set)(bool sleep);
  57. int (* _coex_core_ble_conn_dyn_prio_get)(bool *low, bool *high);
  58. void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status);
  59. void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status);
  60. };
  61. struct ext_funcs_t {
  62. uint32_t ext_version;
  63. int (*_esp_intr_alloc)(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle);
  64. int (*_esp_intr_free)(void **ret_handle);
  65. void *(* _malloc)(size_t size);
  66. void (*_free)(void *p);
  67. void (*_hal_uart_start_tx)(int);
  68. int (*_hal_uart_init_cbs)(int, hal_uart_tx_char, hal_uart_tx_done, hal_uart_rx_char, void *);
  69. int (*_hal_uart_config)(int, int32_t, uint8_t, uint8_t, enum hal_uart_parity, enum hal_uart_flow_ctl);
  70. int (*_hal_uart_close)(int);
  71. void (*_hal_uart_blocking_tx)(int, uint8_t);
  72. int (*_hal_uart_init)(int, void *);
  73. 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);
  74. void (* _task_delete)(void *task_handle);
  75. void (*_osi_assert)(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2);
  76. uint32_t (* _os_random)(void);
  77. uint32_t magic;
  78. };
  79. /* External functions or variables
  80. ************************************************************************
  81. */
  82. extern int ble_plf_set_log_level(int level);
  83. extern int ble_osi_coex_funcs_register(struct osi_coex_funcs_t *coex_funcs);
  84. extern int coex_core_ble_conn_dyn_prio_get(bool *low, bool *high);
  85. extern int ble_controller_init(struct esp_bt_controller_config_t *cfg);
  86. extern int ble_controller_deinit(void);
  87. extern int ble_controller_enable(uint8_t mode);
  88. extern int ble_controller_disable(void);
  89. extern int esp_register_ext_funcs (struct ext_funcs_t *);
  90. extern void esp_unregister_ext_funcs (void);
  91. extern int esp_ble_ll_set_public_addr(const uint8_t *addr);
  92. extern int esp_register_npl_funcs (struct npl_funcs_t *p_npl_func);
  93. extern void esp_unregister_npl_funcs (void);
  94. extern void npl_freertos_mempool_deinit(void);
  95. /* TX power */
  96. int ble_txpwr_set(int power_type, int power_level);
  97. int ble_txpwr_get(int power_type);
  98. extern void bt_bb_v2_init_cmplx(uint8_t i);
  99. extern int os_msys_buf_alloc(void);
  100. extern uint32_t r_os_cputime_get32(void);
  101. extern uint32_t r_os_cputime_ticks_to_usecs(uint32_t ticks);
  102. extern void r_ble_ll_rfmgmt_set_sleep_cb(void *s_cb, void *w_cb, void *s_arg, void *w_arg, uint32_t us_to_enabled);
  103. extern int os_msys_init(void);
  104. extern void os_msys_buf_free(void);
  105. extern void esp_ble_register_trace_funcs(struct ble_ll_trace_func_t *funcs);
  106. extern void bt_bb_set_le_tx_on_delay(uint32_t delay_us);
  107. /* Local Function Declaration
  108. *********************************************************************
  109. */
  110. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status);
  111. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status);
  112. 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);
  113. static void task_delete_wrapper(void *task_handle);
  114. static void hal_uart_start_tx_wrapper(int uart_no);
  115. static int hal_uart_init_cbs_wrapper(int uart_no, hal_uart_tx_char tx_func,
  116. hal_uart_tx_done tx_done, hal_uart_rx_char rx_func, void *arg);
  117. static int hal_uart_config_wrapper(int uart_no, int32_t speed, uint8_t databits, uint8_t stopbits,
  118. enum hal_uart_parity parity, enum hal_uart_flow_ctl flow_ctl);
  119. static int hal_uart_close_wrapper(int uart_no);
  120. static void hal_uart_blocking_tx_wrapper(int port, uint8_t data);
  121. static int hal_uart_init_wrapper(int uart_no, void *cfg);
  122. static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle_in);
  123. static int esp_intr_free_wrapper(void **ret_handle);
  124. static void osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2);
  125. static uint32_t osi_random_wrapper(void);
  126. /* Local variable definition
  127. ***************************************************************************
  128. */
  129. /* Static variable declare */
  130. static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  131. static bool s_is_sleep_state = false;
  132. #ifdef CONFIG_PM_ENABLE
  133. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  134. uint32_t s_sleep_tick;
  135. #endif
  136. #endif
  137. #ifdef CONFIG_PM_ENABLE
  138. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr = NULL;
  139. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock = NULL;
  140. static bool s_pm_lock_acquired = true;
  141. static DRAM_ATTR bool s_btdm_allow_light_sleep;
  142. // pm_lock to prevent light sleep when using main crystal as Bluetooth low power clock
  143. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock;
  144. static void btdm_slp_tmr_callback(void *arg);
  145. #define BTDM_MIN_TIMER_UNCERTAINTY_US (200)
  146. #endif /* #ifdef CONFIG_PM_ENABLE */
  147. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_BLE_RTC_TIMER
  148. #define BLE_RTC_DELAY_US (1100)
  149. #else
  150. #define BLE_RTC_DELAY_US (0)
  151. #endif
  152. static const struct osi_coex_funcs_t s_osi_coex_funcs_ro = {
  153. ._magic = OSI_COEX_MAGIC_VALUE,
  154. ._version = OSI_COEX_VERSION,
  155. ._coex_wifi_sleep_set = NULL,
  156. ._coex_core_ble_conn_dyn_prio_get = NULL,
  157. ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
  158. ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
  159. };
  160. struct ext_funcs_t ext_funcs_ro = {
  161. .ext_version = 0xE0000001,
  162. ._esp_intr_alloc = esp_intr_alloc_wrapper,
  163. ._esp_intr_free = esp_intr_free_wrapper,
  164. ._malloc = malloc,
  165. ._free = free,
  166. ._hal_uart_start_tx = hal_uart_start_tx_wrapper,
  167. ._hal_uart_init_cbs = hal_uart_init_cbs_wrapper,
  168. ._hal_uart_config = hal_uart_config_wrapper,
  169. ._hal_uart_close = hal_uart_close_wrapper,
  170. ._hal_uart_blocking_tx = hal_uart_blocking_tx_wrapper,
  171. ._hal_uart_init = hal_uart_init_wrapper,
  172. ._task_create = task_create_wrapper,
  173. ._task_delete = task_delete_wrapper,
  174. ._osi_assert = osi_assert_wrapper,
  175. ._os_random = osi_random_wrapper,
  176. .magic = 0xA5A5A5A5,
  177. };
  178. static void IRAM_ATTR osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2)
  179. {
  180. assert(0);
  181. }
  182. static uint32_t IRAM_ATTR osi_random_wrapper(void)
  183. {
  184. return esp_random();
  185. }
  186. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
  187. {
  188. #if CONFIG_SW_COEXIST_ENABLE
  189. coex_schm_status_bit_set(type, status);
  190. #endif
  191. }
  192. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
  193. {
  194. #if CONFIG_SW_COEXIST_ENABLE
  195. coex_schm_status_bit_clear(type, status);
  196. #endif
  197. }
  198. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  199. bool esp_vhci_host_check_send_available(void)
  200. {
  201. if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  202. return false;
  203. }
  204. return true;
  205. }
  206. /**
  207. * Allocates an mbuf for use by the nimble host.
  208. */
  209. static struct os_mbuf *
  210. ble_hs_mbuf_gen_pkt(uint16_t leading_space)
  211. {
  212. struct os_mbuf *om;
  213. int rc;
  214. om = os_msys_get_pkthdr(0, 0);
  215. if (om == NULL) {
  216. return NULL;
  217. }
  218. if (om->om_omp->omp_databuf_len < leading_space) {
  219. rc = os_mbuf_free_chain(om);
  220. assert(rc == 0);
  221. return NULL;
  222. }
  223. om->om_data += leading_space;
  224. return om;
  225. }
  226. /**
  227. * Allocates an mbuf suitable for an HCI ACL data packet.
  228. *
  229. * @return An empty mbuf on success; null on memory
  230. * exhaustion.
  231. */
  232. struct os_mbuf *
  233. ble_hs_mbuf_acl_pkt(void)
  234. {
  235. return ble_hs_mbuf_gen_pkt(4 + 1);
  236. }
  237. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  238. {
  239. if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  240. return;
  241. }
  242. if (*(data) == DATA_TYPE_COMMAND) {
  243. struct ble_hci_cmd *cmd = NULL;
  244. cmd = (struct ble_hci_cmd *) ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
  245. memcpy((uint8_t *)cmd, data + 1, len - 1);
  246. ble_hci_trans_hs_cmd_tx((uint8_t *)cmd);
  247. }
  248. if (*(data) == DATA_TYPE_ACL) {
  249. struct os_mbuf *om = os_msys_get_pkthdr(0, 0);
  250. assert(om);
  251. memcpy(om->om_data, &data[1], len - 1);
  252. om->om_len = len - 1;
  253. OS_MBUF_PKTHDR(om)->omp_len = len - 1;
  254. ble_hci_trans_hs_acl_tx(om);
  255. }
  256. }
  257. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  258. {
  259. if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  260. return ESP_FAIL;
  261. }
  262. ble_hci_trans_cfg_hs(ble_hs_hci_rx_evt,NULL,ble_hs_rx_data,NULL);
  263. return ESP_OK;
  264. }
  265. #endif
  266. 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)
  267. {
  268. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  269. }
  270. static void task_delete_wrapper(void *task_handle)
  271. {
  272. vTaskDelete(task_handle);
  273. }
  274. static void hal_uart_start_tx_wrapper(int uart_no)
  275. {
  276. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  277. hal_uart_start_tx(uart_no);
  278. #endif
  279. }
  280. static int hal_uart_init_cbs_wrapper(int uart_no, hal_uart_tx_char tx_func,
  281. hal_uart_tx_done tx_done, hal_uart_rx_char rx_func, void *arg)
  282. {
  283. int rc = -1;
  284. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  285. rc = hal_uart_init_cbs(uart_no, tx_func, tx_done, rx_func, arg);
  286. #endif
  287. return rc;
  288. }
  289. static int hal_uart_config_wrapper(int uart_no, int32_t speed, uint8_t databits, uint8_t stopbits,
  290. enum hal_uart_parity parity, enum hal_uart_flow_ctl flow_ctl)
  291. {
  292. int rc = -1;
  293. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  294. rc = hal_uart_config(uart_no, speed, databits, stopbits, parity, flow_ctl);
  295. #endif
  296. return rc;
  297. }
  298. static int hal_uart_close_wrapper(int uart_no)
  299. {
  300. int rc = -1;
  301. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  302. rc = hal_uart_close(uart_no);
  303. #endif
  304. return rc;
  305. }
  306. static void hal_uart_blocking_tx_wrapper(int port, uint8_t data)
  307. {
  308. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  309. hal_uart_blocking_tx(port, data);
  310. #endif
  311. }
  312. static int hal_uart_init_wrapper(int uart_no, void *cfg)
  313. {
  314. int rc = -1;
  315. #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
  316. rc = hal_uart_init(uart_no, cfg);
  317. #endif
  318. return rc;
  319. }
  320. static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle_in)
  321. {
  322. int rc = esp_intr_alloc(source, flags, handler, arg, (intr_handle_t *)ret_handle_in);
  323. return rc;
  324. }
  325. static int esp_intr_free_wrapper(void **ret_handle)
  326. {
  327. int rc = 0;
  328. rc = esp_intr_free((intr_handle_t) * ret_handle);
  329. *ret_handle = NULL;
  330. return rc;
  331. }
  332. IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg)
  333. {
  334. if (s_is_sleep_state) {
  335. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "sleep state error");
  336. assert(0);
  337. }
  338. s_is_sleep_state = true;
  339. #ifdef CONFIG_PM_ENABLE
  340. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  341. uint32_t tick_invalid = *(uint32_t *)(arg);
  342. if (!tick_invalid) {
  343. s_sleep_tick = r_os_cputime_get32();
  344. assert(enable_tick >= s_sleep_tick);
  345. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  346. uint32_t us_to_sleep = os_cputime_ticks_to_usecs(enable_tick - s_sleep_tick);
  347. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  348. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - BTDM_MIN_TIMER_UNCERTAINTY_US) != ESP_OK) {
  349. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "timer start failed");
  350. }
  351. }
  352. #endif // CONFIG_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  353. if (s_pm_lock_acquired) {
  354. esp_pm_lock_release(s_pm_lock);
  355. s_pm_lock_acquired = false;
  356. }
  357. #endif // CONFIG_PM_ENABLE
  358. }
  359. IRAM_ATTR void controller_wakeup_cb(void *arg)
  360. {
  361. if (!s_is_sleep_state) {
  362. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "wake up state error");
  363. assert(0);
  364. }
  365. s_is_sleep_state = false;
  366. // need to check if need to call pm lock here
  367. #ifdef CONFIG_PM_ENABLE
  368. if (!s_pm_lock_acquired) {
  369. s_pm_lock_acquired = true;
  370. esp_pm_lock_acquire(s_pm_lock);
  371. }
  372. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  373. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TIMER) {
  374. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "wake up source %d", esp_sleep_get_wakeup_cause());
  375. }
  376. #endif
  377. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_BLE_RTC_TIMER
  378. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_BT) {
  379. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "wake up source %d", esp_sleep_get_wakeup_cause());
  380. }
  381. #endif
  382. #endif
  383. }
  384. #ifdef CONFIG_PM_ENABLE
  385. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  386. static void btdm_slp_tmr_callback(void *arg)
  387. {
  388. (void)(arg);
  389. if (!s_pm_lock_acquired) {
  390. s_pm_lock_acquired = true;
  391. esp_pm_lock_acquire(s_pm_lock);
  392. }
  393. }
  394. #endif
  395. #endif // CONFIG_PM_ENABLE
  396. void controller_sleep_init(void)
  397. {
  398. #ifdef CONFIG_NIMBLE_SLEEP_ENABLE
  399. s_is_sleep_state = false;
  400. #ifdef CONFIG_PM_ENABLE
  401. s_btdm_allow_light_sleep = true;
  402. #endif // CONFIG_PM_ENABLE
  403. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "BLE modem sleep is enabled");
  404. // register sleep callbacks
  405. r_ble_ll_rfmgmt_set_sleep_cb(controller_sleep_cb, controller_wakeup_cb, 0, 0, 500 + BLE_RTC_DELAY_US);
  406. #else
  407. #ifdef CONFIG_PM_ENABLE
  408. s_btdm_allow_light_sleep = false;
  409. #endif // CONFIG_PM_ENABLE
  410. #endif // CONFIG_NIMBLE_SLEEP_ENABLE
  411. // enable light sleep
  412. #ifdef CONFIG_PM_ENABLE
  413. if (!s_btdm_allow_light_sleep) {
  414. if (esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btnosleep", &s_light_sleep_pm_lock) != ESP_OK) {
  415. goto error;
  416. }
  417. }
  418. if (esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock) != ESP_OK) {
  419. goto error;
  420. }
  421. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  422. esp_timer_create_args_t create_args = {
  423. .callback = btdm_slp_tmr_callback,
  424. .arg = NULL,
  425. .name = "btSlp"
  426. };
  427. if ( esp_timer_create(&create_args, &s_btdm_slp_tmr) != ESP_OK) {
  428. goto error;
  429. }
  430. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "light sleep enable success, CPU RTC timer wake up");
  431. #endif //CONFIG_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  432. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_BLE_RTC_TIMER
  433. esp_sleep_enable_bt_wakeup();
  434. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "light sleep enable success, BLE RTC timer wake up");
  435. #endif // CONFIG_NIMBLE_WAKEUP_SOURCE_BLE_RTC_TIMER
  436. s_pm_lock_acquired = true;
  437. if (!s_btdm_allow_light_sleep) {
  438. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  439. }
  440. if (s_pm_lock) {
  441. esp_pm_lock_acquire(s_pm_lock);
  442. }
  443. return;
  444. error:
  445. if (!s_btdm_allow_light_sleep) {
  446. if (s_light_sleep_pm_lock != NULL) {
  447. esp_pm_lock_delete(s_light_sleep_pm_lock);
  448. s_light_sleep_pm_lock = NULL;
  449. }
  450. }
  451. if (s_pm_lock != NULL) {
  452. esp_pm_lock_delete(s_pm_lock);
  453. s_pm_lock = NULL;
  454. }
  455. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  456. if (s_btdm_slp_tmr != NULL) {
  457. esp_timer_delete(s_btdm_slp_tmr);
  458. s_btdm_slp_tmr = NULL;
  459. }
  460. #endif // CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  461. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_BLE_RTC_TIMER
  462. esp_sleep_disable_bt_wakeup();
  463. #endif // CONFIG_BT_NIMBLE_WAKEUP_SOURCE_BLE_RTC_TIMER
  464. #endif
  465. }
  466. void controller_sleep_deinit(void)
  467. {
  468. #ifdef CONFIG_PM_ENABLE
  469. if (!s_btdm_allow_light_sleep) {
  470. if (s_light_sleep_pm_lock != NULL) {
  471. esp_pm_lock_delete(s_light_sleep_pm_lock);
  472. s_light_sleep_pm_lock = NULL;
  473. }
  474. }
  475. if (s_pm_lock != NULL) {
  476. esp_pm_lock_delete(s_pm_lock);
  477. s_pm_lock = NULL;
  478. }
  479. #ifdef CONFIG_BT_NIMBLE_WAKEUP_SOURCE_CPU_RTC_TIMER
  480. if (s_btdm_slp_tmr != NULL) {
  481. esp_timer_stop(s_btdm_slp_tmr);
  482. esp_timer_delete(s_btdm_slp_tmr);
  483. s_btdm_slp_tmr = NULL;
  484. }
  485. #endif
  486. s_pm_lock_acquired = false;
  487. #endif
  488. }
  489. esp_err_t esp_bt_controller_init(struct esp_bt_controller_config_t *cfg)
  490. {
  491. if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  492. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state");
  493. return ESP_FAIL;
  494. }
  495. if (cfg == NULL) {
  496. return ESP_ERR_INVALID_ARG;
  497. }
  498. if (esp_register_ext_funcs(&ext_funcs_ro) != 0) {
  499. return ESP_ERR_INVALID_ARG;
  500. }
  501. /* Initialize the function pointers for OS porting */
  502. npl_freertos_funcs_init();
  503. struct npl_funcs_t *p_npl_funcs = npl_freertos_funcs_get();
  504. if (!p_npl_funcs) {
  505. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl functions get failed");
  506. return ESP_ERR_INVALID_ARG;
  507. }
  508. if (esp_register_npl_funcs(p_npl_funcs) != 0) {
  509. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl functions register failed");
  510. return ESP_ERR_INVALID_ARG;
  511. }
  512. if (npl_freertos_mempool_init() != 0) {
  513. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl mempool init failed");
  514. return ESP_ERR_INVALID_ARG;
  515. }
  516. /* Initialize the global memory pool */
  517. if (os_msys_buf_alloc() != 0) {
  518. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "os msys alloc failed");
  519. return ESP_ERR_INVALID_ARG;
  520. }
  521. os_msys_init();
  522. #if CONFIG_BT_NIMBLE_ENABLED
  523. // ble_npl_eventq_init() need to use npl function in rom and must be called after esp_bt_controller_init()
  524. /* Initialize default event queue */
  525. ble_npl_eventq_init(nimble_port_get_dflt_eventq());
  526. #endif
  527. periph_module_enable(PERIPH_BT_MODULE);
  528. // init phy
  529. esp_phy_enable();
  530. // set bb delay
  531. bt_bb_set_le_tx_on_delay(50);
  532. if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) {
  533. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed");
  534. return ESP_ERR_INVALID_ARG;
  535. }
  536. #if CONFIG_SW_COEXIST_ENABLE
  537. coex_init();
  538. #endif
  539. if (ble_controller_init(cfg) != 0) {
  540. return ESP_ERR_NO_MEM;
  541. }
  542. controller_sleep_init();
  543. uint8_t mac[6];
  544. ESP_ERROR_CHECK(esp_read_mac((uint8_t *)mac, ESP_MAC_BT));
  545. swap_in_place(mac, 6);
  546. esp_ble_ll_set_public_addr(mac);
  547. ble_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  548. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  549. ble_hci_trans_cfg_hs(ble_hs_hci_rx_evt,NULL,ble_hs_rx_data,NULL);
  550. #endif
  551. return ESP_OK;
  552. }
  553. esp_err_t esp_bt_controller_deinit(void)
  554. {
  555. if ((ble_controller_status < ESP_BT_CONTROLLER_STATUS_INITED) || (ble_controller_status >= ESP_BT_CONTROLLER_STATUS_ENABLED)) {
  556. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state");
  557. return ESP_FAIL;
  558. }
  559. controller_sleep_deinit();
  560. if (ble_controller_deinit() != 0) {
  561. return ESP_FAIL;
  562. }
  563. #if CONFIG_BT_NIMBLE_ENABLED
  564. /* De-initialize default event queue */
  565. ble_npl_eventq_deinit(nimble_port_get_dflt_eventq());
  566. #endif
  567. os_msys_buf_free();
  568. esp_unregister_npl_funcs();
  569. esp_unregister_ext_funcs();
  570. /* De-initialize npl functions */
  571. npl_freertos_funcs_deinit();
  572. npl_freertos_mempool_deinit();
  573. esp_phy_disable();
  574. ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  575. return ESP_OK;
  576. }
  577. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  578. {
  579. return ble_controller_status;
  580. }
  581. /* extra functions */
  582. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  583. {
  584. esp_err_t stat = ESP_FAIL;
  585. switch (power_type) {
  586. case ESP_BLE_PWR_TYPE_ADV:
  587. case ESP_BLE_PWR_TYPE_SCAN:
  588. case ESP_BLE_PWR_TYPE_DEFAULT:
  589. if (ble_txpwr_set(power_type, power_level) == 0) {
  590. stat = ESP_OK;
  591. }
  592. break;
  593. default:
  594. stat = ESP_ERR_NOT_SUPPORTED;
  595. break;
  596. }
  597. return stat;
  598. }
  599. int ble_txpwr_get(int power_type)
  600. {
  601. return 0;
  602. }
  603. int ble_txpwr_set(int power_type, int power_level)
  604. {
  605. return 0;
  606. }
  607. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  608. {
  609. esp_power_level_t lvl;
  610. switch (power_type) {
  611. case ESP_BLE_PWR_TYPE_ADV:
  612. case ESP_BLE_PWR_TYPE_SCAN:
  613. lvl = (esp_power_level_t)ble_txpwr_get(power_type);
  614. break;
  615. case ESP_BLE_PWR_TYPE_CONN_HDL0:
  616. case ESP_BLE_PWR_TYPE_CONN_HDL1:
  617. case ESP_BLE_PWR_TYPE_CONN_HDL2:
  618. case ESP_BLE_PWR_TYPE_CONN_HDL3:
  619. case ESP_BLE_PWR_TYPE_CONN_HDL4:
  620. case ESP_BLE_PWR_TYPE_CONN_HDL5:
  621. case ESP_BLE_PWR_TYPE_CONN_HDL6:
  622. case ESP_BLE_PWR_TYPE_CONN_HDL7:
  623. case ESP_BLE_PWR_TYPE_CONN_HDL8:
  624. case ESP_BLE_PWR_TYPE_DEFAULT:
  625. lvl = (esp_power_level_t)ble_txpwr_get(ESP_BLE_PWR_TYPE_DEFAULT);
  626. break;
  627. default:
  628. lvl = ESP_PWR_LVL_INVALID;
  629. break;
  630. }
  631. return lvl;
  632. }
  633. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  634. {
  635. if (mode != ESP_BT_MODE_BLE) {
  636. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller mode");
  637. return ESP_FAIL;
  638. }
  639. if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  640. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state");
  641. return ESP_FAIL;
  642. }
  643. #if CONFIG_SW_COEXIST_ENABLE
  644. coex_enable();
  645. #endif
  646. if (ble_controller_enable(mode) != 0) {
  647. return ESP_FAIL;
  648. }
  649. ble_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  650. return ESP_OK;
  651. }
  652. esp_err_t esp_bt_controller_disable(void)
  653. {
  654. if (ble_controller_status < ESP_BT_CONTROLLER_STATUS_ENABLED) {
  655. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state");
  656. return ESP_FAIL;
  657. }
  658. if (ble_controller_disable() != 0) {
  659. return ESP_FAIL;
  660. }
  661. return ESP_OK;
  662. }
  663. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  664. {
  665. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "%s not implemented, return OK", __func__);
  666. return ESP_OK;
  667. }
  668. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  669. {
  670. ESP_LOGW(NIMBLE_PORT_LOG_TAG, "%s not implemented, return OK", __func__);
  671. return ESP_OK;
  672. }