Skip to content

Commit daceb35

Browse files
committed
temperature sensor: Add support on ESP32-S3,
Closes #8086
1 parent 322a224 commit daceb35

File tree

11 files changed

+193
-9
lines changed

11 files changed

+193
-9
lines changed

components/driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ if(${target} STREQUAL "esp32s3")
6767
"spi_slave_hd.c"
6868
"touch_sensor_common.c"
6969
"esp32s3/touch_sensor.c"
70+
"esp32s3/rtc_tempsensor.c"
7071
)
7172
endif()
7273

components/driver/esp32c3/rtc_tempsensor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ esp_err_t temp_sensor_stop(void)
111111
{
112112
APB_SARADC.apb_tsens_ctrl.tsens_pu = 0;
113113
APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = 0;
114+
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
114115
return ESP_OK;
115116
}
116117

components/driver/esp32h2/rtc_tempsensor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ esp_err_t temp_sensor_stop(void)
111111
{
112112
APB_SARADC.apb_tsens_ctrl.tsens_pu = 0;
113113
APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = 0;
114+
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
114115
return ESP_OK;
115116
}
116117

components/driver/esp32s2/rtc_tempsensor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ esp_err_t temp_sensor_stop(void)
129129
vSemaphoreDelete(rtc_tsens_mux);
130130
rtc_tsens_mux = NULL;
131131
}
132+
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
132133
return ESP_OK;
133134
}
134135

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdlib.h>
8+
#include <ctype.h>
9+
#include <math.h>
10+
#include "esp_types.h"
11+
#include "freertos/FreeRTOS.h"
12+
#include "freertos/semphr.h"
13+
#include "esp_log.h"
14+
#include "esp_check.h"
15+
#include "soc/rtc_cntl_reg.h"
16+
#include "soc/rtc_io_reg.h"
17+
#include "soc/rtc_io_struct.h"
18+
#include "soc/sens_reg.h"
19+
#include "soc/sens_struct.h"
20+
#include "driver/temp_sensor.h"
21+
#include "regi2c_ctrl.h"
22+
#include "esp_log.h"
23+
#include "esp_efuse_rtc_calib.h"
24+
25+
static const char *TAG = "tsens";
26+
27+
#define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */
28+
#define TSENS_ADC_FACTOR (0.4386)
29+
#define TSENS_DAC_FACTOR (27.88)
30+
#define TSENS_SYS_OFFSET (20.52)
31+
32+
typedef struct {
33+
int index;
34+
int offset;
35+
int set_val;
36+
int range_min;
37+
int range_max;
38+
int error_max;
39+
} tsens_dac_offset_t;
40+
41+
static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
42+
/* DAC Offset reg_val min max error */
43+
{TSENS_DAC_L0, -2, 5, 50, 125, 3},
44+
{TSENS_DAC_L1, -1, 7, 20, 100, 2},
45+
{TSENS_DAC_L2, 0, 15, -10, 80, 1},
46+
{TSENS_DAC_L3, 1, 11, -30, 50, 2},
47+
{TSENS_DAC_L4, 2, 10, -40, 20, 3},
48+
};
49+
50+
typedef enum {
51+
TSENS_HW_STATE_UNCONFIGURED,
52+
TSENS_HW_STATE_CONFIGURED,
53+
TSENS_HW_STATE_STARTED,
54+
} tsens_hw_state_t;
55+
56+
static tsens_hw_state_t tsens_hw_state = TSENS_HW_STATE_UNCONFIGURED;
57+
58+
static float s_deltaT = NAN; // Unused number
59+
60+
esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
61+
{
62+
esp_err_t err = ESP_OK;
63+
if (tsens_hw_state == TSENS_HW_STATE_STARTED) {
64+
ESP_LOGE(TAG, "Do not configure the temp sensor when it's running!");
65+
err = ESP_ERR_INVALID_STATE;
66+
}
67+
CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
68+
SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
69+
REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val);
70+
SENS.sar_tctrl.tsens_clk_div = tsens.clk_div;
71+
SENS.sar_tctrl.tsens_power_up_force = 1;
72+
SENS.sar_tctrl.tsens_power_up = 1;
73+
SENS.sar_tctrl2.tsens_xpd_force = 1;
74+
ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C",
75+
dac_offset[tsens.dac_offset].range_min,
76+
dac_offset[tsens.dac_offset].range_max,
77+
dac_offset[tsens.dac_offset].error_max);
78+
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
79+
return err;
80+
}
81+
82+
esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)
83+
{
84+
ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified");
85+
CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
86+
SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
87+
tsens->dac_offset = REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC);
88+
for (int i = TSENS_DAC_L0; i < TSENS_DAC_MAX; i++) {
89+
if ((int)tsens->dac_offset == dac_offset[i].set_val) {
90+
tsens->dac_offset = dac_offset[i].index;
91+
break;
92+
}
93+
}
94+
tsens->clk_div = SENS.sar_tctrl.tsens_clk_div;
95+
return ESP_OK;
96+
}
97+
98+
esp_err_t temp_sensor_start(void)
99+
{
100+
esp_err_t err = ESP_OK;
101+
if (tsens_hw_state != TSENS_HW_STATE_CONFIGURED) {
102+
ESP_LOGE(TAG, "Temperature sensor is already running or not be configured");
103+
err = ESP_ERR_INVALID_STATE;
104+
}
105+
SENS.sar_tctrl.tsens_dump_out = 0;
106+
SENS.sar_tctrl.tsens_power_up = 1;
107+
SENS.sar_peri_clk_gate_conf.tsens_clk_en = 1;
108+
tsens_hw_state = TSENS_HW_STATE_STARTED;
109+
return err;
110+
}
111+
112+
esp_err_t temp_sensor_stop(void)
113+
{
114+
SENS.sar_tctrl.tsens_power_up = 0;
115+
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
116+
return ESP_OK;
117+
}
118+
119+
esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
120+
{
121+
ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens_out specified");
122+
SENS.sar_tctrl.tsens_dump_out = 1;
123+
while (!SENS.sar_tctrl.tsens_ready);
124+
*tsens_out = SENS.sar_tctrl.tsens_out;
125+
SENS.sar_tctrl.tsens_dump_out = 0;
126+
return ESP_OK;
127+
}
128+
129+
static void read_delta_t_from_efuse(void)
130+
{
131+
uint32_t version = esp_efuse_rtc_calib_get_ver();
132+
if (version == 1) {
133+
// fetch calibration value for temp sensor from eFuse
134+
s_deltaT = esp_efuse_rtc_calib_get_cal_temp(version);
135+
} else {
136+
// no value to fetch, use 0.
137+
s_deltaT = 0;
138+
}
139+
ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
140+
}
141+
142+
static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
143+
{
144+
if (isnan(s_deltaT)) { //suggests that the value is not initialized
145+
read_delta_t_from_efuse();
146+
}
147+
float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT;
148+
return result;
149+
}
150+
151+
esp_err_t temp_sensor_read_celsius(float *celsius)
152+
{
153+
ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
154+
temp_sensor_config_t tsens;
155+
uint32_t tsens_out = 0;
156+
esp_err_t ret = temp_sensor_get_config(&tsens);
157+
if (ret == ESP_OK) {
158+
ret = temp_sensor_read_raw(&tsens_out);
159+
ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data");
160+
const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
161+
*celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset);
162+
if (*celsius < dac->range_min || *celsius > dac->range_max) {
163+
ESP_LOGW(TAG, "Exceeding the temperature range!");
164+
ret = ESP_ERR_INVALID_STATE;
165+
}
166+
}
167+
return ret;
168+
}

