feat(temperature_sensor): Add new support for temperature sensor ETM on ESP32C6/H2

pull/13338/head
Cao Sen Miao 2024-02-29 18:29:24 +08:00
rodzic e4f167df25
commit 2b2b3be98f
15 zmienionych plików z 244 dodań i 11 usunięć

Wyświetl plik

@ -3,6 +3,9 @@ set(priv_req efuse)
set(public_include "include")
if(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
list(APPEND srcs "src/temperature_sensor.c")
if(CONFIG_SOC_TEMPERATURE_SENSOR_SUPPORT_ETM)
list(APPEND srcs "src/temperature_sensor_etm.c")
endif()
endif()
idf_component_register(SRCS ${srcs}

Wyświetl plik

@ -0,0 +1,64 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_err.h"
#include "esp_etm.h"
#include "driver/temperature_sensor.h"
#include "hal/temperature_sensor_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Temperature Sensor ETM event configuration
*/
typedef struct {
temperature_sensor_etm_event_type_t event_type; /*!< Temperature Sensor ETM event type */
} temperature_sensor_etm_event_config_t;
/**
* @brief Get the ETM event for Temperature Sensor
*
* @note The created ETM event object can be deleted later by calling `esp_etm_del_event`
*
* @param[in] tsens Temperature Sensor handle, allocated by `temperature_sensor_install()`
* @param[in] config Temperature Sensor ETM event configuration
* @param[out] out_event Returned ETM event handle
* @return
* - ESP_OK: Get ETM event successfully
* - ESP_ERR_INVALID_ARG: Get ETM event failed because of invalid argument
* - ESP_FAIL: Get ETM event failed because of other error
*/
esp_err_t temperature_sensor_new_etm_event(temperature_sensor_handle_t tsens, const temperature_sensor_etm_event_config_t *config, esp_etm_event_handle_t *out_event);
/**
* @brief Temperature Sensor ETM task configuration
*/
typedef struct {
temperature_sensor_etm_task_type_t task_type; /*!< Temperature Sensor ETM task type */
} temperature_sensor_etm_task_config_t;
/**
* @brief Get the ETM task for Temperature Sensor
*
* @note The created ETM event object can be deleted later by calling `esp_etm_del_event`
*
* @param[in] tsens Temperature Sensor, allocated by `temperature_sensor_install()`
* @param[in] config Temperature Sensor ETM event configuration
* @param[out] out_task Returned ETM event handle
* @return
* - ESP_OK: Get ETM event successfully
* - ESP_ERR_INVALID_ARG: Get ETM event failed because of invalid argument
* - ESP_FAIL: Get ETM event failed because of other error
*/
esp_err_t temperature_sensor_new_etm_task(temperature_sensor_handle_t tsens, const temperature_sensor_etm_task_config_t *config, esp_etm_task_handle_t *out_task);
#ifdef __cplusplus
}
#endif

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -106,6 +106,9 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co
ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "no mem for temp sensor");
tsens->clk_src = tsens_config->clk_src;
temperature_sensor_power_acquire();
temperature_sensor_ll_clk_sel(tsens->clk_src);
ESP_GOTO_ON_ERROR(temperature_sensor_attribute_table_sort(), err, TAG, "Table sort failed");
ESP_GOTO_ON_ERROR(temperature_sensor_choose_best_range(tsens, tsens_config), err, TAG, "Cannot select the correct range");
@ -140,6 +143,7 @@ esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
ESP_RETURN_ON_ERROR(esp_intr_free(tsens->temp_sensor_isr_handle), TAG, "uninstall interrupt service failed");
}
#endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
temperature_sensor_power_release();
free(tsens);
return ESP_OK;
@ -175,8 +179,6 @@ esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
temperature_sensor_ll_sample_enable(true);
#endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
temperature_sensor_ll_clk_sel(tsens->clk_src);
temperature_sensor_power_acquire();
// After enabling/reseting the temperature sensor,
// the output value gradually approaches the true temperature
// value as the measurement time increases. 300us is recommended.
@ -190,7 +192,6 @@ esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens)
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_power_release();
#if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
temperature_sensor_ll_wakeup_enable(false);
temperature_sensor_ll_sample_enable(false);
@ -259,7 +260,7 @@ esp_err_t temperature_sensor_set_absolute_threshold(temperature_sensor_handle_t
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Temperature sensor has not been installed");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in init state");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in enable state");
ESP_RETURN_ON_FALSE(abs_cfg, ESP_ERR_INVALID_ARG, TAG, "Invalid callback configuration");
temperature_sensor_ll_set_sample_rate(0xffff);
@ -274,7 +275,7 @@ esp_err_t temperature_sensor_set_delta_threshold(temperature_sensor_handle_t tse
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Temperature sensor has not been installed");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in init state");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in enable state");
ESP_RETURN_ON_FALSE(delta_cfg, ESP_ERR_INVALID_ARG, TAG, "Invalid callback configuration");
temperature_sensor_ll_set_sample_rate(0xffff);
@ -289,7 +290,7 @@ esp_err_t temperature_sensor_register_callbacks(temperature_sensor_handle_t tsen
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Temperature sensor has not been installed");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in init state");
ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in enable state");
ESP_RETURN_ON_FALSE(cbs, ESP_ERR_INVALID_ARG, TAG, "callback group pointer is invalid");
#if CONFIG_TEMP_SENSOR_ISR_IRAM_SAFE

