bt.c 59 KB

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