mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 10:47:19 +02:00
Merge branch 'bugfix/bunch_tsens_v5.1' into 'release/v5.1'
fix(temperature_sensor): Fix the value is incorrect if disable and enable again (backport v5.1) See merge request espressif/esp-idf!28625
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -15,6 +15,7 @@
|
|||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include "esp_private/spi_common_internal.h"
|
#include "esp_private/spi_common_internal.h"
|
||||||
#include "esp_private/periph_ctrl.h"
|
#include "esp_private/periph_ctrl.h"
|
||||||
|
#include "esp_private/adc_share_hw_ctrl.h"
|
||||||
#include "hal/spi_ll.h"
|
#include "hal/spi_ll.h"
|
||||||
#include "hal/dac_ll.h"
|
#include "hal/dac_ll.h"
|
||||||
#include "hal/adc_ll.h"
|
#include "hal/adc_ll.h"
|
||||||
@ -141,7 +142,7 @@ esp_err_t dac_dma_periph_init(uint32_t freq_hz, bool is_alternate, bool is_apll)
|
|||||||
esp_err_t ret = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
/* Acquire DMA peripheral */
|
/* Acquire DMA peripheral */
|
||||||
ESP_RETURN_ON_FALSE(spicommon_periph_claim(DAC_DMA_PERIPH_SPI_HOST, "dac_dma"), ESP_ERR_NOT_FOUND, TAG, "Failed to acquire DAC DMA peripheral");
|
ESP_RETURN_ON_FALSE(spicommon_periph_claim(DAC_DMA_PERIPH_SPI_HOST, "dac_dma"), ESP_ERR_NOT_FOUND, TAG, "Failed to acquire DAC DMA peripheral");
|
||||||
periph_module_enable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_claim();
|
||||||
/* Allocate DAC DMA peripheral object */
|
/* Allocate DAC DMA peripheral object */
|
||||||
s_ddp = (dac_dma_periph_spi_t *)heap_caps_calloc(1, sizeof(dac_dma_periph_spi_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
s_ddp = (dac_dma_periph_spi_t *)heap_caps_calloc(1, sizeof(dac_dma_periph_spi_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||||
ESP_GOTO_ON_FALSE(s_ddp, ESP_ERR_NO_MEM, err, TAG, "No memory for DAC DMA object");
|
ESP_GOTO_ON_FALSE(s_ddp, ESP_ERR_NO_MEM, err, TAG, "No memory for DAC DMA object");
|
||||||
@ -173,7 +174,7 @@ esp_err_t dac_dma_periph_deinit(void)
|
|||||||
}
|
}
|
||||||
ESP_RETURN_ON_FALSE(spicommon_periph_free(DAC_DMA_PERIPH_SPI_HOST), ESP_FAIL, TAG, "Failed to release DAC DMA peripheral");
|
ESP_RETURN_ON_FALSE(spicommon_periph_free(DAC_DMA_PERIPH_SPI_HOST), ESP_FAIL, TAG, "Failed to release DAC DMA peripheral");
|
||||||
spi_ll_disable_intr(s_ddp->periph_dev, SPI_LL_INTR_OUT_EOF | SPI_LL_INTR_OUT_TOTAL_EOF);
|
spi_ll_disable_intr(s_ddp->periph_dev, SPI_LL_INTR_OUT_EOF | SPI_LL_INTR_OUT_TOTAL_EOF);
|
||||||
periph_module_disable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_free();
|
||||||
if (s_ddp) {
|
if (s_ddp) {
|
||||||
if (s_ddp->use_apll) {
|
if (s_ddp->use_apll) {
|
||||||
periph_rtc_apll_release();
|
periph_rtc_apll_release();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -176,7 +176,7 @@ esp_err_t adc_digi_deinitialize(void)
|
|||||||
free(s_adc_digi_ctx);
|
free(s_adc_digi_ctx);
|
||||||
s_adc_digi_ctx = NULL;
|
s_adc_digi_ctx = NULL;
|
||||||
|
|
||||||
periph_module_disable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_free();
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@ -319,10 +319,7 @@ esp_err_t adc_digi_initialize(const adc_digi_init_config_t *init_config)
|
|||||||
};
|
};
|
||||||
adc_hal_dma_ctx_config(&s_adc_digi_ctx->hal, &config);
|
adc_hal_dma_ctx_config(&s_adc_digi_ctx->hal, &config);
|
||||||
|
|
||||||
//enable ADC digital part
|
adc_apb_periph_claim();
|
||||||
periph_module_enable(PERIPH_SARADC_MODULE);
|
|
||||||
//reset ADC digital part
|
|
||||||
periph_module_reset(PERIPH_SARADC_MODULE);
|
|
||||||
|
|
||||||
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
|
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
|
||||||
adc_hal_calibration_init(ADC_UNIT_1);
|
adc_hal_calibration_init(ADC_UNIT_1);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -776,7 +776,7 @@ int adc1_get_raw(adc1_channel_t channel)
|
|||||||
return ESP_ERR_TIMEOUT;
|
return ESP_ERR_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
periph_module_enable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_claim();
|
||||||
sar_periph_ctrl_adc_oneshot_power_acquire();
|
sar_periph_ctrl_adc_oneshot_power_acquire();
|
||||||
adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_DEFAULT);
|
adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_DEFAULT);
|
||||||
|
|
||||||
@ -791,7 +791,7 @@ int adc1_get_raw(adc1_channel_t channel)
|
|||||||
ADC_REG_LOCK_EXIT();
|
ADC_REG_LOCK_EXIT();
|
||||||
|
|
||||||
sar_periph_ctrl_adc_oneshot_power_release();
|
sar_periph_ctrl_adc_oneshot_power_release();
|
||||||
periph_module_disable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_free();
|
||||||
adc_lock_release(ADC_UNIT_1);
|
adc_lock_release(ADC_UNIT_1);
|
||||||
|
|
||||||
return raw_out;
|
return raw_out;
|
||||||
@ -833,7 +833,7 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
|
|||||||
return ESP_ERR_TIMEOUT;
|
return ESP_ERR_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
periph_module_enable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_claim();
|
||||||
sar_periph_ctrl_adc_oneshot_power_acquire();
|
sar_periph_ctrl_adc_oneshot_power_acquire();
|
||||||
adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_DEFAULT);
|
adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_DEFAULT);
|
||||||
|
|
||||||
@ -851,7 +851,7 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
|
|||||||
ADC_REG_LOCK_EXIT();
|
ADC_REG_LOCK_EXIT();
|
||||||
|
|
||||||
sar_periph_ctrl_adc_oneshot_power_release();
|
sar_periph_ctrl_adc_oneshot_power_release();
|
||||||
periph_module_disable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_free();
|
||||||
adc_lock_release(ADC_UNIT_2);
|
adc_lock_release(ADC_UNIT_2);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -175,6 +175,11 @@ esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
|
|||||||
|
|
||||||
temperature_sensor_ll_clk_sel(tsens->clk_src);
|
temperature_sensor_ll_clk_sel(tsens->clk_src);
|
||||||
temperature_sensor_power_acquire();
|
temperature_sensor_power_acquire();
|
||||||
|
// After enabling/reseting the temperature sensor,
|
||||||
|
// the output value gradually approaches the true temperature
|
||||||
|
// value as the measurement time increases. 300us is recommended.
|
||||||
|
esp_rom_delay_us(300);
|
||||||
|
|
||||||
tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
|
tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@ -226,7 +231,6 @@ esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, floa
|
|||||||
bool range_changed;
|
bool range_changed;
|
||||||
int16_t tsens_out = temp_sensor_get_raw_value(&range_changed);
|
int16_t tsens_out = temp_sensor_get_raw_value(&range_changed);
|
||||||
*out_celsius = parse_temp_sensor_raw_value(tsens_out);
|
*out_celsius = parse_temp_sensor_raw_value(tsens_out);
|
||||||
|
|
||||||
if (*out_celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *out_celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) {
|
if (*out_celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *out_celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) {
|
||||||
ESP_LOGE(TAG, "Exceeding temperature measure range.");
|
ESP_LOGE(TAG, "Exceeding temperature measure range.");
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
|
@ -68,19 +68,20 @@ TEST_CASE("Double start error cause test", "[temperature_sensor]")
|
|||||||
TEST_CASE("Double Start-Stop test", "[temperature_sensor]")
|
TEST_CASE("Double Start-Stop test", "[temperature_sensor]")
|
||||||
{
|
{
|
||||||
printf("Initializing Temperature sensor\n");
|
printf("Initializing Temperature sensor\n");
|
||||||
float tsens_out;
|
float tsens_result0;
|
||||||
|
float tsens_result1;
|
||||||
temperature_sensor_config_t temp_sensor = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
|
temperature_sensor_config_t temp_sensor = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
|
||||||
temperature_sensor_handle_t temp_handle = NULL;
|
temperature_sensor_handle_t temp_handle = NULL;
|
||||||
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
printf("Temperature sensor started\n");
|
printf("Temperature sensor started\n");
|
||||||
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_result0));
|
||||||
printf("Temperature out celsius %f°C\n", tsens_out);
|
printf("Temperature out celsius %f°C\n", tsens_result0);
|
||||||
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
printf("Temperature sensor started again\n");
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_result1));
|
||||||
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
printf("Temperature out celsius %f°C\n", tsens_result1);
|
||||||
printf("Temperature out celsius %f°C\n", tsens_out);
|
TEST_ASSERT_FLOAT_WITHIN(4.0, tsens_result0, tsens_result1);
|
||||||
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -19,42 +19,6 @@
|
|||||||
|
|
||||||
|
|
||||||
static const char *TAG = "adc_common";
|
static const char *TAG = "adc_common";
|
||||||
static portMUX_TYPE s_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
|
||||||
extern portMUX_TYPE rtc_spinlock;
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------------------
|
|
||||||
* For those who use APB_SARADC periph
|
|
||||||
*----------------------------------------------------------------------------*/
|
|
||||||
static int s_adc_digi_ctrlr_cnt;
|
|
||||||
|
|
||||||
void adc_apb_periph_claim(void)
|
|
||||||
{
|
|
||||||
portENTER_CRITICAL(&s_spinlock);
|
|
||||||
s_adc_digi_ctrlr_cnt++;
|
|
||||||
if (s_adc_digi_ctrlr_cnt == 1) {
|
|
||||||
//enable ADC digital part
|
|
||||||
periph_module_enable(PERIPH_SARADC_MODULE);
|
|
||||||
//reset ADC digital part
|
|
||||||
periph_module_reset(PERIPH_SARADC_MODULE);
|
|
||||||
}
|
|
||||||
|
|
||||||
portEXIT_CRITICAL(&s_spinlock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void adc_apb_periph_free(void)
|
|
||||||
{
|
|
||||||
portENTER_CRITICAL(&s_spinlock);
|
|
||||||
s_adc_digi_ctrlr_cnt--;
|
|
||||||
if (s_adc_digi_ctrlr_cnt == 0) {
|
|
||||||
periph_module_disable(PERIPH_SARADC_MODULE);
|
|
||||||
} else if (s_adc_digi_ctrlr_cnt < 0) {
|
|
||||||
portEXIT_CRITICAL(&s_spinlock);
|
|
||||||
ESP_LOGE(TAG, "%s called, but `s_adc_digi_ctrlr_cnt == 0`", __func__);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
portEXIT_CRITICAL(&s_spinlock);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------
|
/*---------------------------------------------------------------
|
||||||
ADC IOs
|
ADC IOs
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -236,10 +236,7 @@ esp_err_t adc_continuous_new_handle(const adc_continuous_handle_cfg_t *hdl_confi
|
|||||||
adc_ctx->fsm = ADC_FSM_INIT;
|
adc_ctx->fsm = ADC_FSM_INIT;
|
||||||
*ret_handle = adc_ctx;
|
*ret_handle = adc_ctx;
|
||||||
|
|
||||||
//enable ADC digital part
|
adc_apb_periph_claim();
|
||||||
periph_module_enable(PERIPH_SARADC_MODULE);
|
|
||||||
//reset ADC digital part
|
|
||||||
periph_module_reset(PERIPH_SARADC_MODULE);
|
|
||||||
|
|
||||||
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
|
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
|
||||||
adc_hal_calibration_init(ADC_UNIT_1);
|
adc_hal_calibration_init(ADC_UNIT_1);
|
||||||
@ -475,7 +472,7 @@ esp_err_t adc_continuous_deinit(adc_continuous_handle_t handle)
|
|||||||
free(handle);
|
free(handle);
|
||||||
handle = NULL;
|
handle = NULL;
|
||||||
|
|
||||||
periph_module_disable(PERIPH_SARADC_MODULE);
|
adc_apb_periph_free();
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -16,23 +16,6 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*------------------------------------------------------------------------------
|
|
||||||
* For those who use APB_SARADC periph
|
|
||||||
*----------------------------------------------------------------------------*/
|
|
||||||
/**
|
|
||||||
* @brief Claim the usage of the APB_SARADC periph
|
|
||||||
*
|
|
||||||
* Reference count inside
|
|
||||||
*/
|
|
||||||
void adc_apb_periph_claim(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Free the usage of the APB_SARADC periph
|
|
||||||
*
|
|
||||||
* Reference count inside
|
|
||||||
*/
|
|
||||||
void adc_apb_periph_free(void);
|
|
||||||
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------
|
/*---------------------------------------------------------------
|
||||||
ADC IOs
|
ADC IOs
|
||||||
|
@ -4,6 +4,7 @@ set(srcs "test_app_main.c"
|
|||||||
"test_adc_driver.c"
|
"test_adc_driver.c"
|
||||||
"test_adc_driver_iram.c"
|
"test_adc_driver_iram.c"
|
||||||
"test_adc_wifi.c"
|
"test_adc_wifi.c"
|
||||||
|
"test_adc_tsens.c"
|
||||||
"test_common_adc.c")
|
"test_common_adc.c")
|
||||||
|
|
||||||
# In order for the cases defined by `TEST_CASE` to be linked into the final elf,
|
# In order for the cases defined by `TEST_CASE` to be linked into the final elf,
|
||||||
|
83
components/esp_adc/test_apps/adc/main/test_adc_tsens.c
Normal file
83
components/esp_adc/test_apps/adc/main/test_adc_tsens.c
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "soc/adc_periph.h"
|
||||||
|
#include "esp_adc/adc_oneshot.h"
|
||||||
|
#include "driver/gpio.h"
|
||||||
|
#include "driver/rtc_io.h"
|
||||||
|
#include "driver/temperature_sensor.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
#include "test_common_adc.h"
|
||||||
|
#include "test_utils.h"
|
||||||
|
|
||||||
|
#if SOC_TEMP_SENSOR_SUPPORTED && SOC_ADC_SUPPORTED
|
||||||
|
|
||||||
|
static const char *TAG = "adc_tsens";
|
||||||
|
|
||||||
|
#define EXAMPLE_ADC1_CHAN0 ADC_CHANNEL_2
|
||||||
|
#define EXAMPLE_ADC1_CHAN1 ADC_CHANNEL_4
|
||||||
|
#define EXAMPLE_ADC_ATTEN ADC_ATTEN_DB_12
|
||||||
|
|
||||||
|
static int adc_raw[2][10];
|
||||||
|
|
||||||
|
TEST_CASE("Test temperature sensor cannot be influenced by ADC", "[adc]")
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Install temperature sensor, expected temp ranger range: 10~50 ℃");
|
||||||
|
temperature_sensor_handle_t temp_sensor = NULL;
|
||||||
|
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
|
||||||
|
TEST_ESP_OK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
|
||||||
|
int cnt = 2;
|
||||||
|
float tsens_value;
|
||||||
|
while (cnt--) {
|
||||||
|
temperature_sensor_enable(temp_sensor);
|
||||||
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
|
||||||
|
ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||||||
|
TEST_ESP_OK(temperature_sensor_disable(temp_sensor));
|
||||||
|
}
|
||||||
|
|
||||||
|
adc_oneshot_unit_handle_t adc1_handle;
|
||||||
|
adc_oneshot_unit_init_cfg_t init_config1 = {
|
||||||
|
.unit_id = ADC_UNIT_1,
|
||||||
|
};
|
||||||
|
TEST_ESP_OK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
||||||
|
|
||||||
|
//-------------ADC1 Config---------------//
|
||||||
|
adc_oneshot_chan_cfg_t config = {
|
||||||
|
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||||
|
.atten = EXAMPLE_ADC_ATTEN,
|
||||||
|
};
|
||||||
|
TEST_ESP_OK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN0, &config));
|
||||||
|
TEST_ESP_OK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN1, &config));
|
||||||
|
|
||||||
|
cnt = 2;
|
||||||
|
while (cnt--) {
|
||||||
|
TEST_ESP_OK(adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN0, &adc_raw[0][0]));
|
||||||
|
ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN0, adc_raw[0][0]);
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ESP_OK(adc_oneshot_del_unit(adc1_handle));
|
||||||
|
|
||||||
|
cnt = 2;
|
||||||
|
while (cnt--) {
|
||||||
|
temperature_sensor_enable(temp_sensor);
|
||||||
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
|
||||||
|
ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||||||
|
TEST_ESP_OK(temperature_sensor_disable(temp_sensor));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ESP_OK(temperature_sensor_uninstall(temp_sensor));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -28,6 +28,8 @@
|
|||||||
#include "hal/adc_hal_common.h"
|
#include "hal/adc_hal_common.h"
|
||||||
#include "esp_private/adc_share_hw_ctrl.h"
|
#include "esp_private/adc_share_hw_ctrl.h"
|
||||||
#include "esp_private/sar_periph_ctrl.h"
|
#include "esp_private/sar_periph_ctrl.h"
|
||||||
|
#include "esp_private/periph_ctrl.h"
|
||||||
|
#include "soc/periph_defs.h"
|
||||||
//For calibration
|
//For calibration
|
||||||
#if CONFIG_IDF_TARGET_ESP32S2
|
#if CONFIG_IDF_TARGET_ESP32S2
|
||||||
#include "esp_efuse_rtc_table.h"
|
#include "esp_efuse_rtc_table.h"
|
||||||
@ -190,3 +192,39 @@ esp_err_t adc2_wifi_release(void)
|
|||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static portMUX_TYPE s_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------------
|
||||||
|
* For those who use APB_SARADC periph
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
static int s_adc_digi_ctrlr_cnt;
|
||||||
|
|
||||||
|
void adc_apb_periph_claim(void)
|
||||||
|
{
|
||||||
|
portENTER_CRITICAL(&s_spinlock);
|
||||||
|
s_adc_digi_ctrlr_cnt++;
|
||||||
|
if (s_adc_digi_ctrlr_cnt == 1) {
|
||||||
|
//enable ADC digital part
|
||||||
|
periph_module_enable(PERIPH_SARADC_MODULE);
|
||||||
|
//reset ADC digital part
|
||||||
|
periph_module_reset(PERIPH_SARADC_MODULE);
|
||||||
|
}
|
||||||
|
|
||||||
|
portEXIT_CRITICAL(&s_spinlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void adc_apb_periph_free(void)
|
||||||
|
{
|
||||||
|
portENTER_CRITICAL(&s_spinlock);
|
||||||
|
s_adc_digi_ctrlr_cnt--;
|
||||||
|
if (s_adc_digi_ctrlr_cnt == 0) {
|
||||||
|
periph_module_disable(PERIPH_SARADC_MODULE);
|
||||||
|
} else if (s_adc_digi_ctrlr_cnt < 0) {
|
||||||
|
portEXIT_CRITICAL(&s_spinlock);
|
||||||
|
ESP_LOGE(TAG, "%s called, but `s_adc_digi_ctrlr_cnt == 0`", __func__);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
portEXIT_CRITICAL(&s_spinlock);
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -156,6 +156,24 @@ void adc2_cal_include(void);
|
|||||||
#define adc2_cal_include()
|
#define adc2_cal_include()
|
||||||
#endif //CONFIG_IDF_TARGET_*
|
#endif //CONFIG_IDF_TARGET_*
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------------
|
||||||
|
* For those who use APB_SARADC periph
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
/**
|
||||||
|
* @brief Claim the usage of the APB_SARADC periph
|
||||||
|
*
|
||||||
|
* Reference count inside
|
||||||
|
*/
|
||||||
|
void adc_apb_periph_claim(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Free the usage of the APB_SARADC periph
|
||||||
|
*
|
||||||
|
* Reference count inside
|
||||||
|
*/
|
||||||
|
void adc_apb_periph_free(void);
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -15,6 +15,7 @@
|
|||||||
#include "soc/temperature_sensor_periph.h"
|
#include "soc/temperature_sensor_periph.h"
|
||||||
#include "soc/periph_defs.h"
|
#include "soc/periph_defs.h"
|
||||||
#include "esp_private/periph_ctrl.h"
|
#include "esp_private/periph_ctrl.h"
|
||||||
|
#include "esp_private/adc_share_hw_ctrl.h"
|
||||||
|
|
||||||
extern __attribute__((unused)) portMUX_TYPE rtc_spinlock;
|
extern __attribute__((unused)) portMUX_TYPE rtc_spinlock;
|
||||||
|
|
||||||
@ -37,6 +38,7 @@ void temperature_sensor_power_acquire(void)
|
|||||||
portENTER_CRITICAL(&rtc_spinlock);
|
portENTER_CRITICAL(&rtc_spinlock);
|
||||||
s_temperature_sensor_power_cnt++;
|
s_temperature_sensor_power_cnt++;
|
||||||
if (s_temperature_sensor_power_cnt == 1) {
|
if (s_temperature_sensor_power_cnt == 1) {
|
||||||
|
adc_apb_periph_claim();
|
||||||
periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
|
periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
|
||||||
periph_module_reset(PERIPH_TEMPSENSOR_MODULE);
|
periph_module_reset(PERIPH_TEMPSENSOR_MODULE);
|
||||||
regi2c_saradc_enable();
|
regi2c_saradc_enable();
|
||||||
@ -60,6 +62,7 @@ void temperature_sensor_power_release(void)
|
|||||||
temperature_sensor_ll_enable(false);
|
temperature_sensor_ll_enable(false);
|
||||||
regi2c_saradc_disable();
|
regi2c_saradc_disable();
|
||||||
periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
|
periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
|
||||||
|
adc_apb_periph_free();
|
||||||
}
|
}
|
||||||
portEXIT_CRITICAL(&rtc_spinlock);
|
portEXIT_CRITICAL(&rtc_spinlock);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user