forked from espressif/esp-idf
temp_sensor: added enable/disable functions
The previous start/stop functions have been rename to enable/disable.
This commit is contained in:
@@ -61,24 +61,24 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co
|
|||||||
esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens);
|
esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Start temperature measurement.
|
* @brief Enable the temperature sensor
|
||||||
*
|
*
|
||||||
* @param tsens The handle created by `temperature_sensor_install()`.
|
* @param tsens The handle created by `temperature_sensor_install()`.
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK Success
|
* - ESP_OK Success
|
||||||
* - ESP_ERR_INVALID_STATE if temperature sensor is started already.
|
* - ESP_ERR_INVALID_STATE if temperature sensor is enabled already.
|
||||||
*/
|
*/
|
||||||
esp_err_t temperature_sensor_start(temperature_sensor_handle_t tsens);
|
esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Stop temperature sensor measure.
|
* @brief Disable temperature sensor
|
||||||
*
|
*
|
||||||
* @param tsens The handle created by `temperature_sensor_install()`.
|
* @param tsens The handle created by `temperature_sensor_install()`.
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK Success
|
* - ESP_OK Success
|
||||||
* - ESP_ERR_INVALID_STATE if temperature sensor is stopped already.
|
* - ESP_ERR_INVALID_STATE if temperature sensor is not enabled yet.
|
||||||
*/
|
*/
|
||||||
esp_err_t temperature_sensor_stop(temperature_sensor_handle_t tsens);
|
esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read temperature sensor data that is converted to degrees Celsius.
|
* @brief Read temperature sensor data that is converted to degrees Celsius.
|
||||||
@@ -88,8 +88,9 @@ esp_err_t temperature_sensor_stop(temperature_sensor_handle_t tsens);
|
|||||||
* @param out_celsius The measure output value.
|
* @param out_celsius The measure output value.
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK Success
|
* - ESP_OK Success
|
||||||
* - ESP_ERR_INVALID_ARG ARG is NULL.
|
* - ESP_ERR_INVALID_ARG invalid arguments
|
||||||
* - ESP_ERR_INVALID_STATE The ambient temperature is out of range.
|
* - ESP_ERR_INVALID_STATE Temperature sensor is not enabled yet.
|
||||||
|
* - ESP_FAIL Parse the sensor data into ambient temperature failed (e.g. out of the range).
|
||||||
*/
|
*/
|
||||||
esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius);
|
esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius);
|
||||||
|
|
||||||
|
@@ -1,19 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG
|
#if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG
|
||||||
// The local log level must be defined before including esp_log.h
|
// The local log level must be defined before including esp_log.h
|
||||||
// Set the maximum log level for this source file
|
// Set the maximum log level for this source file
|
||||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "sys/lock.h"
|
#include "sys/lock.h"
|
||||||
#include "soc/rtc.h"
|
#include "soc/rtc.h"
|
||||||
@@ -25,7 +24,6 @@
|
|||||||
#include "driver/temperature_sensor.h"
|
#include "driver/temperature_sensor.h"
|
||||||
#include "esp_efuse_rtc_calib.h"
|
#include "esp_efuse_rtc_calib.h"
|
||||||
#include "esp_private/periph_ctrl.h"
|
#include "esp_private/periph_ctrl.h"
|
||||||
#include "hal/temperature_sensor_types.h"
|
|
||||||
#include "hal/temperature_sensor_ll.h"
|
#include "hal/temperature_sensor_ll.h"
|
||||||
|
|
||||||
static const char *TAG = "temperature_sensor";
|
static const char *TAG = "temperature_sensor";
|
||||||
@@ -35,10 +33,9 @@ extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate posi
|
|||||||
#define TEMPERATURE_SENSOR_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
|
#define TEMPERATURE_SENSOR_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TSENS_HW_STATE_UNCONFIGURED,
|
TEMP_SENSOR_FSM_INIT,
|
||||||
TSENS_HW_STATE_CONFIGURED,
|
TEMP_SENSOR_FSM_ENABLE,
|
||||||
TSENS_HW_STATE_STARTED,
|
} temp_sensor_fsm_t;
|
||||||
} temp_sensor_state_t;
|
|
||||||
|
|
||||||
static float s_deltaT = NAN; // unused number
|
static float s_deltaT = NAN; // unused number
|
||||||
|
|
||||||
@@ -46,7 +43,7 @@ typedef struct temperature_sensor_obj_t temperature_sensor_obj_t;
|
|||||||
|
|
||||||
struct temperature_sensor_obj_t {
|
struct temperature_sensor_obj_t {
|
||||||
const temp_sensor_ll_attribute_t *tsens_attribute;
|
const temp_sensor_ll_attribute_t *tsens_attribute;
|
||||||
temp_sensor_state_t tsens_hw_state;
|
temp_sensor_fsm_t fsm;
|
||||||
temperature_sensor_clk_src_t clk_src;
|
temperature_sensor_clk_src_t clk_src;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,9 +86,9 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co
|
|||||||
esp_err_t ret = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
ESP_RETURN_ON_FALSE((tsens_config && ret_tsens), ESP_ERR_INVALID_ARG, TAG, "Invalid argument");
|
ESP_RETURN_ON_FALSE((tsens_config && ret_tsens), ESP_ERR_INVALID_ARG, TAG, "Invalid argument");
|
||||||
ESP_RETURN_ON_FALSE((s_tsens_attribute_copy == NULL), ESP_ERR_INVALID_STATE, TAG, "Already installed");
|
ESP_RETURN_ON_FALSE((s_tsens_attribute_copy == NULL), ESP_ERR_INVALID_STATE, TAG, "Already installed");
|
||||||
temperature_sensor_handle_t tsens;
|
temperature_sensor_handle_t tsens = NULL;
|
||||||
tsens = (temperature_sensor_obj_t *) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT);
|
tsens = (temperature_sensor_obj_t *) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT);
|
||||||
ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "install fail...");
|
ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "no mem for temp sensor");
|
||||||
tsens->clk_src = tsens_config->clk_src;
|
tsens->clk_src = tsens_config->clk_src;
|
||||||
|
|
||||||
periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
|
periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
|
||||||
@@ -103,10 +100,13 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co
|
|||||||
tsens->tsens_attribute->range_min,
|
tsens->tsens_attribute->range_min,
|
||||||
tsens->tsens_attribute->range_max,
|
tsens->tsens_attribute->range_max,
|
||||||
tsens->tsens_attribute->error_max);
|
tsens->tsens_attribute->error_max);
|
||||||
|
|
||||||
TEMPERATURE_SENSOR_ENTER_CRITICAL();
|
TEMPERATURE_SENSOR_ENTER_CRITICAL();
|
||||||
temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
|
temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
|
||||||
|
temperature_sensor_ll_enable(false); // disable the sensor by default
|
||||||
TEMPERATURE_SENSOR_EXIT_CRITICAL();
|
TEMPERATURE_SENSOR_EXIT_CRITICAL();
|
||||||
tsens->tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
|
|
||||||
|
tsens->fsm = TEMP_SENSOR_FSM_INIT;
|
||||||
*ret_tsens = tsens;
|
*ret_tsens = tsens;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
err:
|
err:
|
||||||
@@ -116,52 +116,50 @@ err:
|
|||||||
|
|
||||||
esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
|
esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
|
||||||
{
|
{
|
||||||
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has already been uninstalled");
|
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
||||||
ESP_RETURN_ON_FALSE(tsens->tsens_hw_state != TSENS_HW_STATE_STARTED, ESP_ERR_INVALID_STATE, TAG, "Has not been stopped");
|
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
|
||||||
|
|
||||||
if (s_tsens_attribute_copy) {
|
if (s_tsens_attribute_copy) {
|
||||||
free(s_tsens_attribute_copy);
|
free(s_tsens_attribute_copy);
|
||||||
}
|
}
|
||||||
s_tsens_attribute_copy = NULL;
|
s_tsens_attribute_copy = NULL;
|
||||||
tsens->tsens_hw_state = TSENS_HW_STATE_UNCONFIGURED;
|
|
||||||
heap_caps_free(tsens);
|
periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
|
||||||
tsens = NULL;
|
free(tsens);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t temperature_sensor_start(temperature_sensor_handle_t tsens)
|
esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
|
||||||
{
|
{
|
||||||
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has not been installed");
|
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
||||||
ESP_RETURN_ON_FALSE(tsens->tsens_hw_state == TSENS_HW_STATE_CONFIGURED, ESP_ERR_INVALID_STATE, TAG, "Is already running or has not been configured");
|
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
|
||||||
|
|
||||||
#if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
|
#if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
|
||||||
if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
|
if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
|
||||||
periph_rtc_dig_clk8m_enable();
|
periph_rtc_dig_clk8m_enable();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
temperature_sensor_ll_clk_enable(true);
|
temperature_sensor_ll_clk_enable(true);
|
||||||
temperature_sensor_ll_clk_sel(tsens->clk_src);
|
temperature_sensor_ll_clk_sel(tsens->clk_src);
|
||||||
temperature_sensor_ll_enable(true);
|
temperature_sensor_ll_enable(true);
|
||||||
tsens->tsens_hw_state = TSENS_HW_STATE_STARTED;
|
tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t temperature_sensor_stop(temperature_sensor_handle_t tsens)
|
esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens)
|
||||||
{
|
{
|
||||||
ESP_RETURN_ON_FALSE(tsens->tsens_hw_state == TSENS_HW_STATE_STARTED, ESP_ERR_INVALID_STATE, TAG, "Has not been started");
|
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_ll_enable(false);
|
temperature_sensor_ll_enable(false);
|
||||||
#if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
|
#if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
|
||||||
if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
|
if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
|
||||||
periph_rtc_dig_clk8m_disable();
|
periph_rtc_dig_clk8m_disable();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
|
|
||||||
tsens->tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
|
tsens->fsm = TEMP_SENSOR_FSM_INIT;
|
||||||
{
|
|
||||||
ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "No tsens_out specified");
|
|
||||||
*tsens_out = temperature_sensor_ll_get_raw_value();
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,14 +185,15 @@ 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((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(out_celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "Celsius points to nothing");
|
||||||
ESP_RETURN_ON_FALSE(tsens->tsens_hw_state == TSENS_HW_STATE_STARTED, ESP_ERR_INVALID_ARG, TAG, "Has not been started");
|
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
|
||||||
uint32_t tsens_out = 0;
|
|
||||||
temp_sensor_read_raw(&tsens_out);
|
uint32_t tsens_out = temperature_sensor_ll_get_raw_value();
|
||||||
ESP_LOGV(TAG, "tsens_out %d", tsens_out);
|
ESP_LOGV(TAG, "tsens_out %d", tsens_out);
|
||||||
|
|
||||||
*out_celsius = parse_temp_sensor_raw_value(tsens_out, tsens->tsens_attribute->offset);
|
*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) {
|
if (*out_celsius < tsens->tsens_attribute->range_min || *out_celsius > tsens->tsens_attribute->range_max) {
|
||||||
ESP_LOGW(TAG, "Temperature range exceeded!");
|
ESP_LOGW(TAG, "value out of range, probably invalid");
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
@@ -36,5 +36,17 @@ void tearDown(void)
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
|
// _____ ____
|
||||||
|
// |_ _|__ _ __ ___ _ __ / ___| ___ _ __ ___ ___ _ __
|
||||||
|
// | |/ _ \ '_ ` _ \| '_ \ \___ \ / _ \ '_ \/ __|/ _ \| '__|
|
||||||
|
// | | __/ | | | | | |_) | ___) | __/ | | \__ \ (_) | |
|
||||||
|
// |_|\___|_| |_| |_| .__/ |____/ \___|_| |_|___/\___/|_|
|
||||||
|
// |_|
|
||||||
|
printf(" _____ ____\r\n");
|
||||||
|
printf("|_ _|__ _ __ ___ _ __ / ___| ___ _ __ ___ ___ _ __\r\n");
|
||||||
|
printf(" | |/ _ \\ '_ ` _ \\| '_ \\ \\___ \\ / _ \\ '_ \\/ __|/ _ \\| '__|\r\n");
|
||||||
|
printf(" | | __/ | | | | | |_) | ___) | __/ | | \\__ \\ (_) | |\r\n");
|
||||||
|
printf(" |_|\\___|_| |_| |_| .__/ |____/ \\___|_| |_|___/\\___/|_|\r\n");
|
||||||
|
printf(" |_|\r\n");
|
||||||
unity_run_menu();
|
unity_run_menu();
|
||||||
}
|
}
|
||||||
|
@@ -16,21 +16,25 @@ TEST_CASE("Temperature_sensor_driver_workflow_test", "[temperature_sensor]")
|
|||||||
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
||||||
temperature_sensor_handle_t temp_handle = NULL;
|
temperature_sensor_handle_t temp_handle = NULL;
|
||||||
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_start(temp_handle));
|
// read sensor before enable it should fail
|
||||||
|
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
||||||
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
printf("Temperature sensor started\n");
|
printf("Temperature sensor started\n");
|
||||||
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
||||||
printf("Temperature out celsius %f°C\n", tsens_out);
|
printf("Temperature out celsius %f°C\n", tsens_out);
|
||||||
TEST_ESP_OK(temperature_sensor_stop(temp_handle));
|
// uninstall driver before disable it should fail
|
||||||
|
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, temperature_sensor_uninstall(temp_handle));
|
||||||
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
||||||
// Reconfig the temperature sensor.
|
// Reconfig the temperature sensor.
|
||||||
temp_sensor.range_min = -20;
|
temp_sensor.range_min = -20;
|
||||||
temp_sensor.range_max = 45;
|
temp_sensor.range_max = 45;
|
||||||
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_start(temp_handle));
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
printf("Temperature sensor started again\n");
|
printf("Temperature sensor started again\n");
|
||||||
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
||||||
printf("Temperature out celsius %f°C\n", tsens_out);
|
printf("Temperature out celsius %f°C\n", tsens_out);
|
||||||
TEST_ESP_OK(temperature_sensor_stop(temp_handle));
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,9 +54,9 @@ TEST_CASE("Double start error cause test", "[temperature_sensor]")
|
|||||||
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
||||||
temperature_sensor_handle_t temp_handle = NULL;
|
temperature_sensor_handle_t temp_handle = NULL;
|
||||||
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_start(temp_handle));
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, temperature_sensor_start(temp_handle));
|
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, temperature_sensor_enable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_stop(temp_handle));
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,15 +67,15 @@ TEST_CASE("Double Start-Stop test", "[temperature_sensor]")
|
|||||||
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
||||||
temperature_sensor_handle_t temp_handle = NULL;
|
temperature_sensor_handle_t temp_handle = NULL;
|
||||||
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
TEST_ESP_OK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_start(temp_handle));
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
printf("Temperature sensor started\n");
|
printf("Temperature sensor started\n");
|
||||||
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
||||||
printf("Temperature out celsius %f°C\n", tsens_out);
|
printf("Temperature out celsius %f°C\n", tsens_out);
|
||||||
TEST_ESP_OK(temperature_sensor_stop(temp_handle));
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_start(temp_handle));
|
TEST_ESP_OK(temperature_sensor_enable(temp_handle));
|
||||||
printf("Temperature sensor started again\n");
|
printf("Temperature sensor started again\n");
|
||||||
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
TEST_ESP_OK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
||||||
printf("Temperature out celsius %f°C\n", tsens_out);
|
printf("Temperature out celsius %f°C\n", tsens_out);
|
||||||
TEST_ESP_OK(temperature_sensor_stop(temp_handle));
|
TEST_ESP_OK(temperature_sensor_disable(temp_handle));
|
||||||
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
TEST_ESP_OK(temperature_sensor_uninstall(temp_handle));
|
||||||
}
|
}
|
||||||
|
@@ -30,13 +30,9 @@ Functional Overview
|
|||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
- `Resource Allocation <#resource-allocation>`__ - covers which parameters should be set up to get a temperature sensor handle and how to recycle the resources when temperature sensor finishes working.
|
- `Resource Allocation <#resource-allocation>`__ - covers which parameters should be set up to get a temperature sensor handle and how to recycle the resources when temperature sensor finishes working.
|
||||||
|
- `Enable and Disable Temperature Sensor <#enable-and-disable-temperature-sensor>`__ - covers how to enable and disable the temperature sensor.
|
||||||
- `Start and Stop Temperature <#start-and-stop-temperature>`__ - covers how to start or stop the temperature sensor.
|
|
||||||
|
|
||||||
- `Get Temperature Value <#get-temperature-value>`__ - covers how to get the real-time temperature value.
|
- `Get Temperature Value <#get-temperature-value>`__ - covers how to get the real-time temperature value.
|
||||||
|
|
||||||
- `Power Management <#power-management>`__ - covers how temperature sensor is affected when changing power mode (i.e. light sleep).
|
- `Power Management <#power-management>`__ - covers how temperature sensor is affected when changing power mode (i.e. light sleep).
|
||||||
|
|
||||||
- `Thread Safety <#thread-safety>`__ - covers how to make the driver to be thread safe.
|
- `Thread Safety <#thread-safety>`__ - covers how to make the driver to be thread safe.
|
||||||
|
|
||||||
Resource Allocation
|
Resource Allocation
|
||||||
@@ -48,7 +44,6 @@ In order to install a built-in temperature sensor instance, the first thing is t
|
|||||||
:cpp:type:`temperature_sensor_config_t`:
|
:cpp:type:`temperature_sensor_config_t`:
|
||||||
|
|
||||||
- :cpp:member:`range_min`. The minimum value of testing range you have evaluated.
|
- :cpp:member:`range_min`. The minimum value of testing range you have evaluated.
|
||||||
|
|
||||||
- :cpp:member:`range_max`. The maximum value of testing range you have evaluated.
|
- :cpp:member:`range_max`. The maximum value of testing range you have evaluated.
|
||||||
|
|
||||||
After the ranges are set, the structure could be passed to :cpp:func:`temperature_sensor_install`, which will instantiate the temperature sensor instance and return a handle.
|
After the ranges are set, the structure could be passed to :cpp:func:`temperature_sensor_install`, which will instantiate the temperature sensor instance and return a handle.
|
||||||
@@ -61,7 +56,6 @@ Creating a Temperature Sensor Handle
|
|||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
* Step1: Evaluate the testing range. In this example, the range is 20 °C ~ 50 °C.
|
* Step1: Evaluate the testing range. In this example, the range is 20 °C ~ 50 °C.
|
||||||
|
|
||||||
* Step2: Configure the range and obtain a handle
|
* Step2: Configure the range and obtain a handle
|
||||||
|
|
||||||
.. code:: c
|
.. code:: c
|
||||||
@@ -73,28 +67,27 @@ Creating a Temperature Sensor Handle
|
|||||||
};
|
};
|
||||||
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
||||||
|
|
||||||
Start and Stop Temperature
|
Enable and Disable Temperature Sensor
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
1. Start the temperature sensor by calling :cpp:func:`temperature_sensor_start`. The temperature sensor will now measure the temperature.
|
1. Enable the temperature sensor by calling :cpp:func:`temperature_sensor_enable`. The internal temperature sensor circuit will start to work. The driver state will transit from init to enable.
|
||||||
|
2. To Disable the temperature sensor, please call :cpp:func:`temperature_sensor_disable`.
|
||||||
2. To stop the temperature sensor, please call :cpp:func:`temperature_sensor_stop`.
|
|
||||||
|
|
||||||
Get Temperature Value
|
Get Temperature Value
|
||||||
^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
After the temperature sensor has been installed, you can get the temperature value by following the steps below.
|
After the temperature sensor is enabled by :cpp:func:`temperature_sensor_enable`, user can get the current temperature by calling :cpp:func:`temperature_sensor_get_celsius`.
|
||||||
|
|
||||||
1. To get the current temperature, please call :cpp:func:`temperature_sensor_get_celsius`.
|
|
||||||
|
|
||||||
.. code:: c
|
.. code:: c
|
||||||
|
|
||||||
ESP_ERROR_CHECK(temperature_sensor_start(temp_handle));
|
// Enable temperature sensor
|
||||||
printf("Temperature sensor started\n");
|
ESP_ERROR_CHECK(temperature_sensor_enable(temp_handle));
|
||||||
|
// Get converted sensor data
|
||||||
float tsens_out;
|
float tsens_out;
|
||||||
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
||||||
printf("Temperature in %f °C\n", tsens_out);
|
printf("Temperature in %f °C\n", tsens_out);
|
||||||
ESP_ERROR_CHECK(temperature_sensor_stop(temp_handle));
|
// Disable the temperature sensor if it's not needed and save the power
|
||||||
|
ESP_ERROR_CHECK(temperature_sensor_disable(temp_handle));
|
||||||
|
|
||||||
Power Management
|
Power Management
|
||||||
^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^
|
||||||
@@ -114,12 +107,12 @@ Unexpected Behaviors
|
|||||||
2. When installing the temperature sensor, the driver gives a 'the boundary you gave cannot meet the range of internal temperature sensor' error feedback. It is because the built-in temperature sensor has testing limit. The error due to setting :cpp:type:`temperature_sensor_config_t`:
|
2. When installing the temperature sensor, the driver gives a 'the boundary you gave cannot meet the range of internal temperature sensor' error feedback. It is because the built-in temperature sensor has testing limit. The error due to setting :cpp:type:`temperature_sensor_config_t`:
|
||||||
|
|
||||||
(1) Totally out of range, like 200 °C ~ 300 °C.
|
(1) Totally out of range, like 200 °C ~ 300 °C.
|
||||||
(2) Cross the boundary of each predefined measurement. like 40 °C ~ 110 °C.
|
(2) Cross the boundary of each predefined measurement. like 40 °C ~ 110 °C.
|
||||||
|
|
||||||
Application Example
|
Application Example
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
Temperature sensor reading example: :example:`peripherals/temp_sensor`.
|
* Temperature sensor reading example: :example:`peripherals/temp_sensor`.
|
||||||
|
|
||||||
API Reference
|
API Reference
|
||||||
----------------------------------
|
----------------------------------
|
||||||
|
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
# Temperature Sensor Example
|
# Temperature Sensor Example
|
||||||
|
|
||||||
The ESP32-S2/C3/S3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.
|
The ESP32-S2/C3/S3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.
|
||||||
|
|
||||||
The conversion relationship is the first two columns of the table below. Among them, `offset = 0`(default) is the main measurement option, and other values are extended measurement options.
|
The conversion relationship is the first two columns of the table below. Among them, `offset = 0`(default) is the main measurement option, and other values are extended measurement options.
|
||||||
|
|
||||||
| DAC level | offset | measure range(℃) | measure error(℃) |
|
| DAC level | offset | measure range(℃) | measure error(℃) |
|
||||||
| :-------: | :----: | :--------------: | :--------------: |
|
| :-------: | :----: | :--------------: | :--------------: |
|
||||||
@@ -37,18 +37,17 @@ See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/l
|
|||||||
## Example Output
|
## Example Output
|
||||||
|
|
||||||
```
|
```
|
||||||
I (276) example: Initializing Temperature sensor
|
I (0) cpu_start: Starting scheduler on APP CPU.
|
||||||
I (276) temperature_sensor: temperature range [-10°C ~ 80°C], error < 1°C
|
I (303) example: Install temperature sensor, expected temp ranger range: 10~50 ℃
|
||||||
I (286) example: Temperature sensor started
|
I (303) temperature_sensor: Range [-10°C ~ 80°C], error < 1°C
|
||||||
I (1286) example: Temperature out celsius 21.64
|
I (313) example: Enable temperature sensor
|
||||||
I (2286) example: Temperature out celsius 21.64
|
I (323) example: Read temperature
|
||||||
I (3286) example: Temperature out celsius 21.64
|
I (323) example: Temperature value 26.06 ℃
|
||||||
I (4286) example: Temperature out celsius 22.08
|
I (1323) example: Temperature value 26.06 ℃
|
||||||
I (5286) example: Temperature out celsius 22.08
|
I (2323) example: Temperature value 26.06 ℃
|
||||||
I (6286) example: Temperature out celsius 22.08
|
I (3323) example: Temperature value 26.06 ℃
|
||||||
I (7286) example: Temperature out celsius 22.08
|
I (4323) example: Temperature value 26.06 ℃
|
||||||
I (8286) example: Temperature out celsius 22.08
|
I (5323) example: Temperature value 26.49 ℃
|
||||||
I (9286) example: Temperature out celsius 22.08
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -4,34 +4,29 @@
|
|||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "esp_log.h"
|
|
||||||
#include "esp_check.h"
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
#include "esp_log.h"
|
||||||
#include "driver/temperature_sensor.h"
|
#include "driver/temperature_sensor.h"
|
||||||
|
|
||||||
static const char *TAG = "example";
|
static const char *TAG = "example";
|
||||||
|
|
||||||
void tempsensor_example(void)
|
|
||||||
{
|
|
||||||
// Initialize touch pad peripheral, it will start a timer to run a filter
|
|
||||||
ESP_LOGI(TAG, "Initializing Temperature sensor");
|
|
||||||
float tsens_out;
|
|
||||||
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
|
||||||
temperature_sensor_handle_t temp_handle = NULL;
|
|
||||||
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
|
|
||||||
ESP_ERROR_CHECK(temperature_sensor_start(temp_handle));
|
|
||||||
ESP_LOGI(TAG, "Temperature sensor started");
|
|
||||||
int cnt = 20; //read value for 20 times
|
|
||||||
while (cnt) {
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
|
|
||||||
ESP_LOGI(TAG, "Temperature out celsius %.02f", tsens_out);
|
|
||||||
cnt--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
tempsensor_example();
|
ESP_LOGI(TAG, "Install temperature sensor, expected temp ranger range: 10~50 ℃");
|
||||||
|
temperature_sensor_handle_t temp_sensor = NULL;
|
||||||
|
temperature_sensor_config_t temp_sensor_config = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
|
||||||
|
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Enable temperature sensor");
|
||||||
|
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Read temperature");
|
||||||
|
int cnt = 20;
|
||||||
|
float tsens_value;
|
||||||
|
while (cnt--) {
|
||||||
|
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
|
||||||
|
ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,11 @@ from pytest_embedded.dut import Dut
|
|||||||
@pytest.mark.esp32s3
|
@pytest.mark.esp32s3
|
||||||
@pytest.mark.generic
|
@pytest.mark.generic
|
||||||
def test_temp_sensor_example(dut: Dut) -> None:
|
def test_temp_sensor_example(dut: Dut) -> None:
|
||||||
dut.expect_exact('Initializing Temperature sensor')
|
dut.expect_exact('Install temperature sensor')
|
||||||
dut.expect_exact('Temperature sensor started')
|
dut.expect_exact('Enable temperature sensor')
|
||||||
temp_value = dut.expect(r'Temperature out celsius (\d+\.\d+)', timeout=30)
|
dut.expect_exact('Read temperature')
|
||||||
|
temp_value = dut.expect(r'Temperature value (\d+\.\d+) .*', timeout=5)
|
||||||
# Because the example test only run in the normal temperature environment. So this assert range is meaningful
|
# Because the example test only run in the normal temperature environment. So this assert range is meaningful
|
||||||
assert 0 < float(temp_value.group(1)) < 45
|
assert 0 < float(temp_value.group(1).decode('utf8')) < 50
|
||||||
|
temp_value = dut.expect(r'Temperature value (\d+\.\d+) .*', timeout=5)
|
||||||
|
assert 0 < float(temp_value.group(1).decode('utf8')) < 50
|
||||||
|
Reference in New Issue
Block a user