bt.c 62 KB

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