Skip to content

Commit bf335a3

Browse files
committed
fix(adc): fix adc do not enable/disable regi2c registers
1 parent d6fb99b commit bf335a3

File tree

38 files changed

+208
-359
lines changed

38 files changed

+208
-359
lines changed

components/bootloader_support/src/bootloader_random_esp32c5.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ void bootloader_random_enable(void)
1818
adc_ll_digi_controller_clk_div(0, 0, 0);
1919

2020
// some ADC sensor registers are in power group PERIF_I2C and need to be enabled via PMU
21-
regi2c_ctrl_ll_reset(false);
22-
regi2c_ctrl_ll_i2c_periph_enable();
21+
regi2c_ctrl_ll_i2c_sar_periph_enable();
2322
// enable analog i2c master clock for RNG runtime
2423
ANALOG_CLOCK_ENABLE();
2524

components/bootloader_support/src/bootloader_random_esp32c6.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ void bootloader_random_enable(void)
1818
adc_ll_digi_controller_clk_div(0, 0, 0);
1919

2020
// some ADC sensor registers are in power group PERIF_I2C and need to be enabled via PMU
21-
regi2c_ctrl_ll_reset(false);
22-
regi2c_ctrl_ll_i2c_periph_enable();
21+
regi2c_ctrl_ll_i2c_sar_periph_enable();
2322
// enable analog i2c master clock for RNG runtime
2423
ANALOG_CLOCK_ENABLE();
2524

components/bootloader_support/src/bootloader_random_esp32c61.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ void bootloader_random_enable(void)
1818
adc_ll_digi_controller_clk_div(0, 0, 0);
1919

2020
// some ADC sensor registers are in power group PERIF_I2C and need to be enabled via PMU
21-
regi2c_ctrl_ll_reset(false);
22-
regi2c_ctrl_ll_i2c_periph_enable();
21+
regi2c_ctrl_ll_i2c_sar_periph_enable();
2322
// enable analog i2c master clock for RNG runtime
2423
ANALOG_CLOCK_ENABLE();
2524

components/bootloader_support/src/bootloader_random_esp32h2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void bootloader_random_enable(void)
1818
adc_ll_digi_controller_clk_div(0, 0, 0);
1919

2020
// some ADC sensor registers are in power group PERIF_I2C and need to be enabled via PMU
21-
regi2c_ctrl_ll_i2c_periph_enable();
21+
regi2c_ctrl_ll_i2c_sar_periph_enable();
2222
// enable analog i2c master clock for RNG runtime
2323
ANALOG_CLOCK_ENABLE();
2424

components/driver/deprecated/adc_legacy.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -459,10 +459,6 @@ esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
459459
adc_lock_release(ADC_UNIT_2);
460460
#endif
461461

462-
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
463-
adc_hal_calibration_init(ADC_UNIT_2);
464-
#endif
465-
466462
return ESP_OK;
467463
}
468464

@@ -539,11 +535,6 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
539535
bitwidth = ADC_BITWIDTH_12;
540536
#endif
541537

542-
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
543-
adc_atten_t atten = adc_ll_get_atten(ADC_UNIT_2, channel);
544-
adc_set_hw_calibration_code(ADC_UNIT_2, atten);
545-
#endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
546-
547538
#if CONFIG_IDF_TARGET_ESP32
548539
/** For ESP32S2 and S3, the right to use ADC2 is controlled by the arbiter, and there is no need to set a lock.*/
549540
if (adc_lock_try_acquire(ADC_UNIT_2) != ESP_OK) {
@@ -553,6 +544,12 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
553544
#endif
554545
sar_periph_ctrl_adc_oneshot_power_acquire(); //in critical section with whole rtc module
555546

547+
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
548+
adc_hal_calibration_init(ADC_UNIT_2);
549+
adc_atten_t atten = adc_ll_get_atten(ADC_UNIT_2, channel);
550+
adc_set_hw_calibration_code(ADC_UNIT_2, atten);
551+
#endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
552+
556553
//avoid collision with other tasks
557554
adc2_init(); // in critical section with whole rtc module. because the PWDET use the same registers, place it here.
558555
SARADC2_ENTER();

components/driver/test_apps/legacy_adc_driver/main/test_legacy_adc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,23 @@ TEST_CASE("Legacy ADC oneshot high/low test", "[legacy_adc_oneshot]")
139139

140140
test_adc_set_io_level(ADC_UNIT_1, (adc1_channel_t)ADC1_TEST_CHAN0, 0);
141141
adc_raw = adc1_get_raw(ADC1_TEST_CHAN0);
142-
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_1, ADC1_TEST_CHAN0, adc_raw);
142+
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_1 + 1, ADC1_TEST_CHAN0, adc_raw);
143143
TEST_ASSERT_INT_WITHIN(ADC_TEST_LOW_THRESH, ADC_TEST_LOW_VAL, adc_raw);
144144

