Skip to content

Commit

Permalink
Merge branch 'bugfix/tempsensor_wifi_conflict_v5.1' into 'release/v5.1'
Browse files Browse the repository at this point in the history
temperature_sensor: Fix issue that have conflict with phy / Fix phy pwdet and tsens power cannot be set twice issue (v5.1)

See merge request espressif/esp-idf!24064
  • Loading branch information
jack0c committed Jul 10, 2023
2 parents a09a503 + 0954bab commit cf517e3
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 26 deletions.
26 changes: 14 additions & 12 deletions components/driver/deprecated/rtc_temperature_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "hal/temperature_sensor_ll.h"
#include "driver/temp_sensor_types_legacy.h"
#include "esp_private/periph_ctrl.h"
#include "esp_private/sar_periph_ctrl.h"

static const char *TAG = "tsens";

Expand Down Expand Up @@ -62,8 +63,8 @@ esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
err = ESP_ERR_INVALID_STATE;
}
temperature_sensor_ll_set_clk_div(tsens.clk_div);
temp_sensor_sync_tsens_idx(tsens.dac_offset);
temperature_sensor_ll_set_range(dac_offset[tsens.dac_offset].reg_val);
temperature_sensor_ll_enable(true);
ESP_LOGI(TAG, "Config range [%d°C ~ %d°C], error < %d°C",
dac_offset[tsens.dac_offset].range_min,
dac_offset[tsens.dac_offset].range_max,
Expand Down Expand Up @@ -95,7 +96,7 @@ esp_err_t temp_sensor_start(void)
}
regi2c_saradc_enable();
periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
temperature_sensor_ll_enable(true);
temperature_sensor_power_acquire();
temperature_sensor_ll_clk_enable(true);
temperature_sensor_ll_clk_sel(TEMPERATURE_SENSOR_CLK_SRC_DEFAULT);
tsens_hw_state = TSENS_HW_STATE_STARTED;
Expand All @@ -105,7 +106,7 @@ esp_err_t temp_sensor_start(void)
esp_err_t temp_sensor_stop(void)
{
regi2c_saradc_disable();
temperature_sensor_ll_enable(false);
temperature_sensor_power_release();
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
return ESP_OK;
}
Expand All @@ -124,12 +125,12 @@ static esp_err_t read_delta_t_from_efuse(void)
return ESP_OK;
}

static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
static float parse_temp_sensor_raw_value(uint32_t tsens_raw)
{
if (isnan(s_deltaT)) { //suggests that the value is not initialized
read_delta_t_from_efuse();
}
float result = (TEMPERATURE_SENSOR_LL_ADC_FACTOR * (float)tsens_raw - TEMPERATURE_SENSOR_LL_DAC_FACTOR * dac_offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR) - s_deltaT / 10.0;
float result = tsens_raw - s_deltaT / 10.0;
return result;
}

Expand All @@ -141,16 +142,17 @@ esp_err_t temp_sensor_read_celsius(float *celsius)
return ESP_ERR_INVALID_STATE;
}
temp_sensor_config_t tsens;
uint32_t tsens_out = 0;
temp_sensor_get_config(&tsens);
temp_sensor_read_raw(&tsens_out);
ESP_LOGV(TAG, "tsens_out %"PRIu32, tsens_out);
const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
*celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset);
if (*celsius < dac->range_min || *celsius > dac->range_max) {
ESP_LOGW(TAG, "Exceeding the temperature range!");
bool range_changed;
uint16_t tsens_out = temp_sensor_get_raw_value(&range_changed);
*celsius = parse_temp_sensor_raw_value(tsens_out);
if (*celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) {
ESP_LOGE(TAG, "Exceeding temperature measure range.");
return ESP_ERR_INVALID_STATE;
}
if (range_changed) {
temp_sensor_get_config(&tsens);
}
return ESP_OK;
}