Wyświetl plik

@ -0,0 +1,96 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include "esp_err.h"
#include "esp_etm.h"
#include "esp_check.h"
#include "esp_private/etm_interface.h"
#include "hal/temperature_sensor_ll.h"
#include "driver/temperature_sensor_etm.h"
#include "esp_heap_caps.h"
#if CONFIG_ETM_ENABLE_DEBUG_LOG
// The local log level must be defined before including esp_log.h
// Set the maximum log level for this source file
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#endif
#include "esp_log.h"
#define ETM_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
static const char *TAG = "tsens-etm";
static esp_err_t temperature_sensor_del_etm_event(esp_etm_event_t *event)
{
free(event);
return ESP_OK;
}
static esp_err_t temperature_sensor_del_etm_task(esp_etm_task_t *task)
{
free(task);
return ESP_OK;
}
esp_err_t temperature_sensor_new_etm_event(temperature_sensor_handle_t tsens, const temperature_sensor_etm_event_config_t *config, esp_etm_event_handle_t *out_event)
{
#if CONFIG_ETM_ENABLE_DEBUG_LOG
esp_log_level_set(TAG, ESP_LOG_DEBUG);
#endif
esp_etm_event_t *event = NULL;
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(tsens && config && out_event, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
ESP_GOTO_ON_FALSE(config->event_type < TEMPERATURE_SENSOR_EVENT_MAX, ESP_ERR_INVALID_ARG, err, TAG, "invalid event type");
event = heap_caps_calloc(1, sizeof(esp_etm_event_t), ETM_MEM_ALLOC_CAPS);
ESP_GOTO_ON_FALSE(event, ESP_ERR_NO_MEM, err, TAG, "no memory for ETM event");
uint32_t event_id = TEMPERATURE_SENSOR_LL_ETM_EVENT_TABLE(config->event_type);
ESP_GOTO_ON_FALSE(event_id != 0, ESP_ERR_NOT_SUPPORTED, err, TAG, "not supported event type");
// fill the ETM event object
event->event_id = event_id;
event->trig_periph = ETM_TRIG_PERIPH_TSENS;
event->del = temperature_sensor_del_etm_event;
ESP_LOGD(TAG, "new event @%p, event_id=%"PRIu32, event, event_id);
*out_event = event;
return ESP_OK;
err:
if (event) {
temperature_sensor_del_etm_event(event);
}
return ret;
}
esp_err_t temperature_sensor_new_etm_task(temperature_sensor_handle_t tsens, const temperature_sensor_etm_task_config_t *config, esp_etm_task_handle_t *out_task)
{
#if CONFIG_ETM_ENABLE_DEBUG_LOG
esp_log_level_set(TAG, ESP_LOG_DEBUG);
#endif
esp_etm_task_t *task = NULL;
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(tsens && config && out_task, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
ESP_GOTO_ON_FALSE(config->task_type < TEMPERATURE_SENSOR_TASK_MAX, ESP_ERR_INVALID_ARG, err, TAG, "invalid task type");
task = heap_caps_calloc(1, sizeof(esp_etm_task_t), ETM_MEM_ALLOC_CAPS);
ESP_GOTO_ON_FALSE(task, ESP_ERR_NO_MEM, err, TAG, "no memory for ETM task");
uint32_t task_id = TEMPERATURE_SENSOR_LL_ETM_TASK_TABLE(config->task_type);
ESP_GOTO_ON_FALSE(task_id != 0, ESP_ERR_NOT_SUPPORTED, err, TAG, "not supported task type");
// fill the ETM task object
task->task_id = task_id;
task->trig_periph = ETM_TRIG_PERIPH_TSENS;
task->del = temperature_sensor_del_etm_task;
ESP_LOGD(TAG, "new task @%p, task_id=%"PRIu32, task, task_id);
*out_task = task;
return ESP_OK;
err:
if (task) {
temperature_sensor_del_etm_task(task);
}
return ret;
}

Wyświetl plik

@ -26,6 +26,7 @@ typedef enum {
ETM_TRIG_PERIPH_SYSTIMER, /*!< ETM trigger source: Systimer */
ETM_TRIG_PERIPH_MCPWM, /*!< ETM trigger source: MCPWM */
ETM_TRIG_PERIPH_ANA_CMPR, /*!< ETM trigger source: Analog Comparator */
ETM_TRIG_PERIPH_TSENS, /*!< ETM trigger source: Temperature Sensor */
} etm_trigger_peripheral_t;
/**

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -25,6 +25,7 @@
#include "soc/soc_caps.h"
#include "soc/pcr_struct.h"
#include "soc/interrupts.h"
#include "soc/soc_etm_source.h"
#include "hal/temperature_sensor_types.h"
#include "hal/assert.h"
#include "hal/misc.h"
@ -41,6 +42,17 @@ extern "C" {
#define TEMPERATURE_SENSOR_LL_INTR_MASK APB_SARADC_APB_SARADC_TSENS_INT_ST
#define TEMPERATURE_SENSOR_LL_ETM_EVENT_TABLE(event) \
(uint32_t [TEMPERATURE_SENSOR_EVENT_MAX]){ \
[TEMPERATURE_SENSOR_EVENT_OVER_LIMIT] = TMPSNSR_EVT_OVER_LIMIT, \
}[event]
#define TEMPERATURE_SENSOR_LL_ETM_TASK_TABLE(task) \
(uint32_t [TEMPERATURE_SENSOR_TASK_MAX]){ \
[TEMPERATURE_SENSOR_TASK_START] = TMPSNSR_TASK_START_SAMPLE, \
[TEMPERATURE_SENSOR_TASK_STOP] = TMPSNSR_TASK_STOP_SAMPLE, \
}[task]
typedef enum {
TEMPERATURE_SENSOR_LL_WAKE_ABSOLUTE = 0,
TEMPERATURE_SENSOR_LL_WAKE_DELTA = 1,

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -24,6 +24,7 @@
#include "soc/soc_caps.h"
#include "soc/pcr_struct.h"
#include "soc/interrupts.h"
#include "soc/soc_etm_source.h"
#include "hal/temperature_sensor_types.h"
#include "hal/assert.h"
#include "hal/misc.h"
@ -40,6 +41,17 @@ extern "C" {
#define TEMPERATURE_SENSOR_LL_INTR_MASK APB_SARADC_APB_SARADC_TSENS_INT_ST
#define TEMPERATURE_SENSOR_LL_ETM_EVENT_TABLE(event) \
(uint32_t [TEMPERATURE_SENSOR_EVENT_MAX]){ \
[TEMPERATURE_SENSOR_EVENT_OVER_LIMIT] = TMPSNSR_EVT_OVER_LIMIT, \
}[event]
#define TEMPERATURE_SENSOR_LL_ETM_TASK_TABLE(task) \
(uint32_t [TEMPERATURE_SENSOR_TASK_MAX]){ \
[TEMPERATURE_SENSOR_TASK_START] = TMPSNSR_TASK_START_SAMPLE, \
[TEMPERATURE_SENSOR_TASK_STOP] = TMPSNSR_TASK_STOP_SAMPLE, \
}[task]
typedef enum {
TEMPERATURE_SENSOR_LL_WAKE_ABSOLUTE = 0,
TEMPERATURE_SENSOR_LL_WAKE_DELTA = 1,

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -24,6 +24,7 @@
#include "soc/soc.h"
#include "soc/soc_caps.h"
#include "soc/interrupts.h"
#include "soc/soc_etm_source.h"
#include "hal/temperature_sensor_types.h"
#include "hal/assert.h"
#include "hal/misc.h"
@ -40,6 +41,17 @@ extern "C" {
#define TEMPERATURE_SENSOR_LL_INTR_MASK TSENS_COCPU_TSENS_WAKE_INT_ST
#define TEMPERATURE_SENSOR_LL_ETM_EVENT_TABLE(event) \
(uint32_t [TEMPERATURE_SENSOR_EVENT_MAX]){ \
[TEMPERATURE_SENSOR_EVENT_OVER_LIMIT] = TMPSNSR_EVT_OVER_LIMIT, \
}[event]
#define TEMPERATURE_SENSOR_LL_ETM_TASK_TABLE(task) \
(uint32_t [TEMPERATURE_SENSOR_TASK_MAX]){ \
[TEMPERATURE_SENSOR_TASK_START] = TMPSNSR_TASK_START_SAMPLE, \
[TEMPERATURE_SENSOR_TASK_STOP] = TMPSNSR_TASK_STOP_SAMPLE, \
}[task]
typedef enum {
TEMPERATURE_SENSOR_LL_WAKE_ABSOLUTE = 0,
TEMPERATURE_SENSOR_LL_WAKE_DELTA = 1,

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -22,6 +22,23 @@ typedef soc_periph_temperature_sensor_clk_src_t temperature_sensor_clk_src_t;
typedef int temperature_sensor_clk_src_t;
#endif // SOC_TEMP_SENSOR_SUPPORTED
/**
* @brief temperature sensor event types enum
*/
typedef enum {
TEMPERATURE_SENSOR_EVENT_OVER_LIMIT, /*!< Temperature sensor over limit event */
TEMPERATURE_SENSOR_EVENT_MAX, /*!< Maximum number of temperature sensor events */
} temperature_sensor_etm_event_type_t;
/**
* @brief temperature sensor task types enum
*/
typedef enum {
TEMPERATURE_SENSOR_TASK_START, /*!< Temperature sensor start task */
TEMPERATURE_SENSOR_TASK_STOP, /*!< Temperature sensor stop task */
TEMPERATURE_SENSOR_TASK_MAX, /*!< Maximum number of temperature sensor tasks */
} temperature_sensor_etm_task_type_t;
#ifdef __cplusplus
}
#endif

Wyświetl plik

@ -1331,6 +1331,10 @@ config SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
bool
default y
config SOC_TEMPERATURE_SENSOR_SUPPORT_ETM
bool
default y
config SOC_WIFI_HW_TSF
bool
default y

Wyświetl plik

@ -535,6 +535,7 @@
#define SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC (1)
#define SOC_TEMPERATURE_SENSOR_SUPPORT_XTAL (1)
#define SOC_TEMPERATURE_SENSOR_INTR_SUPPORT (1)
#define SOC_TEMPERATURE_SENSOR_SUPPORT_ETM (1)
/*------------------------------------ WI-FI CAPS ------------------------------------*/
#define SOC_WIFI_HW_TSF (1) /*!< Support hardware TSF */

Wyświetl plik

@ -1299,6 +1299,10 @@ config SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
bool
default y
config SOC_TEMPERATURE_SENSOR_SUPPORT_ETM
bool
default y
config SOC_BLE_SUPPORTED
bool
default y

Wyświetl plik

@ -515,6 +515,7 @@
#define SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC (1)
#define SOC_TEMPERATURE_SENSOR_SUPPORT_XTAL (1)
#define SOC_TEMPERATURE_SENSOR_INTR_SUPPORT (1)
#define SOC_TEMPERATURE_SENSOR_SUPPORT_ETM (1)
/*---------------------------------- Bluetooth CAPS ----------------------------------*/
#define SOC_BLE_SUPPORTED (1) /*!< Support Bluetooth Low Energy hardware */

Wyświetl plik

@ -1399,6 +1399,10 @@ config SOC_TSENS_IS_INDEPENDENT_FROM_ADC
bool
default y
config SOC_TEMPERATURE_SENSOR_SUPPORT_ETM
bool
default y
config SOC_MEM_TCM_SUPPORTED
bool
default y

Wyświetl plik

@ -591,6 +591,7 @@
#define SOC_TEMPERATURE_SENSOR_LP_PLL_SUPPORT (1)
#define SOC_TEMPERATURE_SENSOR_INTR_SUPPORT (1)
#define SOC_TSENS_IS_INDEPENDENT_FROM_ADC (1) /*!< Temperature sensor is a separate module, not share regs with ADC */
#define SOC_TEMPERATURE_SENSOR_SUPPORT_ETM (1)
/*-------------------------- Memory CAPS --------------------------*/
#define SOC_MEM_TCM_SUPPORTED (1)