mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-05 05:34:32 +02:00
fix(temperature_sensor): Fix temperature sensor value accurency in high range variation
This commit is contained in:
@@ -65,9 +65,18 @@ static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_
|
||||
for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
|
||||
if ((tsens_config->range_min >= s_tsens_attribute_copy[i].range_min) && (tsens_config->range_max <= s_tsens_attribute_copy[i].range_max)) {
|
||||
tsens->tsens_attribute = &s_tsens_attribute_copy[i];
|
||||
int original_idx = -1;
|
||||
for (int j = 0; j < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; j++) {
|
||||
if (temperature_sensor_attributes[j].reg_val == s_tsens_attribute_copy[i].reg_val) {
|
||||
original_idx = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (original_idx != -1) {
|
||||
temp_sensor_sync_tsens_idx(original_idx);
|
||||
}
|
||||
break;
|
||||
}
|
||||
temp_sensor_sync_tsens_idx(i);
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(tsens->tsens_attribute != NULL, ESP_ERR_INVALID_ARG, TAG, "Out of testing range");
|
||||
return ESP_OK;
|
||||
@@ -166,6 +175,8 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co
|
||||
tsens->tsens_attribute->range_max,
|
||||
tsens->tsens_attribute->error_max);
|
||||
|
||||
printf("tsens->tsens_attribute->reg_val: %d\n", tsens->tsens_attribute->reg_val);
|
||||
|
||||
temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
|
||||
|
||||
tsens->fsm = TEMP_SENSOR_FSM_INIT;
|
||||
|
@@ -9,6 +9,7 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_private/sar_periph_ctrl.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
#if SOC_TEMP_SENSOR_SUPPORTED
|
||||
#include "hal/temperature_sensor_ll.h"
|
||||
@@ -40,17 +41,22 @@ static const char *TAG_TSENS = "temperature_sensor";
|
||||
# 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)
|
||||
{
|
||||
esp_os_enter_critical(&rtc_spinlock);
|
||||
s_temperature_sensor_power_cnt++;
|
||||
if (s_temperature_sensor_power_cnt == 1) {
|
||||
s_first_temp_read = true;
|
||||
regi2c_saradc_enable();
|
||||
#if !SOC_TSENS_IS_INDEPENDENT_FROM_ADC
|
||||
adc_apb_periph_claim();
|
||||
@@ -60,6 +66,9 @@ 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);
|
||||
timer1 = esp_timer_get_time();
|
||||
}
|
||||
esp_os_exit_critical(&rtc_spinlock);
|
||||
}
|
||||
@@ -101,6 +110,18 @@ int16_t temp_sensor_get_raw_value(bool *range_changed)
|
||||
{
|
||||
esp_os_enter_critical(&rtc_spinlock);
|
||||
|
||||
// When this is the first time reading a value, check whether the time here minus the
|
||||
// initialization time is greater than 200 microseconds (the time for linear regression).
|
||||
// If it is less than 200 microseconds, continue waiting here.
|
||||
if (s_first_temp_read == true) {
|
||||
int64_t timer2 = esp_timer_get_time();
|
||||
int64_t diff = timer2 - timer1;
|
||||
if (diff < TSENS_LINE_REGRESSION_US) {
|
||||
esp_rom_delay_us(TSENS_LINE_REGRESSION_US - diff);
|
||||
}
|
||||
s_first_temp_read = false;
|
||||
}
|
||||
|
||||
int degree = temperature_sensor_get_raw_value();
|
||||
uint8_t temperature_dac;
|
||||
|
||||
|
Reference in New Issue
Block a user