Expand Down
41 changes: 29 additions & 12 deletions components/driver/temperature_sensor/temperature_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "hal/temperature_sensor_ll.h"
#include "soc/temperature_sensor_periph.h"
#include "esp_memory_utils.h"
#include "esp_private/sar_periph_ctrl.h"

static const char *TAG = "temperature_sensor";

Expand Down Expand Up @@ -63,6 +64,7 @@ static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_
tsens->tsens_attribute = &s_tsens_attribute_copy[i];
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;
Expand Down Expand Up @@ -114,7 +116,6 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co

regi2c_saradc_enable();
temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
temperature_sensor_ll_enable(false); // disable the sensor by default

tsens->fsm = TEMP_SENSOR_FSM_INIT;
*ret_tsens = tsens;
Expand Down Expand Up @@ -147,6 +148,20 @@ esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
return ESP_OK;
}

static esp_err_t s_update_tsens_attribute(temperature_sensor_handle_t tsens)
{
uint32_t dac;
ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified");
dac = temperature_sensor_ll_get_offset();
for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
if (dac == s_tsens_attribute_copy[i].reg_val) {
tsens->tsens_attribute = &s_tsens_attribute_copy[i];
break;
}
}
return ESP_OK;
}

esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
{
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
Expand All @@ -165,7 +180,7 @@ esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)

temperature_sensor_ll_clk_enable(true);
temperature_sensor_ll_clk_sel(tsens->clk_src);
temperature_sensor_ll_enable(true);
temperature_sensor_power_acquire();
tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
return ESP_OK;
}
Expand All @@ -175,12 +190,12 @@ esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens)
ESP_RETURN_ON_FALSE(tsens, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");

temperature_sensor_power_release();
#if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
temperature_sensor_ll_wakeup_enable(false);
temperature_sensor_ll_sample_enable(false);
#endif

temperature_sensor_ll_enable(false);
#if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
periph_rtc_dig_clk8m_disable();
Expand All @@ -200,12 +215,12 @@ static esp_err_t read_delta_t_from_efuse(void)
return ESP_OK;
}

static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
static float parse_temp_sensor_raw_value(uint32_t tsens_raw)
{
if (isnan(s_deltaT)) { //suggests that the value is not initialized
read_delta_t_from_efuse();
}
float result = (TEMPERATURE_SENSOR_LL_ADC_FACTOR * (float)tsens_raw - TEMPERATURE_SENSOR_LL_DAC_FACTOR * dac_offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR) - s_deltaT / 10.0;
float result = tsens_raw - s_deltaT / 10.0;
return result;
}

Expand All @@ -214,14 +229,16 @@ esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, floa
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has not been installed");
ESP_RETURN_ON_FALSE(out_celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "Celsius points to nothing");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
bool range_changed;
uint16_t tsens_out = temp_sensor_get_raw_value(&range_changed);
*out_celsius = parse_temp_sensor_raw_value(tsens_out);

uint32_t tsens_out = temperature_sensor_ll_get_raw_value();
ESP_LOGD(TAG, "tsens_out %"PRIu32, tsens_out);

*out_celsius = parse_temp_sensor_raw_value(tsens_out, tsens->tsens_attribute->offset);
if (*out_celsius < tsens->tsens_attribute->range_min || *out_celsius > tsens->tsens_attribute->range_max) {
ESP_LOGW(TAG, "value out of range, probably invalid");
return ESP_FAIL;
if (*out_celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *out_celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) {
ESP_LOGE(TAG, "Exceeding temperature measure range.");
return ESP_ERR_INVALID_STATE;
}
if (range_changed) {
s_update_tsens_attribute(tsens);
}
return ESP_OK;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -23,13 +23,15 @@ TEST_CASE("Temperature_legacy_workflow_test", "[hw_timer]")
TEST_ESP_OK(temp_sensor_read_celsius(&tsens_out));
printf("Temperature out celsius %f°C\n", tsens_out);
TEST_ESP_OK(temp_sensor_stop());
#if !CONFIG_IDF_TARGET_ESP32H2 // disable on eps32h2, seems have some issues on esp32h2
temp_sensor.dac_offset = TSENS_DAC_L3;
TEST_ESP_OK(temp_sensor_set_config(temp_sensor));
TEST_ESP_OK(temp_sensor_start());
printf("Temperature sensor started again\n");
TEST_ESP_OK(temp_sensor_read_celsius(&tsens_out));
printf("Temperature out celsius %f°C\n", tsens_out);
TEST_ESP_OK(temp_sensor_stop());
#endif
}