145145
test_adc_set_io_level(ADC_UNIT_1, (adc1_channel_t)ADC1_TEST_CHAN0, 1);
146146
adc_raw = adc1_get_raw(ADC1_TEST_CHAN0);
147-
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_1, ADC1_TEST_CHAN0, adc_raw);
147+
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_1 + 1, ADC1_TEST_CHAN0, adc_raw);
148148
TEST_ASSERT_INT_WITHIN(ADC_TEST_HIGH_THRESH, ADC_TEST_HIGH_VAL, adc_raw);
149149

150150
#if ADC_TEST_ADC2
151151
test_adc_set_io_level(ADC_UNIT_2, (adc2_channel_t)ADC2_TEST_CHAN0, 0);
152152
TEST_ESP_OK(adc2_get_raw(ADC2_TEST_CHAN0, ADC_WIDTH_BIT_DEFAULT, &adc_raw));
153-
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_2, ADC2_TEST_CHAN0, adc_raw);
153+
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_2 + 1, ADC2_TEST_CHAN0, adc_raw);
154154
TEST_ASSERT_INT_WITHIN(ADC_TEST_LOW_THRESH, ADC_TEST_LOW_VAL, adc_raw);
155155

156156
test_adc_set_io_level(ADC_UNIT_2, (adc2_channel_t)ADC2_TEST_CHAN0, 1);
157157
TEST_ESP_OK(adc2_get_raw(ADC2_TEST_CHAN0, ADC_WIDTH_BIT_DEFAULT, &adc_raw));
158-
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_2, ADC2_TEST_CHAN0, adc_raw);
158+
ESP_LOGI(TAG, "ADC%d Channel %d raw: %d", ADC_UNIT_2 + 1, ADC2_TEST_CHAN0, adc_raw);
159159
TEST_ASSERT_INT_WITHIN(ADC_TEST_HIGH_THRESH, ADC_TEST_HIGH_VAL, adc_raw);
160160
#endif
161161
}

components/esp_adc/adc_continuous.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2016-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -258,10 +258,6 @@ esp_err_t adc_continuous_start(adc_continuous_handle_t handle)
258258
ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_INIT, ESP_ERR_INVALID_STATE, ADC_TAG, "ADC continuous mode isn't in the init state, it's started already");
259259

260260
ANALOG_CLOCK_ENABLE();
261-
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
262-
adc_hal_calibration_init(ADC_UNIT_1);
263-
adc_hal_calibration_init(ADC_UNIT_2);
264-
#endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
265261

