Skip to content

Commit d6a3e73

Browse files
committed
fix(temperature_sensor): Fix temperature sensor value accurency in high range variation
1 parent d2b6f83 commit d6a3e73

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

components/esp_driver_tsens/src/temperature_sensor.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_
6565
for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
6666
if ((tsens_config->range_min >= s_tsens_attribute_copy[i].range_min) && (tsens_config->range_max <= s_tsens_attribute_copy[i].range_max)) {
6767
tsens->tsens_attribute = &s_tsens_attribute_copy[i];
68+
int original_idx = -1;
69+
for (int j = 0; j < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; j++) {
70+
if (temperature_sensor_attributes[j].reg_val == s_tsens_attribute_copy[i].reg_val) {
71+
original_idx = j;
72+
break;
73+
}
74+
}
75+
if (original_idx != -1) {
76+
temp_sensor_sync_tsens_idx(original_idx);
77+
}
6878
break;
6979
}
70-
temp_sensor_sync_tsens_idx(i);
7180
}
7281
ESP_RETURN_ON_FALSE(tsens->tsens_attribute != NULL, ESP_ERR_INVALID_ARG, TAG, "Out of testing range");
7382
return ESP_OK;
@@ -166,6 +175,8 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co
166175
tsens->tsens_attribute->range_max,
167176
tsens->tsens_attribute->error_max);
168177

178+
printf("tsens->tsens_attribute->reg_val: %d\n", tsens->tsens_attribute->reg_val);
179+
169180
temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
170181

171182
tsens->fsm = TEMP_SENSOR_FSM_INIT;

components/esp_hw_support/sar_periph_ctrl_common.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "freertos/FreeRTOS.h"
1010
#include "esp_private/sar_periph_ctrl.h"
1111
#include "esp_log.h"
12+
#include "esp_timer.h"
1213

1314
#if SOC_TEMP_SENSOR_SUPPORTED
1415
#include "hal/temperature_sensor_ll.h"
@@ -40,17 +41,22 @@ static const char *TAG_TSENS = "temperature_sensor";
4041
# define SAR_PERIPH_CTRL_COMMON_FN_ATTR
4142
#endif
4243

44+
#define TSENS_LINE_REGRESSION_US (200)
45+
4346
static int s_record_min = INT_NOT_USED;
4447
static int s_record_max = INT_NOT_USED;
4548
static int s_temperature_sensor_power_cnt;
49+
static bool s_first_temp_read = false;
4650

4751
static uint8_t s_tsens_idx = 2; // Index for temperature attribute, set 2(middle) as default value
52+
static int64_t timer1 = 0;
4853

4954
void temperature_sensor_power_acquire(void)
5055
{
5156
esp_os_enter_critical(&rtc_spinlock);
5257
s_temperature_sensor_power_cnt++;
5358
if (s_temperature_sensor_power_cnt == 1) {
59+
s_first_temp_read = true;
5460
regi2c_saradc_enable();
5561
#if !SOC_TSENS_IS_INDEPENDENT_FROM_ADC
5662
adc_apb_periph_claim();
@@ -60,6 +66,9 @@ void temperature_sensor_power_acquire(void)
6066
temperature_sensor_ll_reset_module();
6167
}
6268
temperature_sensor_ll_enable(true);
69+
// Set the range as recorded.
70+
temperature_sensor_ll_set_range(temperature_sensor_attributes[s_tsens_idx].reg_val);
71+
timer1 = esp_timer_get_time();
6372
}
6473
esp_os_exit_critical(&rtc_spinlock);
6574
}
@@ -101,6 +110,18 @@ int16_t temp_sensor_get_raw_value(bool *range_changed)
101110
{
102111
esp_os_enter_critical(&rtc_spinlock);
103112

113+
// When this is the first time reading a value, check whether the time here minus the
114+
// initialization time is greater than 200 microseconds (the time for linear regression).
115+
// If it is less than 200 microseconds, continue waiting here.
116+
if (s_first_temp_read == true) {
117+
int64_t timer2 = esp_timer_get_time();
118+
int64_t diff = timer2 - timer1;
119+
if (diff < TSENS_LINE_REGRESSION_US) {
120+
esp_rom_delay_us(TSENS_LINE_REGRESSION_US - diff);
121+
}
122+
s_first_temp_read = false;
123+
}
124+
104125
int degree = temperature_sensor_get_raw_value();
105126
uint8_t temperature_dac;
106127

0 commit comments

Comments
 (0)