bt.c 62 KB

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