bt.c 59 KB

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