bt.c 54 KB

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