bt.c 60 KB

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