TEST_CASE("Temperature legacy double start error cause test", "[temperature_sensor]")
Expand Down
1 change: 1 addition & 0 deletions components/esp_hw_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if(NOT BOOTLOADER_BUILD)
"sleep_modem.c"
"regi2c_ctrl.c"
"esp_gpio_reserve.c"
"sar_periph_ctrl_common.c"
"port/${target}/io_mux.c"
"port/${target}/esp_clk_tree.c"
"port/esp_clk_tree_common.c")
Expand Down
30 changes: 30 additions & 0 deletions components/esp_hw_support/include/esp_private/sar_periph_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#pragma once

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -62,6 +65,33 @@ void sar_periph_ctrl_pwdet_power_acquire(void);
*/
void sar_periph_ctrl_pwdet_power_release(void);

/**
* @brief Acquire the temperature sensor power
*/
void temperature_sensor_power_acquire(void);

/**
* @brief Release the temperature sensor power
*/
void temperature_sensor_power_release(void);

/**
* @brief Get the temperature value and choose the temperature sensor range. Will be both used in phy and peripheral.
*
* @param range_changed Pointer to whether range has been changed here. If you don't need this param, you can
* set NULL directly.
*
* @return temperature sensor value.
*/
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
*/
Expand Down
118 changes: 118 additions & 0 deletions components/esp_hw_support/sar_periph_ctrl_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/


#include "soc/soc_caps.h"
#include "freertos/FreeRTOS.h"
#include "esp_private/sar_periph_ctrl.h"
#include "esp_log.h"

#if SOC_TEMP_SENSOR_SUPPORTED
#include "hal/temperature_sensor_ll.h"
#include "soc/temperature_sensor_periph.h"

extern __attribute__((unused)) portMUX_TYPE rtc_spinlock;


/*------------------------------------------------------------------------------------------------------------
-----------------------------------------Temperature Sensor---------------------------------------------------
------------------------------------------------------------------------------------------------------------*/
static const char *TAG_TSENS = "temperature_sensor";

#define INT_NOT_USED 999999

static int s_record_min = INT_NOT_USED;
static int s_record_max = INT_NOT_USED;
static int s_temperature_sensor_power_cnt;

static uint8_t s_tsens_idx = 2; // Index for temperature attribute, set 2(middle) as default value

void temperature_sensor_power_acquire(void)
{
portENTER_CRITICAL(&rtc_spinlock);
s_temperature_sensor_power_cnt++;
if (s_temperature_sensor_power_cnt == 1) {
temperature_sensor_ll_enable(true);
}
portEXIT_CRITICAL(&rtc_spinlock);
}

void temperature_sensor_power_release(void)
{
portENTER_CRITICAL(&rtc_spinlock);
s_temperature_sensor_power_cnt--;
/* Sanity check */
if (s_temperature_sensor_power_cnt < 0) {
portEXIT_CRITICAL(&rtc_spinlock);
ESP_LOGE(TAG_TSENS, "%s called, but s_temperature_sensor_power_cnt == 0", __func__);
abort();
} else if (s_temperature_sensor_power_cnt == 0) {
temperature_sensor_ll_enable(false);
}
portEXIT_CRITICAL(&rtc_spinlock);
}

static 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)
{
portENTER_CRITICAL(&rtc_spinlock);

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

portEXIT_CRITICAL(&rtc_spinlock);
return degree;
}

#endif
Loading

0 comments on commit cf517e3

Please sign in to comment.