bt.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "sdkconfig.h"
  18. #include "esp_heap_caps.h"
  19. #include "esp_heap_caps_init.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "freertos/queue.h"
  23. #include "freertos/semphr.h"
  24. #include "freertos/xtensa_api.h"
  25. #include "freertos/portmacro.h"
  26. #include "xtensa/core-macros.h"
  27. #include "esp_types.h"
  28. #include "esp_system.h"
  29. #include "esp_task.h"
  30. #include "esp_intr_alloc.h"
  31. #include "esp_attr.h"
  32. #include "esp_phy_init.h"
  33. #include "esp_bt.h"
  34. #include "esp_err.h"
  35. #include "esp_log.h"
  36. #include "esp_pm.h"
  37. #include "driver/periph_ctrl.h"
  38. #include "soc/rtc.h"
  39. #include "soc/soc_memory_layout.h"
  40. #include "esp32/clk.h"
  41. #include "esp_coexist_internal.h"
  42. #if !CONFIG_FREERTOS_UNICORE
  43. #include "esp_ipc.h"
  44. #endif
  45. #include "esp_rom_sys.h"
  46. #if CONFIG_BT_ENABLED
  47. /* Macro definition
  48. ************************************************************************
  49. */
  50. #define BTDM_LOG_TAG "BTDM_INIT"
  51. #define BTDM_INIT_PERIOD (5000) /* ms */
  52. /* Bluetooth system and controller config */
  53. #define BTDM_CFG_BT_DATA_RELEASE (1<<0)
  54. #define BTDM_CFG_HCI_UART (1<<1)
  55. #define BTDM_CFG_CONTROLLER_RUN_APP_CPU (1<<2)
  56. #define BTDM_CFG_SCAN_DUPLICATE_OPTIONS (1<<3)
  57. #define BTDM_CFG_SEND_ADV_RESERVED_SIZE (1<<4)
  58. #define BTDM_CFG_BLE_FULL_SCAN_SUPPORTED (1<<5)
  59. /* Sleep mode */
  60. #define BTDM_MODEM_SLEEP_MODE_NONE (0)
  61. #define BTDM_MODEM_SLEEP_MODE_ORIG (1)
  62. #define BTDM_MODEM_SLEEP_MODE_EVED (2) // sleep mode for BLE controller, used only for internal test.
  63. /* Low Power Clock Selection */
  64. #define BTDM_LPCLK_SEL_XTAL (0)
  65. #define BTDM_LPCLK_SEL_XTAL32K (1)
  66. #define BTDM_LPCLK_SEL_RTC_SLOW (2)
  67. #define BTDM_LPCLK_SEL_8M (3)
  68. /* Sleep and wakeup interval control */
  69. #define BTDM_MIN_SLEEP_DURATION (12) // threshold of interval in slots to allow to fall into modem sleep
  70. #define BTDM_MODEM_WAKE_UP_DELAY (4) // delay in slots of modem wake up procedure, including re-enable PHY/RF
  71. #define BT_DEBUG(...)
  72. #define BT_API_CALL_CHECK(info, api_call, ret) \
  73. do{\
  74. esp_err_t __err = (api_call);\
  75. if ((ret) != __err) {\
  76. BT_DEBUG("%s %d %s ret=0x%X\n", __FUNCTION__, __LINE__, (info), __err);\
  77. return __err;\
  78. }\
  79. } while(0)
  80. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  81. #define OSI_VERSION 0x00010002
  82. #define OSI_MAGIC_VALUE 0xFADEBEAD
  83. /* SPIRAM Configuration */
  84. #if CONFIG_SPIRAM_USE_MALLOC
  85. #define BTDM_MAX_QUEUE_NUM (5)
  86. #endif
  87. /* Types definition
  88. ************************************************************************
  89. */
  90. /* VHCI function interface */
  91. typedef struct vhci_host_callback {
  92. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  93. 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*/
  94. } vhci_host_callback_t;
  95. /* Dram region */
  96. typedef struct {
  97. esp_bt_mode_t mode;
  98. intptr_t start;
  99. intptr_t end;
  100. } btdm_dram_available_region_t;
  101. /* PSRAM configuration */
  102. #if CONFIG_SPIRAM_USE_MALLOC
  103. typedef struct {
  104. QueueHandle_t handle;
  105. void *storage;
  106. void *buffer;
  107. } btdm_queue_item_t;
  108. #endif
  109. /* OSI function */
  110. struct osi_funcs_t {
  111. uint32_t _version;
  112. xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
  113. void (*_ints_on)(unsigned int mask);
  114. void (*_interrupt_disable)(void);
  115. void (*_interrupt_restore)(void);
  116. void (*_task_yield)(void);
  117. void (*_task_yield_from_isr)(void);
  118. void *(*_semphr_create)(uint32_t max, uint32_t init);
  119. void (*_semphr_delete)(void *semphr);
  120. int32_t (*_semphr_take_from_isr)(void *semphr, void *hptw);
  121. int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
  122. int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
  123. int32_t (*_semphr_give)(void *semphr);
  124. void *(*_mutex_create)(void);
  125. void (*_mutex_delete)(void *mutex);
  126. int32_t (*_mutex_lock)(void *mutex);
  127. int32_t (*_mutex_unlock)(void *mutex);
  128. void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
  129. void (* _queue_delete)(void *queue);
  130. int32_t (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
  131. int32_t (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
  132. int32_t (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
  133. int32_t (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
  134. int32_t (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  135. void (* _task_delete)(void *task_handle);
  136. bool (* _is_in_isr)(void);
  137. int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
  138. void *(* _malloc)(uint32_t size);
  139. void *(* _malloc_internal)(uint32_t size);
  140. void (* _free)(void *p);
  141. int32_t (* _read_efuse_mac)(uint8_t mac[6]);
  142. void (* _srand)(unsigned int seed);
  143. int (* _rand)(void);
  144. uint32_t (* _btdm_lpcycles_2_us)(uint32_t cycles);
  145. uint32_t (* _btdm_us_2_lpcycles)(uint32_t us);
  146. bool (* _btdm_sleep_check_duration)(uint32_t *slot_cnt);
  147. void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */
  148. void (* _btdm_sleep_enter_phase2)(void);
  149. void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */
  150. void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */
  151. void (* _btdm_sleep_exit_phase3)(void); /* called from task */
  152. bool (* _coex_bt_wakeup_request)(void);
  153. void (* _coex_bt_wakeup_request_end)(void);
  154. int (* _coex_bt_request)(uint32_t event, uint32_t latency, uint32_t duration);
  155. int (* _coex_bt_release)(uint32_t event);
  156. int (* _coex_register_bt_cb)(coex_func_cb_t cb);
  157. uint32_t (* _coex_bb_reset_lock)(void);
  158. void (* _coex_bb_reset_unlock)(uint32_t restore);
  159. uint32_t _magic;
  160. };
  161. /* External functions or values
  162. ************************************************************************
  163. */
  164. /* not for user call, so don't put to include file */
  165. /* OSI */
  166. extern int btdm_osi_funcs_register(void *osi_funcs);
  167. /* Initialise and De-initialise */
  168. extern int btdm_controller_init(uint32_t config_mask, esp_bt_controller_config_t *config_opts);
  169. extern void btdm_controller_deinit(void);
  170. extern int btdm_controller_enable(esp_bt_mode_t mode);
  171. extern void btdm_controller_disable(void);
  172. extern uint8_t btdm_controller_get_mode(void);
  173. extern const char *btdm_controller_get_compile_version(void);
  174. extern void btdm_rf_bb_init_phase2(void); // shall be called after PHY/RF is enabled
  175. /* Sleep */
  176. extern void btdm_controller_enable_sleep(bool enable);
  177. extern void btdm_controller_set_sleep_mode(uint8_t mode);
  178. extern uint8_t btdm_controller_get_sleep_mode(void);
  179. extern bool btdm_power_state_active(void);
  180. extern void btdm_wakeup_request(bool request_lock);
  181. extern void btdm_wakeup_request_end(void);
  182. /* Low Power Clock */
  183. extern bool btdm_lpclk_select_src(uint32_t sel);
  184. extern bool btdm_lpclk_set_div(uint32_t div);
  185. /* VHCI */
  186. extern bool API_vhci_host_check_send_available(void);
  187. extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
  188. extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback);
  189. /* TX power */
  190. extern int ble_txpwr_set(int power_type, int power_level);
  191. extern int ble_txpwr_get(int power_type);
  192. extern int bredr_txpwr_set(int min_power_level, int max_power_level);
  193. extern int bredr_txpwr_get(int *min_power_level, int *max_power_level);
  194. extern void bredr_sco_datapath_set(uint8_t data_path);
  195. extern void btdm_controller_scan_duplicate_list_clear(void);
  196. /* Coexistence */
  197. extern int coex_bt_request(uint32_t event, uint32_t latency, uint32_t duration);
  198. extern int coex_bt_release(uint32_t event);
  199. extern int coex_register_bt_cb(coex_func_cb_t cb);
  200. extern uint32_t coex_bb_reset_lock(void);
  201. extern void coex_bb_reset_unlock(uint32_t restore);
  202. extern void coex_ble_adv_priority_high_set(bool high);
  203. extern char _bss_start_btdm;
  204. extern char _bss_end_btdm;
  205. extern char _data_start_btdm;
  206. extern char _data_end_btdm;
  207. extern uint32_t _data_start_btdm_rom;
  208. extern uint32_t _data_end_btdm_rom;
  209. extern uint32_t _bt_bss_start;
  210. extern uint32_t _bt_bss_end;
  211. extern uint32_t _nimble_bss_start;
  212. extern uint32_t _nimble_bss_end;
  213. extern uint32_t _btdm_bss_start;
  214. extern uint32_t _btdm_bss_end;
  215. extern uint32_t _bt_data_start;
  216. extern uint32_t _bt_data_end;
  217. extern uint32_t _nimble_data_start;
  218. extern uint32_t _nimble_data_end;
  219. extern uint32_t _btdm_data_start;
  220. extern uint32_t _btdm_data_end;
  221. /* Local Function Declare
  222. *********************************************************************
  223. */
  224. #if CONFIG_SPIRAM_USE_MALLOC
  225. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue);
  226. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue);
  227. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  228. static void IRAM_ATTR interrupt_disable(void);
  229. static void IRAM_ATTR interrupt_restore(void);
  230. static void IRAM_ATTR task_yield_from_isr(void);
  231. static void *semphr_create_wrapper(uint32_t max, uint32_t init);
  232. static void semphr_delete_wrapper(void *semphr);
  233. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
  234. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
  235. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
  236. static int32_t semphr_give_wrapper(void *semphr);
  237. static void *mutex_create_wrapper(void);
  238. static void mutex_delete_wrapper(void *mutex);
  239. static int32_t mutex_lock_wrapper(void *mutex);
  240. static int32_t mutex_unlock_wrapper(void *mutex);
  241. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
  242. static void queue_delete_wrapper(void *queue);
  243. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms);
  244. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw);
  245. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
  246. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw);
  247. static int32_t 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);
  248. static void task_delete_wrapper(void *task_handle);
  249. static bool IRAM_ATTR is_in_isr_wrapper(void);
  250. static void IRAM_ATTR cause_sw_intr(void *arg);
  251. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no);
  252. static void *malloc_internal_wrapper(size_t size);
  253. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
  254. static void IRAM_ATTR srand_wrapper(unsigned int seed);
  255. static int IRAM_ATTR rand_wrapper(void);
  256. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles);
  257. static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us);
  258. static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt);
  259. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles);
  260. static void btdm_sleep_enter_phase2_wrapper(void);
  261. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void);
  262. static void btdm_sleep_exit_phase3_wrapper(void);
  263. static bool coex_bt_wakeup_request(void);
  264. static void coex_bt_wakeup_request_end(void);
  265. static int coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration);
  266. static int coex_bt_release_wrapper(uint32_t event);
  267. static int coex_register_bt_cb_wrapper(coex_func_cb_t cb);
  268. static uint32_t coex_bb_reset_lock_wrapper(void);
  269. static void coex_bb_reset_unlock_wrapper(uint32_t restore);
  270. /* Local variable definition
  271. ***************************************************************************
  272. */
  273. /* OSI funcs */
  274. static const struct osi_funcs_t osi_funcs_ro = {
  275. ._version = OSI_VERSION,
  276. ._set_isr = xt_set_interrupt_handler,
  277. ._ints_on = xt_ints_on,
  278. ._interrupt_disable = interrupt_disable,
  279. ._interrupt_restore = interrupt_restore,
  280. ._task_yield = vPortYield,
  281. ._task_yield_from_isr = task_yield_from_isr,
  282. ._semphr_create = semphr_create_wrapper,
  283. ._semphr_delete = semphr_delete_wrapper,
  284. ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
  285. ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
  286. ._semphr_take = semphr_take_wrapper,
  287. ._semphr_give = semphr_give_wrapper,
  288. ._mutex_create = mutex_create_wrapper,
  289. ._mutex_delete = mutex_delete_wrapper,
  290. ._mutex_lock = mutex_lock_wrapper,
  291. ._mutex_unlock = mutex_unlock_wrapper,
  292. ._queue_create = queue_create_wrapper,
  293. ._queue_delete = queue_delete_wrapper,
  294. ._queue_send = queue_send_wrapper,
  295. ._queue_send_from_isr = queue_send_from_isr_wrapper,
  296. ._queue_recv = queue_recv_wrapper,
  297. ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
  298. ._task_create = task_create_wrapper,
  299. ._task_delete = task_delete_wrapper,
  300. ._is_in_isr = is_in_isr_wrapper,
  301. ._cause_sw_intr_to_core = cause_sw_intr_to_core_wrapper,
  302. ._malloc = malloc,
  303. ._malloc_internal = malloc_internal_wrapper,
  304. ._free = free,
  305. ._read_efuse_mac = read_mac_wrapper,
  306. ._srand = srand_wrapper,
  307. ._rand = rand_wrapper,
  308. ._btdm_lpcycles_2_us = btdm_lpcycles_2_us,
  309. ._btdm_us_2_lpcycles = btdm_us_2_lpcycles,
  310. ._btdm_sleep_check_duration = btdm_sleep_check_duration,
  311. ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper,
  312. ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper,
  313. ._btdm_sleep_exit_phase1 = btdm_sleep_exit_phase1_wrapper,
  314. ._btdm_sleep_exit_phase2 = NULL,
  315. ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper,
  316. ._coex_bt_wakeup_request = coex_bt_wakeup_request,
  317. ._coex_bt_wakeup_request_end = coex_bt_wakeup_request_end,
  318. ._coex_bt_request = coex_bt_request_wrapper,
  319. ._coex_bt_release = coex_bt_release_wrapper,
  320. ._coex_register_bt_cb = coex_register_bt_cb_wrapper,
  321. ._coex_bb_reset_lock = coex_bb_reset_lock_wrapper,
  322. ._coex_bb_reset_unlock = coex_bb_reset_unlock_wrapper,
  323. ._magic = OSI_MAGIC_VALUE,
  324. };
  325. /* the mode column will be modified by release function to indicate the available region */
  326. static btdm_dram_available_region_t btdm_dram_available_region[] = {
  327. //following is .data
  328. {ESP_BT_MODE_BTDM, SOC_MEM_BT_DATA_START, SOC_MEM_BT_DATA_END },
  329. //following is memory which HW will use
  330. {ESP_BT_MODE_BTDM, SOC_MEM_BT_EM_BTDM0_START, SOC_MEM_BT_EM_BTDM0_END },
  331. {ESP_BT_MODE_BLE, SOC_MEM_BT_EM_BLE_START, SOC_MEM_BT_EM_BLE_END },
  332. {ESP_BT_MODE_BTDM, SOC_MEM_BT_EM_BTDM1_START, SOC_MEM_BT_EM_BTDM1_END },
  333. {ESP_BT_MODE_CLASSIC_BT, SOC_MEM_BT_EM_BREDR_START, SOC_MEM_BT_EM_BREDR_REAL_END},
  334. //following is .bss
  335. {ESP_BT_MODE_BTDM, SOC_MEM_BT_BSS_START, SOC_MEM_BT_BSS_END },
  336. {ESP_BT_MODE_BTDM, SOC_MEM_BT_MISC_START, SOC_MEM_BT_MISC_END },
  337. };
  338. /* Reserve the full memory region used by Bluetooth Controller,
  339. * some may be released later at runtime. */
  340. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_EM_START, SOC_MEM_BT_EM_BREDR_REAL_END, rom_bt_em);
  341. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_BSS_START, SOC_MEM_BT_BSS_END, rom_bt_bss);
  342. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_MISC_START, SOC_MEM_BT_MISC_END, rom_bt_misc);
  343. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_DATA_START, SOC_MEM_BT_DATA_END, rom_bt_data);
  344. static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
  345. #if CONFIG_SPIRAM_USE_MALLOC
  346. static DRAM_ATTR btdm_queue_item_t btdm_queue_table[BTDM_MAX_QUEUE_NUM];
  347. static DRAM_ATTR SemaphoreHandle_t btdm_queue_table_mux = NULL;
  348. #endif /* #if CONFIG_SPIRAM_USE_MALLOC */
  349. /* Static variable declare */
  350. // timestamp when PHY/RF was switched on
  351. static DRAM_ATTR int64_t s_time_phy_rf_just_enabled = 0;
  352. static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  353. static DRAM_ATTR portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
  354. // measured average low power clock period in micro seconds
  355. static DRAM_ATTR uint32_t btdm_lpcycle_us = 0;
  356. static DRAM_ATTR uint8_t btdm_lpcycle_us_frac = 0; // number of fractional bit for btdm_lpcycle_us
  357. #if CONFIG_BTDM_MODEM_SLEEP_MODE_ORIG
  358. // used low power clock
  359. static DRAM_ATTR uint8_t btdm_lpclk_sel;
  360. #endif /* #ifdef CONFIG_BTDM_MODEM_SLEEP_MODE_ORIG */
  361. #ifdef CONFIG_PM_ENABLE
  362. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr;
  363. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock;
  364. static DRAM_ATTR QueueHandle_t s_pm_lock_sem = NULL;
  365. static DRAM_ATTR bool s_btdm_allow_light_sleep;
  366. // pm_lock to prevent light sleep when using main crystal as Bluetooth low power clock
  367. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock;
  368. static void btdm_slp_tmr_callback(void *arg);
  369. #endif /* #ifdef CONFIG_PM_ENABLE */
  370. static inline void btdm_check_and_init_bb(void)
  371. {
  372. /* init BT-BB if PHY/RF has been switched off since last BT-BB init */
  373. int64_t latest_ts = esp_phy_rf_get_on_ts();
  374. if (latest_ts != s_time_phy_rf_just_enabled ||
  375. s_time_phy_rf_just_enabled == 0) {
  376. btdm_rf_bb_init_phase2();
  377. s_time_phy_rf_just_enabled = latest_ts;
  378. }
  379. }
  380. #if CONFIG_SPIRAM_USE_MALLOC
  381. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue)
  382. {
  383. if (!btdm_queue_table_mux || !queue) {
  384. return NULL;
  385. }
  386. bool ret = false;
  387. btdm_queue_item_t *item;
  388. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  389. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  390. item = &btdm_queue_table[i];
  391. if (item->handle == NULL) {
  392. memcpy(item, queue, sizeof(btdm_queue_item_t));
  393. ret = true;
  394. break;
  395. }
  396. }
  397. xSemaphoreGive(btdm_queue_table_mux);
  398. return ret;
  399. }
  400. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue)
  401. {
  402. if (!btdm_queue_table_mux || !queue) {
  403. return false;
  404. }
  405. bool ret = false;
  406. btdm_queue_item_t *item;
  407. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  408. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  409. item = &btdm_queue_table[i];
  410. if (item->handle == queue->handle) {
  411. memcpy(queue, item, sizeof(btdm_queue_item_t));
  412. memset(item, 0, sizeof(btdm_queue_item_t));
  413. ret = true;
  414. break;
  415. }
  416. }
  417. xSemaphoreGive(btdm_queue_table_mux);
  418. return ret;
  419. }
  420. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  421. static void IRAM_ATTR interrupt_disable(void)
  422. {
  423. if (xPortInIsrContext()) {
  424. portENTER_CRITICAL_ISR(&global_int_mux);
  425. } else {
  426. portENTER_CRITICAL(&global_int_mux);
  427. }
  428. }
  429. static void IRAM_ATTR interrupt_restore(void)
  430. {
  431. if (xPortInIsrContext()) {
  432. portEXIT_CRITICAL_ISR(&global_int_mux);
  433. } else {
  434. portEXIT_CRITICAL(&global_int_mux);
  435. }
  436. }
  437. static void IRAM_ATTR task_yield_from_isr(void)
  438. {
  439. portYIELD_FROM_ISR();
  440. }
  441. static void *semphr_create_wrapper(uint32_t max, uint32_t init)
  442. {
  443. #if !CONFIG_SPIRAM_USE_MALLOC
  444. return (void *)xSemaphoreCreateCounting(max, init);
  445. #else
  446. StaticQueue_t *queue_buffer = NULL;
  447. QueueHandle_t handle = NULL;
  448. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  449. if (!queue_buffer) {
  450. goto error;
  451. }
  452. handle = xSemaphoreCreateCountingStatic(max, init, queue_buffer);
  453. if (!handle) {
  454. goto error;
  455. }
  456. btdm_queue_item_t item = {
  457. .handle = handle,
  458. .storage = NULL,
  459. .buffer = queue_buffer,
  460. };
  461. if (!btdm_queue_generic_register(&item)) {
  462. goto error;
  463. }
  464. return handle;
  465. error:
  466. if (handle) {
  467. vSemaphoreDelete(handle);
  468. }
  469. if (queue_buffer) {
  470. free(queue_buffer);
  471. }
  472. return NULL;
  473. #endif
  474. }
  475. static void semphr_delete_wrapper(void *semphr)
  476. {
  477. #if !CONFIG_SPIRAM_USE_MALLOC
  478. vSemaphoreDelete(semphr);
  479. #else
  480. btdm_queue_item_t item = {
  481. .handle = semphr,
  482. .storage = NULL,
  483. .buffer = NULL,
  484. };
  485. if (btdm_queue_generic_deregister(&item)) {
  486. vSemaphoreDelete(item.handle);
  487. free(item.buffer);
  488. }
  489. return;
  490. #endif
  491. }
  492. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  493. {
  494. return (int32_t)xSemaphoreTakeFromISR(semphr, hptw);
  495. }
  496. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  497. {
  498. return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
  499. }
  500. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
  501. {
  502. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  503. return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
  504. } else {
  505. return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
  506. }
  507. }
  508. static int32_t semphr_give_wrapper(void *semphr)
  509. {
  510. return (int32_t)xSemaphoreGive(semphr);
  511. }
  512. static void *mutex_create_wrapper(void)
  513. {
  514. #if CONFIG_SPIRAM_USE_MALLOC
  515. StaticQueue_t *queue_buffer = NULL;
  516. QueueHandle_t handle = NULL;
  517. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  518. if (!queue_buffer) {
  519. goto error;
  520. }
  521. handle = xSemaphoreCreateMutexStatic(queue_buffer);
  522. if (!handle) {
  523. goto error;
  524. }
  525. btdm_queue_item_t item = {
  526. .handle = handle,
  527. .storage = NULL,
  528. .buffer = queue_buffer,
  529. };
  530. if (!btdm_queue_generic_register(&item)) {
  531. goto error;
  532. }
  533. return handle;
  534. error:
  535. if (handle) {
  536. vSemaphoreDelete(handle);
  537. }
  538. if (queue_buffer) {
  539. free(queue_buffer);
  540. }
  541. return NULL;
  542. #else
  543. return (void *)xSemaphoreCreateMutex();
  544. #endif
  545. }
  546. static void mutex_delete_wrapper(void *mutex)
  547. {
  548. #if !CONFIG_SPIRAM_USE_MALLOC
  549. vSemaphoreDelete(mutex);
  550. #else
  551. btdm_queue_item_t item = {
  552. .handle = mutex,
  553. .storage = NULL,
  554. .buffer = NULL,
  555. };
  556. if (btdm_queue_generic_deregister(&item)) {
  557. vSemaphoreDelete(item.handle);
  558. free(item.buffer);
  559. }
  560. return;
  561. #endif
  562. }
  563. static int32_t mutex_lock_wrapper(void *mutex)
  564. {
  565. return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
  566. }
  567. static int32_t mutex_unlock_wrapper(void *mutex)
  568. {
  569. return (int32_t)xSemaphoreGive(mutex);
  570. }
  571. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
  572. {
  573. #if CONFIG_SPIRAM_USE_MALLOC
  574. StaticQueue_t *queue_buffer = NULL;
  575. uint8_t *queue_storage = NULL;
  576. QueueHandle_t handle = NULL;
  577. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  578. if (!queue_buffer) {
  579. goto error;
  580. }
  581. queue_storage = heap_caps_malloc((queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  582. if (!queue_storage ) {
  583. goto error;
  584. }
  585. handle = xQueueCreateStatic(queue_len, item_size, queue_storage, queue_buffer);
  586. if (!handle) {
  587. goto error;
  588. }
  589. btdm_queue_item_t item = {
  590. .handle = handle,
  591. .storage = queue_storage,
  592. .buffer = queue_buffer,
  593. };
  594. if (!btdm_queue_generic_register(&item)) {
  595. goto error;
  596. }
  597. return handle;
  598. error:
  599. if (handle) {
  600. vQueueDelete(handle);
  601. }
  602. if (queue_storage) {
  603. free(queue_storage);
  604. }
  605. if (queue_buffer) {
  606. free(queue_buffer);
  607. }
  608. return NULL;
  609. #else
  610. return (void *)xQueueCreate(queue_len, item_size);
  611. #endif
  612. }
  613. static void queue_delete_wrapper(void *queue)
  614. {
  615. #if !CONFIG_SPIRAM_USE_MALLOC
  616. vQueueDelete(queue);
  617. #else
  618. btdm_queue_item_t item = {
  619. .handle = queue,
  620. .storage = NULL,
  621. .buffer = NULL,
  622. };
  623. if (btdm_queue_generic_deregister(&item)) {
  624. vQueueDelete(item.handle);
  625. free(item.storage);
  626. free(item.buffer);
  627. }
  628. return;
  629. #endif
  630. }
  631. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms)
  632. {
  633. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  634. return (int32_t)xQueueSend(queue, item, portMAX_DELAY);
  635. } else {
  636. return (int32_t)xQueueSend(queue, item, block_time_ms / portTICK_PERIOD_MS);
  637. }
  638. }
  639. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
  640. {
  641. return (int32_t)xQueueSendFromISR(queue, item, hptw);
  642. }
  643. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms)
  644. {
  645. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  646. return (int32_t)xQueueReceive(queue, item, portMAX_DELAY);
  647. } else {
  648. return (int32_t)xQueueReceive(queue, item, block_time_ms / portTICK_PERIOD_MS);
  649. }
  650. }
  651. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw)
  652. {
  653. return (int32_t)xQueueReceiveFromISR(queue, item, hptw);
  654. }
  655. static int32_t 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)
  656. {
  657. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  658. }
  659. static void task_delete_wrapper(void *task_handle)
  660. {
  661. vTaskDelete(task_handle);
  662. }
  663. static bool IRAM_ATTR is_in_isr_wrapper(void)
  664. {
  665. return !xPortCanYield();
  666. }
  667. static void IRAM_ATTR cause_sw_intr(void *arg)
  668. {
  669. /* just convert void * to int, because the width is the same */
  670. uint32_t intr_no = (uint32_t)arg;
  671. XTHAL_SET_INTSET((1<<intr_no));
  672. }
  673. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no)
  674. {
  675. esp_err_t err = ESP_OK;
  676. #if CONFIG_FREERTOS_UNICORE
  677. cause_sw_intr((void *)intr_no);
  678. #else /* CONFIG_FREERTOS_UNICORE */
  679. if (xPortGetCoreID() == core_id) {
  680. cause_sw_intr((void *)intr_no);
  681. } else {
  682. err = esp_ipc_call(core_id, cause_sw_intr, (void *)intr_no);
  683. }
  684. #endif /* !CONFIG_FREERTOS_UNICORE */
  685. return err;
  686. }
  687. static void *malloc_internal_wrapper(size_t size)
  688. {
  689. return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  690. }
  691. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
  692. {
  693. return esp_read_mac(mac, ESP_MAC_BT);
  694. }
  695. static void IRAM_ATTR srand_wrapper(unsigned int seed)
  696. {
  697. /* empty function */
  698. }
  699. static int IRAM_ATTR rand_wrapper(void)
  700. {
  701. return (int)esp_random();
  702. }
  703. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles)
  704. {
  705. // The number of lp cycles should not lead to overflow. Thrs: 100s
  706. // clock measurement is conducted
  707. uint64_t us = (uint64_t)btdm_lpcycle_us * cycles;
  708. us = (us + (1 << (btdm_lpcycle_us_frac - 1))) >> btdm_lpcycle_us_frac;
  709. return (uint32_t)us;
  710. }
  711. /*
  712. * @brief Converts a duration in slots into a number of low power clock cycles.
  713. */
  714. static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us)
  715. {
  716. // The number of sleep duration(us) should not lead to overflow. Thrs: 100s
  717. // Compute the sleep duration in us to low power clock cycles, with calibration result applied
  718. // clock measurement is conducted
  719. uint64_t cycles = ((uint64_t)(us) << btdm_lpcycle_us_frac) / btdm_lpcycle_us;
  720. return (uint32_t)cycles;
  721. }
  722. static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt)
  723. {
  724. if (*slot_cnt < BTDM_MIN_SLEEP_DURATION) {
  725. return false;
  726. }
  727. /* wake up in advance considering the delay in enabling PHY/RF */
  728. *slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY;
  729. return true;
  730. }
  731. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
  732. {
  733. #ifdef CONFIG_PM_ENABLE
  734. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  735. uint32_t us_to_sleep = btdm_lpcycles_2_us(lpcycles);
  736. #define BTDM_MIN_TIMER_UNCERTAINTY_US (500)
  737. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  738. // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift
  739. // and set the timer in advance
  740. uint32_t uncertainty = (us_to_sleep >> 11);
  741. if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) {
  742. uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US;
  743. }
  744. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) != ESP_OK) {
  745. ESP_LOGW(BTDM_LOG_TAG, "timer start failed");
  746. }
  747. #endif
  748. }
  749. static void btdm_sleep_enter_phase2_wrapper(void)
  750. {
  751. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  752. esp_phy_disable();
  753. #ifdef CONFIG_PM_ENABLE
  754. esp_pm_lock_release(s_pm_lock);
  755. semphr_give_wrapper(s_pm_lock_sem);
  756. #endif
  757. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  758. esp_phy_disable();
  759. // pause bluetooth baseband
  760. periph_module_disable(PERIPH_BT_BASEBAND_MODULE);
  761. }
  762. }
  763. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void)
  764. {
  765. #ifdef CONFIG_PM_ENABLE
  766. if (semphr_take_from_isr_wrapper(s_pm_lock_sem, NULL) == pdTRUE) {
  767. esp_pm_lock_acquire(s_pm_lock);
  768. }
  769. #endif
  770. }
  771. static void btdm_sleep_exit_phase3_wrapper(void)
  772. {
  773. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  774. esp_phy_enable();
  775. btdm_check_and_init_bb();
  776. #ifdef CONFIG_PM_ENABLE
  777. esp_timer_stop(s_btdm_slp_tmr);
  778. #endif
  779. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  780. // resume bluetooth baseband
  781. periph_module_enable(PERIPH_BT_BASEBAND_MODULE);
  782. esp_phy_enable();
  783. }
  784. }
  785. #ifdef CONFIG_PM_ENABLE
  786. static void IRAM_ATTR btdm_slp_tmr_callback(void *arg)
  787. {
  788. if (semphr_take_wrapper(s_pm_lock_sem, 0) == pdTRUE) {
  789. esp_pm_lock_acquire(s_pm_lock);
  790. }
  791. }
  792. #endif
  793. #define BTDM_ASYNC_WAKEUP_REQ_HCI 0
  794. #define BTDM_ASYNC_WAKEUP_REQ_COEX 1
  795. #define BTDM_ASYNC_WAKEUP_REQMAX 2
  796. static bool async_wakeup_request(int event)
  797. {
  798. bool request_lock = false;
  799. switch (event) {
  800. case BTDM_ASYNC_WAKEUP_REQ_HCI:
  801. request_lock = true;
  802. break;
  803. case BTDM_ASYNC_WAKEUP_REQ_COEX:
  804. request_lock = false;
  805. break;
  806. default:
  807. return false;
  808. }
  809. bool do_wakeup_request = false;
  810. if (!btdm_power_state_active()) {
  811. #if CONFIG_PM_ENABLE
  812. if (semphr_take_wrapper(s_pm_lock_sem, 0)) {
  813. esp_pm_lock_acquire(s_pm_lock);
  814. }
  815. esp_timer_stop(s_btdm_slp_tmr);
  816. #endif
  817. do_wakeup_request = true;
  818. btdm_wakeup_request(request_lock);
  819. }
  820. return do_wakeup_request;
  821. }
  822. static void async_wakeup_request_end(int event)
  823. {
  824. bool request_lock = false;
  825. switch (event) {
  826. case BTDM_ASYNC_WAKEUP_REQ_HCI:
  827. request_lock = true;
  828. break;
  829. case BTDM_ASYNC_WAKEUP_REQ_COEX:
  830. request_lock = false;
  831. break;
  832. default:
  833. return;
  834. }
  835. if (request_lock) {
  836. btdm_wakeup_request_end();
  837. }
  838. return;
  839. }
  840. static bool coex_bt_wakeup_request(void)
  841. {
  842. return async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_COEX);
  843. }
  844. static void coex_bt_wakeup_request_end(void)
  845. {
  846. async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_COEX);
  847. return;
  848. }
  849. int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration)
  850. {
  851. #if CONFIG_SW_COEXIST_ENABLE
  852. return coex_bt_request(event, latency, duration);
  853. #else
  854. return 0;
  855. #endif
  856. }
  857. int IRAM_ATTR coex_bt_release_wrapper(uint32_t event)
  858. {
  859. #if CONFIG_SW_COEXIST_ENABLE
  860. return coex_bt_release(event);
  861. #else
  862. return 0;
  863. #endif
  864. }
  865. int coex_register_bt_cb_wrapper(coex_func_cb_t cb)
  866. {
  867. #if CONFIG_SW_COEXIST_ENABLE
  868. return coex_register_bt_cb(cb);
  869. #else
  870. return 0;
  871. #endif
  872. }
  873. uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void)
  874. {
  875. #if CONFIG_SW_COEXIST_ENABLE
  876. return coex_bb_reset_lock();
  877. #else
  878. return 0;
  879. #endif
  880. }
  881. void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore)
  882. {
  883. #if CONFIG_SW_COEXIST_ENABLE
  884. coex_bb_reset_unlock(restore);
  885. #endif
  886. }
  887. bool esp_vhci_host_check_send_available(void)
  888. {
  889. return API_vhci_host_check_send_available();
  890. }
  891. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  892. {
  893. bool do_wakeup_request = async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_HCI);
  894. API_vhci_host_send_packet(data, len);
  895. if (do_wakeup_request) {
  896. async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_HCI);
  897. }
  898. }
  899. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  900. {
  901. return API_vhci_host_register_callback((const vhci_host_callback_t *)callback) == 0 ? ESP_OK : ESP_FAIL;
  902. }
  903. static uint32_t btdm_config_mask_load(void)
  904. {
  905. uint32_t mask = 0x0;
  906. #if CONFIG_BTDM_CTRL_HCI_MODE_UART_H4
  907. mask |= BTDM_CFG_HCI_UART;
  908. #endif
  909. #if CONFIG_BTDM_CTRL_PINNED_TO_CORE == 1
  910. mask |= BTDM_CFG_CONTROLLER_RUN_APP_CPU;
  911. #endif
  912. #if CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED
  913. mask |= BTDM_CFG_BLE_FULL_SCAN_SUPPORTED;
  914. #endif /* CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED */
  915. mask |= BTDM_CFG_SCAN_DUPLICATE_OPTIONS;
  916. mask |= BTDM_CFG_SEND_ADV_RESERVED_SIZE;
  917. return mask;
  918. }
  919. static void btdm_controller_mem_init(void)
  920. {
  921. /* initialise .data section */
  922. memcpy(&_data_start_btdm, (void *)_data_start_btdm_rom, &_data_end_btdm - &_data_start_btdm);
  923. ESP_LOGD(BTDM_LOG_TAG, ".data initialise [0x%08x] <== [0x%08x]", (uint32_t)&_data_start_btdm, _data_start_btdm_rom);
  924. //initial em, .bss section
  925. for (int i = 1; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  926. if (btdm_dram_available_region[i].mode != ESP_BT_MODE_IDLE) {
  927. memset((void *)btdm_dram_available_region[i].start, 0x0, btdm_dram_available_region[i].end - btdm_dram_available_region[i].start);
  928. ESP_LOGD(BTDM_LOG_TAG, ".bss initialise [0x%08x] - [0x%08x]", btdm_dram_available_region[i].start, btdm_dram_available_region[i].end);
  929. }
  930. }
  931. }
  932. static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end)
  933. {
  934. int ret = heap_caps_add_region(start, end);
  935. /* heap_caps_add_region() returns ESP_ERR_INVALID_SIZE if the memory region is
  936. * is too small to fit a heap. This cannot be termed as a fatal error and hence
  937. * we replace it by ESP_OK
  938. */
  939. if (ret == ESP_ERR_INVALID_SIZE) {
  940. return ESP_OK;
  941. }
  942. return ret;
  943. }
  944. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  945. {
  946. bool update = true;
  947. intptr_t mem_start=(intptr_t) NULL, mem_end=(intptr_t) NULL;
  948. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  949. return ESP_ERR_INVALID_STATE;
  950. }
  951. //already released
  952. if (!(mode & btdm_dram_available_region[0].mode)) {
  953. return ESP_ERR_INVALID_STATE;
  954. }
  955. for (int i = 0; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  956. //skip the share mode, idle mode and other mode
  957. if (btdm_dram_available_region[i].mode == ESP_BT_MODE_IDLE
  958. || (mode & btdm_dram_available_region[i].mode) != btdm_dram_available_region[i].mode) {
  959. //clear the bit of the mode which will be released
  960. btdm_dram_available_region[i].mode &= ~mode;
  961. continue;
  962. } else {
  963. //clear the bit of the mode which will be released
  964. btdm_dram_available_region[i].mode &= ~mode;
  965. }
  966. if (update) {
  967. mem_start = btdm_dram_available_region[i].start;
  968. mem_end = btdm_dram_available_region[i].end;
  969. update = false;
  970. }
  971. if (i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t) - 1) {
  972. mem_end = btdm_dram_available_region[i].end;
  973. if (btdm_dram_available_region[i+1].mode != ESP_BT_MODE_IDLE
  974. && (mode & btdm_dram_available_region[i+1].mode) == btdm_dram_available_region[i+1].mode
  975. && mem_end == btdm_dram_available_region[i+1].start) {
  976. continue;
  977. } else {
  978. ESP_LOGD(BTDM_LOG_TAG, "Release DRAM [0x%08x] - [0x%08x]", mem_start, mem_end);
  979. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  980. update = true;
  981. }
  982. } else {
  983. mem_end = btdm_dram_available_region[i].end;
  984. ESP_LOGD(BTDM_LOG_TAG, "Release DRAM [0x%08x] - [0x%08x]", mem_start, mem_end);
  985. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  986. update = true;
  987. }
  988. }
  989. if (mode == ESP_BT_MODE_BTDM) {
  990. mem_start = (intptr_t)&_btdm_bss_start;
  991. mem_end = (intptr_t)&_btdm_bss_end;
  992. if (mem_start != mem_end) {
  993. ESP_LOGD(BTDM_LOG_TAG, "Release BTDM BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  994. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  995. }
  996. mem_start = (intptr_t)&_btdm_data_start;
  997. mem_end = (intptr_t)&_btdm_data_end;
  998. if (mem_start != mem_end) {
  999. ESP_LOGD(BTDM_LOG_TAG, "Release BTDM Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  1000. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1001. }
  1002. }
  1003. return ESP_OK;
  1004. }
  1005. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  1006. {
  1007. int ret;
  1008. intptr_t mem_start, mem_end;
  1009. ret = esp_bt_controller_mem_release(mode);
  1010. if (ret != ESP_OK) {
  1011. return ret;
  1012. }
  1013. if (mode == ESP_BT_MODE_BTDM) {
  1014. mem_start = (intptr_t)&_bt_bss_start;
  1015. mem_end = (intptr_t)&_bt_bss_end;
  1016. if (mem_start != mem_end) {
  1017. ESP_LOGD(BTDM_LOG_TAG, "Release BT BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  1018. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1019. }
  1020. mem_start = (intptr_t)&_bt_data_start;
  1021. mem_end = (intptr_t)&_bt_data_end;
  1022. if (mem_start != mem_end) {
  1023. ESP_LOGD(BTDM_LOG_TAG, "Release BT Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  1024. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1025. }
  1026. mem_start = (intptr_t)&_nimble_bss_start;
  1027. mem_end = (intptr_t)&_nimble_bss_end;
  1028. if (mem_start != mem_end) {
  1029. ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  1030. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1031. }
  1032. mem_start = (intptr_t)&_nimble_data_start;
  1033. mem_end = (intptr_t)&_nimble_data_end;
  1034. if (mem_start != mem_end) {
  1035. ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  1036. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  1037. }
  1038. }
  1039. return ESP_OK;
  1040. }
  1041. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
  1042. {
  1043. esp_err_t err;
  1044. uint32_t btdm_cfg_mask = 0;
  1045. osi_funcs_p = (struct osi_funcs_t *)malloc_internal_wrapper(sizeof(struct osi_funcs_t));
  1046. if (osi_funcs_p == NULL) {
  1047. return ESP_ERR_NO_MEM;
  1048. }
  1049. memcpy(osi_funcs_p, &osi_funcs_ro, sizeof(struct osi_funcs_t));
  1050. if (btdm_osi_funcs_register(osi_funcs_p) != 0) {
  1051. return ESP_ERR_INVALID_ARG;
  1052. }
  1053. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  1054. return ESP_ERR_INVALID_STATE;
  1055. }
  1056. //if all the bt available memory was already released, cannot initialize bluetooth controller
  1057. if (btdm_dram_available_region[0].mode == ESP_BT_MODE_IDLE) {
  1058. return ESP_ERR_INVALID_STATE;
  1059. }
  1060. if (cfg == NULL) {
  1061. return ESP_ERR_INVALID_ARG;
  1062. }
  1063. if (cfg->controller_task_prio != ESP_TASK_BT_CONTROLLER_PRIO
  1064. || cfg->controller_task_stack_size < ESP_TASK_BT_CONTROLLER_STACK) {
  1065. return ESP_ERR_INVALID_ARG;
  1066. }
  1067. //overwrite some parameters
  1068. cfg->bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF;
  1069. cfg->magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL;
  1070. if (((cfg->mode & ESP_BT_MODE_BLE) && (cfg->ble_max_conn <= 0 || cfg->ble_max_conn > BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT))
  1071. || ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_acl_conn <= 0 || cfg->bt_max_acl_conn > BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT))
  1072. || ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_sync_conn > BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT))) {
  1073. return ESP_ERR_INVALID_ARG;
  1074. }
  1075. ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version());
  1076. #if CONFIG_SPIRAM_USE_MALLOC
  1077. btdm_queue_table_mux = xSemaphoreCreateMutex();
  1078. if (btdm_queue_table_mux == NULL) {
  1079. return ESP_ERR_NO_MEM;
  1080. }
  1081. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  1082. #endif
  1083. btdm_controller_mem_init();
  1084. periph_module_enable(PERIPH_BT_MODULE);
  1085. #ifdef CONFIG_PM_ENABLE
  1086. s_btdm_allow_light_sleep = false;
  1087. #endif
  1088. // set default sleep clock cycle and its fractional bits
  1089. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1090. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  1091. #if CONFIG_BTDM_MODEM_SLEEP_MODE_ORIG
  1092. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  1093. #if CONFIG_BTDM_LPCLK_SEL_EXT_32K_XTAL
  1094. // check whether or not EXT_CRYS is working
  1095. if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_32K_XTAL) {
  1096. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL32K; // set default value
  1097. #ifdef CONFIG_PM_ENABLE
  1098. s_btdm_allow_light_sleep = true;
  1099. #endif
  1100. } else {
  1101. ESP_LOGW(BTDM_LOG_TAG, "32.768kHz XTAL not detected, fall back to main XTAL as Bluetooth sleep clock\n"
  1102. "light sleep mode will not be able to apply when bluetooth is enabled");
  1103. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  1104. }
  1105. #else
  1106. btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  1107. #endif
  1108. bool select_src_ret, set_div_ret;
  1109. if (btdm_lpclk_sel == BTDM_LPCLK_SEL_XTAL) {
  1110. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL);
  1111. set_div_ret = btdm_lpclk_set_div(rtc_clk_xtal_freq_get() * 2 - 1);
  1112. assert(select_src_ret && set_div_ret);
  1113. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1114. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  1115. } else { // btdm_lpclk_sel == BTDM_LPCLK_SEL_XTAL32K
  1116. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K);
  1117. set_div_ret = btdm_lpclk_set_div(0);
  1118. assert(select_src_ret && set_div_ret);
  1119. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1120. btdm_lpcycle_us = (RTC_CLK_CAL_FRACT > 15) ? (1000000 << (RTC_CLK_CAL_FRACT - 15)) :
  1121. (1000000 >> (15 - RTC_CLK_CAL_FRACT));
  1122. assert(btdm_lpcycle_us != 0);
  1123. }
  1124. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_ORIG);
  1125. #elif CONFIG_BTDM_MODEM_SLEEP_MODE_EVED
  1126. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_EVED);
  1127. #else
  1128. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE);
  1129. #endif
  1130. #ifdef CONFIG_PM_ENABLE
  1131. if (!s_btdm_allow_light_sleep) {
  1132. if ((err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btLS", &s_light_sleep_pm_lock)) != ESP_OK) {
  1133. goto error;
  1134. }
  1135. }
  1136. if ((err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock)) != ESP_OK) {
  1137. goto error;
  1138. }
  1139. esp_timer_create_args_t create_args = {
  1140. .callback = btdm_slp_tmr_callback,
  1141. .arg = NULL,
  1142. .name = "btSlp"
  1143. };
  1144. if ((err = esp_timer_create(&create_args, &s_btdm_slp_tmr)) != ESP_OK) {
  1145. goto error;
  1146. }
  1147. s_pm_lock_sem = semphr_create_wrapper(1, 0);
  1148. if (s_pm_lock_sem == NULL) {
  1149. err = ESP_ERR_NO_MEM;
  1150. goto error;
  1151. }
  1152. #endif
  1153. #if CONFIG_SW_COEXIST_ENABLE
  1154. coex_init();
  1155. #endif
  1156. btdm_cfg_mask = btdm_config_mask_load();
  1157. if (btdm_controller_init(btdm_cfg_mask, cfg) != 0) {
  1158. err = ESP_ERR_NO_MEM;
  1159. goto error;
  1160. }
  1161. #ifdef CONFIG_BTDM_COEX_BLE_ADV_HIGH_PRIORITY
  1162. coex_ble_adv_priority_high_set(true);
  1163. #else
  1164. coex_ble_adv_priority_high_set(false);
  1165. #endif
  1166. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1167. return ESP_OK;
  1168. error:
  1169. #ifdef CONFIG_PM_ENABLE
  1170. if (!s_btdm_allow_light_sleep) {
  1171. if (s_light_sleep_pm_lock != NULL) {
  1172. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1173. s_light_sleep_pm_lock = NULL;
  1174. }
  1175. }
  1176. if (s_pm_lock != NULL) {
  1177. esp_pm_lock_delete(s_pm_lock);
  1178. s_pm_lock = NULL;
  1179. }
  1180. if (s_btdm_slp_tmr != NULL) {
  1181. esp_timer_delete(s_btdm_slp_tmr);
  1182. s_btdm_slp_tmr = NULL;
  1183. }
  1184. if (s_pm_lock_sem) {
  1185. semphr_delete_wrapper(s_pm_lock_sem);
  1186. s_pm_lock_sem = NULL;
  1187. }
  1188. #endif
  1189. return err;
  1190. }
  1191. esp_err_t esp_bt_controller_deinit(void)
  1192. {
  1193. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1194. return ESP_ERR_INVALID_STATE;
  1195. }
  1196. btdm_controller_deinit();
  1197. periph_module_disable(PERIPH_BT_MODULE);
  1198. #ifdef CONFIG_PM_ENABLE
  1199. if (!s_btdm_allow_light_sleep) {
  1200. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1201. s_light_sleep_pm_lock = NULL;
  1202. }
  1203. esp_pm_lock_delete(s_pm_lock);
  1204. s_pm_lock = NULL;
  1205. esp_timer_stop(s_btdm_slp_tmr);
  1206. esp_timer_delete(s_btdm_slp_tmr);
  1207. s_btdm_slp_tmr = NULL;
  1208. semphr_delete_wrapper(s_pm_lock_sem);
  1209. s_pm_lock_sem = NULL;
  1210. #endif
  1211. #if CONFIG_SPIRAM_USE_MALLOC
  1212. vSemaphoreDelete(btdm_queue_table_mux);
  1213. btdm_queue_table_mux = NULL;
  1214. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  1215. #endif
  1216. free(osi_funcs_p);
  1217. osi_funcs_p = NULL;
  1218. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  1219. btdm_lpcycle_us = 0;
  1220. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE);
  1221. return ESP_OK;
  1222. }
  1223. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  1224. {
  1225. int ret;
  1226. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1227. return ESP_ERR_INVALID_STATE;
  1228. }
  1229. //As the history reason, mode should be equal to the mode which set in esp_bt_controller_init()
  1230. if (mode != btdm_controller_get_mode()) {
  1231. return ESP_ERR_INVALID_ARG;
  1232. }
  1233. #ifdef CONFIG_PM_ENABLE
  1234. if (!s_btdm_allow_light_sleep) {
  1235. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  1236. }
  1237. esp_pm_lock_acquire(s_pm_lock);
  1238. #endif
  1239. esp_phy_enable();
  1240. #if CONFIG_SW_COEXIST_ENABLE
  1241. coex_enable();
  1242. #endif
  1243. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1244. btdm_controller_enable_sleep(true);
  1245. }
  1246. // inititalize bluetooth baseband
  1247. btdm_check_and_init_bb();
  1248. ret = btdm_controller_enable(mode);
  1249. if (ret != 0) {
  1250. #if CONFIG_SW_COEXIST_ENABLE
  1251. coex_disable();
  1252. #endif
  1253. esp_phy_disable();
  1254. #ifdef CONFIG_PM_ENABLE
  1255. if (!s_btdm_allow_light_sleep) {
  1256. esp_pm_lock_release(s_light_sleep_pm_lock);
  1257. }
  1258. esp_pm_lock_release(s_pm_lock);
  1259. #endif
  1260. return ESP_ERR_INVALID_STATE;
  1261. }
  1262. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  1263. return ESP_OK;
  1264. }
  1265. esp_err_t esp_bt_controller_disable(void)
  1266. {
  1267. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1268. return ESP_ERR_INVALID_STATE;
  1269. }
  1270. // disable modem sleep and wake up from sleep mode
  1271. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1272. btdm_controller_enable_sleep(false);
  1273. if (!btdm_power_state_active()) {
  1274. btdm_wakeup_request(false);
  1275. }
  1276. while (!btdm_power_state_active()) {
  1277. esp_rom_delay_us(1000);
  1278. }
  1279. }
  1280. btdm_controller_disable();
  1281. #if CONFIG_SW_COEXIST_ENABLE
  1282. coex_disable();
  1283. #endif
  1284. esp_phy_disable();
  1285. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1286. #ifdef CONFIG_PM_ENABLE
  1287. if (!s_btdm_allow_light_sleep) {
  1288. esp_pm_lock_release(s_light_sleep_pm_lock);
  1289. }
  1290. esp_pm_lock_release(s_pm_lock);
  1291. #endif
  1292. return ESP_OK;
  1293. }
  1294. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  1295. {
  1296. return btdm_controller_status;
  1297. }
  1298. /* extra functions */
  1299. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  1300. {
  1301. if (ble_txpwr_set(power_type, power_level) != 0) {
  1302. return ESP_ERR_INVALID_ARG;
  1303. }
  1304. return ESP_OK;
  1305. }
  1306. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  1307. {
  1308. return (esp_power_level_t)ble_txpwr_get(power_type);
  1309. }
  1310. esp_err_t esp_bredr_tx_power_set(esp_power_level_t min_power_level, esp_power_level_t max_power_level)
  1311. {
  1312. esp_err_t err;
  1313. int ret;
  1314. ret = bredr_txpwr_set(min_power_level, max_power_level);
  1315. if (ret == 0) {
  1316. err = ESP_OK;
  1317. } else if (ret == -1) {
  1318. err = ESP_ERR_INVALID_ARG;
  1319. } else {
  1320. err = ESP_ERR_INVALID_STATE;
  1321. }
  1322. return err;
  1323. }
  1324. esp_err_t esp_bredr_tx_power_get(esp_power_level_t *min_power_level, esp_power_level_t *max_power_level)
  1325. {
  1326. if (bredr_txpwr_get((int *)min_power_level, (int *)max_power_level) != 0) {
  1327. return ESP_ERR_INVALID_ARG;
  1328. }
  1329. return ESP_OK;
  1330. }
  1331. esp_err_t esp_bt_sleep_enable (void)
  1332. {
  1333. esp_err_t status;
  1334. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1335. return ESP_ERR_INVALID_STATE;
  1336. }
  1337. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG ||
  1338. btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1339. btdm_controller_enable_sleep (true);
  1340. status = ESP_OK;
  1341. } else {
  1342. status = ESP_ERR_NOT_SUPPORTED;
  1343. }
  1344. return status;
  1345. }
  1346. esp_err_t esp_bt_sleep_disable (void)
  1347. {
  1348. esp_err_t status;
  1349. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1350. return ESP_ERR_INVALID_STATE;
  1351. }
  1352. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG ||
  1353. btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1354. btdm_controller_enable_sleep (false);
  1355. status = ESP_OK;
  1356. } else {
  1357. status = ESP_ERR_NOT_SUPPORTED;
  1358. }
  1359. return status;
  1360. }
  1361. bool esp_bt_controller_is_sleeping(void)
  1362. {
  1363. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1364. btdm_controller_get_sleep_mode() != BTDM_MODEM_SLEEP_MODE_ORIG) {
  1365. return false;
  1366. }
  1367. return !btdm_power_state_active();
  1368. }
  1369. void esp_bt_controller_wakeup_request(void)
  1370. {
  1371. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1372. btdm_controller_get_sleep_mode() != BTDM_MODEM_SLEEP_MODE_ORIG) {
  1373. return;
  1374. }
  1375. btdm_wakeup_request(false);
  1376. }
  1377. esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path)
  1378. {
  1379. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1380. return ESP_ERR_INVALID_STATE;
  1381. }
  1382. bredr_sco_datapath_set(data_path);
  1383. return ESP_OK;
  1384. }
  1385. esp_err_t esp_ble_scan_dupilcate_list_flush(void)
  1386. {
  1387. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1388. return ESP_ERR_INVALID_STATE;
  1389. }
  1390. btdm_controller_scan_duplicate_list_clear();
  1391. return ESP_OK;
  1392. }
  1393. #endif /* CONFIG_BT_ENABLED */