diff --git a/components/esp_adc/adc_common.c b/components/esp_adc/adc_common.c index 6f393dd43f..1bdb320d5a 100644 --- a/components/esp_adc/adc_common.c +++ b/components/esp_adc/adc_common.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -16,6 +16,7 @@ #include "hal/adc_hal.h" #include "hal/adc_hal_common.h" #include "soc/adc_periph.h" +#include "hal/adc_ll.h" static const char *TAG = "adc_common"; @@ -56,9 +57,12 @@ esp_err_t adc_channel_to_io(adc_unit_t unit_id, adc_channel_t channel, int * con ---------------------------------------------------------------*/ static __attribute__((constructor)) void adc_hw_calibration(void) { - adc_apb_periph_claim(); //Calculate all ICode for (int i = 0; i < SOC_ADC_PERIPH_NUM; i++) { + if (ADC_LL_NEED_APB_PERIPH_CLAIM(i)) { + adc_apb_periph_claim(); + } + adc_hal_calibration_init(i); for (int j = 0; j < SOC_ADC_ATTEN_NUM; j++) { /** @@ -73,7 +77,9 @@ static __attribute__((constructor)) void adc_hw_calibration(void) } #endif } + if (ADC_LL_NEED_APB_PERIPH_CLAIM(i)) { + adc_apb_periph_free(); + } } - adc_apb_periph_free(); } #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED diff --git a/components/esp_adc/adc_oneshot.c b/components/esp_adc/adc_oneshot.c index 0691e22f1a..e6ff4d03ea 100644 --- a/components/esp_adc/adc_oneshot.c +++ b/components/esp_adc/adc_oneshot.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -115,13 +115,15 @@ esp_err_t adc_oneshot_new_unit(const adc_oneshot_unit_init_cfg_t *init_config, a }; adc_oneshot_hal_init(&(unit->hal), &config); - //To enable the APB_SARADC periph if needed - _lock_acquire(&s_ctx.mutex); - s_ctx.apb_periph_ref_cnts++; - if (s_ctx.apb_periph_ref_cnts == 1) { - adc_apb_periph_claim(); + if (ADC_LL_NEED_APB_PERIPH_CLAIM(unit->unit_id)) { + //To enable the APB_SARADC periph if needed + _lock_acquire(&s_ctx.mutex); + s_ctx.apb_periph_ref_cnts++; + if (s_ctx.apb_periph_ref_cnts == 1) { + adc_apb_periph_claim(); + } + _lock_release(&s_ctx.mutex); } - _lock_release(&s_ctx.mutex); if (init_config->ulp_mode == ADC_ULP_MODE_DISABLE) { sar_periph_ctrl_adc_oneshot_power_acquire(); @@ -224,7 +226,6 @@ esp_err_t adc_oneshot_del_unit(adc_oneshot_unit_handle_t handle) _lock_release(&s_ctx.mutex); ESP_LOGD(TAG, "adc unit%"PRId32" is deleted", handle->unit_id); - free(handle); if (ulp_mode == ADC_ULP_MODE_DISABLE) { sar_periph_ctrl_adc_oneshot_power_release(); @@ -234,16 +235,18 @@ esp_err_t adc_oneshot_del_unit(adc_oneshot_unit_handle_t handle) #endif } -#if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED - //To free the APB_SARADC periph if needed - _lock_acquire(&s_ctx.mutex); - s_ctx.apb_periph_ref_cnts--; - assert(s_ctx.apb_periph_ref_cnts >= 0); - if (s_ctx.apb_periph_ref_cnts == 0) { - adc_apb_periph_free(); + if (ADC_LL_NEED_APB_PERIPH_CLAIM(handle->unit_id)) { + //To free the APB_SARADC periph if needed + _lock_acquire(&s_ctx.mutex); + s_ctx.apb_periph_ref_cnts--; + assert(s_ctx.apb_periph_ref_cnts >= 0); + if (s_ctx.apb_periph_ref_cnts == 0) { + adc_apb_periph_free(); + } + _lock_release(&s_ctx.mutex); } - _lock_release(&s_ctx.mutex); -#endif + + free(handle); return ESP_OK; } diff --git a/components/hal/esp32/include/hal/adc_ll.h b/components/hal/esp32/include/hal/adc_ll.h index 8f412ea3d6..80f997b448 100644 --- a/components/hal/esp32/include/hal/adc_ll.h +++ b/components/hal/esp32/include/hal/adc_ll.h @@ -25,6 +25,8 @@ extern "C" { #define ADC_LL_EVENT_ADC1_ONESHOT_DONE (1 << 0) #define ADC_LL_EVENT_ADC2_ONESHOT_DONE (1 << 1) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (0) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32c2/include/hal/adc_ll.h b/components/hal/esp32c2/include/hal/adc_ll.h index 2e234d740c..b54453820e 100644 --- a/components/hal/esp32c2/include/hal/adc_ll.h +++ b/components/hal/esp32c2/include/hal/adc_ll.h @@ -30,6 +30,8 @@ extern "C" { #define ADC_LL_EVENT_ADC1_ONESHOT_DONE BIT(31) #define ADC_LL_EVENT_ADC2_ONESHOT_DONE BIT(30) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32c3/include/hal/adc_ll.h b/components/hal/esp32c3/include/hal/adc_ll.h index 779312f4fa..5d0d3ec523 100644 --- a/components/hal/esp32c3/include/hal/adc_ll.h +++ b/components/hal/esp32c3/include/hal/adc_ll.h @@ -38,6 +38,8 @@ extern "C" { #define ADC_LL_GET_HIGH_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_THRES0_HIGH_INT_ST_M : APB_SARADC_THRES1_HIGH_INT_ST_M) #define ADC_LL_GET_LOW_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_THRES0_LOW_INT_ST_M : APB_SARADC_THRES1_LOW_INT_ST_M) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32c6/include/hal/adc_ll.h b/components/hal/esp32c6/include/hal/adc_ll.h index 2d2e553349..8a55c46f85 100644 --- a/components/hal/esp32c6/include/hal/adc_ll.h +++ b/components/hal/esp32c6/include/hal/adc_ll.h @@ -38,6 +38,8 @@ extern "C" { #define ADC_LL_GET_HIGH_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_APB_SARADC_THRES0_HIGH_INT_ST_M : APB_SARADC_APB_SARADC_THRES1_HIGH_INT_ST_M) #define ADC_LL_GET_LOW_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_APB_SARADC_THRES0_LOW_INT_ST_M : APB_SARADC_APB_SARADC_THRES1_LOW_INT_ST_M) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32h2/include/hal/adc_ll.h b/components/hal/esp32h2/include/hal/adc_ll.h index e64cca4d2a..137a1ed160 100644 --- a/components/hal/esp32h2/include/hal/adc_ll.h +++ b/components/hal/esp32h2/include/hal/adc_ll.h @@ -38,6 +38,8 @@ extern "C" { #define ADC_LL_GET_HIGH_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_APB_SARADC_THRES0_HIGH_INT_ST_M : APB_SARADC_APB_SARADC_THRES1_HIGH_INT_ST_M) #define ADC_LL_GET_LOW_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_APB_SARADC_THRES0_LOW_INT_ST_M : APB_SARADC_APB_SARADC_THRES1_LOW_INT_ST_M) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32p4/include/hal/adc_ll.h b/components/hal/esp32p4/include/hal/adc_ll.h index 881cfd0c63..0ccefdae6c 100644 --- a/components/hal/esp32p4/include/hal/adc_ll.h +++ b/components/hal/esp32p4/include/hal/adc_ll.h @@ -34,6 +34,8 @@ extern "C" { #define LP_ADC_FORCE_XPD_SAR_PD 2 // Force power down #define LP_ADC_FORCE_XPD_SAR_PU 3 // Force power up +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (((ADC_UNIT) == ADC_UNIT_1) ? 0 : 1) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32s2/include/hal/adc_ll.h b/components/hal/esp32s2/include/hal/adc_ll.h index d0b558cc61..7bbc67711a 100644 --- a/components/hal/esp32s2/include/hal/adc_ll.h +++ b/components/hal/esp32s2/include/hal/adc_ll.h @@ -36,6 +36,8 @@ extern "C" { #define ADC_LL_GET_HIGH_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_ADC1_THRES_INT_ST_M : APB_SARADC_ADC2_THRES_INT_ST_M) #define ADC_LL_GET_LOW_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_ADC1_THRES_INT_ST_M : APB_SARADC_ADC2_THRES_INT_ST_M) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (0) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32s3/include/hal/adc_ll.h b/components/hal/esp32s3/include/hal/adc_ll.h index ec8180c048..9205cdc5fd 100644 --- a/components/hal/esp32s3/include/hal/adc_ll.h +++ b/components/hal/esp32s3/include/hal/adc_ll.h @@ -38,6 +38,8 @@ extern "C" { #define ADC_LL_GET_HIGH_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_THRES0_HIGH_INT_ST_M : APB_SARADC_THRES1_HIGH_INT_ST_M) #define ADC_LL_GET_LOW_THRES_MASK(monitor_id) ((monitor_id == 0) ? APB_SARADC_THRES0_LOW_INT_ST_M : APB_SARADC_THRES1_LOW_INT_ST_M) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (0) + /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/