refactor(temperature_sensor): Move temperature calculation from hw_support to hal

This commit is contained in:
C.S.M
2025-07-02 14:30:00 +08:00
parent 29af244a49
commit 0121a14699
17 changed files with 184 additions and 79 deletions

View File

@@ -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;
}

View File

@@ -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"

View File

@@ -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
*/

View File

@@ -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

View File

@@ -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)

View File

@@ -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"

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -0,0 +1,44 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#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

View File

@@ -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;
}