From 0121a146996de422308fa4d7d4da50661e5e2a9f Mon Sep 17 00:00:00 2001 From: "C.S.M" Date: Wed, 2 Jul 2025 14:30:00 +0800 Subject: [PATCH] refactor(temperature_sensor): Move temperature calculation from hw_support to hal --- .../esp_driver_tsens/src/temperature_sensor.c | 3 +- components/esp_hw_support/CMakeLists.txt | 2 +- .../include/esp_private/sar_periph_ctrl.h | 9 +- ..._periph_ctrl_common.c => sar_tsens_ctrl.c} | 74 ++--------------- components/esp_pm/linker.lf | 9 +- components/hal/CMakeLists.txt | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../include/hal/temperature_sensor_ll.h | 4 + .../hal/include/hal/temperature_sensor_hal.h | 44 ++++++++++ components/hal/temperature_sensor_hal.c | 82 +++++++++++++++++++ 17 files changed, 184 insertions(+), 79 deletions(-) rename components/esp_hw_support/{sar_periph_ctrl_common.c => sar_tsens_ctrl.c} (53%) create mode 100644 components/hal/include/hal/temperature_sensor_hal.h create mode 100644 components/hal/temperature_sensor_hal.c diff --git a/components/esp_driver_tsens/src/temperature_sensor.c b/components/esp_driver_tsens/src/temperature_sensor.c index eb5c1514ec..d40db9b671 100644 --- a/components/esp_driver_tsens/src/temperature_sensor.c +++ b/components/esp_driver_tsens/src/temperature_sensor.c @@ -26,6 +26,7 @@ #include "temperature_sensor_private.h" #include "hal/temperature_sensor_ll.h" #include "soc/temperature_sensor_periph.h" +#include "hal/temperature_sensor_hal.h" #include "esp_memory_utils.h" #include "esp_private/sar_periph_ctrl.h" #include "esp_sleep.h" @@ -73,7 +74,7 @@ static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_ } } if (original_idx != -1) { - temp_sensor_sync_tsens_idx(original_idx); + temperature_sensor_hal_sync_tsens_idx(original_idx); } break; } diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index 08954cf9d7..60765f2c93 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -29,7 +29,7 @@ if(NOT non_os_build) "rtc_module.c" "regi2c_ctrl.c" "esp_gpio_reserve.c" - "sar_periph_ctrl_common.c" + "sar_tsens_ctrl.c" "port/${target}/io_mux.c" "port/${target}/esp_clk_tree.c" "dma/esp_dma_utils.c" diff --git a/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h index b6a0fdf741..58bb70e0c0 100644 --- a/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h +++ b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -85,13 +85,6 @@ void temperature_sensor_power_release(void); */ int16_t temp_sensor_get_raw_value(bool *range_changed); -/** - * @brief Synchronize the tsens_idx between sar_periph and driver - * - * @param tsens_idx index value of temperature sensor attribute - */ -void temp_sensor_sync_tsens_idx(int tsens_idx); - /** * @brief Enable SAR power when system wakes up */ diff --git a/components/esp_hw_support/sar_periph_ctrl_common.c b/components/esp_hw_support/sar_tsens_ctrl.c similarity index 53% rename from components/esp_hw_support/sar_periph_ctrl_common.c rename to components/esp_hw_support/sar_tsens_ctrl.c index ff37148231..c12385c322 100644 --- a/components/esp_hw_support/sar_periph_ctrl_common.c +++ b/components/esp_hw_support/sar_tsens_ctrl.c @@ -13,7 +13,7 @@ #if SOC_TEMP_SENSOR_SUPPORTED #include "hal/temperature_sensor_ll.h" -#include "soc/temperature_sensor_periph.h" +#include "hal/temperature_sensor_hal.h" #include "soc/periph_defs.h" #include "esp_private/periph_ctrl.h" #include "esp_private/adc_share_hw_ctrl.h" @@ -35,20 +35,10 @@ static const char *TAG_TSENS = "temperature_sensor"; #define TSENS_RCC_ATOMIC() #endif -#if CONFIG_PM_SLP_IRAM_OPT -# define SAR_PERIPH_CTRL_COMMON_FN_ATTR IRAM_ATTR -#else -# define SAR_PERIPH_CTRL_COMMON_FN_ATTR -#endif - #define TSENS_LINE_REGRESSION_US (200) -static int s_record_min = INT_NOT_USED; -static int s_record_max = INT_NOT_USED; static int s_temperature_sensor_power_cnt; static bool s_first_temp_read = false; - -static uint8_t s_tsens_idx = 2; // Index for temperature attribute, set 2(middle) as default value static int64_t timer1 = 0; void temperature_sensor_power_acquire(void) @@ -66,8 +56,8 @@ void temperature_sensor_power_acquire(void) temperature_sensor_ll_reset_module(); } temperature_sensor_ll_enable(true); - // Set the range as recorded. - temperature_sensor_ll_set_range(temperature_sensor_attributes[s_tsens_idx].reg_val); + // Initialize HAL layer with the same tsens_idx + temperature_sensor_hal_init(); timer1 = esp_timer_get_time(); } esp_os_exit_critical(&rtc_spinlock); @@ -95,19 +85,10 @@ void temperature_sensor_power_release(void) esp_os_exit_critical(&rtc_spinlock); } -static SAR_PERIPH_CTRL_COMMON_FN_ATTR int temperature_sensor_get_raw_value(void) -{ - int raw_value = temperature_sensor_ll_get_raw_value(); - return (TEMPERATURE_SENSOR_LL_ADC_FACTOR * raw_value - TEMPERATURE_SENSOR_LL_DAC_FACTOR * temperature_sensor_attributes[s_tsens_idx].offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR); -} - -void temp_sensor_sync_tsens_idx(int tsens_idx) -{ - s_tsens_idx = tsens_idx; -} - int16_t temp_sensor_get_raw_value(bool *range_changed) { + int16_t result; + esp_os_enter_critical(&rtc_spinlock); // When this is the first time reading a value, check whether the time here minus the @@ -122,49 +103,10 @@ int16_t temp_sensor_get_raw_value(bool *range_changed) s_first_temp_read = false; } - int degree = temperature_sensor_get_raw_value(); - uint8_t temperature_dac; - - // 1. Check whether temperature value is in range - if (s_record_min != INT_NOT_USED && degree >= s_record_min && degree <= s_record_max) { - // If degree is in range, not needed to do any check to save time. Otherwise, choose proper range and record. - if (range_changed != NULL) { - *range_changed = false; - } - esp_os_exit_critical(&rtc_spinlock); - return degree; - } - - // 2. If temperature value is not in range, adjust to proper range - if (degree >= temperature_sensor_attributes[1].range_max) { - s_tsens_idx = 0; - } else if (degree >= temperature_sensor_attributes[2].range_max && degree < temperature_sensor_attributes[1].range_max) { - s_tsens_idx = 1; - } else if (degree <= temperature_sensor_attributes[2].range_min && degree > temperature_sensor_attributes[3].range_min) { - s_tsens_idx = 3; - } else if (degree <= temperature_sensor_attributes[3].range_min) { - s_tsens_idx = 4; - } else { - s_tsens_idx = 2; - } - ESP_EARLY_LOGD(TAG_TSENS, "range changed, change to index %d", s_tsens_idx); - temperature_dac = temperature_sensor_attributes[s_tsens_idx].reg_val; - s_record_min = temperature_sensor_attributes[s_tsens_idx].range_min; - s_record_max = temperature_sensor_attributes[s_tsens_idx].range_max; - - temperature_sensor_ll_set_range(temperature_dac); - - // 3. Then, read value again - // Before reading the temperature value, ticks need to be delayed, otherwise a wrong value will be returned. - // As what has been recommended and tested, 300us is a good interval to get the correct value after adjust range. - esp_rom_delay_us(300); - degree = temperature_sensor_get_raw_value(); - if (range_changed != NULL) { - *range_changed = true; - } - + result = temperature_sensor_hal_get_degree(range_changed); esp_os_exit_critical(&rtc_spinlock); - return degree; + + return result; } #endif diff --git a/components/esp_pm/linker.lf b/components/esp_pm/linker.lf index 71984c2ab6..635b5aeb32 100644 --- a/components/esp_pm/linker.lf +++ b/components/esp_pm/linker.lf @@ -45,9 +45,9 @@ entries: if IDF_TARGET_ESP32H21 != y && IDF_TARGET_ESP32H4 != y: sar_periph_ctrl:sar_periph_ctrl_power_disable (noflash) if SOC_TEMP_SENSOR_SUPPORTED = y: - sar_periph_ctrl_common:temperature_sensor_power_acquire (noflash) - sar_periph_ctrl_common:temperature_sensor_power_release (noflash) - sar_periph_ctrl_common:temp_sensor_get_raw_value (noflash) + sar_tsens_ctrl:temperature_sensor_power_acquire (noflash) + sar_tsens_ctrl:temperature_sensor_power_release (noflash) + sar_tsens_ctrl:temp_sensor_get_raw_value (noflash) regi2c_ctrl:regi2c_saradc_enable (noflash) regi2c_ctrl:regi2c_saradc_disable (noflash) if SOC_ADC_SUPPORTED = y: @@ -89,3 +89,6 @@ entries: rtc_cntl_hal:rtc_cntl_hal_enable_cpu_retention (noflash) if PM_SLP_IRAM_OPT = y && PM_RESTORE_CACHE_TAGMEM_AFTER_LIGHT_SLEEP = y: rtc_cntl_hal:rtc_cntl_hal_enable_tagmem_retention (noflash) + if SOC_TEMP_SENSOR_SUPPORTED = y: + if PM_SLP_IRAM_OPT = y: + temperature_sensor_hal:temperature_sensor_hal_get_raw_value (noflash) diff --git a/components/hal/CMakeLists.txt b/components/hal/CMakeLists.txt index 6b31fca5ad..34200c5b63 100644 --- a/components/hal/CMakeLists.txt +++ b/components/hal/CMakeLists.txt @@ -325,6 +325,10 @@ elseif(NOT BOOTLOADER_BUILD) list(APPEND srcs "touch_sens_hal.c") endif() + if(CONFIG_SOC_TEMP_SENSOR_SUPPORTED) + list(APPEND srcs "temperature_sensor_hal.c") + endif() + if(${target} STREQUAL "esp32s2") list(APPEND srcs "xt_wdt_hal.c" diff --git a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h index 003c1fbafc..53ed7819ca 100644 --- a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h @@ -34,6 +34,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h index ea14092a46..9b319bfba4 100644 --- a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h @@ -34,6 +34,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32c5/include/hal/temperature_sensor_ll.h b/components/hal/esp32c5/include/hal/temperature_sensor_ll.h index 85035b7c84..b14a2a05de 100644 --- a/components/hal/esp32c5/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c5/include/hal/temperature_sensor_ll.h @@ -38,6 +38,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32c6/include/hal/temperature_sensor_ll.h b/components/hal/esp32c6/include/hal/temperature_sensor_ll.h index 54803190e3..82a228b75c 100644 --- a/components/hal/esp32c6/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c6/include/hal/temperature_sensor_ll.h @@ -38,6 +38,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32c61/include/hal/temperature_sensor_ll.h b/components/hal/esp32c61/include/hal/temperature_sensor_ll.h index 16aed27916..59412b75ed 100644 --- a/components/hal/esp32c61/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c61/include/hal/temperature_sensor_ll.h @@ -38,6 +38,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h index 50f3f11d78..df0edc2fed 100644 --- a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h @@ -37,6 +37,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32p4/include/hal/temperature_sensor_ll.h b/components/hal/esp32p4/include/hal/temperature_sensor_ll.h index 7db8114b99..0329349124 100644 --- a/components/hal/esp32p4/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32p4/include/hal/temperature_sensor_ll.h @@ -37,6 +37,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h index 8395ff5ec1..355bc3623a 100644 --- a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h @@ -31,6 +31,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h index 88834ee041..c6be40a443 100644 --- a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h @@ -31,6 +31,10 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT (4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT (278800) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT (205200) +#define TEMPERATURE_SENSOR_LL_DENOMINATOR (10000) #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) diff --git a/components/hal/include/hal/temperature_sensor_hal.h b/components/hal/include/hal/temperature_sensor_hal.h new file mode 100644 index 0000000000..bd76d46004 --- /dev/null +++ b/components/hal/include/hal/temperature_sensor_hal.h @@ -0,0 +1,44 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include "soc/soc_caps.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if SOC_TEMP_SENSOR_SUPPORTED + +/** + * @brief Get raw temperature value with range management + * + * @param range_changed Pointer to store whether range was changed (can be NULL) + * + * @return Temperature sensor raw value + */ +int16_t temperature_sensor_hal_get_degree(bool *range_changed); + +/** + * @brief Initialize temperature sensor HAL + */ +void temperature_sensor_hal_init(void); + +/** + * @brief Sync temperature sensor index from main layer to HAL layer + * + * @param tsens_idx Temperature sensor index to sync + */ +void temperature_sensor_hal_sync_tsens_idx(uint8_t tsens_idx); + +#endif // SOC_TEMP_SENSOR_SUPPORTED + +#ifdef __cplusplus +} +#endif diff --git a/components/hal/temperature_sensor_hal.c b/components/hal/temperature_sensor_hal.c new file mode 100644 index 0000000000..c09432bf78 --- /dev/null +++ b/components/hal/temperature_sensor_hal.c @@ -0,0 +1,82 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hal/temperature_sensor_hal.h" +#include "hal/temperature_sensor_ll.h" +#include "soc/temperature_sensor_periph.h" +#include "hal/log.h" +#include "esp_rom_sys.h" + +__attribute__((unused)) static const char *TAG_TSENS = "temperature_sensor_hal"; + +#define INT_NOT_USED 999999 + +// HAL layer maintains its own copies of these variables to avoid changing existing logic +static int s_hal_record_min = INT_NOT_USED; +static int s_hal_record_max = INT_NOT_USED; +static uint8_t s_hal_tsens_idx = 2; // Index for temperature attribute, set 2(middle) as default value + +int temperature_sensor_hal_get_raw_value(void) +{ + int raw_value = temperature_sensor_ll_get_raw_value(); + return (TEMPERATURE_SENSOR_LL_ADC_FACTOR_INT * raw_value - TEMPERATURE_SENSOR_LL_DAC_FACTOR_INT * temperature_sensor_attributes[s_hal_tsens_idx].offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR_INT) / TEMPERATURE_SENSOR_LL_DENOMINATOR; +} + +int16_t temperature_sensor_hal_get_degree(bool *range_changed) +{ + int degree = temperature_sensor_hal_get_raw_value(); + uint8_t temperature_dac; + + // 1. Check whether temperature value is in range + if (s_hal_record_min != INT_NOT_USED && degree >= s_hal_record_min && degree <= s_hal_record_max) { + // If degree is in range, not needed to do any check to save time. Otherwise, choose proper range and record. + if (range_changed != NULL) { + *range_changed = false; + } + return degree; + } + + // 2. If temperature value is not in range, adjust to proper range + if (degree >= temperature_sensor_attributes[1].range_max) { + s_hal_tsens_idx = 0; + } else if (degree >= temperature_sensor_attributes[2].range_max && degree < temperature_sensor_attributes[1].range_max) { + s_hal_tsens_idx = 1; + } else if (degree <= temperature_sensor_attributes[2].range_min && degree > temperature_sensor_attributes[3].range_min) { + s_hal_tsens_idx = 3; + } else if (degree <= temperature_sensor_attributes[3].range_min) { + s_hal_tsens_idx = 4; + } else { + s_hal_tsens_idx = 2; + } + HAL_LOGD(TAG_TSENS, "range changed, change to index %d", s_hal_tsens_idx); + temperature_dac = temperature_sensor_attributes[s_hal_tsens_idx].reg_val; + s_hal_record_min = temperature_sensor_attributes[s_hal_tsens_idx].range_min; + s_hal_record_max = temperature_sensor_attributes[s_hal_tsens_idx].range_max; + + temperature_sensor_ll_set_range(temperature_dac); + + // 3. Then, read value again + // Before reading the temperature value, ticks need to be delayed, otherwise a wrong value will be returned. + // As what has been recommended and tested, 300us is a good interval to get the correct value after adjust range. + esp_rom_delay_us(300); + degree = temperature_sensor_hal_get_raw_value(); + if (range_changed != NULL) { + *range_changed = true; + } + + return degree; +} + +void temperature_sensor_hal_init(void) +{ + // Set the range as recorded. + temperature_sensor_ll_set_range(temperature_sensor_attributes[s_hal_tsens_idx].reg_val); +} + +void temperature_sensor_hal_sync_tsens_idx(uint8_t tsens_idx) +{ + s_hal_tsens_idx = tsens_idx; +}