bt.c 46 KB

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