diff --git a/components/esp_adc/adc_common.c b/components/esp_adc/adc_common.c index 1b5a8d599e..93720e405d 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 */ @@ -17,6 +17,7 @@ #include "hal/adc_hal_common.h" #include "esp_private/regi2c_ctrl.h" #include "soc/adc_periph.h" +#include "hal/adc_ll.h" static const char *TAG = "adc_common"; @@ -57,10 +58,13 @@ 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(); ANALOG_CLOCK_ENABLE(); //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++) { /** @@ -75,6 +79,9 @@ static __attribute__((constructor)) void adc_hw_calibration(void) } #endif } + if (ADC_LL_NEED_APB_PERIPH_CLAIM(i)) { + adc_apb_periph_free(); + } } ANALOG_CLOCK_DISABLE(); } diff --git a/components/esp_adc/adc_oneshot.c b/components/esp_adc/adc_oneshot.c index 1af0bdb8db..0d185ba341 100644 --- a/components/esp_adc/adc_oneshot.c +++ b/components/esp_adc/adc_oneshot.c @@ -144,15 +144,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); -#if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED - //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); -#endif if (init_config->ulp_mode == ADC_ULP_MODE_DISABLE) { sar_periph_ctrl_adc_oneshot_power_acquire(); @@ -297,18 +297,20 @@ esp_err_t adc_oneshot_del_unit(adc_oneshot_unit_handle_t handle) #endif ESP_ERROR_CHECK(esp_clk_tree_enable_src((soc_module_clk_t)(handle->hal.clk_src), false)); } - free(handle); -#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 + + ESP_LOGD(TAG, "adc unit%"PRId32" is deleted", handle->unit_id); + free(handle); #if CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP //TODO: IDF-8475: Depends to SLEEP_RETENTION_MODULE_CLOCK_MODEM retention module after ADC retention supported. diff --git a/components/hal/esp32/include/hal/adc_ll.h b/components/hal/esp32/include/hal/adc_ll.h index d4cca33a47..8c939d5c8b 100644 --- a/components/hal/esp32/include/hal/adc_ll.h +++ b/components/hal/esp32/include/hal/adc_ll.h @@ -26,7 +26,8 @@ extern "C" { #define ADC_LL_EVENT_ADC1_ONESHOT_DONE (1 << 0) #define ADC_LL_EVENT_ADC2_ONESHOT_DONE (1 << 1) -#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION 0 +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#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 93ef061fd2..c169dd7812 100644 --- a/components/hal/esp32c3/include/hal/adc_ll.h +++ b/components/hal/esp32c3/include/hal/adc_ll.h @@ -38,7 +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_UNIT2_CHANNEL_SUBSTRATION 0 +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) /*--------------------------------------------------------------- Oneshot diff --git a/components/hal/esp32c5/include/hal/adc_ll.h b/components/hal/esp32c5/include/hal/adc_ll.h index 0483dc9d8b..21e616d5c2 100644 --- a/components/hal/esp32c5/include/hal/adc_ll.h +++ b/components/hal/esp32c5/include/hal/adc_ll.h @@ -40,8 +40,9 @@ extern "C" { #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_ADC_FE_ON_MODEM_DOMAIN (1) +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) -#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION 0 /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32c6/include/hal/adc_ll.h b/components/hal/esp32c6/include/hal/adc_ll.h index 7fe369d11f..6afdfa2c75 100644 --- a/components/hal/esp32c6/include/hal/adc_ll.h +++ b/components/hal/esp32c6/include/hal/adc_ll.h @@ -39,8 +39,9 @@ extern "C" { #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_ADC_FE_ON_MODEM_DOMAIN (1) +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) -#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION 0 /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32c61/include/hal/adc_ll.h b/components/hal/esp32c61/include/hal/adc_ll.h index 4b1b1addc9..b69c567e66 100644 --- a/components/hal/esp32c61/include/hal/adc_ll.h +++ b/components/hal/esp32c61/include/hal/adc_ll.h @@ -40,8 +40,9 @@ extern "C" { #define ADC_LL_GET_LOW_THRES_MASK(monitor_id) ((monitor_id == 0) ? SARADC_THRES0_LOW_INT_ST_M : SARADC_THRES1_LOW_INT_ST_M) #define ADC_LL_ADC_FE_ON_MODEM_DOMAIN (1) +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) -#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION 0 /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32h2/include/hal/adc_ll.h b/components/hal/esp32h2/include/hal/adc_ll.h index 3e2baaa7ab..0254ea1480 100644 --- a/components/hal/esp32h2/include/hal/adc_ll.h +++ b/components/hal/esp32h2/include/hal/adc_ll.h @@ -39,8 +39,9 @@ extern "C" { #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_ADC_FE_ON_MODEM_DOMAIN (1) +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (1) -#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION 0 /*--------------------------------------------------------------- Oneshot ---------------------------------------------------------------*/ diff --git a/components/hal/esp32p4/include/hal/adc_ll.h b/components/hal/esp32p4/include/hal/adc_ll.h index f3c7487c1d..a5e87fa1cd 100644 --- a/components/hal/esp32p4/include/hal/adc_ll.h +++ b/components/hal/esp32p4/include/hal/adc_ll.h @@ -35,8 +35,7 @@ extern "C" { #define LP_ADC_FORCE_XPD_SAR_PU 3 // Force power up // ESP32P4 ADC2 channel is 2-7, so we need to subtract 2 to get the correct channel -#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION 2 - +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (2) #define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (((ADC_UNIT) == ADC_UNIT_1) ? 0 : 1) /*--------------------------------------------------------------- diff --git a/components/hal/esp32s2/include/hal/adc_ll.h b/components/hal/esp32s2/include/hal/adc_ll.h index bde4116bde..203cf9b5d5 100644 --- a/components/hal/esp32s2/include/hal/adc_ll.h +++ b/components/hal/esp32s2/include/hal/adc_ll.h @@ -36,7 +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_UNIT2_CHANNEL_SUBSTRATION 0 +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#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 2e14adad1c..064bc28c7c 100644 --- a/components/hal/esp32s3/include/hal/adc_ll.h +++ b/components/hal/esp32s3/include/hal/adc_ll.h @@ -38,7 +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_UNIT2_CHANNEL_SUBSTRATION 0 +#define ADC_LL_UNIT2_CHANNEL_SUBSTRATION (0) +#define ADC_LL_NEED_APB_PERIPH_CLAIM(ADC_UNIT) (0) /*--------------------------------------------------------------- Oneshot