bt.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "sdkconfig.h"
  11. #include "esp_heap_caps.h"
  12. #include "esp_heap_caps_init.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/queue.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/portmacro.h"
  18. #include "esp_types.h"
  19. #include "esp_system.h"
  20. #include "esp_task.h"
  21. #include "riscv/interrupt.h"
  22. #include "esp_attr.h"
  23. #include "esp_phy_init.h"
  24. #include "esp_bt.h"
  25. #include "esp_err.h"
  26. #include "esp_log.h"
  27. #include "esp_pm.h"
  28. #include "esp_ipc.h"
  29. #include "driver/periph_ctrl.h"
  30. #include "soc/rtc.h"
  31. #include "soc/rtc_cntl_reg.h"
  32. #include "soc/soc_memory_layout.h"
  33. #include "esp32c3/clk.h"
  34. #include "esp_coexist_internal.h"
  35. #include "esp32c3/rom/rom_layout.h"
  36. #include "esp_timer.h"
  37. #include "esp_sleep.h"
  38. #if CONFIG_BT_ENABLED
  39. /* Macro definition
  40. ************************************************************************
  41. */
  42. #define BTDM_LOG_TAG "BTDM_INIT"
  43. #define BTDM_INIT_PERIOD (5000) /* ms */
  44. /* Low Power Clock Selection */
  45. #define BTDM_LPCLK_SEL_XTAL (0)
  46. #define BTDM_LPCLK_SEL_XTAL32K (1)
  47. #define BTDM_LPCLK_SEL_RTC_SLOW (2)
  48. #define BTDM_LPCLK_SEL_8M (3)
  49. // wakeup request sources
  50. enum {
  51. BTDM_ASYNC_WAKEUP_SRC_VHCI = 0,
  52. BTDM_ASYNC_WAKEUP_SRC_DISA,
  53. BTDM_ASYNC_WAKEUP_SRC_TMR,
  54. BTDM_ASYNC_WAKEUP_SRC_MAX,
  55. };
  56. // low power control struct
  57. typedef union {
  58. struct {
  59. uint32_t enable : 1; // whether low power mode is required
  60. uint32_t lpclk_sel : 2; // low power clock source
  61. uint32_t mac_bb_pd : 1; // whether hardware(MAC, BB) force-power-down is required during sleep
  62. uint32_t wakeup_timer_required : 1; // whether system timer is needed
  63. uint32_t no_light_sleep : 1; // do not allow system to enter light sleep after bluetooth is enabled
  64. uint32_t reserved : 26; // reserved
  65. };
  66. uint32_t val;
  67. } btdm_lpcntl_t;
  68. // low power control status
  69. typedef union {
  70. struct {
  71. uint32_t pm_lock_released : 1; // whether power management lock is released
  72. uint32_t mac_bb_pd : 1; // whether hardware(MAC, BB) is powered down
  73. uint32_t phy_enabled : 1; // whether phy is switched on
  74. uint32_t wakeup_timer_started : 1; // whether wakeup timer is started
  75. uint32_t reserved : 28; // reserved
  76. };
  77. uint32_t val;
  78. } btdm_lpstat_t;
  79. /* Sleep and wakeup interval control */
  80. #define BTDM_MIN_SLEEP_DURATION (24) // threshold of interval in half slots to allow to fall into modem sleep
  81. #define BTDM_MODEM_WAKE_UP_DELAY (8) // delay in half slots of modem wake up procedure, including re-enable PHY/RF
  82. #define BT_DEBUG(...)
  83. #define BT_API_CALL_CHECK(info, api_call, ret) \
  84. do{\
  85. esp_err_t __err = (api_call);\
  86. if ((ret) != __err) {\
  87. BT_DEBUG("%s %d %s ret=0x%X\n", __FUNCTION__, __LINE__, (info), __err);\
  88. return __err;\
  89. }\
  90. } while(0)
  91. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  92. #define OSI_VERSION 0x00010006
  93. #define OSI_MAGIC_VALUE 0xFADEBEAD
  94. /* Types definition
  95. ************************************************************************
  96. */
  97. /* vendor dependent signals to be posted to controller task */
  98. typedef enum {
  99. BTDM_VND_OL_SIG_WAKEUP_TMR = 0,
  100. BTDM_VND_OL_SIG_NUM,
  101. } btdm_vnd_ol_sig_t;
  102. /* prototype of function to handle vendor dependent signals */
  103. typedef void (* btdm_vnd_ol_task_func_t)(void *param);
  104. /* VHCI function interface */
  105. typedef struct vhci_host_callback {
  106. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  107. 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*/
  108. } vhci_host_callback_t;
  109. /* Dram region */
  110. typedef struct {
  111. esp_bt_mode_t mode;
  112. intptr_t start;
  113. intptr_t end;
  114. } btdm_dram_available_region_t;
  115. typedef void (* osi_intr_handler)(void);
  116. /* OSI function */
  117. struct osi_funcs_t {
  118. uint32_t _magic;
  119. uint32_t _version;
  120. void (*_interrupt_set)(int cpu_no, int intr_source, int interrupt_no, int interrpt_prio);
  121. void (*_interrupt_clear)(int interrupt_source, int interrupt_no);
  122. void (*_interrupt_handler_set)(int interrupt_no, intr_handler_t fn, void *arg);
  123. void (*_interrupt_disable)(void);
  124. void (*_interrupt_restore)(void);
  125. void (*_task_yield)(void);
  126. void (*_task_yield_from_isr)(void);
  127. void *(*_semphr_create)(uint32_t max, uint32_t init);
  128. void (*_semphr_delete)(void *semphr);
  129. int (*_semphr_take_from_isr)(void *semphr, void *hptw);
  130. int (*_semphr_give_from_isr)(void *semphr, void *hptw);
  131. int (*_semphr_take)(void *semphr, uint32_t block_time_ms);
  132. int (*_semphr_give)(void *semphr);
  133. void *(*_mutex_create)(void);
  134. void (*_mutex_delete)(void *mutex);
  135. int (*_mutex_lock)(void *mutex);
  136. int (*_mutex_unlock)(void *mutex);
  137. void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
  138. void (* _queue_delete)(void *queue);
  139. int (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
  140. int (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
  141. int (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
  142. int (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
  143. int (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  144. void (* _task_delete)(void *task_handle);
  145. bool (* _is_in_isr)(void);
  146. int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
  147. void *(* _malloc)(size_t size);
  148. void *(* _malloc_internal)(size_t size);
  149. void (* _free)(void *p);
  150. int (* _read_efuse_mac)(uint8_t mac[6]);
  151. void (* _srand)(unsigned int seed);
  152. int (* _rand)(void);
  153. uint32_t (* _btdm_lpcycles_2_hus)(uint32_t cycles, uint32_t *error_corr);
  154. uint32_t (* _btdm_hus_2_lpcycles)(uint32_t hus);
  155. bool (* _btdm_sleep_check_duration)(int32_t *slot_cnt);
  156. void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */
  157. void (* _btdm_sleep_enter_phase2)(void);
  158. void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */
  159. void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */
  160. void (* _btdm_sleep_exit_phase3)(void); /* called from task */
  161. void (* _coex_wifi_sleep_set)(bool sleep);
  162. int (* _coex_core_ble_conn_dyn_prio_get)(bool *low, bool *high);
  163. void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status);
  164. void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status);
  165. void (* _interrupt_on)(int intr_num);
  166. void (* _interrupt_off)(int intr_num);
  167. void (* _esp_hw_power_down)(void);
  168. void (* _esp_hw_power_up)(void);
  169. void (* _ets_backup_dma_copy)(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_rem);
  170. };
  171. /* External functions or values
  172. ************************************************************************
  173. */
  174. /* not for user call, so don't put to include file */
  175. /* OSI */
  176. extern int btdm_osi_funcs_register(void *osi_funcs);
  177. /* Initialise and De-initialise */
  178. extern int btdm_controller_init(esp_bt_controller_config_t *config_opts);
  179. extern void btdm_controller_deinit(void);
  180. extern int btdm_controller_enable(esp_bt_mode_t mode);
  181. extern void btdm_controller_disable(void);
  182. extern uint8_t btdm_controller_get_mode(void);
  183. extern const char *btdm_controller_get_compile_version(void);
  184. extern void btdm_rf_bb_init_phase2(void); // shall be called after PHY/RF is enabled
  185. /* Sleep */
  186. extern void btdm_controller_enable_sleep(bool enable);
  187. extern uint8_t btdm_controller_get_sleep_mode(void);
  188. extern bool btdm_power_state_active(void);
  189. extern void btdm_wakeup_request(void);
  190. extern void btdm_in_wakeup_requesting_set(bool in_wakeup_requesting);
  191. /* vendor dependent tasks to be posted and handled by controller task*/
  192. extern int btdm_vnd_offload_task_register(btdm_vnd_ol_sig_t sig, btdm_vnd_ol_task_func_t func);
  193. extern int btdm_vnd_offload_task_deregister(btdm_vnd_ol_sig_t sig);
  194. extern int btdm_vnd_offload_post_from_isr(btdm_vnd_ol_sig_t sig, void *param, bool need_yield);
  195. extern int btdm_vnd_offload_post(btdm_vnd_ol_sig_t sig, void *param);
  196. /* Low Power Clock */
  197. extern bool btdm_lpclk_select_src(uint32_t sel);
  198. extern bool btdm_lpclk_set_div(uint32_t div);
  199. extern int btdm_hci_tl_io_event_post(int event);
  200. /* VHCI */
  201. extern bool API_vhci_host_check_send_available(void);
  202. extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
  203. extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback);
  204. /* TX power */
  205. extern int ble_txpwr_set(int power_type, int power_level);
  206. extern int ble_txpwr_get(int power_type);
  207. extern uint16_t l2c_ble_link_get_tx_buf_num(void);
  208. extern int coex_core_ble_conn_dyn_prio_get(bool *low, bool *high);
  209. extern bool btdm_deep_sleep_mem_init(void);
  210. extern void btdm_deep_sleep_mem_deinit(void);
  211. extern void btdm_ble_power_down_dma_copy(bool copy);
  212. extern uint8_t btdm_sleep_clock_sync(void);
  213. #if CONFIG_MAC_BB_PD
  214. extern void esp_mac_bb_power_down(void);
  215. extern void esp_mac_bb_power_up(void);
  216. extern void ets_backup_dma_copy(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem);
  217. #endif
  218. extern char _bss_start_btdm;
  219. extern char _bss_end_btdm;
  220. extern char _data_start_btdm;
  221. extern char _data_end_btdm;
  222. extern uint32_t _data_start_btdm_rom;
  223. extern uint32_t _data_end_btdm_rom;
  224. extern uint32_t _bt_bss_start;
  225. extern uint32_t _bt_bss_end;
  226. extern uint32_t _btdm_bss_start;
  227. extern uint32_t _btdm_bss_end;
  228. extern uint32_t _bt_data_start;
  229. extern uint32_t _bt_data_end;
  230. extern uint32_t _btdm_data_start;
  231. extern uint32_t _btdm_data_end;
  232. extern char _bt_tmp_bss_start;
  233. extern char _bt_tmp_bss_end;
  234. /* Local Function Declare
  235. *********************************************************************
  236. */
  237. static void interrupt_set_wrapper(int cpu_no, int intr_source, int intr_num, int intr_prio);
  238. static void interrupt_clear_wrapper(int intr_source, int intr_num);
  239. static void interrupt_handler_set_wrapper(int n, intr_handler_t fn, void *arg);
  240. static void IRAM_ATTR interrupt_disable(void);
  241. static void IRAM_ATTR interrupt_restore(void);
  242. static void IRAM_ATTR task_yield_from_isr(void);
  243. static void *semphr_create_wrapper(uint32_t max, uint32_t init);
  244. static void semphr_delete_wrapper(void *semphr);
  245. static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
  246. static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
  247. static int semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
  248. static int semphr_give_wrapper(void *semphr);
  249. static void *mutex_create_wrapper(void);
  250. static void mutex_delete_wrapper(void *mutex);
  251. static int mutex_lock_wrapper(void *mutex);
  252. static int mutex_unlock_wrapper(void *mutex);
  253. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
  254. static void queue_delete_wrapper(void *queue);
  255. static int queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms);
  256. static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw);
  257. static int queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
  258. static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw);
  259. static int 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);
  260. static void task_delete_wrapper(void *task_handle);
  261. static bool IRAM_ATTR is_in_isr_wrapper(void);
  262. static void *malloc_internal_wrapper(size_t size);
  263. static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
  264. static void IRAM_ATTR srand_wrapper(unsigned int seed);
  265. static int IRAM_ATTR rand_wrapper(void);
  266. static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles, uint32_t *error_corr);
  267. static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t hus);
  268. static bool IRAM_ATTR btdm_sleep_check_duration(int32_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 void coex_wifi_sleep_set_hook(bool sleep);
  273. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status);
  274. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status);
  275. static void interrupt_on_wrapper(int intr_num);
  276. static void interrupt_off_wrapper(int intr_num);
  277. static void btdm_hw_mac_power_up_wrapper(void);
  278. static void btdm_hw_mac_power_down_wrapper(void);
  279. static void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem);
  280. static void btdm_slp_tmr_callback(void *arg);
  281. /* Local variable definition
  282. ***************************************************************************
  283. */
  284. /* OSI funcs */
  285. static const struct osi_funcs_t osi_funcs_ro = {
  286. ._magic = OSI_MAGIC_VALUE,
  287. ._version = OSI_VERSION,
  288. ._interrupt_set = interrupt_set_wrapper,
  289. ._interrupt_clear = interrupt_clear_wrapper,
  290. ._interrupt_handler_set = interrupt_handler_set_wrapper,
  291. ._interrupt_disable = interrupt_disable,
  292. ._interrupt_restore = interrupt_restore,
  293. ._task_yield = vPortYield,
  294. ._task_yield_from_isr = task_yield_from_isr,
  295. ._semphr_create = semphr_create_wrapper,
  296. ._semphr_delete = semphr_delete_wrapper,
  297. ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
  298. ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
  299. ._semphr_take = semphr_take_wrapper,
  300. ._semphr_give = semphr_give_wrapper,
  301. ._mutex_create = mutex_create_wrapper,
  302. ._mutex_delete = mutex_delete_wrapper,
  303. ._mutex_lock = mutex_lock_wrapper,
  304. ._mutex_unlock = mutex_unlock_wrapper,
  305. ._queue_create = queue_create_wrapper,
  306. ._queue_delete = queue_delete_wrapper,
  307. ._queue_send = queue_send_wrapper,
  308. ._queue_send_from_isr = queue_send_from_isr_wrapper,
  309. ._queue_recv = queue_recv_wrapper,
  310. ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
  311. ._task_create = task_create_wrapper,
  312. ._task_delete = task_delete_wrapper,
  313. ._is_in_isr = is_in_isr_wrapper,
  314. ._cause_sw_intr_to_core = NULL,
  315. ._malloc = malloc,
  316. ._malloc_internal = malloc_internal_wrapper,
  317. ._free = free,
  318. ._read_efuse_mac = read_mac_wrapper,
  319. ._srand = srand_wrapper,
  320. ._rand = rand_wrapper,
  321. ._btdm_lpcycles_2_hus = btdm_lpcycles_2_hus,
  322. ._btdm_hus_2_lpcycles = btdm_hus_2_lpcycles,
  323. ._btdm_sleep_check_duration = btdm_sleep_check_duration,
  324. ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper,
  325. ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper,
  326. ._btdm_sleep_exit_phase1 = NULL,
  327. ._btdm_sleep_exit_phase2 = NULL,
  328. ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper,
  329. ._coex_wifi_sleep_set = coex_wifi_sleep_set_hook,
  330. ._coex_core_ble_conn_dyn_prio_get = coex_core_ble_conn_dyn_prio_get,
  331. ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
  332. ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
  333. ._interrupt_on = interrupt_on_wrapper,
  334. ._interrupt_off = interrupt_off_wrapper,
  335. ._esp_hw_power_down = btdm_hw_mac_power_down_wrapper,
  336. ._esp_hw_power_up = btdm_hw_mac_power_up_wrapper,
  337. ._ets_backup_dma_copy = btdm_backup_dma_copy_wrapper,
  338. };
  339. static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
  340. /* Static variable declare */
  341. static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  342. static DRAM_ATTR portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
  343. // low power control struct
  344. static DRAM_ATTR btdm_lpcntl_t s_lp_cntl;
  345. // low power status struct
  346. static DRAM_ATTR btdm_lpstat_t s_lp_stat;
  347. // measured average low power clock period in micro seconds
  348. static DRAM_ATTR uint32_t btdm_lpcycle_us = 0;
  349. // number of fractional bit for btdm_lpcycle_us
  350. static DRAM_ATTR uint8_t btdm_lpcycle_us_frac = 0;
  351. // semaphore used for blocking VHCI API to wait for controller to wake up
  352. static DRAM_ATTR QueueHandle_t s_wakeup_req_sem = NULL;
  353. // wakeup timer
  354. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr;
  355. #ifdef CONFIG_PM_ENABLE
  356. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock;
  357. // pm_lock to prevent light sleep due to incompatibility currently
  358. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock;
  359. #endif
  360. void IRAM_ATTR btdm_hw_mac_power_down_wrapper(void)
  361. {
  362. #if CONFIG_MAC_BB_PD
  363. // Bluetooth module power down
  364. SET_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_BT_FORCE_ISO);
  365. SET_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_BT_FORCE_PD);
  366. esp_mac_bb_power_down();
  367. #endif
  368. }
  369. void IRAM_ATTR btdm_hw_mac_power_up_wrapper(void)
  370. {
  371. #if CONFIG_MAC_BB_PD
  372. // Bluetooth module power up
  373. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_BT_FORCE_PD);
  374. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_BT_FORCE_ISO);
  375. esp_mac_bb_power_up();
  376. #endif
  377. }
  378. static inline void esp_bt_power_domain_on(void)
  379. {
  380. // Bluetooth module power up
  381. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_BT_FORCE_PD);
  382. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_BT_FORCE_ISO);
  383. esp_wifi_bt_power_domain_on();
  384. }
  385. static inline void esp_bt_power_domain_off(void)
  386. {
  387. // Bluetooth module power down
  388. SET_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_BT_FORCE_ISO);
  389. SET_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_BT_FORCE_PD);
  390. esp_wifi_bt_power_domain_off();
  391. }
  392. void IRAM_ATTR btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem)
  393. {
  394. #if CONFIG_MAC_BB_PD
  395. ets_backup_dma_copy(reg, mem_addr, num, to_mem);
  396. #endif
  397. }
  398. static void interrupt_set_wrapper(int cpu_no, int intr_source, int intr_num, int intr_prio)
  399. {
  400. intr_matrix_route(intr_source, intr_num);
  401. esprv_intc_int_set_priority(intr_num, intr_prio);
  402. //esprv_intc_int_enable_level(1 << intr_num);
  403. esprv_intc_int_set_type(intr_num, 0);
  404. }
  405. static void interrupt_clear_wrapper(int intr_source, int intr_num)
  406. {
  407. }
  408. static void interrupt_handler_set_wrapper(int n, intr_handler_t fn, void *arg)
  409. {
  410. intr_handler_set(n, fn, arg);
  411. }
  412. static void interrupt_on_wrapper(int intr_num)
  413. {
  414. esprv_intc_int_enable(1 << intr_num);
  415. }
  416. static void interrupt_off_wrapper(int intr_num)
  417. {
  418. esprv_intc_int_disable(1<<intr_num);
  419. }
  420. static void IRAM_ATTR interrupt_disable(void)
  421. {
  422. if (xPortInIsrContext()) {
  423. portENTER_CRITICAL_ISR(&global_int_mux);
  424. } else {
  425. portENTER_CRITICAL(&global_int_mux);
  426. }
  427. }
  428. static void IRAM_ATTR interrupt_restore(void)
  429. {
  430. if (xPortInIsrContext()) {
  431. portEXIT_CRITICAL_ISR(&global_int_mux);
  432. } else {
  433. portEXIT_CRITICAL(&global_int_mux);
  434. }
  435. }
  436. static void IRAM_ATTR task_yield_from_isr(void)
  437. {
  438. portYIELD_FROM_ISR();
  439. }
  440. static void *semphr_create_wrapper(uint32_t max, uint32_t init)
  441. {
  442. return (void *)xSemaphoreCreateCounting(max, init);
  443. }
  444. static void semphr_delete_wrapper(void *semphr)
  445. {
  446. vSemaphoreDelete(semphr);
  447. }
  448. static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  449. {
  450. return (int)xSemaphoreTakeFromISR(semphr, hptw);
  451. }
  452. static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  453. {
  454. return (int)xSemaphoreGiveFromISR(semphr, hptw);
  455. }
  456. static int semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
  457. {
  458. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  459. return (int)xSemaphoreTake(semphr, portMAX_DELAY);
  460. } else {
  461. return (int)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
  462. }
  463. }
  464. static int semphr_give_wrapper(void *semphr)
  465. {
  466. return (int)xSemaphoreGive(semphr);
  467. }
  468. static void *mutex_create_wrapper(void)
  469. {
  470. return (void *)xSemaphoreCreateMutex();
  471. }
  472. static void mutex_delete_wrapper(void *mutex)
  473. {
  474. vSemaphoreDelete(mutex);
  475. }
  476. static int mutex_lock_wrapper(void *mutex)
  477. {
  478. return (int)xSemaphoreTake(mutex, portMAX_DELAY);
  479. }
  480. static int mutex_unlock_wrapper(void *mutex)
  481. {
  482. return (int)xSemaphoreGive(mutex);
  483. }
  484. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
  485. {
  486. return (void *)xQueueCreate(queue_len, item_size);
  487. }
  488. static void queue_delete_wrapper(void *queue)
  489. {
  490. vQueueDelete(queue);
  491. }
  492. static int queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms)
  493. {
  494. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  495. return (int)xQueueSend(queue, item, portMAX_DELAY);
  496. } else {
  497. return (int)xQueueSend(queue, item, block_time_ms / portTICK_PERIOD_MS);
  498. }
  499. }
  500. static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
  501. {
  502. return (int)xQueueSendFromISR(queue, item, hptw);
  503. }
  504. static int queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms)
  505. {
  506. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  507. return (int)xQueueReceive(queue, item, portMAX_DELAY);
  508. } else {
  509. return (int)xQueueReceive(queue, item, block_time_ms / portTICK_PERIOD_MS);
  510. }
  511. }
  512. static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw)
  513. {
  514. return (int)xQueueReceiveFromISR(queue, item, hptw);
  515. }
  516. static int 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)
  517. {
  518. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  519. }
  520. static void task_delete_wrapper(void *task_handle)
  521. {
  522. vTaskDelete(task_handle);
  523. }
  524. static bool IRAM_ATTR is_in_isr_wrapper(void)
  525. {
  526. return (bool)xPortInIsrContext();
  527. }
  528. static void *malloc_internal_wrapper(size_t size)
  529. {
  530. return heap_caps_malloc(size, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL|MALLOC_CAP_DMA);
  531. }
  532. static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
  533. {
  534. int ret = esp_read_mac(mac, ESP_MAC_BT);
  535. ESP_LOGI(BTDM_LOG_TAG, "Bluetooth MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  536. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  537. return ret;
  538. }
  539. static void IRAM_ATTR srand_wrapper(unsigned int seed)
  540. {
  541. /* empty function */
  542. }
  543. static int IRAM_ATTR rand_wrapper(void)
  544. {
  545. return (int)esp_random();
  546. }
  547. static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles, uint32_t *error_corr)
  548. {
  549. uint64_t local_error_corr = (error_corr == NULL) ? 0 : (uint64_t)(*error_corr);
  550. uint64_t res = (uint64_t)btdm_lpcycle_us * cycles * 2;
  551. local_error_corr += res;
  552. res = (local_error_corr >> btdm_lpcycle_us_frac);
  553. local_error_corr -= (res << btdm_lpcycle_us_frac);
  554. if (error_corr) {
  555. *error_corr = (uint32_t) local_error_corr;
  556. }
  557. return (uint32_t)res;
  558. }
  559. /*
  560. * @brief Converts a duration in half us into a number of low power clock cycles.
  561. */
  562. static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t hus)
  563. {
  564. // The number of sleep duration(us) should not lead to overflow. Thrs: 100s
  565. // Compute the sleep duration in us to low power clock cycles, with calibration result applied
  566. // clock measurement is conducted
  567. uint64_t cycles = ((uint64_t)(hus) << btdm_lpcycle_us_frac) / btdm_lpcycle_us;
  568. cycles >>= 1;
  569. return (uint32_t)cycles;
  570. }
  571. static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *half_slot_cnt)
  572. {
  573. if (*half_slot_cnt < BTDM_MIN_SLEEP_DURATION) {
  574. return false;
  575. }
  576. /* wake up in advance considering the delay in enabling PHY/RF */
  577. *half_slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY;
  578. return true;
  579. }
  580. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
  581. {
  582. if (s_lp_cntl.wakeup_timer_required == 0) {
  583. return;
  584. }
  585. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  586. uint32_t us_to_sleep = btdm_lpcycles_2_hus(lpcycles, NULL) >> 1;
  587. #define BTDM_MIN_TIMER_UNCERTAINTY_US (1800)
  588. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  589. // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift
  590. // and set the timer in advance
  591. uint32_t uncertainty = (us_to_sleep >> 11);
  592. if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) {
  593. uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US;
  594. }
  595. assert (s_lp_stat.wakeup_timer_started == 0);
  596. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) == ESP_OK) {
  597. s_lp_stat.wakeup_timer_started = 1;
  598. } else {
  599. ESP_LOGE(BTDM_LOG_TAG, "timer start failed");
  600. assert(0);
  601. }
  602. }
  603. static void btdm_sleep_enter_phase2_wrapper(void)
  604. {
  605. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  606. if (s_lp_stat.phy_enabled) {
  607. esp_phy_disable();
  608. s_lp_stat.phy_enabled = 0;
  609. } else {
  610. assert(0);
  611. }
  612. if (s_lp_stat.pm_lock_released == 0) {
  613. #ifdef CONFIG_PM_ENABLE
  614. esp_pm_lock_release(s_pm_lock);
  615. #endif
  616. s_lp_stat.pm_lock_released = 1;
  617. }
  618. }
  619. }
  620. static void btdm_sleep_exit_phase3_wrapper(void)
  621. {
  622. #ifdef CONFIG_PM_ENABLE
  623. // If BT wakeup before esp timer coming due to timer task have no chance to run.
  624. // Then we will not run into `btdm_sleep_exit_phase0` and acquire PM lock,
  625. // Do it again here to fix this issue.
  626. if (s_lp_stat.pm_lock_released) {
  627. esp_pm_lock_acquire(s_pm_lock);
  628. s_lp_stat.pm_lock_released = 0;
  629. }
  630. #endif
  631. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  632. if (s_lp_stat.phy_enabled == 0) {
  633. esp_phy_enable();
  634. s_lp_stat.phy_enabled = 1;
  635. }
  636. }
  637. // If BT wakeup before esp timer coming due to timer task have no chance to run.
  638. // Then we will not run into `btdm_sleep_exit_phase0` and stop esp timer,
  639. // Do it again here to fix this issue.
  640. if (s_lp_cntl.wakeup_timer_required && s_lp_stat.wakeup_timer_started) {
  641. esp_timer_stop(s_btdm_slp_tmr);
  642. s_lp_stat.wakeup_timer_started = 0;
  643. }
  644. // wait for the sleep state to change
  645. // the procedure duration is at micro-second level or less
  646. while (btdm_sleep_clock_sync()) {
  647. ;
  648. }
  649. }
  650. static void IRAM_ATTR btdm_sleep_exit_phase0(void *param)
  651. {
  652. assert(s_lp_cntl.enable == 1);
  653. #ifdef CONFIG_PM_ENABLE
  654. if (s_lp_stat.pm_lock_released) {
  655. esp_pm_lock_acquire(s_pm_lock);
  656. s_lp_stat.pm_lock_released = 0;
  657. }
  658. #endif
  659. int event = (int) param;
  660. if (event == BTDM_ASYNC_WAKEUP_SRC_VHCI || event == BTDM_ASYNC_WAKEUP_SRC_DISA) {
  661. btdm_wakeup_request();
  662. }
  663. if (s_lp_cntl.wakeup_timer_required && s_lp_stat.wakeup_timer_started) {
  664. esp_timer_stop(s_btdm_slp_tmr);
  665. s_lp_stat.wakeup_timer_started = 0;
  666. }
  667. if (event == BTDM_ASYNC_WAKEUP_SRC_VHCI || event == BTDM_ASYNC_WAKEUP_SRC_DISA) {
  668. semphr_give_wrapper(s_wakeup_req_sem);
  669. }
  670. }
  671. static void IRAM_ATTR btdm_slp_tmr_callback(void *arg)
  672. {
  673. #ifdef CONFIG_PM_ENABLE
  674. btdm_vnd_offload_post(BTDM_VND_OL_SIG_WAKEUP_TMR, (void *)BTDM_ASYNC_WAKEUP_SRC_TMR);
  675. #endif
  676. }
  677. static bool async_wakeup_request(int event)
  678. {
  679. if (s_lp_cntl.enable == 0) {
  680. return false;
  681. }
  682. bool do_wakeup_request = false;
  683. switch (event) {
  684. case BTDM_ASYNC_WAKEUP_SRC_VHCI:
  685. case BTDM_ASYNC_WAKEUP_SRC_DISA:
  686. btdm_in_wakeup_requesting_set(true);
  687. if (!btdm_power_state_active()) {
  688. btdm_vnd_offload_post(BTDM_VND_OL_SIG_WAKEUP_TMR, (void *)event);
  689. do_wakeup_request = true;
  690. semphr_take_wrapper(s_wakeup_req_sem, OSI_FUNCS_TIME_BLOCKING);
  691. }
  692. break;
  693. default:
  694. break;
  695. }
  696. return do_wakeup_request;
  697. }
  698. static void async_wakeup_request_end(int event)
  699. {
  700. if (s_lp_cntl.enable == 0) {
  701. return;
  702. }
  703. bool allow_to_sleep;
  704. switch (event) {
  705. case BTDM_ASYNC_WAKEUP_SRC_VHCI:
  706. case BTDM_ASYNC_WAKEUP_SRC_DISA:
  707. allow_to_sleep = true;
  708. break;
  709. default:
  710. allow_to_sleep = true;
  711. break;
  712. }
  713. if (allow_to_sleep) {
  714. btdm_in_wakeup_requesting_set(false);
  715. }
  716. return;
  717. }
  718. static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
  719. {
  720. #if CONFIG_SW_COEXIST_ENABLE
  721. coex_schm_status_bit_set(type, status);
  722. #endif
  723. }
  724. static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
  725. {
  726. #if CONFIG_SW_COEXIST_ENABLE
  727. coex_schm_status_bit_clear(type, status);
  728. #endif
  729. }
  730. bool esp_vhci_host_check_send_available(void)
  731. {
  732. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  733. return false;
  734. }
  735. return API_vhci_host_check_send_available();
  736. }
  737. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  738. {
  739. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  740. return;
  741. }
  742. async_wakeup_request(BTDM_ASYNC_WAKEUP_SRC_VHCI);
  743. API_vhci_host_send_packet(data, len);
  744. async_wakeup_request_end(BTDM_ASYNC_WAKEUP_SRC_VHCI);
  745. }
  746. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  747. {
  748. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  749. return ESP_FAIL;
  750. }
  751. return API_vhci_host_register_callback((const vhci_host_callback_t *)callback) == 0 ? ESP_OK : ESP_FAIL;
  752. }
  753. static void btdm_controller_mem_init(void)
  754. {
  755. extern void btdm_controller_rom_data_init(void );
  756. btdm_controller_rom_data_init();
  757. }
  758. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  759. {
  760. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  761. return ESP_OK;
  762. }
  763. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  764. {
  765. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  766. return ESP_OK;
  767. }
  768. static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end)
  769. {
  770. int ret = heap_caps_add_region(start, end);
  771. /* heap_caps_add_region() returns ESP_ERR_INVALID_SIZE if the memory region is
  772. * is too small to fit a heap. This cannot be termed as a fatal error and hence
  773. * we replace it by ESP_OK
  774. */
  775. if (ret == ESP_ERR_INVALID_SIZE) {
  776. return ESP_OK;
  777. }
  778. return ret;
  779. }
  780. // release wifi and coex memory, free about 720 bytes,
  781. void esp_release_wifi_and_coex_mem(void)
  782. {
  783. ESP_ERROR_CHECK(try_heap_caps_add_region((intptr_t)ets_rom_layout_p->dram_start_coexist, (intptr_t)ets_rom_layout_p->dram_end_pp));
  784. ESP_ERROR_CHECK(try_heap_caps_add_region((intptr_t)ets_rom_layout_p->data_start_interface_coexist,(intptr_t)ets_rom_layout_p->bss_end_interface_pp));
  785. }
  786. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  787. static void IRAM_ATTR btdm_mac_bb_power_down_cb(void)
  788. {
  789. if (s_lp_cntl.mac_bb_pd && s_lp_stat.mac_bb_pd == 0) {
  790. #if (CONFIG_MAC_BB_PD)
  791. btdm_ble_power_down_dma_copy(true);
  792. #endif
  793. s_lp_stat.mac_bb_pd = 1;
  794. }
  795. }
  796. static void IRAM_ATTR btdm_mac_bb_power_up_cb(void)
  797. {
  798. #if (CONFIG_MAC_BB_PD)
  799. if (s_lp_cntl.mac_bb_pd && s_lp_stat.mac_bb_pd) {
  800. btdm_ble_power_down_dma_copy(false);
  801. s_lp_stat.mac_bb_pd = 0;
  802. }
  803. #endif
  804. }
  805. #endif
  806. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
  807. {
  808. esp_err_t err = ESP_FAIL;
  809. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  810. return ESP_ERR_INVALID_STATE;
  811. }
  812. if (cfg == NULL) {
  813. return ESP_ERR_INVALID_ARG;
  814. }
  815. if (cfg->controller_task_prio != ESP_TASK_BT_CONTROLLER_PRIO
  816. || cfg->controller_task_stack_size < ESP_TASK_BT_CONTROLLER_STACK) {
  817. ESP_LOGE(BTDM_LOG_TAG, "Invalid controller task prioriy or stack size");
  818. return ESP_ERR_INVALID_ARG;
  819. }
  820. if (cfg->bluetooth_mode != ESP_BT_MODE_BLE) {
  821. ESP_LOGE(BTDM_LOG_TAG, "%s controller only support BLE only mode", __func__);
  822. return ESP_ERR_NOT_SUPPORTED;
  823. }
  824. if (cfg->bluetooth_mode & ESP_BT_MODE_BLE) {
  825. if ((cfg->ble_max_act <= 0) || (cfg->ble_max_act > BT_CTRL_BLE_MAX_ACT_LIMIT)) {
  826. ESP_LOGE(BTDM_LOG_TAG, "Invalid value of ble_max_act");
  827. return ESP_ERR_INVALID_ARG;
  828. }
  829. }
  830. if (cfg->sleep_mode == ESP_BT_SLEEP_MODE_1) {
  831. if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_NONE) {
  832. ESP_LOGE(BTDM_LOG_TAG, "SLEEP_MODE_1 enabled but sleep clock not configured");
  833. return ESP_ERR_INVALID_ARG;
  834. }
  835. }
  836. // overwrite some parameters
  837. cfg->magic = ESP_BT_CTRL_CONFIG_MAGIC_VAL;
  838. #if CONFIG_MAC_BB_PD
  839. esp_mac_bb_pd_mem_init();
  840. #endif
  841. esp_bt_power_domain_on();
  842. btdm_controller_mem_init();
  843. #if CONFIG_MAC_BB_PD
  844. if (esp_register_mac_bb_pd_callback(btdm_mac_bb_power_down_cb) != 0) {
  845. err = ESP_ERR_INVALID_ARG;
  846. goto error;
  847. }
  848. if (esp_register_mac_bb_pu_callback(btdm_mac_bb_power_up_cb) != 0) {
  849. err = ESP_ERR_INVALID_ARG;
  850. goto error;
  851. }
  852. #endif
  853. osi_funcs_p = (struct osi_funcs_t *)malloc_internal_wrapper(sizeof(struct osi_funcs_t));
  854. if (osi_funcs_p == NULL) {
  855. return ESP_ERR_NO_MEM;
  856. }
  857. memcpy(osi_funcs_p, &osi_funcs_ro, sizeof(struct osi_funcs_t));
  858. if (btdm_osi_funcs_register(osi_funcs_p) != 0) {
  859. return ESP_ERR_INVALID_ARG;
  860. }
  861. ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version());
  862. // init low-power control resources
  863. do {
  864. // set default values for global states or resources
  865. s_lp_stat.val = 0;
  866. s_lp_cntl.val = 0;
  867. s_wakeup_req_sem = NULL;
  868. s_btdm_slp_tmr = NULL;
  869. // configure and initialize resources
  870. s_lp_cntl.enable = (cfg->sleep_mode == ESP_BT_SLEEP_MODE_1) ? 1 : 0;
  871. s_lp_cntl.no_light_sleep = 1;
  872. if (s_lp_cntl.enable) {
  873. #if (CONFIG_MAC_BB_PD)
  874. if (!btdm_deep_sleep_mem_init()) {
  875. err = ESP_ERR_NO_MEM;
  876. goto error;
  877. }
  878. s_lp_cntl.mac_bb_pd = 1;
  879. #endif
  880. #ifdef CONFIG_PM_ENABLE
  881. s_lp_cntl.wakeup_timer_required = 1;
  882. #endif
  883. // async wakeup semaphore for VHCI
  884. s_wakeup_req_sem = semphr_create_wrapper(1, 0);
  885. if (s_wakeup_req_sem == NULL) {
  886. err = ESP_ERR_NO_MEM;
  887. goto error;
  888. }
  889. btdm_vnd_offload_task_register(BTDM_VND_OL_SIG_WAKEUP_TMR, btdm_sleep_exit_phase0);
  890. }
  891. if (s_lp_cntl.wakeup_timer_required) {
  892. esp_timer_create_args_t create_args = {
  893. .callback = btdm_slp_tmr_callback,
  894. .arg = NULL,
  895. .name = "btSlp",
  896. };
  897. if ((err = esp_timer_create(&create_args, &s_btdm_slp_tmr)) != ESP_OK) {
  898. goto error;
  899. }
  900. }
  901. // set default bluetooth sleep clock cycle and its fractional bits
  902. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  903. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  904. // set default bluetooth sleep clock source
  905. s_lp_cntl.lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
  906. #if CONFIG_BT_CTRL_LPCLK_SEL_EXT_32K_XTAL
  907. // check whether or not EXT_CRYS is working
  908. if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_32K_XTAL) {
  909. s_lp_cntl.lpclk_sel = BTDM_LPCLK_SEL_XTAL32K; // External 32 kHz XTAL
  910. s_lp_cntl.no_light_sleep = 0;
  911. } else {
  912. ESP_LOGW(BTDM_LOG_TAG, "32.768kHz XTAL not detected, fall back to main XTAL as Bluetooth sleep clock\n"
  913. "light sleep mode will not be able to apply when bluetooth is enabled");
  914. }
  915. #elif (CONFIG_BT_CTRL_LPCLK_SEL_RTC_SLOW)
  916. // check whether or not EXT_CRYS is working
  917. if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_RTC) {
  918. s_lp_cntl.lpclk_sel = BTDM_LPCLK_SEL_RTC_SLOW; // Internal 150 kHz RC oscillator
  919. ESP_LOGW(BTDM_LOG_TAG, "Internal 150kHz RC osciallator. The accuracy of this clock is a lot larger than 500ppm which is "
  920. "required in Bluetooth communication, so don't select this option in scenarios such as BLE connection state.");
  921. } else {
  922. ESP_LOGW(BT_LOG_TAG, "Internal 150kHz RC oscillator not detected.");
  923. assert(0);
  924. }
  925. #else
  926. s_lp_cntl.no_light_sleep = 1;
  927. #endif
  928. bool select_src_ret __attribute__((unused));
  929. bool set_div_ret __attribute__((unused));
  930. if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_XTAL) {
  931. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL);
  932. set_div_ret = btdm_lpclk_set_div(rtc_clk_xtal_freq_get() * 2);
  933. assert(select_src_ret && set_div_ret);
  934. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  935. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  936. } else if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_XTAL32K) {
  937. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K);
  938. set_div_ret = btdm_lpclk_set_div(0);
  939. assert(select_src_ret && set_div_ret);
  940. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  941. btdm_lpcycle_us = (RTC_CLK_CAL_FRACT > 15) ? (1000000 << (RTC_CLK_CAL_FRACT - 15)) :
  942. (1000000 >> (15 - RTC_CLK_CAL_FRACT));
  943. assert(btdm_lpcycle_us != 0);
  944. } else if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_RTC_SLOW) {
  945. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_RTC_SLOW);
  946. set_div_ret = btdm_lpclk_set_div(0);
  947. assert(select_src_ret && set_div_ret);
  948. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  949. btdm_lpcycle_us = esp_clk_slowclk_cal_get();
  950. } else {
  951. err = ESP_ERR_INVALID_ARG;
  952. goto error;
  953. }
  954. #ifdef CONFIG_PM_ENABLE
  955. if (s_lp_cntl.no_light_sleep) {
  956. if ((err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btLS", &s_light_sleep_pm_lock)) != ESP_OK) {
  957. err = ESP_ERR_NO_MEM;
  958. goto error;
  959. }
  960. }
  961. if ((err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock)) != ESP_OK) {
  962. err = ESP_ERR_NO_MEM;
  963. goto error;
  964. } else {
  965. s_lp_stat.pm_lock_released = 1;
  966. }
  967. #endif
  968. } while (0);
  969. #if CONFIG_SW_COEXIST_ENABLE
  970. coex_init();
  971. #endif
  972. periph_module_enable(PERIPH_BT_MODULE);
  973. esp_phy_enable();
  974. s_lp_stat.phy_enabled = 1;
  975. if (btdm_controller_init(cfg) != 0) {
  976. err = ESP_ERR_NO_MEM;
  977. goto error;
  978. }
  979. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  980. return ESP_OK;
  981. error:
  982. if (s_lp_stat.phy_enabled) {
  983. esp_phy_disable();
  984. s_lp_stat.phy_enabled = 0;
  985. }
  986. do {
  987. // deinit low power control resources
  988. #ifdef CONFIG_PM_ENABLE
  989. if (s_lp_cntl.no_light_sleep) {
  990. if (s_light_sleep_pm_lock != NULL) {
  991. esp_pm_lock_delete(s_light_sleep_pm_lock);
  992. s_light_sleep_pm_lock = NULL;
  993. }
  994. }
  995. if (s_pm_lock != NULL) {
  996. esp_pm_lock_delete(s_pm_lock);
  997. s_pm_lock = NULL;
  998. s_lp_stat.pm_lock_released = 0;
  999. }
  1000. #endif
  1001. if (s_lp_cntl.wakeup_timer_required && s_btdm_slp_tmr != NULL) {
  1002. esp_timer_delete(s_btdm_slp_tmr);
  1003. s_btdm_slp_tmr = NULL;
  1004. }
  1005. #if (CONFIG_MAC_BB_PD)
  1006. if (s_lp_cntl.mac_bb_pd) {
  1007. btdm_deep_sleep_mem_deinit();
  1008. s_lp_cntl.mac_bb_pd = 0;
  1009. }
  1010. #endif
  1011. if (s_lp_cntl.enable) {
  1012. btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR);
  1013. if (s_wakeup_req_sem != NULL) {
  1014. semphr_delete_wrapper(s_wakeup_req_sem);
  1015. s_wakeup_req_sem = NULL;
  1016. }
  1017. }
  1018. } while (0);
  1019. #if CONFIG_MAC_BB_PD
  1020. esp_unregister_mac_bb_pd_callback(btdm_mac_bb_power_down_cb);
  1021. esp_unregister_mac_bb_pu_callback(btdm_mac_bb_power_up_cb);
  1022. #endif
  1023. if (osi_funcs_p != NULL) {
  1024. free(osi_funcs_p);
  1025. osi_funcs_p = NULL;
  1026. }
  1027. return err;
  1028. }
  1029. esp_err_t esp_bt_controller_deinit(void)
  1030. {
  1031. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1032. return ESP_ERR_INVALID_STATE;
  1033. }
  1034. btdm_controller_deinit();
  1035. periph_module_disable(PERIPH_BT_MODULE);
  1036. if (s_lp_stat.phy_enabled) {
  1037. esp_phy_disable();
  1038. s_lp_stat.phy_enabled = 0;
  1039. } else {
  1040. assert(0);
  1041. }
  1042. // deinit low power control resources
  1043. do {
  1044. #if (CONFIG_MAC_BB_PD)
  1045. btdm_deep_sleep_mem_deinit();
  1046. #endif
  1047. #ifdef CONFIG_PM_ENABLE
  1048. if (s_lp_cntl.no_light_sleep) {
  1049. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1050. s_light_sleep_pm_lock = NULL;
  1051. }
  1052. esp_pm_lock_delete(s_pm_lock);
  1053. s_pm_lock = NULL;
  1054. s_lp_stat.pm_lock_released = 0;
  1055. #endif
  1056. if (s_lp_cntl.wakeup_timer_required) {
  1057. if (s_lp_stat.wakeup_timer_started) {
  1058. esp_timer_stop(s_btdm_slp_tmr);
  1059. }
  1060. s_lp_stat.wakeup_timer_started = 0;
  1061. esp_timer_delete(s_btdm_slp_tmr);
  1062. s_btdm_slp_tmr = NULL;
  1063. }
  1064. if (s_lp_cntl.enable) {
  1065. btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR);
  1066. semphr_delete_wrapper(s_wakeup_req_sem);
  1067. s_wakeup_req_sem = NULL;
  1068. }
  1069. } while (0);
  1070. #if CONFIG_MAC_BB_PD
  1071. esp_unregister_mac_bb_pd_callback(btdm_mac_bb_power_down_cb);
  1072. esp_unregister_mac_bb_pu_callback(btdm_mac_bb_power_up_cb);
  1073. #endif
  1074. esp_bt_power_domain_off();
  1075. free(osi_funcs_p);
  1076. osi_funcs_p = NULL;
  1077. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  1078. btdm_lpcycle_us = 0;
  1079. return ESP_OK;
  1080. }
  1081. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  1082. {
  1083. int ret = ESP_OK;
  1084. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1085. return ESP_ERR_INVALID_STATE;
  1086. }
  1087. //As the history reason, mode should be equal to the mode which set in esp_bt_controller_init()
  1088. if (mode != btdm_controller_get_mode()) {
  1089. ESP_LOGE(BTDM_LOG_TAG, "invalid mode %d, controller support mode is %d", mode, btdm_controller_get_mode());
  1090. return ESP_ERR_INVALID_ARG;
  1091. }
  1092. #if CONFIG_SW_COEXIST_ENABLE
  1093. coex_enable();
  1094. #endif
  1095. // enable low power mode
  1096. do {
  1097. #ifdef CONFIG_PM_ENABLE
  1098. if (s_lp_cntl.no_light_sleep) {
  1099. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  1100. }
  1101. esp_pm_lock_acquire(s_pm_lock);
  1102. s_lp_stat.pm_lock_released = 0;
  1103. #endif
  1104. if (s_lp_cntl.enable) {
  1105. btdm_controller_enable_sleep(true);
  1106. }
  1107. } while (0);
  1108. if (btdm_controller_enable(mode) != 0) {
  1109. ret = ESP_ERR_INVALID_STATE;
  1110. goto error;
  1111. }
  1112. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  1113. return ret;
  1114. error:
  1115. // disable low power mode
  1116. do {
  1117. btdm_controller_enable_sleep(false);
  1118. #ifdef CONFIG_PM_ENABLE
  1119. if (s_lp_cntl.no_light_sleep) {
  1120. esp_pm_lock_release(s_light_sleep_pm_lock);
  1121. }
  1122. if (s_lp_stat.pm_lock_released == 0) {
  1123. esp_pm_lock_release(s_pm_lock);
  1124. s_lp_stat.pm_lock_released = 1;
  1125. }
  1126. #endif
  1127. } while (0);
  1128. return ret;
  1129. }
  1130. esp_err_t esp_bt_controller_disable(void)
  1131. {
  1132. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1133. return ESP_ERR_INVALID_STATE;
  1134. }
  1135. async_wakeup_request(BTDM_ASYNC_WAKEUP_SRC_DISA);
  1136. while (!btdm_power_state_active()){}
  1137. btdm_controller_disable();
  1138. async_wakeup_request_end(BTDM_ASYNC_WAKEUP_SRC_DISA);
  1139. #if CONFIG_SW_COEXIST_ENABLE
  1140. coex_disable();
  1141. #endif
  1142. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1143. // disable low power mode
  1144. do {
  1145. #ifdef CONFIG_PM_ENABLE
  1146. if (s_lp_cntl.no_light_sleep) {
  1147. esp_pm_lock_release(s_light_sleep_pm_lock);
  1148. }
  1149. if (s_lp_stat.pm_lock_released == 0) {
  1150. esp_pm_lock_release(s_pm_lock);
  1151. s_lp_stat.pm_lock_released = 1;
  1152. } else {
  1153. assert(0);
  1154. }
  1155. #endif
  1156. } while (0);
  1157. return ESP_OK;
  1158. }
  1159. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  1160. {
  1161. return btdm_controller_status;
  1162. }
  1163. /* extra functions */
  1164. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  1165. {
  1166. esp_err_t stat = ESP_FAIL;
  1167. switch (power_type) {
  1168. case ESP_BLE_PWR_TYPE_ADV:
  1169. case ESP_BLE_PWR_TYPE_SCAN:
  1170. case ESP_BLE_PWR_TYPE_DEFAULT:
  1171. if (ble_txpwr_set(power_type, power_level) == 0) {
  1172. stat = ESP_OK;
  1173. }
  1174. break;
  1175. default:
  1176. stat = ESP_ERR_NOT_SUPPORTED;
  1177. break;
  1178. }
  1179. return stat;
  1180. }
  1181. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  1182. {
  1183. esp_power_level_t lvl;
  1184. switch (power_type) {
  1185. case ESP_BLE_PWR_TYPE_ADV:
  1186. case ESP_BLE_PWR_TYPE_SCAN:
  1187. lvl = (esp_power_level_t)ble_txpwr_get(power_type);
  1188. break;
  1189. case ESP_BLE_PWR_TYPE_CONN_HDL0:
  1190. case ESP_BLE_PWR_TYPE_CONN_HDL1:
  1191. case ESP_BLE_PWR_TYPE_CONN_HDL2:
  1192. case ESP_BLE_PWR_TYPE_CONN_HDL3:
  1193. case ESP_BLE_PWR_TYPE_CONN_HDL4:
  1194. case ESP_BLE_PWR_TYPE_CONN_HDL5:
  1195. case ESP_BLE_PWR_TYPE_CONN_HDL6:
  1196. case ESP_BLE_PWR_TYPE_CONN_HDL7:
  1197. case ESP_BLE_PWR_TYPE_CONN_HDL8:
  1198. case ESP_BLE_PWR_TYPE_DEFAULT:
  1199. lvl = (esp_power_level_t)ble_txpwr_get(ESP_BLE_PWR_TYPE_DEFAULT);
  1200. break;
  1201. default:
  1202. lvl = ESP_PWR_LVL_INVALID;
  1203. break;
  1204. }
  1205. return lvl;
  1206. }
  1207. esp_err_t esp_bt_sleep_enable (void)
  1208. {
  1209. esp_err_t status;
  1210. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1211. return ESP_ERR_INVALID_STATE;
  1212. }
  1213. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1214. btdm_controller_enable_sleep (true);
  1215. status = ESP_OK;
  1216. } else {
  1217. status = ESP_ERR_NOT_SUPPORTED;
  1218. }
  1219. return status;
  1220. }
  1221. esp_err_t esp_bt_sleep_disable (void)
  1222. {
  1223. esp_err_t status;
  1224. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1225. return ESP_ERR_INVALID_STATE;
  1226. }
  1227. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1228. btdm_controller_enable_sleep (false);
  1229. status = ESP_OK;
  1230. } else {
  1231. status = ESP_ERR_NOT_SUPPORTED;
  1232. }
  1233. return status;
  1234. }
  1235. bool esp_bt_controller_is_sleeping(void)
  1236. {
  1237. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1238. btdm_controller_get_sleep_mode() != ESP_BT_SLEEP_MODE_1) {
  1239. return false;
  1240. }
  1241. return !btdm_power_state_active();
  1242. }
  1243. void esp_bt_controller_wakeup_request(void)
  1244. {
  1245. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1246. btdm_controller_get_sleep_mode() != ESP_BT_SLEEP_MODE_1) {
  1247. return;
  1248. }
  1249. btdm_wakeup_request();
  1250. }
  1251. int IRAM_ATTR esp_bt_h4tl_eif_io_event_notify(int event)
  1252. {
  1253. return btdm_hci_tl_io_event_post(event);
  1254. }
  1255. uint16_t esp_bt_get_tx_buf_num(void)
  1256. {
  1257. return l2c_ble_link_get_tx_buf_num();
  1258. }
  1259. static void coex_wifi_sleep_set_hook(bool sleep)
  1260. {
  1261. }
  1262. #endif /* CONFIG_BT_ENABLED */