bt.c 46 KB

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