bt.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "sdkconfig.h"
  18. #include "esp_heap_caps.h"
  19. #include "esp_heap_caps_init.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "freertos/queue.h"
  23. #include "freertos/semphr.h"
  24. #include "freertos/xtensa_api.h"
  25. #include "freertos/portmacro.h"
  26. #include "xtensa/core-macros.h"
  27. #include "esp_types.h"
  28. #include "esp_system.h"
  29. #include "esp_task.h"
  30. #include "esp_intr_alloc.h"
  31. #include "esp_attr.h"
  32. #include "esp_phy_init.h"
  33. #include "esp_bt.h"
  34. #include "esp_err.h"
  35. #include "esp_log.h"
  36. #include "esp_pm.h"
  37. #include "esp_ipc.h"
  38. #include "driver/periph_ctrl.h"
  39. #include "soc/rtc.h"
  40. #include "soc/soc_memory_layout.h"
  41. #include "esp32/clk.h"
  42. #include "esp_coexist_internal.h"
  43. #if CONFIG_BT_ENABLED
  44. /* Macro definition
  45. ************************************************************************
  46. */
  47. #define BTDM_LOG_TAG "BTDM_INIT"
  48. #define BTDM_INIT_PERIOD (5000) /* ms */
  49. /* Bluetooth system and controller config */
  50. #define BTDM_CFG_BT_DATA_RELEASE (1<<0)
  51. #define BTDM_CFG_HCI_UART (1<<1)
  52. #define BTDM_CFG_CONTROLLER_RUN_APP_CPU (1<<2)
  53. #define BTDM_CFG_SCAN_DUPLICATE_OPTIONS (1<<3)
  54. #define BTDM_CFG_SEND_ADV_RESERVED_SIZE (1<<4)
  55. #define BTDM_CFG_BLE_FULL_SCAN_SUPPORTED (1<<5)
  56. /* Sleep mode */
  57. #define BTDM_MODEM_SLEEP_MODE_NONE (0)
  58. #define BTDM_MODEM_SLEEP_MODE_ORIG (1)
  59. #define BTDM_MODEM_SLEEP_MODE_EVED (2) // sleep mode for BLE controller, used only for internal test.
  60. /* Low Power Clock Selection */
  61. #define BTDM_LPCLK_SEL_XTAL (0)
  62. #define BTDM_LPCLK_SEL_XTAL32K (1)
  63. #define BTDM_LPCLK_SEL_RTC_SLOW (2)
  64. #define BTDM_LPCLK_SEL_8M (3)
  65. /* Sleep and wakeup interval control */
  66. #define BTDM_MIN_SLEEP_DURATION (12) // threshold of interval in slots to allow to fall into modem sleep
  67. #define BTDM_MODEM_WAKE_UP_DELAY (4) // delay in slots of modem wake up procedure, including re-enable PHY/RF
  68. #ifdef CONFIG_PM_ENABLE
  69. #ifndef CONFIG_BTDM_LPCLK_SEL_MAIN_XTAL
  70. #define BTDM_ALLOW_LIGHT_SLEEP 1
  71. #else
  72. #define BTDM_ALLOW_LIGHT_SLEEP 0
  73. #endif
  74. #endif
  75. #define BT_DEBUG(...)
  76. #define BT_API_CALL_CHECK(info, api_call, ret) \
  77. do{\
  78. esp_err_t __err = (api_call);\
  79. if ((ret) != __err) {\
  80. BT_DEBUG("%s %d %s ret=0x%X\n", __FUNCTION__, __LINE__, (info), __err);\
  81. return __err;\
  82. }\
  83. } while(0)
  84. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  85. #define OSI_VERSION 0x00010001
  86. #define OSI_MAGIC_VALUE 0xFADEBEAD
  87. /* SPIRAM Configuration */
  88. #if CONFIG_SPIRAM_USE_MALLOC
  89. #define BTDM_MAX_QUEUE_NUM (5)
  90. #endif
  91. /* Types definition
  92. ************************************************************************
  93. */
  94. /* VHCI function interface */
  95. typedef struct vhci_host_callback {
  96. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  97. 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*/
  98. } vhci_host_callback_t;
  99. /* Dram region */
  100. typedef struct {
  101. esp_bt_mode_t mode;
  102. intptr_t start;
  103. intptr_t end;
  104. } btdm_dram_available_region_t;
  105. /* PSRAM configuration */
  106. #if CONFIG_SPIRAM_USE_MALLOC
  107. typedef struct {
  108. QueueHandle_t handle;
  109. void *storage;
  110. void *buffer;
  111. } btdm_queue_item_t;
  112. #endif
  113. /* OSI function */
  114. struct osi_funcs_t {
  115. uint32_t _version;
  116. xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
  117. void (*_ints_on)(unsigned int mask);
  118. void (*_interrupt_disable)(void);
  119. void (*_interrupt_restore)(void);
  120. void (*_task_yield)(void);
  121. void (*_task_yield_from_isr)(void);
  122. void *(*_semphr_create)(uint32_t max, uint32_t init);
  123. void (*_semphr_delete)(void *semphr);
  124. int32_t (*_semphr_take_from_isr)(void *semphr, void *hptw);
  125. int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
  126. int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
  127. int32_t (*_semphr_give)(void *semphr);
  128. void *(*_mutex_create)(void);
  129. void (*_mutex_delete)(void *mutex);
  130. int32_t (*_mutex_lock)(void *mutex);
  131. int32_t (*_mutex_unlock)(void *mutex);
  132. void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
  133. void (* _queue_delete)(void *queue);
  134. int32_t (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
  135. int32_t (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
  136. int32_t (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
  137. int32_t (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
  138. int32_t (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  139. void (* _task_delete)(void *task_handle);
  140. bool (* _is_in_isr)(void);
  141. int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
  142. void *(* _malloc)(uint32_t size);
  143. void *(* _malloc_internal)(uint32_t size);
  144. void (* _free)(void *p);
  145. int32_t (* _read_efuse_mac)(uint8_t mac[6]);
  146. void (* _srand)(unsigned int seed);
  147. int (* _rand)(void);
  148. uint32_t (* _btdm_lpcycles_2_us)(uint32_t cycles);
  149. uint32_t (* _btdm_us_2_lpcycles)(uint32_t us);
  150. bool (* _btdm_sleep_check_duration)(uint32_t *slot_cnt);
  151. void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */
  152. void (* _btdm_sleep_enter_phase2)(void);
  153. void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */
  154. void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */
  155. void (* _btdm_sleep_exit_phase3)(void); /* called from task */
  156. int (* _coex_bt_request)(uint32_t event, uint32_t latency, uint32_t duration);
  157. int (* _coex_bt_release)(uint32_t event);
  158. int (* _coex_register_bt_cb)(coex_func_cb_t cb);
  159. uint32_t (* _coex_bb_reset_lock)(void);
  160. void (* _coex_bb_reset_unlock)(uint32_t restore);
  161. uint32_t _magic;
  162. };
  163. /* External functions or values
  164. ************************************************************************
  165. */
  166. /* not for user call, so don't put to include file */
  167. /* OSI */
  168. extern int btdm_osi_funcs_register(void *osi_funcs);
  169. /* Initialise and De-initialise */
  170. extern int btdm_controller_init(uint32_t config_mask, esp_bt_controller_config_t *config_opts);
  171. extern void btdm_controller_deinit(void);
  172. extern int btdm_controller_enable(esp_bt_mode_t mode);
  173. extern void btdm_controller_disable(void);
  174. extern uint8_t btdm_controller_get_mode(void);
  175. extern const char *btdm_controller_get_compile_version(void);
  176. extern void btdm_rf_bb_init_phase2(void); // shall be called after PHY/RF is enabled
  177. /* Sleep */
  178. extern void btdm_controller_enable_sleep(bool enable);
  179. extern void btdm_controller_set_sleep_mode(uint8_t mode);
  180. extern uint8_t btdm_controller_get_sleep_mode(void);
  181. extern bool btdm_power_state_active(void);
  182. extern void btdm_wakeup_request(bool request_lock);
  183. extern void btdm_wakeup_request_end(void);
  184. /* Low Power Clock */
  185. extern bool btdm_lpclk_select_src(uint32_t sel);
  186. extern bool btdm_lpclk_set_div(uint32_t div);
  187. /* VHCI */
  188. extern bool API_vhci_host_check_send_available(void);
  189. extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
  190. extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback);
  191. /* TX power */
  192. extern int ble_txpwr_set(int power_type, int power_level);
  193. extern int ble_txpwr_get(int power_type);
  194. extern int bredr_txpwr_set(int min_power_level, int max_power_level);
  195. extern int bredr_txpwr_get(int *min_power_level, int *max_power_level);
  196. extern void bredr_sco_datapath_set(uint8_t data_path);
  197. extern void btdm_controller_scan_duplicate_list_clear(void);
  198. /* Coexistence */
  199. extern int coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration);
  200. extern int coex_bt_release_wrapper(uint32_t event);
  201. extern int coex_register_bt_cb_wrapper(coex_func_cb_t cb);
  202. extern uint32_t coex_bb_reset_lock_wrapper(void);
  203. extern void coex_bb_reset_unlock_wrapper(uint32_t restore);
  204. extern char _bss_start_btdm;
  205. extern char _bss_end_btdm;
  206. extern char _data_start_btdm;
  207. extern char _data_end_btdm;
  208. extern uint32_t _data_start_btdm_rom;
  209. extern uint32_t _data_end_btdm_rom;
  210. extern uint32_t _bt_bss_start;
  211. extern uint32_t _bt_bss_end;
  212. extern uint32_t _nimble_bss_start;
  213. extern uint32_t _nimble_bss_end;
  214. extern uint32_t _btdm_bss_start;
  215. extern uint32_t _btdm_bss_end;
  216. extern uint32_t _bt_data_start;
  217. extern uint32_t _bt_data_end;
  218. extern uint32_t _nimble_data_start;
  219. extern uint32_t _nimble_data_end;
  220. extern uint32_t _btdm_data_start;
  221. extern uint32_t _btdm_data_end;
  222. /* Local Function Declare
  223. *********************************************************************
  224. */
  225. #if CONFIG_SPIRAM_USE_MALLOC
  226. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue);
  227. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue);
  228. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  229. static void IRAM_ATTR interrupt_disable(void);
  230. static void IRAM_ATTR interrupt_restore(void);
  231. static void IRAM_ATTR task_yield_from_isr(void);
  232. static void *semphr_create_wrapper(uint32_t max, uint32_t init);
  233. static void semphr_delete_wrapper(void *semphr);
  234. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
  235. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
  236. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
  237. static int32_t semphr_give_wrapper(void *semphr);
  238. static void *mutex_create_wrapper(void);
  239. static void mutex_delete_wrapper(void *mutex);
  240. static int32_t mutex_lock_wrapper(void *mutex);
  241. static int32_t mutex_unlock_wrapper(void *mutex);
  242. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
  243. static void queue_delete_wrapper(void *queue);
  244. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms);
  245. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw);
  246. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
  247. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw);
  248. static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  249. static void task_delete_wrapper(void *task_handle);
  250. static bool IRAM_ATTR is_in_isr_wrapper(void);
  251. static void IRAM_ATTR cause_sw_intr(void *arg);
  252. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no);
  253. static void *malloc_internal_wrapper(size_t size);
  254. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
  255. static void IRAM_ATTR srand_wrapper(unsigned int seed);
  256. static int IRAM_ATTR rand_wrapper(void);
  257. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles);
  258. static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us);
  259. static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt);
  260. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles);
  261. static void btdm_sleep_enter_phase2_wrapper(void);
  262. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void);
  263. static void btdm_sleep_exit_phase3_wrapper(void);
  264. /* Local variable definition
  265. ***************************************************************************
  266. */
  267. /* OSI funcs */
  268. static const struct osi_funcs_t osi_funcs_ro = {
  269. ._version = OSI_VERSION,
  270. ._set_isr = xt_set_interrupt_handler,
  271. ._ints_on = xt_ints_on,
  272. ._interrupt_disable = interrupt_disable,
  273. ._interrupt_restore = interrupt_restore,
  274. ._task_yield = vPortYield,
  275. ._task_yield_from_isr = task_yield_from_isr,
  276. ._semphr_create = semphr_create_wrapper,
  277. ._semphr_delete = semphr_delete_wrapper,
  278. ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
  279. ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
  280. ._semphr_take = semphr_take_wrapper,
  281. ._semphr_give = semphr_give_wrapper,
  282. ._mutex_create = mutex_create_wrapper,
  283. ._mutex_delete = mutex_delete_wrapper,
  284. ._mutex_lock = mutex_lock_wrapper,
  285. ._mutex_unlock = mutex_unlock_wrapper,
  286. ._queue_create = queue_create_wrapper,
  287. ._queue_delete = queue_delete_wrapper,
  288. ._queue_send = queue_send_wrapper,
  289. ._queue_send_from_isr = queue_send_from_isr_wrapper,
  290. ._queue_recv = queue_recv_wrapper,
  291. ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
  292. ._task_create = task_create_wrapper,
  293. ._task_delete = task_delete_wrapper,
  294. ._is_in_isr = is_in_isr_wrapper,
  295. ._cause_sw_intr_to_core = cause_sw_intr_to_core_wrapper,
  296. ._malloc = malloc,
  297. ._malloc_internal = malloc_internal_wrapper,
  298. ._free = free,
  299. ._read_efuse_mac = read_mac_wrapper,
  300. ._srand = srand_wrapper,
  301. ._rand = rand_wrapper,
  302. ._btdm_lpcycles_2_us = btdm_lpcycles_2_us,
  303. ._btdm_us_2_lpcycles = btdm_us_2_lpcycles,
  304. ._btdm_sleep_check_duration = btdm_sleep_check_duration,
  305. ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper,
  306. ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper,
  307. ._btdm_sleep_exit_phase1 = btdm_sleep_exit_phase1_wrapper,
  308. ._btdm_sleep_exit_phase2 = NULL,
  309. ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper,
  310. ._coex_bt_request = coex_bt_request_wrapper,
  311. ._coex_bt_release = coex_bt_release_wrapper,
  312. ._coex_register_bt_cb = coex_register_bt_cb_wrapper,
  313. ._coex_bb_reset_lock = coex_bb_reset_lock_wrapper,
  314. ._coex_bb_reset_unlock = coex_bb_reset_unlock_wrapper,
  315. ._magic = OSI_MAGIC_VALUE,
  316. };
  317. /* the mode column will be modified by release function to indicate the available region */
  318. static btdm_dram_available_region_t btdm_dram_available_region[] = {
  319. //following is .data
  320. {ESP_BT_MODE_BTDM, SOC_MEM_BT_DATA_START, SOC_MEM_BT_DATA_END },
  321. //following is memory which HW will use
  322. {ESP_BT_MODE_BTDM, SOC_MEM_BT_EM_BTDM0_START, SOC_MEM_BT_EM_BTDM0_END },
  323. {ESP_BT_MODE_BLE, SOC_MEM_BT_EM_BLE_START, SOC_MEM_BT_EM_BLE_END },
  324. {ESP_BT_MODE_BTDM, SOC_MEM_BT_EM_BTDM1_START, SOC_MEM_BT_EM_BTDM1_END },
  325. {ESP_BT_MODE_CLASSIC_BT, SOC_MEM_BT_EM_BREDR_START, SOC_MEM_BT_EM_BREDR_REAL_END},
  326. //following is .bss
  327. {ESP_BT_MODE_BTDM, SOC_MEM_BT_BSS_START, SOC_MEM_BT_BSS_END },
  328. {ESP_BT_MODE_BTDM, SOC_MEM_BT_MISC_START, SOC_MEM_BT_MISC_END },
  329. };
  330. /* Reserve the full memory region used by Bluetooth Controller,
  331. * some may be released later at runtime. */
  332. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_EM_START, SOC_MEM_BT_EM_BREDR_REAL_END, rom_bt_em);
  333. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_BSS_START, SOC_MEM_BT_BSS_END, rom_bt_bss);
  334. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_MISC_START, SOC_MEM_BT_MISC_END, rom_bt_misc);
  335. SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_DATA_START, SOC_MEM_BT_DATA_END, rom_bt_data);
  336. static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
  337. #if CONFIG_SPIRAM_USE_MALLOC
  338. static DRAM_ATTR btdm_queue_item_t btdm_queue_table[BTDM_MAX_QUEUE_NUM];
  339. static DRAM_ATTR SemaphoreHandle_t btdm_queue_table_mux = NULL;
  340. #endif /* #if CONFIG_SPIRAM_USE_MALLOC */
  341. /* Static variable declare */
  342. // timestamp when PHY/RF was switched on
  343. static DRAM_ATTR int64_t s_time_phy_rf_just_enabled = 0;
  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. // measured average low power clock period in micro seconds
  347. static DRAM_ATTR uint32_t btdm_lpcycle_us = 0;
  348. static DRAM_ATTR uint8_t btdm_lpcycle_us_frac = 0; // number of fractional bit for btdm_lpcycle_us
  349. #ifdef CONFIG_PM_ENABLE
  350. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr;
  351. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock;
  352. static DRAM_ATTR QueueHandle_t s_pm_lock_sem = NULL;
  353. #if !BTDM_ALLOW_LIGHT_SLEEP
  354. // pm_lock to prevent light sleep when using main crystal as Bluetooth low power clock
  355. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock;
  356. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  357. static void btdm_slp_tmr_callback(void *arg);
  358. #endif
  359. static inline void btdm_check_and_init_bb(void)
  360. {
  361. /* init BT-BB if PHY/RF has been switched off since last BT-BB init */
  362. int64_t latest_ts = esp_phy_rf_get_on_ts();
  363. if (latest_ts != s_time_phy_rf_just_enabled ||
  364. s_time_phy_rf_just_enabled == 0) {
  365. btdm_rf_bb_init_phase2();
  366. s_time_phy_rf_just_enabled = latest_ts;
  367. }
  368. }
  369. #if CONFIG_SPIRAM_USE_MALLOC
  370. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue)
  371. {
  372. if (!btdm_queue_table_mux || !queue) {
  373. return NULL;
  374. }
  375. bool ret = false;
  376. btdm_queue_item_t *item;
  377. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  378. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  379. item = &btdm_queue_table[i];
  380. if (item->handle == NULL) {
  381. memcpy(item, queue, sizeof(btdm_queue_item_t));
  382. ret = true;
  383. break;
  384. }
  385. }
  386. xSemaphoreGive(btdm_queue_table_mux);
  387. return ret;
  388. }
  389. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue)
  390. {
  391. if (!btdm_queue_table_mux || !queue) {
  392. return false;
  393. }
  394. bool ret = false;
  395. btdm_queue_item_t *item;
  396. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  397. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  398. item = &btdm_queue_table[i];
  399. if (item->handle == queue->handle) {
  400. memcpy(queue, item, sizeof(btdm_queue_item_t));
  401. memset(item, 0, sizeof(btdm_queue_item_t));
  402. ret = true;
  403. break;
  404. }
  405. }
  406. xSemaphoreGive(btdm_queue_table_mux);
  407. return ret;
  408. }
  409. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  410. static void IRAM_ATTR interrupt_disable(void)
  411. {
  412. if (xPortInIsrContext()) {
  413. portENTER_CRITICAL_ISR(&global_int_mux);
  414. } else {
  415. portENTER_CRITICAL(&global_int_mux);
  416. }
  417. }
  418. static void IRAM_ATTR interrupt_restore(void)
  419. {
  420. if (xPortInIsrContext()) {
  421. portEXIT_CRITICAL_ISR(&global_int_mux);
  422. } else {
  423. portEXIT_CRITICAL(&global_int_mux);
  424. }
  425. }
  426. static void IRAM_ATTR task_yield_from_isr(void)
  427. {
  428. portYIELD_FROM_ISR();
  429. }
  430. static void *semphr_create_wrapper(uint32_t max, uint32_t init)
  431. {
  432. #if !CONFIG_SPIRAM_USE_MALLOC
  433. return (void *)xSemaphoreCreateCounting(max, init);
  434. #else
  435. StaticQueue_t *queue_buffer = NULL;
  436. QueueHandle_t handle = NULL;
  437. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  438. if (!queue_buffer) {
  439. goto error;
  440. }
  441. handle = xSemaphoreCreateCountingStatic(max, init, queue_buffer);
  442. if (!handle) {
  443. goto error;
  444. }
  445. btdm_queue_item_t item = {
  446. .handle = handle,
  447. .storage = NULL,
  448. .buffer = queue_buffer,
  449. };
  450. if (!btdm_queue_generic_register(&item)) {
  451. goto error;
  452. }
  453. return handle;
  454. error:
  455. if (handle) {
  456. vSemaphoreDelete(handle);
  457. }
  458. if (queue_buffer) {
  459. free(queue_buffer);
  460. }
  461. return NULL;
  462. #endif
  463. }
  464. static void semphr_delete_wrapper(void *semphr)
  465. {
  466. #if !CONFIG_SPIRAM_USE_MALLOC
  467. vSemaphoreDelete(semphr);
  468. #else
  469. btdm_queue_item_t item = {
  470. .handle = semphr,
  471. .storage = NULL,
  472. .buffer = NULL,
  473. };
  474. if (btdm_queue_generic_deregister(&item)) {
  475. vSemaphoreDelete(item.handle);
  476. free(item.buffer);
  477. }
  478. return;
  479. #endif
  480. }
  481. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  482. {
  483. return (int32_t)xSemaphoreTakeFromISR(semphr, hptw);
  484. }
  485. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  486. {
  487. return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
  488. }
  489. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
  490. {
  491. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  492. return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
  493. } else {
  494. return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
  495. }
  496. }
  497. static int32_t semphr_give_wrapper(void *semphr)
  498. {
  499. return (int32_t)xSemaphoreGive(semphr);
  500. }
  501. static void *mutex_create_wrapper(void)
  502. {
  503. #if CONFIG_SPIRAM_USE_MALLOC
  504. StaticQueue_t *queue_buffer = NULL;
  505. QueueHandle_t handle = NULL;
  506. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  507. if (!queue_buffer) {
  508. goto error;
  509. }
  510. handle = xSemaphoreCreateMutexStatic(queue_buffer);
  511. if (!handle) {
  512. goto error;
  513. }
  514. btdm_queue_item_t item = {
  515. .handle = handle,
  516. .storage = NULL,
  517. .buffer = queue_buffer,
  518. };
  519. if (!btdm_queue_generic_register(&item)) {
  520. goto error;
  521. }
  522. return handle;
  523. error:
  524. if (handle) {
  525. vSemaphoreDelete(handle);
  526. }
  527. if (queue_buffer) {
  528. free(queue_buffer);
  529. }
  530. return NULL;
  531. #else
  532. return (void *)xSemaphoreCreateMutex();
  533. #endif
  534. }
  535. static void mutex_delete_wrapper(void *mutex)
  536. {
  537. #if !CONFIG_SPIRAM_USE_MALLOC
  538. vSemaphoreDelete(mutex);
  539. #else
  540. btdm_queue_item_t item = {
  541. .handle = mutex,
  542. .storage = NULL,
  543. .buffer = NULL,
  544. };
  545. if (btdm_queue_generic_deregister(&item)) {
  546. vSemaphoreDelete(item.handle);
  547. free(item.buffer);
  548. }
  549. return;
  550. #endif
  551. }
  552. static int32_t mutex_lock_wrapper(void *mutex)
  553. {
  554. return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
  555. }
  556. static int32_t mutex_unlock_wrapper(void *mutex)
  557. {
  558. return (int32_t)xSemaphoreGive(mutex);
  559. }
  560. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
  561. {
  562. #if CONFIG_SPIRAM_USE_MALLOC
  563. StaticQueue_t *queue_buffer = NULL;
  564. uint8_t *queue_storage = NULL;
  565. QueueHandle_t handle = NULL;
  566. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  567. if (!queue_buffer) {
  568. goto error;
  569. }
  570. queue_storage = heap_caps_malloc((queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  571. if (!queue_storage ) {
  572. goto error;
  573. }
  574. handle = xQueueCreateStatic(queue_len, item_size, queue_storage, queue_buffer);
  575. if (!handle) {
  576. goto error;
  577. }
  578. btdm_queue_item_t item = {
  579. .handle = handle,
  580. .storage = queue_storage,
  581. .buffer = queue_buffer,
  582. };
  583. if (!btdm_queue_generic_register(&item)) {
  584. goto error;
  585. }
  586. return handle;
  587. error:
  588. if (handle) {
  589. vQueueDelete(handle);
  590. }
  591. if (queue_storage) {
  592. free(queue_storage);
  593. }
  594. if (queue_buffer) {
  595. free(queue_buffer);
  596. }
  597. return NULL;
  598. #else
  599. return (void *)xQueueCreate(queue_len, item_size);
  600. #endif
  601. }
  602. static void queue_delete_wrapper(void *queue)
  603. {
  604. #if !CONFIG_SPIRAM_USE_MALLOC
  605. vQueueDelete(queue);
  606. #else
  607. btdm_queue_item_t item = {
  608. .handle = queue,
  609. .storage = NULL,
  610. .buffer = NULL,
  611. };
  612. if (btdm_queue_generic_deregister(&item)) {
  613. vQueueDelete(item.handle);
  614. free(item.storage);
  615. free(item.buffer);
  616. }
  617. return;
  618. #endif
  619. }
  620. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms)
  621. {
  622. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  623. return (int32_t)xQueueSend(queue, item, portMAX_DELAY);
  624. } else {
  625. return (int32_t)xQueueSend(queue, item, block_time_ms / portTICK_PERIOD_MS);
  626. }
  627. }
  628. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
  629. {
  630. return (int32_t)xQueueSendFromISR(queue, item, hptw);
  631. }
  632. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms)
  633. {
  634. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  635. return (int32_t)xQueueReceive(queue, item, portMAX_DELAY);
  636. } else {
  637. return (int32_t)xQueueReceive(queue, item, block_time_ms / portTICK_PERIOD_MS);
  638. }
  639. }
  640. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw)
  641. {
  642. return (int32_t)xQueueReceiveFromISR(queue, item, hptw);
  643. }
  644. static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id)
  645. {
  646. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  647. }
  648. static void task_delete_wrapper(void *task_handle)
  649. {
  650. vTaskDelete(task_handle);
  651. }
  652. static bool IRAM_ATTR is_in_isr_wrapper(void)
  653. {
  654. return (bool)xPortInIsrContext();
  655. }
  656. static void IRAM_ATTR cause_sw_intr(void *arg)
  657. {
  658. /* just convert void * to int, because the width is the same */
  659. uint32_t intr_no = (uint32_t)arg;
  660. XTHAL_SET_INTSET((1<<intr_no));
  661. }
  662. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no)
  663. {
  664. esp_err_t err = ESP_OK;
  665. if (xPortGetCoreID() == core_id) {
  666. cause_sw_intr((void *)intr_no);
  667. } else {
  668. err = esp_ipc_call(core_id, cause_sw_intr, (void *)intr_no);
  669. }
  670. return err;
  671. }
  672. static void *malloc_internal_wrapper(size_t size)
  673. {
  674. return heap_caps_malloc(size, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
  675. }
  676. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
  677. {
  678. return esp_read_mac(mac, ESP_MAC_BT);
  679. }
  680. static void IRAM_ATTR srand_wrapper(unsigned int seed)
  681. {
  682. /* empty function */
  683. }
  684. static int IRAM_ATTR rand_wrapper(void)
  685. {
  686. return (int)esp_random();
  687. }
  688. static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles)
  689. {
  690. // The number of lp cycles should not lead to overflow. Thrs: 100s
  691. // clock measurement is conducted
  692. uint64_t us = (uint64_t)btdm_lpcycle_us * cycles;
  693. us = (us + (1 << (btdm_lpcycle_us_frac - 1))) >> btdm_lpcycle_us_frac;
  694. return (uint32_t)us;
  695. }
  696. /*
  697. * @brief Converts a duration in slots into a number of low power clock cycles.
  698. */
  699. static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us)
  700. {
  701. // The number of sleep duration(us) should not lead to overflow. Thrs: 100s
  702. // Compute the sleep duration in us to low power clock cycles, with calibration result applied
  703. // clock measurement is conducted
  704. uint64_t cycles = ((uint64_t)(us) << btdm_lpcycle_us_frac) / btdm_lpcycle_us;
  705. return (uint32_t)cycles;
  706. }
  707. static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt)
  708. {
  709. if (*slot_cnt < BTDM_MIN_SLEEP_DURATION) {
  710. return false;
  711. }
  712. /* wake up in advance considering the delay in enabling PHY/RF */
  713. *slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY;
  714. return true;
  715. }
  716. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
  717. {
  718. #ifdef CONFIG_PM_ENABLE
  719. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  720. uint32_t us_to_sleep = btdm_lpcycles_2_us(lpcycles);
  721. #define BTDM_MIN_TIMER_UNCERTAINTY_US (500)
  722. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  723. // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift
  724. // and set the timer in advance
  725. uint32_t uncertainty = (us_to_sleep >> 11);
  726. if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) {
  727. uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US;
  728. }
  729. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) != ESP_OK) {
  730. ESP_LOGW(BTDM_LOG_TAG, "timer start failed");
  731. }
  732. #endif
  733. }
  734. static void btdm_sleep_enter_phase2_wrapper(void)
  735. {
  736. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  737. esp_modem_sleep_enter(MODEM_BLE_MODULE);
  738. esp_modem_sleep_enter(MODEM_CLASSIC_BT_MODULE);
  739. #ifdef CONFIG_PM_ENABLE
  740. esp_pm_lock_release(s_pm_lock);
  741. semphr_give_wrapper(s_pm_lock_sem);
  742. #endif
  743. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  744. esp_modem_sleep_enter(MODEM_BLE_MODULE);
  745. // pause bluetooth baseband
  746. periph_module_disable(PERIPH_BT_BASEBAND_MODULE);
  747. }
  748. }
  749. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void)
  750. {
  751. #ifdef CONFIG_PM_ENABLE
  752. if (semphr_take_from_isr_wrapper(s_pm_lock_sem, NULL) == pdTRUE) {
  753. esp_pm_lock_acquire(s_pm_lock);
  754. }
  755. #endif
  756. }
  757. static void btdm_sleep_exit_phase3_wrapper(void)
  758. {
  759. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  760. esp_modem_sleep_exit(MODEM_BLE_MODULE);
  761. esp_modem_sleep_exit(MODEM_CLASSIC_BT_MODULE);
  762. btdm_check_and_init_bb();
  763. #ifdef CONFIG_PM_ENABLE
  764. esp_timer_stop(s_btdm_slp_tmr);
  765. #endif
  766. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  767. // resume bluetooth baseband
  768. periph_module_enable(PERIPH_BT_BASEBAND_MODULE);
  769. esp_modem_sleep_exit(MODEM_BLE_MODULE);
  770. }
  771. }
  772. #ifdef CONFIG_PM_ENABLE
  773. static void IRAM_ATTR btdm_slp_tmr_callback(void *arg)
  774. {
  775. if (semphr_take_wrapper(s_pm_lock_sem, 0) == pdTRUE) {
  776. esp_pm_lock_acquire(s_pm_lock);
  777. }
  778. }
  779. #endif
  780. bool esp_vhci_host_check_send_available(void)
  781. {
  782. return API_vhci_host_check_send_available();
  783. }
  784. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  785. {
  786. bool do_wakeup_request = false;
  787. if (!btdm_power_state_active()) {
  788. #if CONFIG_PM_ENABLE
  789. if (semphr_take_wrapper(s_pm_lock_sem, 0)) {
  790. esp_pm_lock_acquire(s_pm_lock);
  791. }
  792. esp_timer_stop(s_btdm_slp_tmr);
  793. #endif
  794. do_wakeup_request = true;
  795. btdm_wakeup_request(true);
  796. }
  797. API_vhci_host_send_packet(data, len);
  798. if (do_wakeup_request) {
  799. btdm_wakeup_request_end();
  800. }
  801. }
  802. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  803. {
  804. return API_vhci_host_register_callback((const vhci_host_callback_t *)callback) == 0 ? ESP_OK : ESP_FAIL;
  805. }
  806. static uint32_t btdm_config_mask_load(void)
  807. {
  808. uint32_t mask = 0x0;
  809. #if CONFIG_BTDM_CTRL_HCI_MODE_UART_H4
  810. mask |= BTDM_CFG_HCI_UART;
  811. #endif
  812. #if CONFIG_BTDM_CTRL_PINNED_TO_CORE == 1
  813. mask |= BTDM_CFG_CONTROLLER_RUN_APP_CPU;
  814. #endif
  815. #if CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED
  816. mask |= BTDM_CFG_BLE_FULL_SCAN_SUPPORTED;
  817. #endif /* CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED */
  818. mask |= BTDM_CFG_SCAN_DUPLICATE_OPTIONS;
  819. mask |= BTDM_CFG_SEND_ADV_RESERVED_SIZE;
  820. return mask;
  821. }
  822. static void btdm_controller_mem_init(void)
  823. {
  824. /* initialise .data section */
  825. memcpy(&_data_start_btdm, (void *)_data_start_btdm_rom, &_data_end_btdm - &_data_start_btdm);
  826. ESP_LOGD(BTDM_LOG_TAG, ".data initialise [0x%08x] <== [0x%08x]", (uint32_t)&_data_start_btdm, _data_start_btdm_rom);
  827. //initial em, .bss section
  828. for (int i = 1; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  829. if (btdm_dram_available_region[i].mode != ESP_BT_MODE_IDLE) {
  830. memset((void *)btdm_dram_available_region[i].start, 0x0, btdm_dram_available_region[i].end - btdm_dram_available_region[i].start);
  831. ESP_LOGD(BTDM_LOG_TAG, ".bss initialise [0x%08x] - [0x%08x]", btdm_dram_available_region[i].start, btdm_dram_available_region[i].end);
  832. }
  833. }
  834. }
  835. static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end)
  836. {
  837. int ret = heap_caps_add_region(start, end);
  838. /* heap_caps_add_region() returns ESP_ERR_INVALID_SIZE if the memory region is
  839. * is too small to fit a heap. This cannot be termed as a fatal error and hence
  840. * we replace it by ESP_OK
  841. */
  842. if (ret == ESP_ERR_INVALID_SIZE) {
  843. return ESP_OK;
  844. }
  845. return ret;
  846. }
  847. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  848. {
  849. bool update = true;
  850. intptr_t mem_start, mem_end;
  851. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  852. return ESP_ERR_INVALID_STATE;
  853. }
  854. //already released
  855. if (!(mode & btdm_dram_available_region[0].mode)) {
  856. return ESP_ERR_INVALID_STATE;
  857. }
  858. for (int i = 0; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  859. //skip the share mode, idle mode and other mode
  860. if (btdm_dram_available_region[i].mode == ESP_BT_MODE_IDLE
  861. || (mode & btdm_dram_available_region[i].mode) != btdm_dram_available_region[i].mode) {
  862. //clear the bit of the mode which will be released
  863. btdm_dram_available_region[i].mode &= ~mode;
  864. continue;
  865. } else {
  866. //clear the bit of the mode which will be released
  867. btdm_dram_available_region[i].mode &= ~mode;
  868. }
  869. if (update) {
  870. mem_start = btdm_dram_available_region[i].start;
  871. mem_end = btdm_dram_available_region[i].end;
  872. update = false;
  873. }
  874. if (i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t) - 1) {
  875. mem_end = btdm_dram_available_region[i].end;
  876. if (btdm_dram_available_region[i+1].mode != ESP_BT_MODE_IDLE
  877. && (mode & btdm_dram_available_region[i+1].mode) == btdm_dram_available_region[i+1].mode
  878. && mem_end == btdm_dram_available_region[i+1].start) {
  879. continue;
  880. } else {
  881. ESP_LOGD(BTDM_LOG_TAG, "Release DRAM [0x%08x] - [0x%08x]", mem_start, mem_end);
  882. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  883. update = true;
  884. }
  885. } else {
  886. mem_end = btdm_dram_available_region[i].end;
  887. ESP_LOGD(BTDM_LOG_TAG, "Release DRAM [0x%08x] - [0x%08x]", mem_start, mem_end);
  888. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  889. update = true;
  890. }
  891. }
  892. if (mode == ESP_BT_MODE_BTDM) {
  893. mem_start = (intptr_t)&_btdm_bss_start;
  894. mem_end = (intptr_t)&_btdm_bss_end;
  895. if (mem_start != mem_end) {
  896. ESP_LOGD(BTDM_LOG_TAG, "Release BTDM BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  897. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  898. }
  899. mem_start = (intptr_t)&_btdm_data_start;
  900. mem_end = (intptr_t)&_btdm_data_end;
  901. if (mem_start != mem_end) {
  902. ESP_LOGD(BTDM_LOG_TAG, "Release BTDM Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  903. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  904. }
  905. }
  906. return ESP_OK;
  907. }
  908. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  909. {
  910. int ret;
  911. intptr_t mem_start, mem_end;
  912. ret = esp_bt_controller_mem_release(mode);
  913. if (ret != ESP_OK) {
  914. return ret;
  915. }
  916. if (mode == ESP_BT_MODE_BTDM) {
  917. mem_start = (intptr_t)&_bt_bss_start;
  918. mem_end = (intptr_t)&_bt_bss_end;
  919. if (mem_start != mem_end) {
  920. ESP_LOGD(BTDM_LOG_TAG, "Release BT BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  921. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  922. }
  923. mem_start = (intptr_t)&_bt_data_start;
  924. mem_end = (intptr_t)&_bt_data_end;
  925. if (mem_start != mem_end) {
  926. ESP_LOGD(BTDM_LOG_TAG, "Release BT Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  927. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  928. }
  929. mem_start = (intptr_t)&_nimble_bss_start;
  930. mem_end = (intptr_t)&_nimble_bss_end;
  931. if (mem_start != mem_end) {
  932. ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE BSS [0x%08x] - [0x%08x]", mem_start, mem_end);
  933. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  934. }
  935. mem_start = (intptr_t)&_nimble_data_start;
  936. mem_end = (intptr_t)&_nimble_data_end;
  937. if (mem_start != mem_end) {
  938. ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE Data [0x%08x] - [0x%08x]", mem_start, mem_end);
  939. ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end));
  940. }
  941. }
  942. return ESP_OK;
  943. }
  944. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
  945. {
  946. esp_err_t err;
  947. uint32_t btdm_cfg_mask = 0;
  948. osi_funcs_p = (struct osi_funcs_t *)malloc_internal_wrapper(sizeof(struct osi_funcs_t));
  949. if (osi_funcs_p == NULL) {
  950. return ESP_ERR_NO_MEM;
  951. }
  952. memcpy(osi_funcs_p, &osi_funcs_ro, sizeof(struct osi_funcs_t));
  953. if (btdm_osi_funcs_register(osi_funcs_p) != 0) {
  954. return ESP_ERR_INVALID_ARG;
  955. }
  956. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  957. return ESP_ERR_INVALID_STATE;
  958. }
  959. //if all the bt available memory was already released, cannot initialize bluetooth controller
  960. if (btdm_dram_available_region[0].mode == ESP_BT_MODE_IDLE) {
  961. return ESP_ERR_INVALID_STATE;
  962. }
  963. if (cfg == NULL) {
  964. return ESP_ERR_INVALID_ARG;
  965. }
  966. if (cfg->controller_task_prio != ESP_TASK_BT_CONTROLLER_PRIO
  967. || cfg->controller_task_stack_size < ESP_TASK_BT_CONTROLLER_STACK) {
  968. return ESP_ERR_INVALID_ARG;
  969. }
  970. //overwrite some parameters
  971. cfg->bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF;
  972. cfg->magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL;
  973. if (((cfg->mode & ESP_BT_MODE_BLE) && (cfg->ble_max_conn <= 0 || cfg->ble_max_conn > BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT))
  974. || ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_acl_conn <= 0 || cfg->bt_max_acl_conn > BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT))
  975. || ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_sync_conn > BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT))) {
  976. return ESP_ERR_INVALID_ARG;
  977. }
  978. ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version());
  979. #if CONFIG_SPIRAM_USE_MALLOC
  980. btdm_queue_table_mux = xSemaphoreCreateMutex();
  981. if (btdm_queue_table_mux == NULL) {
  982. return ESP_ERR_NO_MEM;
  983. }
  984. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  985. #endif
  986. #ifdef CONFIG_PM_ENABLE
  987. #if !BTDM_ALLOW_LIGHT_SLEEP
  988. if ((err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btLS", &s_light_sleep_pm_lock)) != ESP_OK) {
  989. goto error;
  990. }
  991. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  992. if ((err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock)) != ESP_OK) {
  993. goto error;
  994. }
  995. esp_timer_create_args_t create_args = {
  996. .callback = btdm_slp_tmr_callback,
  997. .arg = NULL,
  998. .name = "btSlp"
  999. };
  1000. if ((err = esp_timer_create(&create_args, &s_btdm_slp_tmr)) != ESP_OK) {
  1001. goto error;
  1002. }
  1003. s_pm_lock_sem = semphr_create_wrapper(1, 0);
  1004. if (s_pm_lock_sem == NULL) {
  1005. err = ESP_ERR_NO_MEM;
  1006. goto error;
  1007. }
  1008. #endif
  1009. btdm_controller_mem_init();
  1010. periph_module_enable(PERIPH_BT_MODULE);
  1011. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1012. btdm_lpcycle_us = 32 << btdm_lpcycle_us_frac;
  1013. #if CONFIG_BTDM_MODEM_SLEEP_MODE_ORIG
  1014. bool select_src_ret = false;
  1015. bool set_div_ret = false;
  1016. #if CONFIG_BTDM_LPCLK_SEL_MAIN_XTAL
  1017. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL);
  1018. set_div_ret = btdm_lpclk_set_div(rtc_clk_xtal_freq_get() * 2 - 1);
  1019. assert(select_src_ret && set_div_ret);
  1020. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1021. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  1022. #elif CONFIG_BTDM_LPCLK_SEL_EXT_32K_XTAL
  1023. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K);
  1024. set_div_ret = btdm_lpclk_set_div(0);
  1025. assert(select_src_ret && set_div_ret);
  1026. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  1027. btdm_lpcycle_us = (RTC_CLK_CAL_FRACT > 15) ? (1000000 << (RTC_CLK_CAL_FRACT - 15)) :
  1028. (1000000 >> (15 - RTC_CLK_CAL_FRACT));
  1029. assert(btdm_lpcycle_us != 0);
  1030. #endif // CONFIG_BTDM_LPCLK_SEL_XX
  1031. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_ORIG);
  1032. #elif CONFIG_BTDM_MODEM_SLEEP_MODE_EVED
  1033. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_EVED);
  1034. #else
  1035. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE);
  1036. #endif
  1037. btdm_cfg_mask = btdm_config_mask_load();
  1038. if (btdm_controller_init(btdm_cfg_mask, cfg) != 0) {
  1039. err = ESP_ERR_NO_MEM;
  1040. goto error;
  1041. }
  1042. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1043. return ESP_OK;
  1044. error:
  1045. #ifdef CONFIG_PM_ENABLE
  1046. #if !BTDM_ALLOW_LIGHT_SLEEP
  1047. if (s_light_sleep_pm_lock != NULL) {
  1048. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1049. s_light_sleep_pm_lock = NULL;
  1050. }
  1051. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  1052. if (s_pm_lock != NULL) {
  1053. esp_pm_lock_delete(s_pm_lock);
  1054. s_pm_lock = NULL;
  1055. }
  1056. if (s_btdm_slp_tmr != NULL) {
  1057. esp_timer_delete(s_btdm_slp_tmr);
  1058. s_btdm_slp_tmr = NULL;
  1059. }
  1060. if (s_pm_lock_sem) {
  1061. semphr_delete_wrapper(s_pm_lock_sem);
  1062. s_pm_lock_sem = NULL;
  1063. }
  1064. #endif
  1065. return err;
  1066. }
  1067. esp_err_t esp_bt_controller_deinit(void)
  1068. {
  1069. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1070. return ESP_ERR_INVALID_STATE;
  1071. }
  1072. btdm_controller_deinit();
  1073. periph_module_disable(PERIPH_BT_MODULE);
  1074. #ifdef CONFIG_PM_ENABLE
  1075. #if !BTDM_ALLOW_LIGHT_SLEEP
  1076. esp_pm_lock_delete(s_light_sleep_pm_lock);
  1077. s_light_sleep_pm_lock = NULL;
  1078. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  1079. esp_pm_lock_delete(s_pm_lock);
  1080. s_pm_lock = NULL;
  1081. esp_timer_stop(s_btdm_slp_tmr);
  1082. esp_timer_delete(s_btdm_slp_tmr);
  1083. s_btdm_slp_tmr = NULL;
  1084. semphr_delete_wrapper(s_pm_lock_sem);
  1085. s_pm_lock_sem = NULL;
  1086. #endif
  1087. #if CONFIG_SPIRAM_USE_MALLOC
  1088. vSemaphoreDelete(btdm_queue_table_mux);
  1089. btdm_queue_table_mux = NULL;
  1090. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  1091. #endif
  1092. free(osi_funcs_p);
  1093. osi_funcs_p = NULL;
  1094. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  1095. btdm_lpcycle_us = 0;
  1096. btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE);
  1097. return ESP_OK;
  1098. }
  1099. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  1100. {
  1101. int ret;
  1102. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  1103. return ESP_ERR_INVALID_STATE;
  1104. }
  1105. //As the history reason, mode should be equal to the mode which set in esp_bt_controller_init()
  1106. if (mode != btdm_controller_get_mode()) {
  1107. return ESP_ERR_INVALID_ARG;
  1108. }
  1109. #ifdef CONFIG_PM_ENABLE
  1110. #if !BTDM_ALLOW_LIGHT_SLEEP
  1111. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  1112. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  1113. esp_pm_lock_acquire(s_pm_lock);
  1114. #endif
  1115. esp_phy_load_cal_and_init(PHY_BT_MODULE);
  1116. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_NONE) {
  1117. //Just register to sleep module, make the modem sleep modules check BT sleep status when sleep enter.
  1118. //Thus, it will prevent WIFI from disabling RF when BT is not in sleep but is using RF.
  1119. esp_modem_sleep_register(MODEM_BLE_MODULE);
  1120. esp_modem_sleep_register(MODEM_CLASSIC_BT_MODULE);
  1121. esp_modem_sleep_exit(MODEM_BLE_MODULE);
  1122. esp_modem_sleep_exit(MODEM_CLASSIC_BT_MODULE);
  1123. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1124. esp_modem_sleep_register(MODEM_BLE_MODULE);
  1125. esp_modem_sleep_register(MODEM_CLASSIC_BT_MODULE);
  1126. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1127. esp_modem_sleep_register(MODEM_BLE_MODULE);
  1128. }
  1129. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1130. btdm_controller_enable_sleep(true);
  1131. }
  1132. // inititalize bluetooth baseband
  1133. btdm_check_and_init_bb();
  1134. ret = btdm_controller_enable(mode);
  1135. if (ret) {
  1136. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_NONE
  1137. || btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1138. esp_modem_sleep_deregister(MODEM_BLE_MODULE);
  1139. esp_modem_sleep_deregister(MODEM_CLASSIC_BT_MODULE);
  1140. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1141. esp_modem_sleep_deregister(MODEM_BLE_MODULE);
  1142. }
  1143. esp_phy_rf_deinit(PHY_BT_MODULE);
  1144. #ifdef CONFIG_PM_ENABLE
  1145. #if !BTDM_ALLOW_LIGHT_SLEEP
  1146. esp_pm_lock_release(s_light_sleep_pm_lock);
  1147. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  1148. esp_pm_lock_release(s_pm_lock);
  1149. #endif
  1150. return ESP_ERR_INVALID_STATE;
  1151. }
  1152. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  1153. return ESP_OK;
  1154. }
  1155. esp_err_t esp_bt_controller_disable(void)
  1156. {
  1157. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1158. return ESP_ERR_INVALID_STATE;
  1159. }
  1160. // disable modem sleep and wake up from sleep mode
  1161. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1162. btdm_controller_enable_sleep(false);
  1163. if (!btdm_power_state_active()) {
  1164. btdm_wakeup_request(false);
  1165. }
  1166. while (!btdm_power_state_active()) {
  1167. ets_delay_us(1000);
  1168. }
  1169. }
  1170. btdm_controller_disable();
  1171. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_NONE
  1172. || btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1173. esp_modem_sleep_deregister(MODEM_BLE_MODULE);
  1174. esp_modem_sleep_deregister(MODEM_CLASSIC_BT_MODULE);
  1175. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1176. esp_modem_sleep_deregister(MODEM_BLE_MODULE);
  1177. }
  1178. esp_phy_rf_deinit(PHY_BT_MODULE);
  1179. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  1180. #ifdef CONFIG_PM_ENABLE
  1181. #if !BTDM_ALLOW_LIGHT_SLEEP
  1182. esp_pm_lock_release(s_light_sleep_pm_lock);
  1183. #endif /* #if !BTDM_ALLOW_LIGHT_SLEEP */
  1184. esp_pm_lock_release(s_pm_lock);
  1185. #endif
  1186. return ESP_OK;
  1187. }
  1188. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  1189. {
  1190. return btdm_controller_status;
  1191. }
  1192. /* extra functions */
  1193. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  1194. {
  1195. if (ble_txpwr_set(power_type, power_level) != 0) {
  1196. return ESP_ERR_INVALID_ARG;
  1197. }
  1198. return ESP_OK;
  1199. }
  1200. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  1201. {
  1202. return (esp_power_level_t)ble_txpwr_get(power_type);
  1203. }
  1204. esp_err_t esp_bredr_tx_power_set(esp_power_level_t min_power_level, esp_power_level_t max_power_level)
  1205. {
  1206. esp_err_t err;
  1207. int ret;
  1208. ret = bredr_txpwr_set(min_power_level, max_power_level);
  1209. if (ret == 0) {
  1210. err = ESP_OK;
  1211. } else if (ret == -1) {
  1212. err = ESP_ERR_INVALID_ARG;
  1213. } else {
  1214. err = ESP_ERR_INVALID_STATE;
  1215. }
  1216. return err;
  1217. }
  1218. esp_err_t esp_bredr_tx_power_get(esp_power_level_t *min_power_level, esp_power_level_t *max_power_level)
  1219. {
  1220. if (bredr_txpwr_get((int *)min_power_level, (int *)max_power_level) != 0) {
  1221. return ESP_ERR_INVALID_ARG;
  1222. }
  1223. return ESP_OK;
  1224. }
  1225. esp_err_t esp_bt_sleep_enable (void)
  1226. {
  1227. esp_err_t status;
  1228. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1229. return ESP_ERR_INVALID_STATE;
  1230. }
  1231. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1232. esp_modem_sleep_register(MODEM_BLE_MODULE);
  1233. esp_modem_sleep_register(MODEM_CLASSIC_BT_MODULE);
  1234. btdm_controller_enable_sleep (true);
  1235. status = ESP_OK;
  1236. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1237. esp_modem_sleep_register(MODEM_BLE_MODULE);
  1238. btdm_controller_enable_sleep (true);
  1239. status = ESP_OK;
  1240. } else {
  1241. status = ESP_ERR_NOT_SUPPORTED;
  1242. }
  1243. return status;
  1244. }
  1245. esp_err_t esp_bt_sleep_disable (void)
  1246. {
  1247. esp_err_t status;
  1248. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1249. return ESP_ERR_INVALID_STATE;
  1250. }
  1251. if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) {
  1252. esp_modem_sleep_deregister(MODEM_BLE_MODULE);
  1253. esp_modem_sleep_deregister(MODEM_CLASSIC_BT_MODULE);
  1254. btdm_controller_enable_sleep (false);
  1255. status = ESP_OK;
  1256. } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) {
  1257. esp_modem_sleep_deregister(MODEM_BLE_MODULE);
  1258. btdm_controller_enable_sleep (false);
  1259. status = ESP_OK;
  1260. } else {
  1261. status = ESP_ERR_NOT_SUPPORTED;
  1262. }
  1263. return status;
  1264. }
  1265. bool esp_bt_controller_is_sleeping(void)
  1266. {
  1267. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1268. btdm_controller_get_sleep_mode() != BTDM_MODEM_SLEEP_MODE_ORIG) {
  1269. return false;
  1270. }
  1271. return !btdm_power_state_active();
  1272. }
  1273. void esp_bt_controller_wakeup_request(void)
  1274. {
  1275. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1276. btdm_controller_get_sleep_mode() != BTDM_MODEM_SLEEP_MODE_ORIG) {
  1277. return;
  1278. }
  1279. btdm_wakeup_request(false);
  1280. }
  1281. esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path)
  1282. {
  1283. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1284. return ESP_ERR_INVALID_STATE;
  1285. }
  1286. bredr_sco_datapath_set(data_path);
  1287. return ESP_OK;
  1288. }
  1289. esp_err_t esp_ble_scan_dupilcate_list_flush(void)
  1290. {
  1291. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1292. return ESP_ERR_INVALID_STATE;
  1293. }
  1294. btdm_controller_scan_duplicate_list_clear();
  1295. return ESP_OK;
  1296. }
  1297. #endif /* CONFIG_BT_ENABLED */