bt.c 62 KB

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