Merge branch 'bugfix/tempsensor_wifi_conflict_v4.3' into 'release/v4.3'

temperature_sensor: Fix issue that have conflict with phy / Fix phy pwdet and tsens power cannot be set twice issue (backport v4.3)

See merge request espressif/esp-idf!24968
This commit is contained in:
Jiang Jiang Jian 2023-07-27 23:05:17 +08:00
commit f57d0fcc5e
10 changed files with 405 additions and 61 deletions

View File

@ -31,6 +31,24 @@ typedef enum {
TSENS_DAC_DEFAULT = TSENS_DAC_L2,
} temp_sensor_dac_offset_t;
/**
* @brief tsens dac offset, internal use only
*/
typedef struct {
int index; /*!< temperature dac offset index */
int offset; /*!< temperature dac offset */
int set_val; /*!< temperature dac set value */
int range_min; /*!< temperature current range minimum */
int range_max; /*!< temperature current range maximum */
int error_max; /*!< temperature current range error */
} tsens_dac_offset_t;
extern const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX];
#define TSENS_ADC_FACTOR (0.4386)
#define TSENS_DAC_FACTOR (27.88)
#define TSENS_SYS_OFFSET (20.52)
/**
* @brief Configuration for temperature sensor reading
*/

View File

@ -28,6 +28,7 @@
#include "regi2c_ctrl.h"
#include "esp32c3/rom/ets_sys.h"
#include "esp32c3/esp_efuse_rtc_calib.h"
#include "esp_private/sar_periph_ctrl.h"
static const char *TAG = "tsens";
@ -38,20 +39,10 @@ static const char *TAG = "tsens";
} \
})
#define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */
#define TSENS_ADC_FACTOR (0.4386)
#define TSENS_DAC_FACTOR (27.88)
#define TSENS_SYS_OFFSET (20.52)
#define TEMPERATURE_SENSOR_MEASURE_MAX (125)
#define TEMPERATURE_SENSOR_MEASURE_MIN (-40)
typedef struct {
int index;
int offset;
int set_val;
int range_min;
int range_max;
int error_max;
} tsens_dac_offset_t;
static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
/* DAC Offset reg_val min max error */
{TSENS_DAC_L0, -2, 5, 50, 125, 3},
{TSENS_DAC_L1, -1, 7, 20, 100, 2},
@ -70,7 +61,7 @@ esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val);
APB_SARADC.apb_tsens_ctrl.tsens_clk_div = tsens.clk_div;
APB_SARADC.apb_tsens_ctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT;
APB_SARADC.apb_tsens_ctrl2.tsens_xpd_force = 1;
temp_sensor_sync_tsens_idx(tsens.dac_offset);
ESP_LOGD(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C",
dac_offset[tsens.dac_offset].range_min,
dac_offset[tsens.dac_offset].range_max,
@ -98,13 +89,13 @@ esp_err_t temp_sensor_start(void)
{
REG_SET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_TSENS_CLK_EN);
APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = 1;
APB_SARADC.apb_tsens_ctrl.tsens_pu = 1;
temperature_sensor_power_acquire();
return ESP_OK;
}
esp_err_t temp_sensor_stop(void)
{
APB_SARADC.apb_tsens_ctrl.tsens_pu = 0;
temperature_sensor_power_release();
APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = 0;
return ESP_OK;
}
@ -129,12 +120,12 @@ static void read_delta_t_from_efuse(void)
ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
}
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 = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT / 10.0;
float result = tsens_raw - s_deltaT / 10.0;
return result;
}
@ -142,18 +133,16 @@ esp_err_t temp_sensor_read_celsius(float *celsius)
{
TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG);
temp_sensor_config_t tsens;
uint32_t tsens_out = 0;
esp_err_t ret = temp_sensor_get_config(&tsens);
if (ret == ESP_OK) {
ret = temp_sensor_read_raw(&tsens_out);
printf("tsens_out %d\r\n", tsens_out);
TSENS_CHECK(ret == ESP_OK, ret);
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!");
ret = ESP_ERR_INVALID_STATE;
}
temp_sensor_get_config(&tsens);
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_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_MEASURE_MAX) {
ESP_LOGE(TAG, "Exceeding temperature measure range.");
return ESP_ERR_INVALID_STATE;
}
return ret;
if (range_changed) {
temp_sensor_get_config(&tsens);
}
return ESP_OK;
}

View File