components/efuse/esp32s3/esp_efuse_rtc_calib.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, in
9696

9797
return ESP_OK;
9898
}
99+
100+
float esp_efuse_rtc_calib_get_cal_temp(int version)
101+
{
102+
assert(version == 1);
103+
const esp_efuse_desc_t** cal_temp_efuse;
104+
cal_temp_efuse = ESP_EFUSE_TEMP_CALIB;
105+
int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse);
106+
assert(cal_temp_size == 9);
107+
108+
uint32_t cal_temp = 0;
109+
esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size);
110+
assert(err == ESP_OK);
111+
(void)err;
112+
// BIT(8) stands for sign: 1: negtive, 0: positive
113+
return ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
114+
}

components/soc/esp32s3/include/soc/soc_caps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define SOC_FLASH_ENCRYPTION_XTS_AES_256 1
4040
#define SOC_PSRAM_DMA_CAPABLE 1
4141
#define SOC_XT_WDT_SUPPORTED 1
42+
#define SOC_TEMP_SENSOR_SUPPORTED 1
4243

4344
/*-------------------------- SOC CAPS ----------------------------------------*/
4445
#define SOC_APPCPU_HAS_CLOCK_GATING_BUG (1)

docs/doxygen/Doxyfile_esp32s3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ INPUT += \
88
$(PROJECT_PATH)/components/driver/include/driver/pcnt.h \
99
$(PROJECT_PATH)/components/soc/$(IDF_TARGET)/include/soc/touch_sensor_channel.h \
1010
$(PROJECT_PATH)/components/driver/$(IDF_TARGET)/include/driver/touch_sensor.h \
11+
$(PROJECT_PATH)/components/driver/esp32s2/include/driver/temp_sensor.h \
1112
$(PROJECT_PATH)/components/ulp/include/esp32s3/ulp_riscv.h \
1213
$(PROJECT_PATH)/components/usb/include/usb/usb_helpers.h \
1314
$(PROJECT_PATH)/components/usb/include/usb/usb_host.h \

docs/en/api-reference/peripherals/temp_sensor.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
Temperature Sensor
22
===================
33

4-
.. only:: esp32s3
5-
6-
.. warning::
7-
8-
This feature is not supported in v4.4
9-
104
Overview
115
--------
126

examples/peripherals/temp_sensor/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| Supported Targets | ESP32-S2 | ESP32-C3 |
2-
| ----------------- | -------- | -------- |
1+
| Supported Targets | ESP32-S2 | ESP32-C3 | ESP32-S3 |
2+
| ----------------- | -------- | -------- | -------- |
33

44
# Temperature Sensor Example
55

examples/peripherals/temp_sensor/main/temp_sensor_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/* Note: ESP32 don't support temperature sensor */
1616

17-
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
17+
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
1818
#include "driver/temp_sensor.h"
1919

2020
static const char *TAG = "TempSensor";

0 commit comments

Comments
 (0)