bt.c 59 KB

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