diff --git a/components/esp_driver_ana_cmpr/ana_cmpr.c b/components/esp_driver_ana_cmpr/ana_cmpr.c index e0dea5e7c4..4e1bc429e6 100644 --- a/components/esp_driver_ana_cmpr/ana_cmpr.c +++ b/components/esp_driver_ana_cmpr/ana_cmpr.c @@ -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 */ @@ -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_unit_t unit = config->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, "unit has been allocated already"); 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]->is_enabled = false; s_ana_cmpr[unit]->pm_lock = NULL; + s_ana_cmpr[unit]->unit = unit; #if CONFIG_PM_ENABLE /* 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 /* 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); + // 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); if (config->ref_src == ANA_CMPR_REF_SRC_INTERNAL) { diff --git a/components/esp_driver_ana_cmpr/include/driver/ana_cmpr.h b/components/esp_driver_ana_cmpr/include/driver/ana_cmpr.h index a02a3b2000..013bafb385 100644 --- a/components/esp_driver_ana_cmpr/include/driver/ana_cmpr.h +++ b/components/esp_driver_ana_cmpr/include/driver/ana_cmpr.h @@ -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 */ @@ -15,7 +15,6 @@ extern "C" { /** * @brief Analog comparator unit configuration - * */ typedef struct { 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` */ 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) - * otherwise the larger the higher, 7 is NMI */ + int intr_priority; /*!< The interrupt priority, range 1~3. + If set to 0, the driver will automatically select a relative low priority (1,2,3) */ 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. * Normally used for debug/test scenario */ @@ -38,7 +37,6 @@ typedef struct { /** * @brief Analog comparator internal reference configuration - * */ typedef struct { 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 - * */ typedef struct { - uint32_t wait_us; /*!< The wait time of re-enabling the interrupt after the last triggering, - * it is used to avoid the spurious triggering while the source signal crossing the reference signal. - * The value should regarding how fast the source signal changes, e.g., a rapid signal requires - * a small wait time, otherwise the next crosses may be missed. - * (Unit: micro second) - */ + uint32_t wait_us; /*!< The wait time to prevent frequent interrupts caused by signal noise or bouncing. + During the specified wait_us period, no new interrupts will be triggered. + Set the value according to the signal characteristics. A rapid signal requires a small wait time, + otherwise the next cross event may be missed. */ } ana_cmpr_debounce_config_t; /** diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/CMakeLists.txt b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/CMakeLists.txt index e0307f0483..2f1b359811 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/CMakeLists.txt +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/CMakeLists.txt @@ -13,5 +13,5 @@ endif() idf_component_register(SRCS ${srcs} 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) diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.c b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.c index 21154fed14..ad98904864 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.c +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.c @@ -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 */ #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_config_t config = { @@ -45,10 +45,11 @@ TEST_CASE("ana_cmpr_unit_install_uninstall", "[ana_cmpr]") config.ref_src = ANA_CMPR_REF_SRC_EXTERNAL; 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_ARG, ana_cmpr_del_unit(NULL)); 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); @@ -67,7 +68,7 @@ TEST_CASE("ana_cmpr_internal_reference", "[ana_cmpr]") }; TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_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)); ana_cmpr_event_callbacks_t cbs = { diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.h b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.h index 16ace49358..15f0184945 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.h +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr.h @@ -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 */ @@ -15,14 +15,20 @@ #include "esp_rom_sys.h" #include "soc/soc_caps.h" #include "driver/ana_cmpr.h" +#include "driver/ana_cmpr_etm.h" +#include "driver/gpio.h" #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 #else #define TEST_ANA_CMPR_UNIT_ID 0 #endif +#ifdef __cplusplus +extern "C" { +#endif + /** * @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 */ void test_simulate_src_signal(int src_chan, uint32_t val); +#ifdef __cplusplus +} +#endif diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_etm.c b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_etm.c index 53d1fbde88..ecb473e390 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_etm.c +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_etm.c @@ -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 */ #include #include -#include "unity.h" -#include "unity_test_utils.h" #include "esp_attr.h" #include "esp_etm.h" #include "driver/gpio.h" #include "driver/gptimer.h" -#include "driver/ana_cmpr.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 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_config_t config = { - .unit = TEST_ANA_CMPR_UNIT, + .unit = TEST_ANA_CMPR_UNIT_ID, .clk_src = ANA_CMPR_CLK_SRC_DEFAULT, .ref_src = ANA_CMPR_REF_SRC_INTERNAL, .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) { 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 = { .intr_type = GPIO_INTR_DISABLE, .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_CASE("analog_comparator_etm_event", "[etm]") +TEST_CASE("ana_cmpr etm event", "[ana_cmpr][etm]") { gptimer_handle_t gptimer = test_ana_cmpr_gptimer_init(); ana_cmpr_handle_t cmpr = test_ana_cmpr_init(); diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_iram.c b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_iram.c index 957bd39980..41132d19dc 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_iram.c +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_ana_cmpr_iram.c @@ -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 */ #include "test_ana_cmpr.h" -#include "unity_test_utils.h" #include "unity_test_utils_cache.h" 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); } -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 = { .handle = NULL, diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_app_main.c b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_app_main.c index 9b6762fcc8..4b5deb3110 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_app_main.c +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/main/test_app_main.c @@ -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 */ @@ -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) { 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"); } diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/sdkconfig.ci.release b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/sdkconfig.ci.release index 91d93f163e..199b0cf97c 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/sdkconfig.ci.release +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/sdkconfig.ci.release @@ -1,5 +1,6 @@ CONFIG_PM_ENABLE=y CONFIG_FREERTOS_USE_TICKLESS_IDLE=y +CONFIG_PM_DFS_INIT_AUTO=y CONFIG_COMPILER_OPTIMIZATION_SIZE=y CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y diff --git a/components/hal/esp32h2/include/hal/ana_cmpr_ll.h b/components/hal/esp32h2/include/hal/ana_cmpr_ll.h index c75fc5ddc5..627590171c 100644 --- a/components/hal/esp32h2/include/hal/ana_cmpr_ll.h +++ b/components/hal/esp32h2/include/hal/ana_cmpr_ll.h @@ -14,6 +14,8 @@ #define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit]) #define ANALOG_CMPR_LL_EVENT_CROSS (1 << 0) +#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) (ANALOG_CMPR_LL_EVENT_CROSS) + #ifdef __cplusplus extern "C" { #endif diff --git a/components/hal/esp32p4/include/hal/ana_cmpr_ll.h b/components/hal/esp32p4/include/hal/ana_cmpr_ll.h index d1075ed64a..beec91ddda 100644 --- a/components/hal/esp32p4/include/hal/ana_cmpr_ll.h +++ b/components/hal/esp32p4/include/hal/ana_cmpr_ll.h @@ -18,11 +18,12 @@ extern "C" { #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_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_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))