temperature_sensor: Apply new-shared interface in temperature sensor driver

This commit is contained in:
Cao Sen Miao 2023-07-24 14:34:55 +08:00
parent 6bb129620f
commit f0ca580c80
12 changed files with 510 additions and 99 deletions

View File

@ -23,13 +23,16 @@ typedef enum {
TSENS_DAC_DEFAULT = TSENS_DAC_L2,
} temp_sensor_dac_offset_t;
/**
* @brief tsens dac offset, internal use only
*/
typedef struct {
int index;
int offset;
int set_val;
int range_min;
int range_max;
int error_max;
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];

View File

@ -21,10 +21,13 @@
#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 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 */
@ -57,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,
@ -90,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;
@ -123,12 +126,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;
}
@ -136,18 +139,16 @@ 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;
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

@ -34,13 +34,16 @@ typedef struct {
uint8_t clk_div; /*!< Default: 6 */
} temp_sensor_config_t;
/**
* @brief tsens dac offset, internal use only
*/
typedef struct {
int index;
int offset;
int set_val;
int range_min;
int range_max;
int error_max;
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];

View File

@ -21,10 +21,14 @@
#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 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 */
@ -60,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,
@ -102,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;
@ -147,12 +148,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;
}
@ -160,17 +161,17 @@ 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;
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

@ -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.
*/

View File

@ -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)
#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},
@ -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,
@ -102,8 +91,8 @@ 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;
@ -111,7 +100,7 @@ esp_err_t temp_sensor_start(void)
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;
}
@ -139,12 +128,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;
}
@ -152,17 +141,17 @@ 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;
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

