From c205bbf90f8be39836e270bb5a598a0b347d43d8 Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Mon, 25 Dec 2023 12:23:18 +0800 Subject: [PATCH] fix(temperature_sensor): Fix the value is incorrect if disable and enable again Closes https://github.com/espressif/esp-idf/issues/12864 --- .../main/test_temperature_sensor.c | 13 +++++++------ components/esp_hw_support/sar_periph_ctrl_common.c | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/components/driver/test_apps/temperature_sensor/main/test_temperature_sensor.c b/components/driver/test_apps/temperature_sensor/main/test_temperature_sensor.c index 8bcffd0e3d..6bc7fe4e86 100644 --- a/components/driver/test_apps/temperature_sensor/main/test_temperature_sensor.c +++ b/components/driver/test_apps/temperature_sensor/main/test_temperature_sensor.c @@ -68,19 +68,20 @@ TEST_CASE("Double start error cause test", "[temperature_sensor]") TEST_CASE("Double Start-Stop test", "[temperature_sensor]") { 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_handle_t temp_handle = NULL; TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle)); TEST_ESP_OK(temperature_sensor_enable(temp_handle)); printf("Temperature sensor started\n"); - TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out)); - printf("Temperature out celsius %f°C\n", tsens_out); + TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_result0)); + printf("Temperature out celsius %f°C\n", tsens_result0); TEST_ESP_OK(temperature_sensor_disable(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_out)); - printf("Temperature out celsius %f°C\n", tsens_out); + TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_result1)); + printf("Temperature out celsius %f°C\n", tsens_result1); + TEST_ASSERT_FLOAT_WITHIN(4.0, tsens_result0, tsens_result1); TEST_ESP_OK(temperature_sensor_disable(temp_handle)); TEST_ESP_OK(temperature_sensor_uninstall(temp_handle)); } diff --git a/components/esp_hw_support/sar_periph_ctrl_common.c b/components/esp_hw_support/sar_periph_ctrl_common.c index 1173a82cef..2d016c4c1b 100644 --- a/components/esp_hw_support/sar_periph_ctrl_common.c +++ b/components/esp_hw_support/sar_periph_ctrl_common.c @@ -44,6 +44,10 @@ void temperature_sensor_power_acquire(void) temperature_sensor_ll_enable(true); } portEXIT_CRITICAL(&rtc_spinlock); + // 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); } void temperature_sensor_power_release(void)