temperature_sensor: Add new interface and reference counts so that phy and driver can use together

pull/11810/head
Cao Sen Miao 2023-05-17 17:40:59 +08:00 zatwierdzone przez Xiao Xufeng
rodzic a6a962d5f8
commit ca41902ccf
5 zmienionych plików z 167 dodań i 1 usunięć

Wyświetl plik

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

Wyświetl plik

@ -16,6 +16,7 @@ if(NOT BOOTLOADER_BUILD)
"sleep_gpio.c"
"sleep_mac_bb.c"
"regi2c_ctrl.c"
"sar_periph_ctrl_common.c"
"adc_share_hw_ctrl.c")
if(NOT CONFIG_IDF_TARGET_ESP32 AND NOT CONFIG_IDF_TARGET_ESP32S2)

Wyświetl plik

@ -14,6 +14,9 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -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
*/

Wyświetl plik

@ -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

Wyświetl plik

@ -8,6 +8,9 @@
#include "esp_attr.h"
#include "esp_private/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.
@ -58,3 +61,17 @@ void phy_set_pwdet_power(bool en)
sar_periph_ctrl_pwdet_power_release();
}
}
void phy_set_tsens_power(bool 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);
}