@ -21,6 +21,60 @@
extern "C" {
#endif
/**
* Initialise SAR related peripheral register settings
* Should only be used when running into app stage
*/
void sar_periph_ctrl_init(void);
/*------------------------------------------------------------------------------
* ADC Power
*----------------------------------------------------------------------------*/
/**
* @brief Acquire the ADC oneshot mode power
*/
void sar_periph_ctrl_adc_oneshot_power_acquire(void);
/**
* @brief Release the ADC oneshot mode power
*/
void sar_periph_ctrl_adc_oneshot_power_release(void);
/**
* @brief Acquire the ADC continuous mode power
*/
void sar_periph_ctrl_adc_continuous_power_acquire(void);
/**
* @brief Release the ADC ADC continuous mode power
*/
void sar_periph_ctrl_adc_continuous_power_release(void);
/*------------------------------------------------------------------------------
* PWDET Power
*----------------------------------------------------------------------------*/
/**
* @brief Acquire the PWDET Power
*/
void sar_periph_ctrl_pwdet_power_acquire(void);
/**
* @brief Release the PWDET Power
*/
void sar_periph_ctrl_pwdet_power_release(void);
/**
* @brief Enable SAR power when system wakes up
*/
void sar_periph_ctrl_power_enable(void);
/**
* @brief Disable SAR power when system goes to sleep
*/
void sar_periph_ctrl_power_disable(void);
/**
* @brief Acquire the temperature sensor power
*/
@ -41,6 +95,14 @@ void temperature_sensor_power_release(void);
*/
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

@ -4,19 +4,131 @@
* 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.
*
* Related peripherals are:
* - ADC
* - PWDET
* - Temp Sensor
*/
#include "soc/soc_caps.h"
#include "sdkconfig.h"
#include "esp_log.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 "hal/sar_ctrl_ll.h"
#include "hal/adc_ll.h"
#include "driver/temp_sensor.h"
#include "regi2c_ctrl.h"
static const char *TAG = "sar_periph_ctrl";
extern portMUX_TYPE rtc_spinlock;
extern __attribute__((unused)) portMUX_TYPE rtc_spinlock;
void sar_periph_ctrl_init(void)
{
//Put SAR control mux to FSM state
sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM);
//Add other periph power control initialisation here
}
void sar_periph_ctrl_power_enable(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM);
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
void sar_periph_ctrl_power_disable(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_OFF);
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
/*------------------------------------------------------------------------------
* PWDET Power
*----------------------------------------------------------------------------*/
static int s_pwdet_power_on_cnt;
void sar_periph_ctrl_pwdet_power_acquire(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
s_pwdet_power_on_cnt++;
if (s_pwdet_power_on_cnt == 1) {
sar_ctrl_ll_set_power_mode_from_pwdet(SAR_CTRL_LL_POWER_ON);
}
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
void sar_periph_ctrl_pwdet_power_release(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
s_pwdet_power_on_cnt--;
/* Sanity check */
if (s_pwdet_power_on_cnt < 0) {
portEXIT_CRITICAL(&rtc_spinlock);
ESP_LOGE(TAG, "%s called, but s_pwdet_power_on_cnt == 0", __func__);
abort();
} else if (s_pwdet_power_on_cnt == 0) {
sar_ctrl_ll_set_power_mode_from_pwdet(SAR_CTRL_LL_POWER_FSM);
}
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
/*------------------------------------------------------------------------------
* ADC Power
*----------------------------------------------------------------------------*/
static int s_saradc_power_on_cnt;
static void s_sar_adc_power_acquire(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
s_saradc_power_on_cnt++;
if (s_saradc_power_on_cnt == 1) {
adc_ll_digi_set_power_manage(ADC_POWER_SW_ON);
}
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
static void s_sar_adc_power_release(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
s_saradc_power_on_cnt--;
if (s_saradc_power_on_cnt < 0) {
portEXIT_CRITICAL(&rtc_spinlock);
ESP_LOGE(TAG, "%s called, but s_saradc_power_on_cnt == 0", __func__);
abort();
} else if (s_saradc_power_on_cnt == 0) {
adc_ll_digi_set_power_manage(ADC_POWER_BY_FSM);
}
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
void sar_periph_ctrl_adc_oneshot_power_acquire(void)
{
s_sar_adc_power_acquire();
}
void sar_periph_ctrl_adc_oneshot_power_release(void)
{
s_sar_adc_power_release();
}
void sar_periph_ctrl_adc_continuous_power_acquire(void)
{
s_sar_adc_power_acquire();
}
void sar_periph_ctrl_adc_continuous_power_release(void)
{
s_sar_adc_power_release();
}
/*------------------------------------------------------------------------------------------------------------
-----------------------------------------Temperature Sensor---------------------------------------------------

View File

@ -25,9 +25,5 @@ if(NOT BOOTLOADER_BUILD)
"sar_periph_ctrl.c")
endif()
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

@ -4,20 +4,105 @@
* 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.
*
* Related peripherals are:
* - ADC
* - PWDET
* - Temp Sensor
*/
#include "soc/soc_caps.h"
#include "sdkconfig.h"
#include "esp_log.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 "hal/sar_ctrl_ll.h"
#include "hal/adc_ll.h"
#include "driver/temp_sensor.h"
#include "regi2c_ctrl.h"
static const char *TAG = "sar_periph_ctrl";
extern portMUX_TYPE rtc_spinlock;
extern __attribute__((unused)) portMUX_TYPE rtc_spinlock;
void sar_periph_ctrl_init(void)
{
//Put SAR control mux to FSM state
sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM);
//Add other periph power control initialisation here
}
void sar_periph_ctrl_power_enable(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM);
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
void sar_periph_ctrl_power_disable(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_OFF);
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
/*------------------------------------------------------------------------------
* PWDET Power
*----------------------------------------------------------------------------*/
static int s_pwdet_power_on_cnt;
void sar_periph_ctrl_pwdet_power_acquire(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
s_pwdet_power_on_cnt++;
if (s_pwdet_power_on_cnt == 1) {
sar_ctrl_ll_set_power_mode_from_pwdet(SAR_CTRL_LL_POWER_ON);
}
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
void sar_periph_ctrl_pwdet_power_release(void)
{
portENTER_CRITICAL_SAFE(&rtc_spinlock);
s_pwdet_power_on_cnt--;
/* Sanity check */
if (s_pwdet_power_on_cnt < 0) {
portEXIT_CRITICAL(&rtc_spinlock);
ESP_LOGE(TAG, "%s called, but s_pwdet_power_on_cnt == 0", __func__);
abort();
} else if (s_pwdet_power_on_cnt == 0) {
sar_ctrl_ll_set_power_mode_from_pwdet(SAR_CTRL_LL_POWER_FSM);
}
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
}
/*------------------------------------------------------------------------------
* ADC Power
*----------------------------------------------------------------------------*/
void sar_periph_ctrl_adc_oneshot_power_acquire(void)
{
//Keep oneshot mode power controlled by HW, leave this function for compatibility
}
void sar_periph_ctrl_adc_oneshot_power_release(void)
{
//Keep oneshot mode power controlled by HW, leave this function for compatibility
}
void sar_periph_ctrl_adc_continuous_power_acquire(void)
{
adc_ll_digi_set_power_manage(ADC_POWER_SW_ON);
}
void sar_periph_ctrl_adc_continuous_power_release(void)
{
adc_ll_digi_set_power_manage(ADC_POWER_BY_FSM);
}
/*------------------------------------------------------------------------------------------------------------
-----------------------------------------Temperature Sensor---------------------------------------------------
@ -40,6 +125,7 @@ void temperature_sensor_power_acquire(void)
SENS.sar_tctrl.tsens_power_up_force = true;
SENS.sar_tctrl2.tsens_xpd_force = true;
SENS.sar_tctrl.tsens_power_up = true;
SENS.sar_tctrl2.tsens_clkgate_en = true;
}
portEXIT_CRITICAL(&rtc_spinlock);
}
@ -57,6 +143,7 @@ void temperature_sensor_power_release(void)
SENS.sar_tctrl.tsens_power_up_force = false;
SENS.sar_tctrl2.tsens_xpd_force = false;
SENS.sar_tctrl.tsens_power_up = false;
SENS.sar_tctrl2.tsens_clkgate_en = false;
}
portEXIT_CRITICAL(&rtc_spinlock);
}
@ -123,4 +210,4 @@ int16_t temp_sensor_get_raw_value(bool *range_changed)
portEXIT_CRITICAL(&rtc_spinlock);
return degree;
}
}

View File

@ -21,6 +21,7 @@
#include "esp_private/sar_periph_ctrl.h"
#include "hal/sar_ctrl_ll.h"
#include "hal/adc_ll.h"
#include "driver/temp_sensor.h"
static const char *TAG = "sar_periph_ctrl";
extern portMUX_TYPE rtc_spinlock;
@ -117,3 +118,109 @@ void sar_periph_ctrl_adc_continuous_power_release(void)
{
s_sar_power_release();
}
/*------------------------------------------------------------------------------
* 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 = 1;
SENS.sar_tctrl.tsens_power_up = 1;
SENS.sar_tctrl2.tsens_xpd_force = 1;
}
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 = 0;
SENS.sar_tctrl.tsens_power_up = 0;
SENS.sar_tctrl2.tsens_xpd_force = 0;
}
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

@ -8,6 +8,9 @@
#include "esp_attr.h"
#include "regi2c_ctrl.h"
#include "esp_private/sar_periph_ctrl.h"
#include "esp_private/sar_periph_ctrl.h"
#include "freertos/FreeRTOS.h"
/*
* This file is used to override the hooks provided by the PHY lib for some system features.
@ -15,6 +18,8 @@
*/
static bool s_wifi_adc_xpd_flag;
static bool s_wifi_pwdet_xpd_flag;
static bool s_wifi_tsens_xpd_flag;
void include_esp_phy_override(void)
{
@ -52,9 +57,36 @@ IRAM_ATTR void phy_i2c_exit_critical(void)
void phy_set_pwdet_power(bool en)
{
if (s_wifi_pwdet_xpd_flag == en) {
/* ignore repeated calls to phy_set_pwdet_power when the state is already correct */
return;
}
s_wifi_pwdet_xpd_flag = en;
if (en) {
sar_periph_ctrl_pwdet_power_acquire();
} else {
sar_periph_ctrl_pwdet_power_release();
}
}
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);
}