bt.c 62 KB

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