feat(tsens): graduate temperature sensor hal component

This commit is contained in:
laokaiyao
2025-12-08 14:46:21 +08:00
parent 7c1e88e3d4
commit 2cb958a9e3
35 changed files with 126 additions and 91 deletions
@@ -10,5 +10,6 @@ endif()
idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${public_include}
REQUIRES esp_hal_ana_conv
PRIV_REQUIRES ${priv_req}
)
@@ -25,7 +25,7 @@
#include "esp_private/periph_ctrl.h"
#include "temperature_sensor_private.h"
#include "hal/temperature_sensor_ll.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "hal/temperature_sensor_hal.h"
#include "esp_memory_utils.h"
#include "esp_private/sar_periph_ctrl.h"
@@ -7,7 +7,7 @@
#pragma once
#include <stdlib.h>
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "hal/temperature_sensor_types.h"
#include "driver/temperature_sensor.h"
#include "esp_intr_alloc.h"
@@ -16,7 +16,7 @@
#include "nvs_flash.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "unity.h"
#include "unity_test_utils.h"
#include "driver/temperature_sensor.h"
@@ -15,6 +15,7 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${target}/include")
list(APPEND includes "${target}/include")
endif()
# ADC related source files
if(CONFIG_SOC_ADC_SUPPORTED)
list(APPEND srcs "${target}/adc_periph.c" "adc_hal_common.c" "adc_oneshot_hal.c")
if(CONFIG_SOC_ADC_DMA_SUPPORTED)
@@ -22,10 +23,16 @@ if(CONFIG_SOC_ADC_SUPPORTED)
endif()
endif()
# DAC related source files
if(CONFIG_SOC_DAC_SUPPORTED)
list(APPEND srcs "${target}/dac_periph.c")
endif()
# Temperature sensor related source files
if(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
list(APPEND srcs "temperature_sensor_hal.c" "${target}/temperature_sensor_periph.c")
endif()
idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${includes}
REQUIRES ${requires})
+31 -2
View File
@@ -5,7 +5,7 @@
## Overview
The `esp_hal_ana_conv` component provides a **Hardware Abstraction Layer** for analog conversion related peripherals across all ESP-IDF supported targets. This HAL currently supports Analog-to-Digital Converter (ADC), Digital-to-Analog Converter (DAC) and other analog conversion related peripherals. The HAL enables analog signal acquisition, generation, and monitoring capabilities.
The `esp_hal_ana_conv` component provides a **Hardware Abstraction Layer** for analog conversion related peripherals across all ESP-IDF supported targets. This HAL currently supports Analog-to-Digital Converter (ADC), Digital-to-Analog Converter (DAC), Temperature Sensor and other analog conversion related peripherals. The HAL enables analog signal acquisition, generation, monitoring, and temperature measurement capabilities.
## Architecture
@@ -34,6 +34,12 @@ This HAL supports various DAC controller types depending on the ESP chip:
- **RTC Controller**: Direct voltage output control
- **DMA Controller**: High-speed data output via I2S or SPI DMA
### Temperature Sensor Controllers
This HAL supports temperature sensor functionality depending on the ESP chip:
- **Temperature Sensor**: On-chip temperature monitoring with automatic range management
## ADC Features
### Work Modes
@@ -88,9 +94,32 @@ This HAL supports various DAC controller types depending on the ESP chip:
- **ADC-DAC Synchronization**: Synchronized operation with ADC RTC controller
- **Power Management**: Independent power control per channel
## Temperature Sensor Features
### Measurement Capabilities
- **Temperature Range**: Typically -40°C to 125°C (chip-dependent)
- **Automatic Range Management**: Five configurable temperature ranges with optimized accuracy for each range
- **Raw Value Reading**: Direct access to raw temperature sensor values
- **Degree Conversion**: Automatic conversion from raw values to temperature in degrees Celsius
### Range Management
- **Multi-Range Support**: Five temperature ranges with different offset and error characteristics
- **Dynamic Range Switching**: Automatic range adjustment based on measured temperature for optimal accuracy
- **Range Tracking**: HAL layer maintains range state to minimize unnecessary range switches
### Advanced Features
- **Sleep Retention**: Register context retention during sleep modes (chip-dependent)
- **Clock Source Selection**: Flexible clock source configuration (XTAL, FOSC, etc., chip-dependent)
- **ETM Support**: Event Task Matrix integration for temperature monitoring events and tasks
- **Wake-up Functionality**: Temperature-based wake-up from sleep modes
- **Power Control**: Independent power and clock gating control
## Usage
The HAL functions primarily serve ESP-IDF peripheral drivers such as `esp_adc` and `esp_driver_dac`.
The HAL functions primarily serve ESP-IDF peripheral drivers such as `esp_adc`, `esp_driver_dac`, and `esp_driver_tsens`.
Advanced developers can use these interfaces directly when implementing custom drivers, with the understanding that API stability is not guaranteed.
@@ -86,15 +86,15 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl
{
uint8_t clk_sel = 0;
switch (clk_src) {
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
}
APB_SARADC.saradc_apb_tsens_ctrl2.saradc_tsens_clk_sel = clk_sel;
}
@@ -166,7 +166,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
uint32_t cal_temp = 0;
cal_temp = EFUSE.rd_blk2_data2.temp_calib;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
/*Offset reg_val min max error */
@@ -86,15 +86,15 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl
{
uint8_t clk_sel = 0;
switch (clk_src) {
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
}
APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = clk_sel;
}
@@ -168,7 +168,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
}
uint32_t cal_temp = EFUSE.rd_sys_part1_data4.temp_calib;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
/*Offset reg_val min max error */
@@ -100,15 +100,15 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl
{
uint8_t clk_sel = 0;
switch (clk_src) {
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
}
PCR.tsens_clk_conf.tsens_clk_sel = clk_sel;
}
@@ -280,7 +280,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
{
uint32_t cal_temp = EFUSE.rd_sys_part1_data4.temperature_sensor;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -6,7 +6,7 @@
#include <stdint.h>
#include "soc/regdma.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "soc/apb_saradc_reg.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
@@ -100,15 +100,15 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl
{
uint8_t clk_sel = 0;
switch (clk_src) {
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
}
PCR.tsens_clk_conf.tsens_clk_sel = clk_sel;
}
@@ -280,7 +280,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
{
uint32_t cal_temp = EFUSE.rd_sys_part1_data4.temp_calib;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -6,7 +6,7 @@
#include <stdint.h>
#include "soc/regdma.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "soc/apb_saradc_reg.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
@@ -100,15 +100,15 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl
{
uint8_t clk_sel = 0;
switch (clk_src) {
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
}
PCR.tsens_clk_conf.tsens_clk_sel = clk_sel;
}
@@ -280,7 +280,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
{
uint32_t cal_temp = EFUSE0.rd_sys_part1_data4.temperature_sensor;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -6,7 +6,7 @@
#include <stdint.h>
#include "soc/regdma.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "soc/apb_saradc_reg.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
@@ -99,15 +99,15 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl
{
uint8_t clk_sel = 0;
switch (clk_src) {
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
clk_sel = 1;
break;
case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
clk_sel = 0;
break;
default:
HAL_ASSERT(false);
break;
}
PCR.tsens_clk_conf.tsens_clk_sel = clk_sel;
}
@@ -279,7 +279,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
{
uint32_t cal_temp = EFUSE.rd_sys_part1_data4.temp_calib;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -6,7 +6,7 @@
#include <stdint.h>
#include "soc/regdma.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "soc/apb_saradc_reg.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
@@ -273,7 +273,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
#ifdef EFUSE_TEMPERATURE_SENSOR
uint32_t cal_temp = EFUSE.rd_sys_part2_data3.temperature_sensor;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
#else
return 0;
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
/*Offset reg_val min max error */
@@ -157,7 +157,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
}
uint32_t cal_temp = EFUSE.rd_sys_part1_data4.temp_calib;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
/*Offset reg_val min max error */
@@ -157,7 +157,7 @@ static inline int temperature_sensor_ll_load_calib_param(void)
}
uint32_t cal_temp = EFUSE.rd_sys_part1_data4.temp_calib;
// BIT(8) stands for sign: 1: negative, 0: positive
int tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
int tsens_cal = ((cal_temp & BIT(8)) != 0) ? -(uint8_t)cal_temp : (uint8_t)cal_temp;
return tsens_cal;
}
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = {
/*Offset reg_val min max error */
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -6,7 +6,7 @@
#include "hal/temperature_sensor_hal.h"
#include "hal/temperature_sensor_ll.h"
#include "soc/temperature_sensor_periph.h"
#include "hal/temperature_sensor_periph.h"
#include "hal/log.h"
#include "esp_rom_sys.h"
+1 -1
View File
@@ -80,12 +80,12 @@ if(NOT non_os_build)
list(APPEND priv_requires esp_driver_gpio # for GPIO and RTC (by sleep_gpio and sleep_modes)
esp_timer
esp_hal_touch_sens # for touch sensor wakeup (introduced in sleep_modes.c)
esp_hal_i2s # required by `sleep_system_peripheral.c`
esp_pm)
list(APPEND priv_requires esp_mm
esp_hal_mspi
esp_hal_uart
esp_hal_i2s # required by `sleep_system_peripheral.c`
)
if(CONFIG_IDF_TARGET_ESP32 OR CONFIG_IDF_TARGET_ESP32S2)
+4
View File
@@ -95,5 +95,9 @@ archive: libsoc.a
entries:
if PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP && ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND:
gpio_periph: GPIO_HOLD_MASK (noflash)
[mapping:hal_tsens_pm]
archive: libesp_hal_ana_conv.a
entries:
if PM_SLP_IRAM_OPT = y && SOC_TEMP_SENSOR_SUPPORTED = y:
temperature_sensor_periph:temperature_sensor_attributes (noflash)
+2 -1
View File
@@ -55,7 +55,8 @@ endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include" "${idf_target}/include"
PRIV_REQUIRES nvs_flash esp_driver_gpio efuse esp_timer esp_wifi esptool_py
# 'esp_hal_ana_conv' is required by 'phy_common.c' for the temperature sensor hal
PRIV_REQUIRES nvs_flash esp_driver_gpio esp_hal_ana_conv efuse esp_timer esp_wifi esptool_py
LDFRAGMENTS "${ldfragments}"
EMBED_FILES ${embed_files}
)
+2 -2
View File
@@ -90,8 +90,8 @@ entries:
if PM_SLP_IRAM_OPT = y && PM_RESTORE_CACHE_TAGMEM_AFTER_LIGHT_SLEEP = y:
rtc_cntl_hal:rtc_cntl_hal_enable_tagmem_retention (noflash)
[mapping:hal_pm]
archive: libhal.a
[mapping:esp_hal_ana_conv_pm]
archive: libesp_hal_ana_conv.a
entries:
if SOC_TEMP_SENSOR_SUPPORTED = y:
if PM_SLP_IRAM_OPT = y:
-3
View File
@@ -150,9 +150,6 @@ elseif(NOT BOOTLOADER_BUILD)
list(APPEND srcs "ds_hal.c")
endif()
if(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
list(APPEND srcs "temperature_sensor_hal.c")
endif()
endif()
idf_component_register(SRCS ${srcs}
-4
View File
@@ -74,10 +74,6 @@ if(CONFIG_SOC_I3C_MASTER_SUPPORTED)
list(APPEND srcs "${target_folder}/i3c_master_periph.c")
endif()
if(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
list(APPEND srcs "${target_folder}/temperature_sensor_periph.c")
endif()
if(CONFIG_SOC_MPI_SUPPORTED)
list(APPEND srcs "${target_folder}/mpi_periph.c")
endif()
+1 -1
View File
@@ -166,6 +166,7 @@ INPUT = \
$(PROJECT_PATH)/components/esp_event/include/esp_event.h \
$(PROJECT_PATH)/components/esp_hal_ana_conv/include/hal/adc_types.h \
$(PROJECT_PATH)/components/esp_hal_ana_conv/include/hal/dac_types.h \
$(PROJECT_PATH)/components/esp_hal_ana_conv/include/hal/temperature_sensor_types.h \
$(PROJECT_PATH)/components/esp_hal_gpio/include/hal/gpio_types.h \
$(PROJECT_PATH)/components/esp_hal_gpio/include/hal/rtc_io_types.h \
$(PROJECT_PATH)/components/esp_hal_gpio/include/hal/sdm_types.h \
@@ -270,7 +271,6 @@ INPUT = \
$(PROJECT_PATH)/components/hal/include/hal/color_types.h \
$(PROJECT_PATH)/components/hal/include/hal/mcpwm_types.h \
$(PROJECT_PATH)/components/hal/include/hal/sdio_slave_types.h \
$(PROJECT_PATH)/components/hal/include/hal/temperature_sensor_types.h \
$(PROJECT_PATH)/components/hal/include/hal/efuse_hal.h \
$(PROJECT_PATH)/components/hal/include/hal/eth_types.h \
$(PROJECT_PATH)/components/hal/include/hal/lp_core_types.h \