bt.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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_attr.h"
  31. #include "esp_phy_init.h"
  32. #include "esp_bt.h"
  33. #include "esp_err.h"
  34. #include "esp_log.h"
  35. #include "esp_pm.h"
  36. #include "esp_ipc.h"
  37. #include "driver/periph_ctrl.h"
  38. #include "soc/rtc.h"
  39. #include "soc/rtc_cntl_reg.h"
  40. #include "soc/soc_memory_layout.h"
  41. #include "esp_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. /* Low Power Clock Selection */
  50. #define BTDM_LPCLK_SEL_XTAL (0)
  51. #define BTDM_LPCLK_SEL_XTAL32K (1)
  52. #define BTDM_LPCLK_SEL_RTC_SLOW (2)
  53. #define BTDM_LPCLK_SEL_8M (3)
  54. /* Sleep and wakeup interval control */
  55. #define BTDM_MIN_SLEEP_DURATION (24) // threshold of interval in half slots to allow to fall into modem sleep
  56. #define BTDM_MODEM_WAKE_UP_DELAY (8) // delay in half slots of modem wake up procedure, including re-enable PHY/RF
  57. #define BTDM_MODEM_SLEEP_IN_EFFECT (1)
  58. #define BT_DEBUG(...)
  59. #define BT_API_CALL_CHECK(info, api_call, ret) \
  60. do{\
  61. esp_err_t __err = (api_call);\
  62. if ((ret) != __err) {\
  63. BT_DEBUG("%s %d %s ret=0x%X\n", __FUNCTION__, __LINE__, (info), __err);\
  64. return __err;\
  65. }\
  66. } while(0)
  67. #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
  68. #define OSI_VERSION 0x00010002
  69. #define OSI_MAGIC_VALUE 0xFADEBEAD
  70. /* SPIRAM Configuration */
  71. #if CONFIG_SPIRAM_USE_MALLOC
  72. #define BTDM_MAX_QUEUE_NUM (5)
  73. #endif
  74. /* Types definition
  75. ************************************************************************
  76. */
  77. /* VHCI function interface */
  78. typedef struct vhci_host_callback {
  79. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  80. 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*/
  81. } vhci_host_callback_t;
  82. /* Dram region */
  83. typedef struct {
  84. esp_bt_mode_t mode;
  85. intptr_t start;
  86. intptr_t end;
  87. } btdm_dram_available_region_t;
  88. /* PSRAM configuration */
  89. #if CONFIG_SPIRAM_USE_MALLOC
  90. typedef struct {
  91. QueueHandle_t handle;
  92. void *storage;
  93. void *buffer;
  94. } btdm_queue_item_t;
  95. #endif
  96. static uint8_t own_bda[6];
  97. /* OSI function */
  98. struct osi_funcs_t {
  99. uint32_t _magic;
  100. uint32_t _version;
  101. xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
  102. void (*_ints_on)(unsigned int mask);
  103. void (*_interrupt_disable)(void);
  104. void (*_interrupt_restore)(void);
  105. void (*_task_yield)(void);
  106. void (*_task_yield_from_isr)(void);
  107. void *(*_semphr_create)(uint32_t max, uint32_t init);
  108. void (*_semphr_delete)(void *semphr);
  109. int32_t (*_semphr_take_from_isr)(void *semphr, void *hptw);
  110. int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
  111. int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
  112. int32_t (*_semphr_give)(void *semphr);
  113. void *(*_mutex_create)(void);
  114. void (*_mutex_delete)(void *mutex);
  115. int32_t (*_mutex_lock)(void *mutex);
  116. int32_t (*_mutex_unlock)(void *mutex);
  117. void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
  118. void (* _queue_delete)(void *queue);
  119. int32_t (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
  120. int32_t (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
  121. int32_t (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
  122. int32_t (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
  123. int32_t (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id);
  124. void (* _task_delete)(void *task_handle);
  125. bool (* _is_in_isr)(void);
  126. int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
  127. void *(* _malloc)(uint32_t size);
  128. void *(* _malloc_internal)(uint32_t size);
  129. void (* _free)(void *p);
  130. int32_t (* _read_efuse_mac)(uint8_t mac[6]);
  131. void (* _srand)(unsigned int seed);
  132. int (* _rand)(void);
  133. uint32_t (* _btdm_lpcycles_2_hus)(uint32_t cycles, uint32_t *error_corr);
  134. uint32_t (* _btdm_hus_2_lpcycles)(uint32_t hus);
  135. bool (* _btdm_sleep_check_duration)(int32_t *slot_cnt);
  136. void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */
  137. void (* _btdm_sleep_enter_phase2)(void);
  138. void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */
  139. void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */
  140. void (* _btdm_sleep_exit_phase3)(void); /* called from task */
  141. };
  142. /* External functions or values
  143. ************************************************************************
  144. */
  145. /* not for user call, so don't put to include file */
  146. /* OSI */
  147. extern int btdm_osi_funcs_register(void *osi_funcs);
  148. /* Initialise and De-initialise */
  149. extern int btdm_controller_init(esp_bt_controller_config_t *config_opts);
  150. extern void btdm_controller_deinit(void);
  151. extern int btdm_controller_enable(esp_bt_mode_t mode);
  152. extern void btdm_controller_disable(void);
  153. extern uint8_t btdm_controller_get_mode(void);
  154. extern const char *btdm_controller_get_compile_version(void);
  155. extern void btdm_rf_bb_init_phase2(void); // shall be called after PHY/RF is enabled
  156. /* Sleep */
  157. extern void btdm_controller_enable_sleep(bool enable);
  158. extern uint8_t btdm_controller_get_sleep_mode(void);
  159. extern bool btdm_power_state_active(void);
  160. extern void btdm_wakeup_request(bool request_lock);
  161. extern void btdm_wakeup_request_end(void);
  162. /* Low Power Clock */
  163. extern bool btdm_lpclk_select_src(uint32_t sel);
  164. extern bool btdm_lpclk_set_div(uint32_t div);
  165. extern int btdm_hci_tl_io_event_post(int event);
  166. /* VHCI */
  167. extern bool API_vhci_host_check_send_available(void);
  168. extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
  169. extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback);
  170. /* TX power */
  171. extern int ble_txpwr_set(int power_type, int power_level);
  172. extern int ble_txpwr_get(int power_type);
  173. extern uint16_t l2c_ble_link_get_tx_buf_num(void);
  174. extern char _bss_start_btdm;
  175. extern char _bss_end_btdm;
  176. extern char _data_start_btdm;
  177. extern char _data_end_btdm;
  178. extern uint32_t _data_start_btdm_rom;
  179. extern uint32_t _data_end_btdm_rom;
  180. extern uint32_t _bt_bss_start;
  181. extern uint32_t _bt_bss_end;
  182. extern uint32_t _btdm_bss_start;
  183. extern uint32_t _btdm_bss_end;
  184. extern uint32_t _bt_data_start;
  185. extern uint32_t _bt_data_end;
  186. extern uint32_t _btdm_data_start;
  187. extern uint32_t _btdm_data_end;
  188. extern char _bt_tmp_bss_start;
  189. extern char _bt_tmp_bss_end;
  190. /* Local Function Declare
  191. *********************************************************************
  192. */
  193. #if CONFIG_SPIRAM_USE_MALLOC
  194. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue);
  195. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue);
  196. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  197. static void IRAM_ATTR interrupt_disable(void);
  198. static void IRAM_ATTR interrupt_restore(void);
  199. static void IRAM_ATTR task_yield_from_isr(void);
  200. static void *semphr_create_wrapper(uint32_t max, uint32_t init);
  201. static void semphr_delete_wrapper(void *semphr);
  202. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
  203. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
  204. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
  205. static int32_t semphr_give_wrapper(void *semphr);
  206. static void *mutex_create_wrapper(void);
  207. static void mutex_delete_wrapper(void *mutex);
  208. static int32_t mutex_lock_wrapper(void *mutex);
  209. static int32_t mutex_unlock_wrapper(void *mutex);
  210. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
  211. static void queue_delete_wrapper(void *queue);
  212. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms);
  213. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw);
  214. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
  215. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw);
  216. 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);
  217. static void task_delete_wrapper(void *task_handle);
  218. static bool IRAM_ATTR is_in_isr_wrapper(void);
  219. static void IRAM_ATTR cause_sw_intr(void *arg);
  220. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no);
  221. static void *malloc_internal_wrapper(size_t size);
  222. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
  223. static void IRAM_ATTR srand_wrapper(unsigned int seed);
  224. static int IRAM_ATTR rand_wrapper(void);
  225. static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles, uint32_t *error_corr);
  226. static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t hus);
  227. static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *slot_cnt);
  228. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles);
  229. static void btdm_sleep_enter_phase2_wrapper(void);
  230. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void);
  231. static void btdm_sleep_exit_phase3_wrapper(void);
  232. /* Local variable definition
  233. ***************************************************************************
  234. */
  235. /* OSI funcs */
  236. static const struct osi_funcs_t osi_funcs_ro = {
  237. ._magic = OSI_MAGIC_VALUE,
  238. ._version = OSI_VERSION,
  239. ._set_isr = xt_set_interrupt_handler,
  240. ._ints_on = xt_ints_on,
  241. ._interrupt_disable = interrupt_disable,
  242. ._interrupt_restore = interrupt_restore,
  243. ._task_yield = vPortYield,
  244. ._task_yield_from_isr = task_yield_from_isr,
  245. ._semphr_create = semphr_create_wrapper,
  246. ._semphr_delete = semphr_delete_wrapper,
  247. ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
  248. ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
  249. ._semphr_take = semphr_take_wrapper,
  250. ._semphr_give = semphr_give_wrapper,
  251. ._mutex_create = mutex_create_wrapper,
  252. ._mutex_delete = mutex_delete_wrapper,
  253. ._mutex_lock = mutex_lock_wrapper,
  254. ._mutex_unlock = mutex_unlock_wrapper,
  255. ._queue_create = queue_create_wrapper,
  256. ._queue_delete = queue_delete_wrapper,
  257. ._queue_send = queue_send_wrapper,
  258. ._queue_send_from_isr = queue_send_from_isr_wrapper,
  259. ._queue_recv = queue_recv_wrapper,
  260. ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
  261. ._task_create = task_create_wrapper,
  262. ._task_delete = task_delete_wrapper,
  263. ._is_in_isr = is_in_isr_wrapper,
  264. ._cause_sw_intr_to_core = cause_sw_intr_to_core_wrapper,
  265. ._malloc = malloc,
  266. ._malloc_internal = malloc_internal_wrapper,
  267. ._free = free,
  268. ._read_efuse_mac = read_mac_wrapper,
  269. ._srand = srand_wrapper,
  270. ._rand = rand_wrapper,
  271. ._btdm_lpcycles_2_hus = btdm_lpcycles_2_hus,
  272. ._btdm_hus_2_lpcycles = btdm_hus_2_lpcycles,
  273. ._btdm_sleep_check_duration = btdm_sleep_check_duration,
  274. ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper,
  275. ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper,
  276. ._btdm_sleep_exit_phase1 = btdm_sleep_exit_phase1_wrapper,
  277. ._btdm_sleep_exit_phase2 = NULL,
  278. ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper,
  279. //._coex_bt_request = coex_bt_request_wrapper,
  280. //._coex_bt_release = coex_bt_release_wrapper,
  281. // ._coex_register_bt_cb = coex_register_bt_cb_wrapper,
  282. // ._coex_bb_reset_lock = coex_bb_reset_lock_wrapper,
  283. //._coex_bb_reset_unlock = coex_bb_reset_unlock_wrapper,
  284. };
  285. static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
  286. #if CONFIG_SPIRAM_USE_MALLOC
  287. static DRAM_ATTR btdm_queue_item_t btdm_queue_table[BTDM_MAX_QUEUE_NUM];
  288. static DRAM_ATTR SemaphoreHandle_t btdm_queue_table_mux = NULL;
  289. #endif /* #if CONFIG_SPIRAM_USE_MALLOC */
  290. /* Static variable declare */
  291. // timestamp when PHY/RF was switched on
  292. // static DRAM_ATTR int64_t s_time_phy_rf_just_enabled = 0;
  293. static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  294. static DRAM_ATTR portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
  295. // measured average low power clock period in micro seconds
  296. static DRAM_ATTR uint32_t btdm_lpcycle_us = 0;
  297. static DRAM_ATTR uint8_t btdm_lpcycle_us_frac = 0; // number of fractional bit for btdm_lpcycle_us
  298. #ifdef CONFIG_PM_ENABLE
  299. static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr;
  300. static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock;
  301. static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock; // pm_lock to prevent light sleep due to incompatibility currently
  302. static DRAM_ATTR QueueHandle_t s_pm_lock_sem = NULL;
  303. static void btdm_slp_tmr_callback(void *arg);
  304. #endif
  305. static inline void btdm_check_and_init_bb(void)
  306. {
  307. // todo:
  308. // btdm_rf_bb_init_phase2();
  309. }
  310. #if CONFIG_SPIRAM_USE_MALLOC
  311. static bool btdm_queue_generic_register(const btdm_queue_item_t *queue)
  312. {
  313. if (!btdm_queue_table_mux || !queue) {
  314. return NULL;
  315. }
  316. bool ret = false;
  317. btdm_queue_item_t *item;
  318. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  319. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  320. item = &btdm_queue_table[i];
  321. if (item->handle == NULL) {
  322. memcpy(item, queue, sizeof(btdm_queue_item_t));
  323. ret = true;
  324. break;
  325. }
  326. }
  327. xSemaphoreGive(btdm_queue_table_mux);
  328. return ret;
  329. }
  330. static bool btdm_queue_generic_deregister(btdm_queue_item_t *queue)
  331. {
  332. if (!btdm_queue_table_mux || !queue) {
  333. return false;
  334. }
  335. bool ret = false;
  336. btdm_queue_item_t *item;
  337. xSemaphoreTake(btdm_queue_table_mux, portMAX_DELAY);
  338. for (int i = 0; i < BTDM_MAX_QUEUE_NUM; ++i) {
  339. item = &btdm_queue_table[i];
  340. if (item->handle == queue->handle) {
  341. memcpy(queue, item, sizeof(btdm_queue_item_t));
  342. memset(item, 0, sizeof(btdm_queue_item_t));
  343. ret = true;
  344. break;
  345. }
  346. }
  347. xSemaphoreGive(btdm_queue_table_mux);
  348. return ret;
  349. }
  350. #endif /* CONFIG_SPIRAM_USE_MALLOC */
  351. static void IRAM_ATTR interrupt_disable(void)
  352. {
  353. if (xPortInIsrContext()) {
  354. portENTER_CRITICAL_ISR(&global_int_mux);
  355. } else {
  356. portENTER_CRITICAL(&global_int_mux);
  357. }
  358. }
  359. static void IRAM_ATTR interrupt_restore(void)
  360. {
  361. if (xPortInIsrContext()) {
  362. portEXIT_CRITICAL_ISR(&global_int_mux);
  363. } else {
  364. portEXIT_CRITICAL(&global_int_mux);
  365. }
  366. }
  367. static void IRAM_ATTR task_yield_from_isr(void)
  368. {
  369. portYIELD_FROM_ISR();
  370. }
  371. static void *semphr_create_wrapper(uint32_t max, uint32_t init)
  372. {
  373. #if !CONFIG_SPIRAM_USE_MALLOC
  374. return (void *)xSemaphoreCreateCounting(max, init);
  375. #else
  376. StaticQueue_t *queue_buffer = NULL;
  377. QueueHandle_t handle = NULL;
  378. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  379. if (!queue_buffer) {
  380. goto error;
  381. }
  382. handle = xSemaphoreCreateCountingStatic(max, init, queue_buffer);
  383. if (!handle) {
  384. goto error;
  385. }
  386. btdm_queue_item_t item = {
  387. .handle = handle,
  388. .storage = NULL,
  389. .buffer = queue_buffer,
  390. };
  391. if (!btdm_queue_generic_register(&item)) {
  392. goto error;
  393. }
  394. return handle;
  395. error:
  396. if (handle) {
  397. vSemaphoreDelete(handle);
  398. }
  399. if (queue_buffer) {
  400. free(queue_buffer);
  401. }
  402. return NULL;
  403. #endif
  404. }
  405. static void semphr_delete_wrapper(void *semphr)
  406. {
  407. #if !CONFIG_SPIRAM_USE_MALLOC
  408. vSemaphoreDelete(semphr);
  409. #else
  410. btdm_queue_item_t item = {
  411. .handle = semphr,
  412. .storage = NULL,
  413. .buffer = NULL,
  414. };
  415. if (btdm_queue_generic_deregister(&item)) {
  416. vSemaphoreDelete(item.handle);
  417. free(item.buffer);
  418. }
  419. return;
  420. #endif
  421. }
  422. static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw)
  423. {
  424. return (int32_t)xSemaphoreTakeFromISR(semphr, hptw);
  425. }
  426. static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
  427. {
  428. return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
  429. }
  430. static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
  431. {
  432. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  433. return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
  434. } else {
  435. return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
  436. }
  437. }
  438. static int32_t semphr_give_wrapper(void *semphr)
  439. {
  440. return (int32_t)xSemaphoreGive(semphr);
  441. }
  442. static void *mutex_create_wrapper(void)
  443. {
  444. #if CONFIG_SPIRAM_USE_MALLOC
  445. StaticQueue_t *queue_buffer = NULL;
  446. QueueHandle_t handle = NULL;
  447. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  448. if (!queue_buffer) {
  449. goto error;
  450. }
  451. handle = xSemaphoreCreateMutexStatic(queue_buffer);
  452. if (!handle) {
  453. goto error;
  454. }
  455. btdm_queue_item_t item = {
  456. .handle = handle,
  457. .storage = NULL,
  458. .buffer = queue_buffer,
  459. };
  460. if (!btdm_queue_generic_register(&item)) {
  461. goto error;
  462. }
  463. return handle;
  464. error:
  465. if (handle) {
  466. vSemaphoreDelete(handle);
  467. }
  468. if (queue_buffer) {
  469. free(queue_buffer);
  470. }
  471. return NULL;
  472. #else
  473. return (void *)xSemaphoreCreateMutex();
  474. #endif
  475. }
  476. static void mutex_delete_wrapper(void *mutex)
  477. {
  478. #if !CONFIG_SPIRAM_USE_MALLOC
  479. vSemaphoreDelete(mutex);
  480. #else
  481. btdm_queue_item_t item = {
  482. .handle = mutex,
  483. .storage = NULL,
  484. .buffer = NULL,
  485. };
  486. if (btdm_queue_generic_deregister(&item)) {
  487. vSemaphoreDelete(item.handle);
  488. free(item.buffer);
  489. }
  490. return;
  491. #endif
  492. }
  493. static int32_t mutex_lock_wrapper(void *mutex)
  494. {
  495. return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
  496. }
  497. static int32_t mutex_unlock_wrapper(void *mutex)
  498. {
  499. return (int32_t)xSemaphoreGive(mutex);
  500. }
  501. static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
  502. {
  503. #if CONFIG_SPIRAM_USE_MALLOC
  504. StaticQueue_t *queue_buffer = NULL;
  505. uint8_t *queue_storage = NULL;
  506. QueueHandle_t handle = NULL;
  507. queue_buffer = heap_caps_malloc(sizeof(StaticQueue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  508. if (!queue_buffer) {
  509. goto error;
  510. }
  511. queue_storage = heap_caps_malloc((queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  512. if (!queue_storage ) {
  513. goto error;
  514. }
  515. handle = xQueueCreateStatic(queue_len, item_size, queue_storage, queue_buffer);
  516. if (!handle) {
  517. goto error;
  518. }
  519. btdm_queue_item_t item = {
  520. .handle = handle,
  521. .storage = queue_storage,
  522. .buffer = queue_buffer,
  523. };
  524. if (!btdm_queue_generic_register(&item)) {
  525. goto error;
  526. }
  527. return handle;
  528. error:
  529. if (handle) {
  530. vQueueDelete(handle);
  531. }
  532. if (queue_storage) {
  533. free(queue_storage);
  534. }
  535. if (queue_buffer) {
  536. free(queue_buffer);
  537. }
  538. return NULL;
  539. #else
  540. return (void *)xQueueCreate(queue_len, item_size);
  541. #endif
  542. }
  543. static void queue_delete_wrapper(void *queue)
  544. {
  545. #if !CONFIG_SPIRAM_USE_MALLOC
  546. vQueueDelete(queue);
  547. #else
  548. btdm_queue_item_t item = {
  549. .handle = queue,
  550. .storage = NULL,
  551. .buffer = NULL,
  552. };
  553. if (btdm_queue_generic_deregister(&item)) {
  554. vQueueDelete(item.handle);
  555. free(item.storage);
  556. free(item.buffer);
  557. }
  558. return;
  559. #endif
  560. }
  561. static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_ms)
  562. {
  563. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  564. return (int32_t)xQueueSend(queue, item, portMAX_DELAY);
  565. } else {
  566. return (int32_t)xQueueSend(queue, item, block_time_ms / portTICK_PERIOD_MS);
  567. }
  568. }
  569. static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
  570. {
  571. return (int32_t)xQueueSendFromISR(queue, item, hptw);
  572. }
  573. static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms)
  574. {
  575. if (block_time_ms == OSI_FUNCS_TIME_BLOCKING) {
  576. return (int32_t)xQueueReceive(queue, item, portMAX_DELAY);
  577. } else {
  578. return (int32_t)xQueueReceive(queue, item, block_time_ms / portTICK_PERIOD_MS);
  579. }
  580. }
  581. static int32_t IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, void *hptw)
  582. {
  583. return (int32_t)xQueueReceiveFromISR(queue, item, hptw);
  584. }
  585. 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)
  586. {
  587. return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
  588. }
  589. static void task_delete_wrapper(void *task_handle)
  590. {
  591. vTaskDelete(task_handle);
  592. }
  593. static bool IRAM_ATTR is_in_isr_wrapper(void)
  594. {
  595. return (bool)xPortInIsrContext();
  596. }
  597. static void IRAM_ATTR cause_sw_intr(void *arg)
  598. {
  599. /* just convert void * to int, because the width is the same */
  600. uint32_t intr_no = (uint32_t)arg;
  601. XTHAL_SET_INTSET((1<<intr_no));
  602. }
  603. static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no)
  604. {
  605. esp_err_t err = ESP_OK;
  606. if (xPortGetCoreID() == core_id) {
  607. cause_sw_intr((void *)intr_no);
  608. } else {
  609. err = esp_ipc_call(core_id, cause_sw_intr, (void *)intr_no);
  610. }
  611. return err;
  612. }
  613. static void *malloc_internal_wrapper(size_t size)
  614. {
  615. return heap_caps_malloc(size, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL|MALLOC_CAP_DMA);
  616. }
  617. static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
  618. {
  619. esp_read_mac(mac, ESP_MAC_BT);
  620. ESP_LOGI(BTDM_LOG_TAG, "Bluetooth MAC: 0x%02x:%02x:%02x:%02x:%02x:%02x\n",
  621. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  622. return ESP_OK;
  623. }
  624. static void IRAM_ATTR srand_wrapper(unsigned int seed)
  625. {
  626. /* empty function */
  627. }
  628. static int IRAM_ATTR rand_wrapper(void)
  629. {
  630. return (int)esp_random();
  631. }
  632. static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles, uint32_t *error_corr)
  633. {
  634. uint64_t local_error_corr = (error_corr == NULL) ? 0 : (uint64_t)(*error_corr);
  635. uint64_t res = (uint64_t)btdm_lpcycle_us * cycles * 2;
  636. local_error_corr += res;
  637. res = (local_error_corr >> btdm_lpcycle_us_frac);
  638. local_error_corr -= (res << btdm_lpcycle_us_frac);
  639. if (error_corr) {
  640. *error_corr = (uint32_t) local_error_corr;
  641. }
  642. return (uint32_t)res;
  643. }
  644. /*
  645. * @brief Converts a duration in half us into a number of low power clock cycles.
  646. */
  647. static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t hus)
  648. {
  649. // The number of sleep duration(us) should not lead to overflow. Thrs: 100s
  650. // Compute the sleep duration in us to low power clock cycles, with calibration result applied
  651. // clock measurement is conducted
  652. uint64_t cycles = ((uint64_t)(hus) << btdm_lpcycle_us_frac) / btdm_lpcycle_us;
  653. cycles >>= 1;
  654. return (uint32_t)cycles;
  655. }
  656. static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *half_slot_cnt)
  657. {
  658. if (*half_slot_cnt < BTDM_MIN_SLEEP_DURATION) {
  659. return false;
  660. }
  661. /* wake up in advance considering the delay in enabling PHY/RF */
  662. *half_slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY;
  663. return true;
  664. }
  665. static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
  666. {
  667. #ifdef CONFIG_PM_ENABLE
  668. // start a timer to wake up and acquire the pm_lock before modem_sleep awakes
  669. uint32_t us_to_sleep = btdm_lpcycles_2_us(lpcycles);
  670. #define BTDM_MIN_TIMER_UNCERTAINTY_US (1800)
  671. assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US);
  672. // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift
  673. // and set the timer in advance
  674. uint32_t uncertainty = (us_to_sleep >> 11);
  675. if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) {
  676. uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US;
  677. }
  678. if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) != ESP_OK) {
  679. ESP_LOGW(BTDM_LOG_TAG, "timer start failed");
  680. }
  681. #endif
  682. }
  683. static void btdm_sleep_enter_phase2_wrapper(void)
  684. {
  685. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  686. #if BTDM_MODEM_SLEEP_IN_EFFECT
  687. //esp_phy_disable();
  688. #endif /* BTDM_MODEM_SLEEP_IN_EFFECT */
  689. #ifdef CONFIG_PM_ENABLE
  690. esp_pm_lock_release(s_pm_lock);
  691. semphr_give_wrapper(s_pm_lock_sem);
  692. #endif
  693. }
  694. }
  695. static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void)
  696. {
  697. #ifdef CONFIG_PM_ENABLE
  698. if (semphr_take_from_isr_wrapper(s_pm_lock_sem, NULL) == pdTRUE) {
  699. esp_pm_lock_acquire(s_pm_lock);
  700. }
  701. #endif
  702. }
  703. static void btdm_sleep_exit_phase3_wrapper(void)
  704. {
  705. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  706. #if BTDM_MODEM_SLEEP_IN_EFFECT
  707. //esp_phy_enable();
  708. #endif /* BTDM_MODEM_SLEEP_IN_EFFECT */
  709. btdm_check_and_init_bb();
  710. #ifdef CONFIG_PM_ENABLE
  711. esp_timer_stop(s_btdm_slp_tmr);
  712. #endif
  713. }
  714. }
  715. #ifdef CONFIG_PM_ENABLE
  716. static void IRAM_ATTR btdm_slp_tmr_callback(void *arg)
  717. {
  718. if (semphr_take_wrapper(s_pm_lock_sem, 0) == pdTRUE) {
  719. esp_pm_lock_acquire(s_pm_lock);
  720. }
  721. }
  722. #endif
  723. bool esp_vhci_host_check_send_available(void)
  724. {
  725. return API_vhci_host_check_send_available();
  726. }
  727. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
  728. {
  729. bool do_wakeup_request = false;
  730. if (!btdm_power_state_active()) {
  731. #if CONFIG_PM_ENABLE
  732. if (semphr_take_wrapper(s_pm_lock_sem, 0)) {
  733. esp_pm_lock_acquire(s_pm_lock);
  734. }
  735. esp_timer_stop(s_btdm_slp_tmr);
  736. #endif
  737. do_wakeup_request = true;
  738. btdm_wakeup_request(true);
  739. }
  740. API_vhci_host_send_packet(data, len);
  741. if (do_wakeup_request) {
  742. btdm_wakeup_request_end();
  743. }
  744. }
  745. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
  746. {
  747. return API_vhci_host_register_callback((const vhci_host_callback_t *)callback) == 0 ? ESP_OK : ESP_FAIL;
  748. }
  749. static void btdm_controller_mem_init(void)
  750. {
  751. memset(&_bss_start_btdm, 0, &_bss_end_btdm - &_bss_start_btdm);
  752. memcpy(&_data_start_btdm, (void *)_data_start_btdm_rom, &_data_end_btdm - &_data_start_btdm);
  753. // memset(&_bt_tmp_bss_start, 0, &_bt_tmp_bss_end - &_bt_tmp_bss_start);
  754. }
  755. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  756. {
  757. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  758. return ESP_OK;
  759. }
  760. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode)
  761. {
  762. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  763. return ESP_OK;
  764. }
  765. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
  766. {
  767. esp_err_t err;
  768. btdm_controller_mem_init();
  769. osi_funcs_p = (struct osi_funcs_t *)malloc_internal_wrapper(sizeof(struct osi_funcs_t));
  770. if (osi_funcs_p == NULL) {
  771. return ESP_ERR_NO_MEM;
  772. }
  773. memcpy(osi_funcs_p, &osi_funcs_ro, sizeof(struct osi_funcs_t));
  774. if (btdm_osi_funcs_register(osi_funcs_p) != 0) {
  775. return ESP_ERR_INVALID_ARG;
  776. }
  777. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  778. return ESP_ERR_INVALID_STATE;
  779. }
  780. if (cfg == NULL) {
  781. return ESP_ERR_INVALID_ARG;
  782. }
  783. if (cfg->controller_task_prio != ESP_TASK_BT_CONTROLLER_PRIO
  784. || cfg->controller_task_stack_size < ESP_TASK_BT_CONTROLLER_STACK) {
  785. ESP_LOGE(BTDM_LOG_TAG, "Invalid controller task prioriy or stack size");
  786. return ESP_ERR_INVALID_ARG;
  787. }
  788. //overwrite some parameters
  789. cfg->magic = ESP_BT_CTRL_CONFIG_MAGIC_VAL;
  790. if (cfg->bluetooth_mode != ESP_BT_MODE_BLE) {
  791. ESP_LOGE(BTDM_LOG_TAG, "%s controller only support BLE only mode", __func__);
  792. return ESP_ERR_NOT_SUPPORTED;
  793. }
  794. if (cfg->bluetooth_mode & ESP_BT_MODE_BLE) {
  795. if ((cfg->ble_max_act <= 0) || (cfg->ble_max_act > BT_CTRL_BLE_MAX_ACT_LIMIT)) {
  796. ESP_LOGE(BTDM_LOG_TAG, "Invalid value of ble_max_act");
  797. return ESP_ERR_INVALID_ARG;
  798. }
  799. }
  800. if (cfg->sleep_mode == ESP_BT_SLEEP_MODE_1) {
  801. if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_NONE) {
  802. ESP_LOGE(BTDM_LOG_TAG, "SLEEP_MODE_1 enabled but sleep clock not configured");
  803. return ESP_ERR_INVALID_ARG;
  804. }
  805. }
  806. read_mac_wrapper(own_bda);
  807. ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version());
  808. #if CONFIG_SPIRAM_USE_MALLOC
  809. btdm_queue_table_mux = xSemaphoreCreateMutex();
  810. if (btdm_queue_table_mux == NULL) {
  811. return ESP_ERR_NO_MEM;
  812. }
  813. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  814. #endif
  815. if (cfg->sleep_mode == ESP_BT_SLEEP_MODE_1) {
  816. #ifdef CONFIG_PM_ENABLE
  817. if ((err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "btLS", &s_light_sleep_pm_lock)) != ESP_OK) {
  818. goto error;
  819. }
  820. if ((err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "bt", &s_pm_lock)) != ESP_OK) {
  821. goto error;
  822. }
  823. esp_timer_create_args_t create_args = {
  824. .callback = btdm_slp_tmr_callback,
  825. .arg = NULL,
  826. .name = "btSlp"
  827. };
  828. if ((err = esp_timer_create(&create_args, &s_btdm_slp_tmr)) != ESP_OK) {
  829. goto error;
  830. }
  831. s_pm_lock_sem = semphr_create_wrapper(1, 0);
  832. if (s_pm_lock_sem == NULL) {
  833. err = ESP_ERR_NO_MEM;
  834. goto error;
  835. }
  836. #endif
  837. do {// todo: rewrite this block of code for chip
  838. #if CONFIG_IDF_ENV_FPGA
  839. // overwrite the sleep clock for FPGA
  840. cfg->sleep_clock = ESP_BT_SLEEP_CLOCK_FPGA_32K;
  841. ESP_LOGW(BTDM_LOG_TAG, "%s sleep clock overwrite on FPGA", __func__);
  842. #endif
  843. bool select_src_ret = false;
  844. bool set_div_ret = false;
  845. if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_MAIN_XTAL) {
  846. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL);
  847. set_div_ret = btdm_lpclk_set_div(rtc_clk_xtal_freq_get() * 2);
  848. assert(select_src_ret && set_div_ret);
  849. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  850. btdm_lpcycle_us = 2 << (btdm_lpcycle_us_frac);
  851. } else if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_EXT_32K_XTAL) {
  852. select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K);
  853. set_div_ret = btdm_lpclk_set_div(0);
  854. assert(select_src_ret && set_div_ret);
  855. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  856. btdm_lpcycle_us = esp_clk_slowclk_cal_get();
  857. assert(btdm_lpcycle_us != 0);
  858. } else if (cfg->sleep_clock == ESP_BT_SLEEP_CLOCK_FPGA_32K) {
  859. // on FPGA, the low power clock is hard-wired to a 32kHz(clock cycle 31.25us) oscillator
  860. btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT;
  861. btdm_lpcycle_us = 125 << (btdm_lpcycle_us_frac - 2);
  862. }
  863. } while (0);
  864. }
  865. periph_module_enable(PERIPH_BT_MODULE);
  866. // must do fpga_init and phy init before controller init
  867. esp_phy_enable();
  868. if (btdm_controller_init(cfg) != 0) {
  869. err = ESP_ERR_NO_MEM;
  870. goto error;
  871. }
  872. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  873. return ESP_OK;
  874. error:
  875. #ifdef CONFIG_PM_ENABLE
  876. if (s_light_sleep_pm_lock != NULL) {
  877. esp_pm_lock_delete(s_light_sleep_pm_lock);
  878. s_light_sleep_pm_lock = NULL;
  879. }
  880. if (s_pm_lock != NULL) {
  881. esp_pm_lock_delete(s_pm_lock);
  882. s_pm_lock = NULL;
  883. }
  884. if (s_btdm_slp_tmr != NULL) {
  885. esp_timer_delete(s_btdm_slp_tmr);
  886. s_btdm_slp_tmr = NULL;
  887. }
  888. if (s_pm_lock_sem) {
  889. semphr_delete_wrapper(s_pm_lock_sem);
  890. s_pm_lock_sem = NULL;
  891. }
  892. #endif
  893. return err;
  894. }
  895. esp_err_t esp_bt_controller_deinit(void)
  896. {
  897. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  898. return ESP_ERR_INVALID_STATE;
  899. }
  900. btdm_controller_deinit();
  901. periph_module_disable(PERIPH_BT_MODULE);
  902. esp_phy_disable();
  903. #ifdef CONFIG_PM_ENABLE
  904. esp_pm_lock_delete(s_light_sleep_pm_lock);
  905. s_light_sleep_pm_lock = NULL;
  906. esp_pm_lock_delete(s_pm_lock);
  907. s_pm_lock = NULL;
  908. esp_timer_stop(s_btdm_slp_tmr);
  909. esp_timer_delete(s_btdm_slp_tmr);
  910. s_btdm_slp_tmr = NULL;
  911. semphr_delete_wrapper(s_pm_lock_sem);
  912. s_pm_lock_sem = NULL;
  913. #endif
  914. #if CONFIG_SPIRAM_USE_MALLOC
  915. vSemaphoreDelete(btdm_queue_table_mux);
  916. btdm_queue_table_mux = NULL;
  917. memset(btdm_queue_table, 0, sizeof(btdm_queue_item_t) * BTDM_MAX_QUEUE_NUM);
  918. #endif
  919. free(osi_funcs_p);
  920. osi_funcs_p = NULL;
  921. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
  922. btdm_lpcycle_us = 0;
  923. return ESP_OK;
  924. }
  925. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
  926. {
  927. int ret;
  928. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
  929. return ESP_ERR_INVALID_STATE;
  930. }
  931. //As the history reason, mode should be equal to the mode which set in esp_bt_controller_init()
  932. if (mode != btdm_controller_get_mode()) {
  933. ESP_LOGE(BTDM_LOG_TAG, "invalid mode %d, controller support mode is %d", mode, btdm_controller_get_mode());
  934. return ESP_ERR_INVALID_ARG;
  935. }
  936. #ifdef CONFIG_PM_ENABLE
  937. esp_pm_lock_acquire(s_light_sleep_pm_lock);
  938. esp_pm_lock_acquire(s_pm_lock);
  939. #endif
  940. // esp_phy_enable();
  941. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  942. coex_enable();
  943. #endif
  944. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  945. btdm_controller_enable_sleep(true);
  946. }
  947. // inititalize bluetooth baseband
  948. btdm_check_and_init_bb();
  949. ret = btdm_controller_enable(mode);
  950. if (ret) {
  951. // esp_phy_disable();
  952. #ifdef CONFIG_PM_ENABLE
  953. esp_pm_lock_release(s_light_sleep_pm_lock);
  954. esp_pm_lock_release(s_pm_lock);
  955. #endif
  956. return ESP_ERR_INVALID_STATE;
  957. }
  958. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
  959. return ESP_OK;
  960. }
  961. esp_err_t esp_bt_controller_disable(void)
  962. {
  963. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  964. return ESP_ERR_INVALID_STATE;
  965. }
  966. // disable modem sleep and wake up from sleep mode
  967. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  968. btdm_controller_enable_sleep(false);
  969. if (!btdm_power_state_active()) {
  970. btdm_wakeup_request(true);
  971. }
  972. while (!btdm_power_state_active()) {
  973. esp_rom_delay_us(1000);
  974. }
  975. }
  976. btdm_controller_disable();
  977. #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
  978. coex_disable();
  979. #endif
  980. // esp_phy_disable();
  981. btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
  982. #ifdef CONFIG_PM_ENABLE
  983. esp_pm_lock_release(s_light_sleep_pm_lock);
  984. esp_pm_lock_release(s_pm_lock);
  985. #endif
  986. return ESP_OK;
  987. }
  988. esp_bt_controller_status_t esp_bt_controller_get_status(void)
  989. {
  990. return btdm_controller_status;
  991. }
  992. uint8_t* esp_bt_get_mac(void)
  993. {
  994. return own_bda;
  995. }
  996. /* extra functions */
  997. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level)
  998. {
  999. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__);
  1000. return ESP_OK;
  1001. }
  1002. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
  1003. {
  1004. ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return 0", __func__);
  1005. return 0;
  1006. }
  1007. esp_err_t esp_bt_sleep_enable (void)
  1008. {
  1009. esp_err_t status;
  1010. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1011. return ESP_ERR_INVALID_STATE;
  1012. }
  1013. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1014. btdm_controller_enable_sleep (true);
  1015. status = ESP_OK;
  1016. } else {
  1017. status = ESP_ERR_NOT_SUPPORTED;
  1018. }
  1019. return status;
  1020. }
  1021. esp_err_t esp_bt_sleep_disable (void)
  1022. {
  1023. esp_err_t status;
  1024. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  1025. return ESP_ERR_INVALID_STATE;
  1026. }
  1027. if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) {
  1028. btdm_controller_enable_sleep (false);
  1029. status = ESP_OK;
  1030. } else {
  1031. status = ESP_ERR_NOT_SUPPORTED;
  1032. }
  1033. return status;
  1034. }
  1035. bool esp_bt_controller_is_sleeping(void)
  1036. {
  1037. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1038. btdm_controller_get_sleep_mode() != ESP_BT_SLEEP_MODE_1) {
  1039. return false;
  1040. }
  1041. return !btdm_power_state_active();
  1042. }
  1043. void esp_bt_controller_wakeup_request(void)
  1044. {
  1045. if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED ||
  1046. btdm_controller_get_sleep_mode() != ESP_BT_SLEEP_MODE_1) {
  1047. return;
  1048. }
  1049. btdm_wakeup_request(false);
  1050. }
  1051. int IRAM_ATTR esp_bt_h4tl_eif_io_event_notify(int event)
  1052. {
  1053. return btdm_hci_tl_io_event_post(event);
  1054. }
  1055. void force_wifi_mode(int arg)
  1056. {
  1057. }
  1058. void unforce_wifi_mode(void)
  1059. {
  1060. }
  1061. void bt_bb_init_cmplx_reg(void)
  1062. {
  1063. }
  1064. uint16_t esp_bt_get_tx_buf_num(void)
  1065. {
  1066. return l2c_ble_link_get_tx_buf_num();
  1067. }
  1068. void coex_wifi_sleep_set(bool sleep)
  1069. {
  1070. }
  1071. #endif /* CONFIG_BT_ENABLED */