Merge branch 'feature/esp32c2_optimize_ble_init_v5.0' into 'release/v5.0'

Fixed memory leak when RAM free size is insufficient or setting ext scan...

See merge request espressif/esp-idf!21262
This commit is contained in:
Jiang Jiang Jian
2022-12-02 20:37:16 +08:00
10 changed files with 110 additions and 95 deletions
+32 -10
View File
@@ -52,6 +52,8 @@
#include "esp_private/periph_ctrl.h"
#include "esp_sleep.h"
#include "soc/syscon_reg.h"
#include "soc/dport_access.h"
/* Macro definition
************************************************************************
*/
@@ -60,7 +62,7 @@
#define OSI_COEX_VERSION 0x00010006
#define OSI_COEX_MAGIC_VALUE 0xFADEBEAD
#define EXT_FUNC_VERSION 0x20220125
#define EXT_FUNC_VERSION 0x20221122
#define EXT_FUNC_MAGIC_VALUE 0xA5A5A5A5
#define BT_ASSERT_PRINT ets_printf
@@ -102,6 +104,7 @@ struct ext_funcs_t {
int (* _ecc_gen_key_pair)(uint8_t *public, uint8_t *priv);
int (* _ecc_gen_dh_key)(const uint8_t *remote_pub_key_x, const uint8_t *remote_pub_key_y, const uint8_t *local_priv_key, uint8_t *dhkey);
void (* _esp_reset_rpa_moudle)(void);
void (* _esp_bt_track_pll_cap)(void);
uint32_t magic;
};
@@ -232,7 +235,8 @@ struct ext_funcs_t ext_funcs_ro = {
static void IRAM_ATTR esp_reset_rpa_moudle(void)
{
periph_module_reset(PERIPH_MODEM_RPA_MODULE);
DPORT_SET_PERI_REG_MASK(SYSTEM_CORE_RST_EN_REG, BLE_RPA_REST_BIT);
DPORT_CLEAR_PERI_REG_MASK(SYSTEM_CORE_RST_EN_REG, BLE_RPA_REST_BIT);
}
static void IRAM_ATTR osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2)
@@ -399,7 +403,7 @@ static int ble_hci_unregistered_hook(void*, void*)
static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle_in)
{
int rc = esp_intr_alloc(source, flags, handler, arg, (intr_handle_t *)ret_handle_in);
int rc = esp_intr_alloc(source, flags | ESP_INTR_FLAG_IRAM, handler, arg, (intr_handle_t *)ret_handle_in);
return rc;
}
@@ -641,6 +645,8 @@ void ble_rtc_clk_init(void)
esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
{
esp_err_t ret = ESP_OK;
if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state");
return ESP_FAIL;
@@ -659,7 +665,6 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
/* Initialize the function pointers for OS porting */
npl_freertos_funcs_init();
struct npl_funcs_t *p_npl_funcs = npl_freertos_funcs_get();
if (!p_npl_funcs) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl functions get failed");
@@ -668,18 +673,21 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
if (esp_register_npl_funcs(p_npl_funcs) != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl functions register failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_mem;
}
if (npl_freertos_mempool_init() != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl mempool init failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_mem;
}
/* Initialize the global memory pool */
if (os_msys_buf_alloc() != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "os msys alloc failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_mem;
}
os_msys_init();
@@ -699,7 +707,8 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_phy;
}
#if CONFIG_SW_COEXIST_ENABLE
@@ -708,7 +717,8 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
int rc = ble_controller_init(cfg);
if (rc != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_init failed %d", rc);
return ESP_ERR_NO_MEM;
ret = ESP_ERR_NO_MEM;
goto free_phy;
}
controller_sleep_init();
@@ -724,8 +734,20 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
ble_hci_trans_cfg_hs((ble_hci_trans_rx_cmd_fn *)ble_hci_unregistered_hook,NULL,
(ble_hci_trans_rx_acl_fn *)ble_hci_unregistered_hook,NULL);
return ESP_OK;
free_phy:
esp_phy_disable();
esp_phy_pd_mem_deinit();
#if CONFIG_BT_NIMBLE_ENABLED
ble_npl_eventq_deinit(nimble_port_get_dflt_eventq());
#endif // CONFIG_BT_NIMBLE_ENABLED
free_mem:
os_msys_buf_free();
npl_freertos_mempool_deinit();
esp_unregister_npl_funcs();
npl_freertos_funcs_deinit();
esp_unregister_ext_funcs();
return ret;
}
esp_err_t esp_bt_controller_deinit(void)