fix(ana_cmpr): ETM event not work for Unit 1

This commit is contained in:
morris
2025-04-10 16:07:53 +08:00
parent 8e473c6bed
commit d20d3c7a60
11 changed files with 46 additions and 38 deletions

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -119,7 +119,7 @@ esp_err_t ana_cmpr_new_unit(const ana_cmpr_config_t *config, ana_cmpr_handle_t *
ANA_CMPR_NULL_POINTER_CHECK(ret_cmpr); ANA_CMPR_NULL_POINTER_CHECK(ret_cmpr);
ana_cmpr_unit_t unit = config->unit; ana_cmpr_unit_t unit = config->unit;
ANA_CMPR_UNIT_CHECK(unit); ANA_CMPR_UNIT_CHECK(unit);
ESP_RETURN_ON_FALSE(config->intr_priority >= 0 && config->intr_priority <= 7, ESP_ERR_INVALID_ARG, TAG, "interrupt priority should be within 0~7"); ESP_RETURN_ON_FALSE(config->intr_priority >= 0 && config->intr_priority <= 3, ESP_ERR_INVALID_ARG, TAG, "interrupt priority should be within 0~3");
ESP_RETURN_ON_FALSE(!s_ana_cmpr[unit], ESP_ERR_INVALID_STATE, TAG, ESP_RETURN_ON_FALSE(!s_ana_cmpr[unit], ESP_ERR_INVALID_STATE, TAG,
"unit has been allocated already"); "unit has been allocated already");
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
@ -134,6 +134,7 @@ esp_err_t ana_cmpr_new_unit(const ana_cmpr_config_t *config, ana_cmpr_handle_t *
s_ana_cmpr[unit]->intr_priority = config->intr_priority; s_ana_cmpr[unit]->intr_priority = config->intr_priority;
s_ana_cmpr[unit]->is_enabled = false; s_ana_cmpr[unit]->is_enabled = false;
s_ana_cmpr[unit]->pm_lock = NULL; s_ana_cmpr[unit]->pm_lock = NULL;
s_ana_cmpr[unit]->unit = unit;
#if CONFIG_PM_ENABLE #if CONFIG_PM_ENABLE
/* Create PM lock */ /* Create PM lock */
@ -163,6 +164,9 @@ esp_err_t ana_cmpr_new_unit(const ana_cmpr_config_t *config, ana_cmpr_handle_t *
#endif // SOC_ANA_CMPR_CAN_DISTINGUISH_EDGE #endif // SOC_ANA_CMPR_CAN_DISTINGUISH_EDGE
/* Record the interrupt mask, the interrupt will be lazy installed when register the callbacks */ /* Record the interrupt mask, the interrupt will be lazy installed when register the callbacks */
s_ana_cmpr[unit]->intr_mask = analog_cmpr_ll_get_intr_mask_by_type(s_ana_cmpr[unit]->dev, config->cross_type); s_ana_cmpr[unit]->intr_mask = analog_cmpr_ll_get_intr_mask_by_type(s_ana_cmpr[unit]->dev, config->cross_type);
// disable the interrupt by default, and clear pending status
analog_cmpr_ll_enable_intr(s_ana_cmpr[unit]->dev, ANALOG_CMPR_LL_ALL_INTR_MASK(unit), false);
analog_cmpr_ll_clear_intr(s_ana_cmpr[unit]->dev, ANALOG_CMPR_LL_ALL_INTR_MASK(unit));
portEXIT_CRITICAL(&s_spinlock); portEXIT_CRITICAL(&s_spinlock);
if (config->ref_src == ANA_CMPR_REF_SRC_INTERNAL) { if (config->ref_src == ANA_CMPR_REF_SRC_INTERNAL) {

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -15,7 +15,6 @@ extern "C" {
/** /**
* @brief Analog comparator unit configuration * @brief Analog comparator unit configuration
*
*/ */
typedef struct { typedef struct {
ana_cmpr_unit_t unit; /*!< Analog comparator unit */ ana_cmpr_unit_t unit; /*!< Analog comparator unit */
@ -28,8 +27,8 @@ typedef struct {
* for external reference, the reference signal should be connect to `ANA_CMPRx_EXT_REF_GPIO` * for external reference, the reference signal should be connect to `ANA_CMPRx_EXT_REF_GPIO`
*/ */
ana_cmpr_cross_type_t cross_type; /*!< The crossing types that can trigger interrupt */ ana_cmpr_cross_type_t cross_type; /*!< The crossing types that can trigger interrupt */
int intr_priority; /*!< The interrupt priority, range 0~7, if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) int intr_priority; /*!< The interrupt priority, range 1~3.
* otherwise the larger the higher, 7 is NMI */ If set to 0, the driver will automatically select a relative low priority (1,2,3) */
struct { struct {
uint32_t io_loop_back: 1; /*!< Enable this field when the other signals that output on the comparison pins are supposed to be fed back. uint32_t io_loop_back: 1; /*!< Enable this field when the other signals that output on the comparison pins are supposed to be fed back.
* Normally used for debug/test scenario */ * Normally used for debug/test scenario */
@ -38,7 +37,6 @@ typedef struct {
/** /**
* @brief Analog comparator internal reference configuration * @brief Analog comparator internal reference configuration
*
*/ */
typedef struct { typedef struct {
ana_cmpr_ref_voltage_t ref_volt; /*!< The internal reference voltage. It can be specified to a certain fixed percentage of ana_cmpr_ref_voltage_t ref_volt; /*!< The internal reference voltage. It can be specified to a certain fixed percentage of
@ -48,15 +46,12 @@ typedef struct {
/** /**
* @brief Analog comparator debounce filter configuration * @brief Analog comparator debounce filter configuration
*
*/ */
typedef struct { typedef struct {
uint32_t wait_us; /*!< The wait time of re-enabling the interrupt after the last triggering, uint32_t wait_us; /*!< The wait time to prevent frequent interrupts caused by signal noise or bouncing.
* it is used to avoid the spurious triggering while the source signal crossing the reference signal. During the specified wait_us period, no new interrupts will be triggered.
* The value should regarding how fast the source signal changes, e.g., a rapid signal requires Set the value according to the signal characteristics. A rapid signal requires a small wait time,
* a small wait time, otherwise the next crosses may be missed. otherwise the next cross event may be missed. */
* (Unit: micro second)
*/
} ana_cmpr_debounce_config_t; } ana_cmpr_debounce_config_t;
/** /**

View File

@ -13,5 +13,5 @@ endif()
idf_component_register(SRCS ${srcs} idf_component_register(SRCS ${srcs}
INCLUDE_DIRS "." INCLUDE_DIRS "."
PRIV_REQUIRES unity esp_driver_gpio esp_driver_ana_cmpr esp_driver_gptimer PRIV_REQUIRES unity esp_driver_gpio esp_driver_ana_cmpr esp_driver_gptimer esp_pm
WHOLE_ARCHIVE) WHOLE_ARCHIVE)

View File

@ -1,12 +1,12 @@
/* /*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include "test_ana_cmpr.h" #include "test_ana_cmpr.h"
TEST_CASE("ana_cmpr_unit_install_uninstall", "[ana_cmpr]") TEST_CASE("ana_cmpr unit install/uninstall", "[ana_cmpr]")
{ {
ana_cmpr_handle_t cmpr = NULL; ana_cmpr_handle_t cmpr = NULL;
ana_cmpr_config_t config = { ana_cmpr_config_t config = {
@ -45,10 +45,11 @@ TEST_CASE("ana_cmpr_unit_install_uninstall", "[ana_cmpr]")
config.ref_src = ANA_CMPR_REF_SRC_EXTERNAL; config.ref_src = ANA_CMPR_REF_SRC_EXTERNAL;
TEST_ESP_OK(ana_cmpr_new_unit(&config, &cmpr)); TEST_ESP_OK(ana_cmpr_new_unit(&config, &cmpr));
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, ana_cmpr_set_internal_reference(cmpr, &ref_cfg)); TEST_ESP_ERR(ESP_ERR_INVALID_STATE, ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, ana_cmpr_del_unit(NULL));
TEST_ESP_OK(ana_cmpr_del_unit(cmpr)); TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
} }
TEST_CASE("ana_cmpr_internal_reference", "[ana_cmpr]") TEST_CASE("ana_cmpr internal reference", "[ana_cmpr]")
{ {
int src_chan = test_init_src_chan_gpio(TEST_ANA_CMPR_UNIT_ID); int src_chan = test_init_src_chan_gpio(TEST_ANA_CMPR_UNIT_ID);
@ -67,7 +68,7 @@ TEST_CASE("ana_cmpr_internal_reference", "[ana_cmpr]")
}; };
TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_cfg)); TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
ana_cmpr_debounce_config_t dbc_cfg = { ana_cmpr_debounce_config_t dbc_cfg = {
.wait_us = 10.0, .wait_us = 10,
}; };
TEST_ESP_OK(ana_cmpr_set_debounce(cmpr, &dbc_cfg)); TEST_ESP_OK(ana_cmpr_set_debounce(cmpr, &dbc_cfg));
ana_cmpr_event_callbacks_t cbs = { ana_cmpr_event_callbacks_t cbs = {

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -15,14 +15,20 @@
#include "esp_rom_sys.h" #include "esp_rom_sys.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "driver/ana_cmpr.h" #include "driver/ana_cmpr.h"
#include "driver/ana_cmpr_etm.h"
#include "driver/gpio.h"
#if CONFIG_IDF_TARGET_ESP32P4 #if CONFIG_IDF_TARGET_ESP32P4
// The pin of unit 0 is used for other purpose on P4 runner, use unit 1 instead // The pin of unit 0 is not exposed on some ESP32-P4 runner, so test unit 1 by default
#define TEST_ANA_CMPR_UNIT_ID 1 #define TEST_ANA_CMPR_UNIT_ID 1
#else #else
#define TEST_ANA_CMPR_UNIT_ID 0 #define TEST_ANA_CMPR_UNIT_ID 0
#endif #endif
#ifdef __cplusplus
extern "C" {
#endif
/** /**
* @brief Test default on cross callback * @brief Test default on cross callback
* *
@ -50,3 +56,6 @@ int test_init_src_chan_gpio(int unit_id);
* @param val 0 to set low, others to set high * @param val 0 to set low, others to set high
*/ */
void test_simulate_src_signal(int src_chan, uint32_t val); void test_simulate_src_signal(int src_chan, uint32_t val);
#ifdef __cplusplus
}
#endif

View File

@ -1,22 +1,18 @@
/* /*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include "unity.h"
#include "unity_test_utils.h"
#include "esp_attr.h" #include "esp_attr.h"
#include "esp_etm.h" #include "esp_etm.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/gptimer.h" #include "driver/gptimer.h"
#include "driver/ana_cmpr.h"
#include "driver/gptimer_etm.h" #include "driver/gptimer_etm.h"
#include "driver/ana_cmpr_etm.h" #include "test_ana_cmpr.h"
#define TEST_ANA_CMPR_UNIT 0
#define TEST_TIME_US 5000 #define TEST_TIME_US 5000
static gptimer_handle_t test_ana_cmpr_gptimer_init(void) static gptimer_handle_t test_ana_cmpr_gptimer_init(void)
@ -44,7 +40,7 @@ static ana_cmpr_handle_t test_ana_cmpr_init(void)
ana_cmpr_handle_t cmpr = NULL; ana_cmpr_handle_t cmpr = NULL;
ana_cmpr_config_t config = { ana_cmpr_config_t config = {
.unit = TEST_ANA_CMPR_UNIT, .unit = TEST_ANA_CMPR_UNIT_ID,
.clk_src = ANA_CMPR_CLK_SRC_DEFAULT, .clk_src = ANA_CMPR_CLK_SRC_DEFAULT,
.ref_src = ANA_CMPR_REF_SRC_INTERNAL, .ref_src = ANA_CMPR_REF_SRC_INTERNAL,
.cross_type = ANA_CMPR_CROSS_ANY, .cross_type = ANA_CMPR_CROSS_ANY,
@ -74,7 +70,7 @@ static void test_ana_cmpr_deinit(ana_cmpr_handle_t cmpr)
static int test_ana_cmpr_src_gpio_init(void) static int test_ana_cmpr_src_gpio_init(void)
{ {
int gpio_num = -1; int gpio_num = -1;
TEST_ESP_OK(ana_cmpr_get_gpio(TEST_ANA_CMPR_UNIT, ANA_CMPR_SOURCE_CHAN, &gpio_num)); TEST_ESP_OK(ana_cmpr_get_gpio(TEST_ANA_CMPR_UNIT_ID, ANA_CMPR_SOURCE_CHAN, &gpio_num));
gpio_config_t io_conf = { gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE, .intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT, .mode = GPIO_MODE_OUTPUT,
@ -145,7 +141,7 @@ static void test_ana_cmpr_deinit_etm(test_ana_cmpr_etm_handles_t handles)
TEST_ESP_OK(esp_etm_del_channel(handles.etm_neg_handle)); TEST_ESP_OK(esp_etm_del_channel(handles.etm_neg_handle));
} }
TEST_CASE("analog_comparator_etm_event", "[etm]") TEST_CASE("ana_cmpr etm event", "[ana_cmpr][etm]")
{ {
gptimer_handle_t gptimer = test_ana_cmpr_gptimer_init(); gptimer_handle_t gptimer = test_ana_cmpr_gptimer_init();
ana_cmpr_handle_t cmpr = test_ana_cmpr_init(); ana_cmpr_handle_t cmpr = test_ana_cmpr_init();

View File

@ -1,11 +1,10 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include "test_ana_cmpr.h" #include "test_ana_cmpr.h"
#include "unity_test_utils.h"
#include "unity_test_utils_cache.h" #include "unity_test_utils_cache.h"
typedef struct { typedef struct {
@ -33,7 +32,7 @@ static void IRAM_ATTR test_ana_cmpr_iram_safety(void *args)
ana_cmpr_set_cross_type(data->handle, ANA_CMPR_CROSS_POS); ana_cmpr_set_cross_type(data->handle, ANA_CMPR_CROSS_POS);
} }
TEST_CASE("ana_cmpr_internal_reference_iram_safe", "[ana_cmpr]") TEST_CASE("ana_cmpr works with cache disabled", "[ana_cmpr]")
{ {
test_ana_cmpr_data_t test_data = { test_ana_cmpr_data_t test_data = {
.handle = NULL, .handle = NULL,

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -17,7 +17,7 @@ static size_t before_free_32bit;
static void check_leak(size_t before_free, size_t after_free, const char *type) static void check_leak(size_t before_free, size_t after_free, const char *type)
{ {
ssize_t delta = after_free - before_free; ssize_t delta = after_free - before_free;
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\\n", type, before_free, after_free, delta); printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
} }

View File

@ -1,5 +1,6 @@
CONFIG_PM_ENABLE=y CONFIG_PM_ENABLE=y
CONFIG_FREERTOS_USE_TICKLESS_IDLE=y CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
CONFIG_PM_DFS_INIT_AUTO=y
CONFIG_COMPILER_OPTIMIZATION_SIZE=y CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@ -14,6 +14,8 @@
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit]) #define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
#define ANALOG_CMPR_LL_EVENT_CROSS (1 << 0) #define ANALOG_CMPR_LL_EVENT_CROSS (1 << 0)
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) (ANALOG_CMPR_LL_EVENT_CROSS)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -18,11 +18,12 @@ extern "C" {
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit]) #define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
#define ANALOG_CMPR_LL_GET_UNIT(hw) ((hw) == (&ANALOG_CMPR[0]) ? 0 : 1) #define ANALOG_CMPR_LL_GET_UNIT(hw) ((hw) == (&ANALOG_CMPR[0]) ? 0 : 1)
#define ANALOG_CMPR_LL_EVENT_CROSS (1 << 0)
#define ANALOG_CMPR_LL_ETM_EVENTS_PER_UNIT (2)
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit) (1UL << ((int)unit * 3)) #define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit) (1UL << ((int)unit * 3 + 0))
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit) (1UL << ((int)unit * 3 + 1)) #define ANALOG_CMPR_LL_POS_CROSS_MASK(unit) (1UL << ((int)unit * 3 + 1))
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit) (1UL << ((int)unit * 3 + 2))
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit))
#define ANALOG_CMPR_LL_ETM_SOURCE(unit, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type)) #define ANALOG_CMPR_LL_ETM_SOURCE(unit, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type))