@ -42,6 +42,24 @@ typedef struct {
uint8_t clk_div; /*!< Default: 6 */
} temp_sensor_config_t;
/**
* @brief tsens dac offset, internal use only
*/
typedef struct {
int index; /*!< temperature dac offset index */
int offset; /*!< temperature dac offset */
int set_val; /*!< temperature dac set value */
int range_min; /*!< temperature current range minimum */
int range_max; /*!< temperature current range maximum */
int error_max; /*!< temperature current range error */
} tsens_dac_offset_t;
extern const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX];
#define TSENS_ADC_FACTOR (0.4386)
#define TSENS_DAC_FACTOR (27.88)
#define TSENS_SYS_OFFSET (20.52)
/**
* @brief temperature sensor default setting.
*/

View File

@ -28,6 +28,7 @@
#include "regi2c_ctrl.h"
#include "esp_log.h"
#include "esp32s2/esp_efuse_rtc_table.h"
#include "esp_private/sar_periph_ctrl.h"
static const char *TAG = "tsens";
@ -38,20 +39,11 @@ static const char *TAG = "tsens";
} \
})
#define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */
#define TSENS_ADC_FACTOR (0.4386)
#define TSENS_DAC_FACTOR (27.88)
#define TSENS_SYS_OFFSET (20.52)
#define TEMPERATURE_SENSOR_MEASURE_MAX (125)
#define TEMPERATURE_SENSOR_MEASURE_MIN (-40)
typedef struct {
int index;
int offset;
int set_val;
int range_min;
int range_max;
int error_max;
} tsens_dac_offset_t;
static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
/* DAC Offset reg_val min max error */
{TSENS_DAC_L0, -2, 5, 50, 125, 3},
{TSENS_DAC_L1, -1, 7, 20, 100, 2},
@ -72,11 +64,10 @@ esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val);
SENS.sar_tctrl.tsens_clk_div = tsens.clk_div;
SENS.sar_tctrl.tsens_power_up_force = 1;
SENS.sar_tctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT;
SENS.sar_tctrl2.tsens_xpd_force = 1;
SENS.sar_tctrl2.tsens_reset = 1;// Reset the temp sensor.
SENS.sar_tctrl2.tsens_reset = 0;// Clear the reset status.
temp_sensor_sync_tsens_idx(tsens.dac_offset);
ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C",
dac_offset[tsens.dac_offset].range_min,
dac_offset[tsens.dac_offset].range_max,
@ -108,15 +99,15 @@ esp_err_t temp_sensor_start(void)
rtc_tsens_mux = xSemaphoreCreateMutex();
}
TSENS_CHECK(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM);
temperature_sensor_power_acquire();
SENS.sar_tctrl.tsens_dump_out = 0;
SENS.sar_tctrl2.tsens_clkgate_en = 1;
SENS.sar_tctrl.tsens_power_up = 1;
return ESP_OK;
}
esp_err_t temp_sensor_stop(void)
{
SENS.sar_tctrl.tsens_power_up = 0;
temperature_sensor_power_release();
SENS.sar_tctrl2.tsens_clkgate_en = 0;
if (rtc_tsens_mux != NULL) {
vSemaphoreDelete(rtc_tsens_mux);
@ -151,12 +142,12 @@ static void read_delta_t_from_efuse(void)
ESP_LOGD(TAG, "s_deltaT = %f\n", s_deltaT);
}
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 = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT;
float result = tsens_raw - s_deltaT / 10.0;
return result;
}
@ -164,17 +155,17 @@ esp_err_t temp_sensor_read_celsius(float *celsius)
{
TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG);
temp_sensor_config_t tsens;
uint32_t tsens_out = 0;
esp_err_t ret = temp_sensor_get_config(&tsens);
if (ret == ESP_OK) {
ret = temp_sensor_read_raw(&tsens_out);
TSENS_CHECK(ret == ESP_OK, ret);
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!");
ret = ESP_ERR_INVALID_STATE;
}
temp_sensor_get_config(&tsens);
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_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_MEASURE_MAX) {
ESP_LOGE(TAG, "Exceeding temperature measure range.");
return ESP_ERR_INVALID_STATE;
}
return ret;
if (range_changed) {
temp_sensor_get_config(&tsens);
}
return ESP_OK;
}

View File

@ -0,0 +1,54 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* SAR related peripherals are interdependent. This file
* provides a united control to these registers, as multiple
* components require these controls.
*
* See target/sar_periph_ctrl.c to know involved peripherals
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @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);
#ifdef __cplusplus
}
#endif

View File

@ -7,6 +7,10 @@ set(srcs "cpu_util_esp32c3.c"
"rtc_time.c"
)
if(NOT BOOTLOADER_BUILD)
list(APPEND srcs "sar_periph_ctrl.c")
endif()
add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}")
target_sources(${COMPONENT_LIB} PRIVATE "${srcs}")

