Skip to content

Commit

Permalink
Merge branch 'bugfix/tempsensor_wifi_conflict_v4.4' into 'release/v4.4'
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 (v4.4)

See merge request espressif/esp-idf!25253
  • Loading branch information
ginkgm committed Sep 7, 2023
2 parents da65a5c + 8ce60fa commit df5df38
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 98 deletions.
18 changes: 18 additions & 0 deletions components/driver/esp32c3/include/driver/temp_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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
*/
Expand Down
51 changes: 20 additions & 31 deletions components/driver/esp32c3/rtc_tempsensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,15 @@
#include "regi2c_ctrl.h"
#include "esp32c3/rom/ets_sys.h"
#include "esp_efuse_rtc_calib.h"
#include "esp_private/sar_periph_ctrl.h"

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},
Expand Down Expand Up @@ -69,7 +60,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,
Expand Down Expand Up @@ -102,14 +93,14 @@ 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();
tsens_hw_state = TSENS_HW_STATE_STARTED;
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;
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
return ESP_OK;
Expand All @@ -135,31 +126,29 @@ 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(int16_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;
}

esp_err_t temp_sensor_read_celsius(float *celsius)
{
ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
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);
ESP_LOGV(TAG, "tsens_out %d", tsens_out);
ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data");
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;
int16_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;
}
18 changes: 18 additions & 0 deletions components/driver/esp32s2/include/driver/temp_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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.
*/
Expand Down
57 changes: 23 additions & 34 deletions components/driver/esp32s2/rtc_tempsensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@
#include "regi2c_ctrl.h"
#include "esp_log.h"
#include "esp_efuse_rtc_table.h"
#include "esp_private/sar_periph_ctrl.h"

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)

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] = {
#define TEMPERATURE_SENSOR_MEASURE_MAX (125)
#define TEMPERATURE_SENSOR_MEASURE_MIN (-40)


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},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -114,17 +105,15 @@ esp_err_t temp_sensor_start(void)
rtc_tsens_mux = xSemaphoreCreateMutex();
}
ESP_RETURN_ON_FALSE(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM, TAG, "failed to create mutex");
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;
tsens_hw_state = TSENS_HW_STATE_STARTED;
return err;
}

esp_err_t temp_sensor_stop(void)
{
SENS.sar_tctrl.tsens_power_up = 0;
SENS.sar_tctrl2.tsens_clkgate_en = 0;
temperature_sensor_power_release();
if (rtc_tsens_mux != NULL) {
vSemaphoreDelete(rtc_tsens_mux);
rtc_tsens_mux = NULL;
Expand Down Expand Up @@ -159,30 +148,30 @@ 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(int16_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;
}

esp_err_t temp_sensor_read_celsius(float *celsius)
{
ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
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);
ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data");
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;
int16_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;

}
18 changes: 18 additions & 0 deletions components/driver/esp32s3/include/driver/temp_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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.
*/
Expand Down
55 changes: 22 additions & 33 deletions components/driver/esp32s3/rtc_tempsensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@
#include "regi2c_ctrl.h"
#include "esp_log.h"
#include "esp_efuse_rtc_calib.h"
#include "esp_private/sar_periph_ctrl.h"

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)

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] = {
#define TEMPERATURE_SENSOR_MEASURE_MAX (125)
#define TEMPERATURE_SENSOR_MEASURE_MIN (-40)


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},
Expand Down Expand Up @@ -68,9 +60,6 @@ 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_tctrl.tsens_power_up = 1;
SENS.sar_tctrl2.tsens_xpd_force = 1;
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,
Expand Down Expand Up @@ -102,16 +91,16 @@ esp_err_t temp_sensor_start(void)
ESP_LOGE(TAG, "Temperature sensor is already running or not be configured");
err = ESP_ERR_INVALID_STATE;
}
temperature_sensor_power_acquire();
SENS.sar_tctrl.tsens_dump_out = 0;
SENS.sar_tctrl.tsens_power_up = 1;
SENS.sar_peri_clk_gate_conf.tsens_clk_en = 1;
tsens_hw_state = TSENS_HW_STATE_STARTED;
return err;
}

esp_err_t temp_sensor_stop(void)
{
SENS.sar_tctrl.tsens_power_up = 0;
temperature_sensor_power_release();
tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
return ESP_OK;
}
Expand Down Expand Up @@ -139,30 +128,30 @@ 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(int16_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;
}

esp_err_t temp_sensor_read_celsius(float *celsius)
{
ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
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);
ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data");
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;
int16_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;

}
Loading

0 comments on commit df5df38

Please sign in to comment.