266262
//reset ADC digital part to reset ADC sampling EOF counter
267263
ADC_BUS_CLK_ATOMIC() {
@@ -284,9 +280,11 @@ esp_err_t adc_continuous_start(adc_continuous_handle_t handle)
284280

285281
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
286282
if (handle->use_adc1) {
283+
adc_hal_calibration_init(ADC_UNIT_1);
287284
adc_set_hw_calibration_code(ADC_UNIT_1, handle->adc1_atten);
288285
}
289286
if (handle->use_adc2) {
287+
adc_hal_calibration_init(ADC_UNIT_2);
290288
adc_set_hw_calibration_code(ADC_UNIT_2, handle->adc2_atten);
291289
}
292290
#endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED

components/esp_hw_support/port/esp32c2/sar_periph_ctrl.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -19,6 +19,7 @@
1919
#include "esp_log.h"
2020
#include "freertos/FreeRTOS.h"
2121
#include "esp_private/sar_periph_ctrl.h"
22+
#include "esp_private/regi2c_ctrl.h"
2223
#include "hal/sar_ctrl_ll.h"
2324
#include "hal/adc_ll.h"
2425

@@ -83,13 +84,14 @@ void sar_periph_ctrl_pwdet_power_release(void)
8384
/*------------------------------------------------------------------------------
8485
* ADC Power
8586
*----------------------------------------------------------------------------*/
86-
static int s_saradc_power_on_cnt;
87+
static int s_sar_power_on_cnt;
8788

8889
static void s_sar_adc_power_acquire(void)
8990
{
9091
portENTER_CRITICAL_SAFE(&rtc_spinlock);
91-
s_saradc_power_on_cnt++;
92-
if (s_saradc_power_on_cnt == 1) {
92+
regi2c_saradc_enable();
93+
s_sar_power_on_cnt++;
94+
if (s_sar_power_on_cnt == 1) {
9395
adc_ll_digi_set_power_manage(ADC_LL_POWER_SW_ON);
9496
}
9597
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
@@ -98,14 +100,15 @@ static void s_sar_adc_power_acquire(void)
98100
static void s_sar_adc_power_release(void)
99101
{
100102
portENTER_CRITICAL_SAFE(&rtc_spinlock);
101-
s_saradc_power_on_cnt--;
102-
if (s_saradc_power_on_cnt < 0) {
103+
s_sar_power_on_cnt--;
104+
if (s_sar_power_on_cnt < 0) {
103105
portEXIT_CRITICAL(&rtc_spinlock);
104-
ESP_LOGE(TAG, "%s called, but s_saradc_power_on_cnt == 0", __func__);
106+
ESP_LOGE(TAG, "%s called, but s_sar_power_on_cnt == 0", __func__);
105107
abort();
106-
} else if (s_saradc_power_on_cnt == 0) {
108+
} else if (s_sar_power_on_cnt == 0) {
107109
adc_ll_digi_set_power_manage(ADC_LL_POWER_BY_FSM);
108110
}
111+
regi2c_saradc_disable();
109112
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
110113
}
111114

components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -19,6 +19,7 @@
1919
#include "esp_log.h"
2020
#include "freertos/FreeRTOS.h"
2121
#include "esp_private/sar_periph_ctrl.h"
22+
#include "esp_private/regi2c_ctrl.h"
2223
#include "hal/sar_ctrl_ll.h"
2324
#include "hal/adc_ll.h"
2425

@@ -83,13 +84,14 @@ void sar_periph_ctrl_pwdet_power_release(void)
8384
/*------------------------------------------------------------------------------
8485
* ADC Power
8586
*----------------------------------------------------------------------------*/
86-
static int s_saradc_power_on_cnt;
87+
static int s_sar_power_on_cnt;
8788

8889
static void s_sar_adc_power_acquire(void)
8990
{
9091
portENTER_CRITICAL_SAFE(&rtc_spinlock);
91-
s_saradc_power_on_cnt++;
92-
if (s_saradc_power_on_cnt == 1) {
92+
regi2c_saradc_enable();
93+
s_sar_power_on_cnt++;
94+
if (s_sar_power_on_cnt == 1) {
9395
adc_ll_digi_set_power_manage(ADC_LL_POWER_SW_ON);
9496
}
9597
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
@@ -98,14 +100,15 @@ static void s_sar_adc_power_acquire(void)
98100
static void s_sar_adc_power_release(void)
99101
{
100102
portENTER_CRITICAL_SAFE(&rtc_spinlock);
101-
s_saradc_power_on_cnt--;
102-
if (s_saradc_power_on_cnt < 0) {
103+
s_sar_power_on_cnt--;
104+
if (s_sar_power_on_cnt < 0) {
103105
portEXIT_CRITICAL(&rtc_spinlock);
104-
ESP_LOGE(TAG, "%s called, but s_saradc_power_on_cnt == 0", __func__);
106+
ESP_LOGE(TAG, "%s called, but s_sar_power_on_cnt == 0", __func__);
105107
abort();
106-
} else if (s_saradc_power_on_cnt == 0) {
108+
} else if (s_sar_power_on_cnt == 0) {
107109
adc_ll_digi_set_power_manage(ADC_LL_POWER_BY_FSM);
108110
}
111+
regi2c_saradc_disable();
109112
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
110113
}
111114

components/esp_hw_support/port/esp32c5/pmu_init.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ static void pmu_lp_system_init_default(pmu_context_t *ctx)
208208

209209
void pmu_init(void)
210210
{
211-
/* Peripheral reg i2c power up */
212-
regi2c_ctrl_ll_reset(false);
213-
regi2c_ctrl_ll_i2c_periph_enable();
214-
215211
pmu_hp_system_init_default(PMU_instance());
216212
pmu_lp_system_init_default(PMU_instance());
217213

0 commit comments

Comments
 (0)