View File

@ -0,0 +1,116 @@
/*
* 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"
#include "soc/apb_saradc_struct.h"
#include "private_include/regi2c_saradc.h"
#include "driver/temp_sensor.h"
#include "regi2c_ctrl.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) {
APB_SARADC.apb_tsens_ctrl.tsens_pu = 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) {
APB_SARADC.apb_tsens_ctrl.tsens_pu = false;
}
portEXIT_CRITICAL(&rtc_spinlock);
}
static int temperature_sensor_get_raw_value(void)
{
int raw_value = APB_SARADC.apb_tsens_ctrl.tsens_out;
return (TSENS_ADC_FACTOR * raw_value - TSENS_DAC_FACTOR * dac_offset[s_tsens_idx].offset - TSENS_SYS_OFFSET);
}
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 >= dac_offset[1].range_max) {
s_tsens_idx = 0;
} else if (degree >= dac_offset[2].range_max && degree < dac_offset[1].range_max) {
s_tsens_idx = 1;
} else if (degree <= dac_offset[2].range_min && degree > dac_offset[3].range_min) {
s_tsens_idx = 3;
} else if (degree <= dac_offset[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 = dac_offset[s_tsens_idx].set_val;
s_record_min = dac_offset[s_tsens_idx].range_min;
s_record_max = dac_offset[s_tsens_idx].range_max;
REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, 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;
}

View File

@ -11,6 +11,10 @@ set(srcs
"rtc_wdt.c"
"regi2c_ctrl.c")
if(NOT BOOTLOADER_BUILD)
list(APPEND srcs "sar_periph_ctrl.c")
endif()
add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}")
target_sources(${COMPONENT_LIB} PRIVATE "${srcs}")

View File

@ -0,0 +1,126 @@
/*
* 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"
#include "soc/apb_saradc_struct.h"
#include "soc/sens_struct.h"
#include "private_include/regi2c_saradc.h"
#include "driver/temp_sensor.h"
#include "regi2c_ctrl.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) {
SENS.sar_tctrl.tsens_power_up_force = true;
SENS.sar_tctrl2.tsens_xpd_force = true;
SENS.sar_tctrl.tsens_power_up = 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) {
SENS.sar_tctrl.tsens_power_up_force = false;
SENS.sar_tctrl2.tsens_xpd_force = false;
SENS.sar_tctrl.tsens_power_up = false;
}
portEXIT_CRITICAL(&rtc_spinlock);
}
static int temperature_sensor_get_raw_value(void)
{
SENS.sar_tctrl.tsens_dump_out = 1;
while (!SENS.sar_tctrl.tsens_ready) {
}
SENS.sar_tctrl.tsens_dump_out = 0;
int raw_value = SENS.sar_tctrl.tsens_out;
return (TSENS_ADC_FACTOR * raw_value - TSENS_DAC_FACTOR * dac_offset[s_tsens_idx].offset - TSENS_SYS_OFFSET);
}
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 >= dac_offset[1].range_max) {
s_tsens_idx = 0;
} else if (degree >= dac_offset[2].range_max && degree < dac_offset[1].range_max) {
s_tsens_idx = 1;
} else if (degree <= dac_offset[2].range_min && degree > dac_offset[3].range_min) {
s_tsens_idx = 3;
} else if (degree <= dac_offset[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 = dac_offset[s_tsens_idx].set_val;
s_record_min = dac_offset[s_tsens_idx].range_min;
s_record_max = dac_offset[s_tsens_idx].range_max;
REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, 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;
}

View File

@ -7,6 +7,8 @@
#include <stdbool.h>
#include "esp_attr.h"
#include "driver/adc.h"
#include "esp_private/sar_periph_ctrl.h"
/*
* This file is used to override the hooks provided by the PHY lib for some system features.
@ -14,6 +16,7 @@
*/
static bool s_wifi_adc_xpd_flag;
static bool s_wifi_tsens_xpd_flag;
void include_esp_phy_override(void)
{
@ -51,3 +54,24 @@ IRAM_ATTR void phy_i2c_exit_critical(void)
{
regi2c_exit_critical();
}
void phy_set_tsens_power(bool en)
{
if (s_wifi_tsens_xpd_flag == en) {
/* ignore repeated calls to phy_set_tsens_power when the state is already correct */
return;
}
s_wifi_tsens_xpd_flag = en;
if (en) {
temperature_sensor_power_acquire();
} else {
temperature_sensor_power_release();
}
}
int16_t phy_get_tsens_value(void)
{
return temp_sensor_get_raw_value(NULL);
}