fix(adc): fix ESP32-P4 ADC2 oneshot error and refactor apb claim macor

This commit is contained in:
gaoxu
2025-09-10 14:38:47 +08:00
committed by Gao Xu
parent 060b4c9ef4
commit 5e06c779b0
11 changed files with 44 additions and 23 deletions
+9 -3
View File
@@ -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
+20 -20
View File
@@ -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();
@@ -277,9 +277,6 @@ esp_err_t adc_oneshot_del_unit(adc_oneshot_unit_handle_t handle)
s_ctx.units[handle->unit_id] = NULL;
_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();
} else {
@@ -288,16 +285,19 @@ 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
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.
@@ -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
---------------------------------------------------------------*/
@@ -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
---------------------------------------------------------------*/
@@ -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
---------------------------------------------------------------*/
@@ -39,6 +39,7 @@ 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)
#define ADC_LL_ADC_FE_ON_MODEM_DOMAIN (1)
/*---------------------------------------------------------------
Oneshot
@@ -38,6 +38,7 @@ 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)
#define ADC_LL_ADC_FE_ON_MODEM_DOMAIN (1)
/*---------------------------------------------------------------
Oneshot
@@ -38,6 +38,7 @@ 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)
#define ADC_LL_ADC_FE_ON_MODEM_DOMAIN (1)
/*---------------------------------------------------------------
Oneshot
@@ -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
---------------------------------------------------------------*/
@@ -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
---------------------------------------------------------------*/
@@ -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
---------------------------------------------------------------*/