From 79d6e573e6b54804f587df7e80a6e000aeff1ba2 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Wed, 12 Nov 2025 16:57:43 +0800 Subject: [PATCH] feat(hal): graudate the ana_cmpr hal driver into a new component --- components/esp_driver_ana_cmpr/CMakeLists.txt | 1 + .../esp_driver_ana_cmpr/ana_cmpr_private.h | 2 +- .../analog_comparator/CMakeLists.txt | 10 ++- components/esp_hal_ana_cmpr/CMakeLists.txt | 21 ++++++ components/esp_hal_ana_cmpr/README.md | 72 +++++++++++++++++++ .../esp32c5/ana_cmpr_periph.c | 4 +- .../esp32c5/include/hal/ana_cmpr_ll.h | 1 - .../esp32c61/ana_cmpr_periph.c | 4 +- .../esp32c61/include/hal/ana_cmpr_ll.h | 1 - .../esp32h2/ana_cmpr_periph.c | 4 +- .../esp32h2/include/hal/ana_cmpr_ll.h | 0 .../esp32h21/include/hal/ana_cmpr_ll.h | 0 .../esp32p4/ana_cmpr_periph.c | 4 +- .../esp32p4/include/hal/ana_cmpr_ll.h | 1 - .../include/hal}/ana_cmpr_periph.h | 2 +- .../include/hal/ana_cmpr_types.h | 0 components/soc/CMakeLists.txt | 4 -- .../system/g1_components/CMakeLists.txt | 3 + 18 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 components/esp_hal_ana_cmpr/CMakeLists.txt create mode 100644 components/esp_hal_ana_cmpr/README.md rename components/{soc => esp_hal_ana_cmpr}/esp32c5/ana_cmpr_periph.c (89%) rename components/{hal => esp_hal_ana_cmpr}/esp32c5/include/hal/ana_cmpr_ll.h (99%) rename components/{soc => esp_hal_ana_cmpr}/esp32c61/ana_cmpr_periph.c (89%) rename components/{hal => esp_hal_ana_cmpr}/esp32c61/include/hal/ana_cmpr_ll.h (99%) rename components/{soc => esp_hal_ana_cmpr}/esp32h2/ana_cmpr_periph.c (89%) rename components/{hal => esp_hal_ana_cmpr}/esp32h2/include/hal/ana_cmpr_ll.h (100%) rename components/{hal => esp_hal_ana_cmpr}/esp32h21/include/hal/ana_cmpr_ll.h (100%) rename components/{soc => esp_hal_ana_cmpr}/esp32p4/ana_cmpr_periph.c (93%) rename components/{hal => esp_hal_ana_cmpr}/esp32p4/include/hal/ana_cmpr_ll.h (99%) rename components/{soc/include/soc => esp_hal_ana_cmpr/include/hal}/ana_cmpr_periph.h (92%) rename components/{hal => esp_hal_ana_cmpr}/include/hal/ana_cmpr_types.h (100%) diff --git a/components/esp_driver_ana_cmpr/CMakeLists.txt b/components/esp_driver_ana_cmpr/CMakeLists.txt index c73f5f7a4c..acc80a1f1c 100644 --- a/components/esp_driver_ana_cmpr/CMakeLists.txt +++ b/components/esp_driver_ana_cmpr/CMakeLists.txt @@ -19,5 +19,6 @@ endif() idf_component_register(SRCS ${srcs} INCLUDE_DIRS "include" PRIV_REQUIRES "${priv_requires}" + REQUIRES esp_hal_ana_cmpr LDFRAGMENTS "linker.lf" ) diff --git a/components/esp_driver_ana_cmpr/ana_cmpr_private.h b/components/esp_driver_ana_cmpr/ana_cmpr_private.h index 73fb051653..b4d003421f 100644 --- a/components/esp_driver_ana_cmpr/ana_cmpr_private.h +++ b/components/esp_driver_ana_cmpr/ana_cmpr_private.h @@ -24,7 +24,7 @@ #include "driver/ana_cmpr_types.h" #include "soc/soc_caps.h" #include "hal/ana_cmpr_ll.h" -#include "soc/ana_cmpr_periph.h" +#include "hal/ana_cmpr_periph.h" #define TAG "ana_cmpr" diff --git a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/CMakeLists.txt b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/CMakeLists.txt index eb163cfa99..3585f5482f 100644 --- a/components/esp_driver_ana_cmpr/test_apps/analog_comparator/CMakeLists.txt +++ b/components/esp_driver_ana_cmpr/test_apps/analog_comparator/CMakeLists.txt @@ -9,9 +9,17 @@ project(test_ana_cmpr) idf_build_get_property(elf EXECUTABLE) if(CONFIG_COMPILER_DUMP_RTL_FILES) + # Collect RTL directories in a variable for readability. Join them + # with commas so they are passed as a single --rtl-dirs argument to the script. + set(ANA_CMPR_RTL_DIRS + ${CMAKE_BINARY_DIR}/esp-idf/esp_driver_ana_cmpr + ${CMAKE_BINARY_DIR}/esp-idf/hal + ${CMAKE_BINARY_DIR}/esp-idf/esp_hal_ana_cmpr + ) + string(JOIN "," ANA_CMPR_RTL_DIRS_JOINED ${ANA_CMPR_RTL_DIRS}) add_custom_target(check_test_app_sections ALL COMMAND ${PYTHON} $ENV{IDF_PATH}/tools/ci/check_callgraph.py - --rtl-dirs ${CMAKE_BINARY_DIR}/esp-idf/esp_driver_ana_cmpr/,${CMAKE_BINARY_DIR}/esp-idf/hal/ + --rtl-dirs ${ANA_CMPR_RTL_DIRS_JOINED} --elf-file ${CMAKE_BINARY_DIR}/test_ana_cmpr.elf find-refs --from-sections=.iram0.text diff --git a/components/esp_hal_ana_cmpr/CMakeLists.txt b/components/esp_hal_ana_cmpr/CMakeLists.txt new file mode 100644 index 0000000000..637c237c33 --- /dev/null +++ b/components/esp_hal_ana_cmpr/CMakeLists.txt @@ -0,0 +1,21 @@ +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + return() # This component is not supported by the POSIX/Linux simulator +endif() + +set(srcs) +set(includes "include") +if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${target}/include") + list(APPEND includes "${target}/include") +endif() + +# Analog comparator related source files +if(CONFIG_SOC_ANA_CMPR_SUPPORTED) + list(APPEND srcs "${target}/ana_cmpr_periph.c") +endif() + + +idf_component_register(SRCS ${srcs} + INCLUDE_DIRS ${includes} + REQUIRES soc hal) diff --git a/components/esp_hal_ana_cmpr/README.md b/components/esp_hal_ana_cmpr/README.md new file mode 100644 index 0000000000..b6fd675a61 --- /dev/null +++ b/components/esp_hal_ana_cmpr/README.md @@ -0,0 +1,72 @@ +# ESP Hardware Abstraction Layer for Analog Comparator Peripheral + +> [!NOTE] +> This component is currently in beta. Its API, behavior, and compatibility may change at any time and without notice; backward compatibility is not guaranteed. Use caution when integrating into production systems. + +## Overview + +The `esp_hal_ana_cmpr` component provides a **Hardware Abstraction Layer** for Analog Comparator peripherals across supported ESP-IDF targets. Analog comparators compare two analog voltage signals and generate digital outputs based on the comparison result, enabling threshold detection, zero-crossing detection, and analog signal monitoring applications. + +## Architecture + +The Analog Comparator HAL is structured in two main sub-layers: + +1. **HAL Layer (Upper)**: Defines the operational steps and data structures required to control analog comparator peripherals (e.g., initialization, reference voltage configuration, cross detection setup, interrupt handling). + +2. **Low-Level Layer (Bottom)**: Serves as a translation layer between the HAL and the register files defined in the `soc` component, handling target-specific register configurations. + +## Supported Controllers + +This HAL supports analog comparator controllers on the following ESP chips: + +- **ESP32-P4**: Dual analog comparator units (ANA_CMPR_U0, ANA_CMPR_U1) +- **Others**: Single analog comparator unit + +## Features + +### Reference Voltage Configuration + +- **Internal Reference Voltage**: Configurable internal reference voltage from 0% to 70% VDD in 10% steps (0.0V, 0.1VDD, 0.2VDD, ..., 0.7VDD) +- **External Reference Voltage**: Support for external reference voltage via dedicated GPIO pad (typically GPIO10) +- **Reference Source Selection**: Switch between internal and external reference sources + +### Cross Detection + +- **Positive Cross Detection**: Detects when the input analog signal crosses the reference voltage from low to high +- **Negative Cross Detection**: Detects when the input analog signal crosses the reference voltage from high to low +- **Any Cross Detection**: Detects both positive and negative crossings +- **Debounce Filtering**: Configurable debounce cycle to ensure stable cross detection and filter out noise + +### Interrupt Handling + +- **Multiple Interrupt Types**: Support for positive cross, negative cross, and any cross interrupts +- **Interrupt Masking**: Flexible interrupt enable/disable control per interrupt type +- **Interrupt Status**: Read interrupt status and clear interrupt flags +- **Interrupt Register Access**: Direct access to interrupt status register for advanced use cases + +### Signal Processing + +- **Debounce Cycle**: Configurable debounce cycle count to filter out glitches and ensure stable detection +- **Hardware Filtering**: Built-in hardware filtering for noise reduction + +### Event Task Matrix (ETM) Support + +- **ETM Event Source**: Analog comparator cross events can be used as ETM event sources for precise timing and synchronization +- **Multiple Event Types**: Support for positive and negative cross events as separate ETM sources + +### Peripheral Configuration + +- **GPIO Mapping**: Each comparator unit has dedicated source GPIO and external reference GPIO pins +- **Module Identification**: Each comparator unit has a unique module name for identification +- **Interrupt Source**: Each comparator unit has a dedicated interrupt source + +## Usage + +The HAL functions primarily serve ESP-IDF peripheral drivers such as `esp_driver_ana_cmpr`. + +Advanced developers can use these interfaces directly when implementing custom drivers, with the understanding that API stability is not guaranteed. + +## Dependencies + +- `soc`: Provides chip-specific register definitions +- `hal`: Core hardware abstraction utilities and macros diff --git a/components/soc/esp32c5/ana_cmpr_periph.c b/components/esp_hal_ana_cmpr/esp32c5/ana_cmpr_periph.c similarity index 89% rename from components/soc/esp32c5/ana_cmpr_periph.c rename to components/esp_hal_ana_cmpr/esp32c5/ana_cmpr_periph.c index fe5f78628e..74aaf40158 100644 --- a/components/soc/esp32c5/ana_cmpr_periph.c +++ b/components/esp_hal_ana_cmpr/esp32c5/ana_cmpr_periph.c @@ -1,10 +1,10 @@ /* * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Apache-2.0 OR MIT + * SPDX-License-Identifier: Apache-2.0 */ -#include "soc/ana_cmpr_periph.h" +#include "hal/ana_cmpr_periph.h" #include "soc/ana_cmpr_struct.h" const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { diff --git a/components/hal/esp32c5/include/hal/ana_cmpr_ll.h b/components/esp_hal_ana_cmpr/esp32c5/include/hal/ana_cmpr_ll.h similarity index 99% rename from components/hal/esp32c5/include/hal/ana_cmpr_ll.h rename to components/esp_hal_ana_cmpr/esp32c5/include/hal/ana_cmpr_ll.h index d114e393ce..946ad41041 100644 --- a/components/hal/esp32c5/include/hal/ana_cmpr_ll.h +++ b/components/esp_hal_ana_cmpr/esp32c5/include/hal/ana_cmpr_ll.h @@ -28,7 +28,6 @@ extern "C" { #define ANALOG_CMPR_LL_ETM_SOURCE(unit, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type)) - /** * @brief Enable analog comparator * diff --git a/components/soc/esp32c61/ana_cmpr_periph.c b/components/esp_hal_ana_cmpr/esp32c61/ana_cmpr_periph.c similarity index 89% rename from components/soc/esp32c61/ana_cmpr_periph.c rename to components/esp_hal_ana_cmpr/esp32c61/ana_cmpr_periph.c index ea10304429..f71d8ae4e5 100644 --- a/components/soc/esp32c61/ana_cmpr_periph.c +++ b/components/esp_hal_ana_cmpr/esp32c61/ana_cmpr_periph.c @@ -1,10 +1,10 @@ /* * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Apache-2.0 OR MIT + * SPDX-License-Identifier: Apache-2.0 */ -#include "soc/ana_cmpr_periph.h" +#include "hal/ana_cmpr_periph.h" #include "soc/ana_cmpr_struct.h" const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { diff --git a/components/hal/esp32c61/include/hal/ana_cmpr_ll.h b/components/esp_hal_ana_cmpr/esp32c61/include/hal/ana_cmpr_ll.h similarity index 99% rename from components/hal/esp32c61/include/hal/ana_cmpr_ll.h rename to components/esp_hal_ana_cmpr/esp32c61/include/hal/ana_cmpr_ll.h index d114e393ce..946ad41041 100644 --- a/components/hal/esp32c61/include/hal/ana_cmpr_ll.h +++ b/components/esp_hal_ana_cmpr/esp32c61/include/hal/ana_cmpr_ll.h @@ -28,7 +28,6 @@ extern "C" { #define ANALOG_CMPR_LL_ETM_SOURCE(unit, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type)) - /** * @brief Enable analog comparator * diff --git a/components/soc/esp32h2/ana_cmpr_periph.c b/components/esp_hal_ana_cmpr/esp32h2/ana_cmpr_periph.c similarity index 89% rename from components/soc/esp32h2/ana_cmpr_periph.c rename to components/esp_hal_ana_cmpr/esp32h2/ana_cmpr_periph.c index 6f7d1a1ee1..5d568ee3f0 100644 --- a/components/soc/esp32h2/ana_cmpr_periph.c +++ b/components/esp_hal_ana_cmpr/esp32h2/ana_cmpr_periph.c @@ -1,10 +1,10 @@ /* * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Apache-2.0 OR MIT + * SPDX-License-Identifier: Apache-2.0 */ -#include "soc/ana_cmpr_periph.h" +#include "hal/ana_cmpr_periph.h" #include "soc/ana_cmpr_struct.h" const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { diff --git a/components/hal/esp32h2/include/hal/ana_cmpr_ll.h b/components/esp_hal_ana_cmpr/esp32h2/include/hal/ana_cmpr_ll.h similarity index 100% rename from components/hal/esp32h2/include/hal/ana_cmpr_ll.h rename to components/esp_hal_ana_cmpr/esp32h2/include/hal/ana_cmpr_ll.h diff --git a/components/hal/esp32h21/include/hal/ana_cmpr_ll.h b/components/esp_hal_ana_cmpr/esp32h21/include/hal/ana_cmpr_ll.h similarity index 100% rename from components/hal/esp32h21/include/hal/ana_cmpr_ll.h rename to components/esp_hal_ana_cmpr/esp32h21/include/hal/ana_cmpr_ll.h diff --git a/components/soc/esp32p4/ana_cmpr_periph.c b/components/esp_hal_ana_cmpr/esp32p4/ana_cmpr_periph.c similarity index 93% rename from components/soc/esp32p4/ana_cmpr_periph.c rename to components/esp_hal_ana_cmpr/esp32p4/ana_cmpr_periph.c index 8f9614260f..976bca4933 100644 --- a/components/soc/esp32p4/ana_cmpr_periph.c +++ b/components/esp_hal_ana_cmpr/esp32p4/ana_cmpr_periph.c @@ -1,10 +1,10 @@ /* * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Apache-2.0 OR MIT + * SPDX-License-Identifier: Apache-2.0 */ -#include "soc/ana_cmpr_periph.h" +#include "hal/ana_cmpr_periph.h" #include "soc/ana_cmpr_struct.h" const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { diff --git a/components/hal/esp32p4/include/hal/ana_cmpr_ll.h b/components/esp_hal_ana_cmpr/esp32p4/include/hal/ana_cmpr_ll.h similarity index 99% rename from components/hal/esp32p4/include/hal/ana_cmpr_ll.h rename to components/esp_hal_ana_cmpr/esp32p4/include/hal/ana_cmpr_ll.h index 0a0962d2cd..922a802f50 100644 --- a/components/hal/esp32p4/include/hal/ana_cmpr_ll.h +++ b/components/esp_hal_ana_cmpr/esp32p4/include/hal/ana_cmpr_ll.h @@ -28,7 +28,6 @@ extern "C" { #define ANALOG_CMPR_LL_ETM_SOURCE(unit, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type)) - /** * @brief Enable analog comparator * diff --git a/components/soc/include/soc/ana_cmpr_periph.h b/components/esp_hal_ana_cmpr/include/hal/ana_cmpr_periph.h similarity index 92% rename from components/soc/include/soc/ana_cmpr_periph.h rename to components/esp_hal_ana_cmpr/include/hal/ana_cmpr_periph.h index bcaba1912a..3be9e7e444 100644 --- a/components/soc/include/soc/ana_cmpr_periph.h +++ b/components/esp_hal_ana_cmpr/include/hal/ana_cmpr_periph.h @@ -1,7 +1,7 @@ /* * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Apache-2.0 OR MIT + * SPDX-License-Identifier: Apache-2.0 */ #pragma once diff --git a/components/hal/include/hal/ana_cmpr_types.h b/components/esp_hal_ana_cmpr/include/hal/ana_cmpr_types.h similarity index 100% rename from components/hal/include/hal/ana_cmpr_types.h rename to components/esp_hal_ana_cmpr/include/hal/ana_cmpr_types.h diff --git a/components/soc/CMakeLists.txt b/components/soc/CMakeLists.txt index de4d8f97a1..2d8e3b53f1 100644 --- a/components/soc/CMakeLists.txt +++ b/components/soc/CMakeLists.txt @@ -68,10 +68,6 @@ if(CONFIG_SOC_ADC_SUPPORTED) list(APPEND srcs "${target_folder}/adc_periph.c") endif() -if(CONFIG_SOC_ANA_CMPR_SUPPORTED) - list(APPEND srcs "${target_folder}/ana_cmpr_periph.c") -endif() - if(CONFIG_SOC_DEBUG_PROBE_SUPPORTED) list(APPEND srcs "${target_folder}/debug_probe_periph.c") endif() diff --git a/tools/test_apps/system/g1_components/CMakeLists.txt b/tools/test_apps/system/g1_components/CMakeLists.txt index 0332fdf590..60d07f9d0e 100644 --- a/tools/test_apps/system/g1_components/CMakeLists.txt +++ b/tools/test_apps/system/g1_components/CMakeLists.txt @@ -29,6 +29,9 @@ foreach(hal_dir ${esp_hal_component_dirs}) endif() endforeach() +# Remove the esp_hal_xxx that not compiled in G1 +list(REMOVE_ITEM esp_hal_components "esp_hal_ana_cmpr") + # These components are currently included into "G1" build, but shouldn't. # After removing the extra dependencies, remove the components from this list as well. set(extra_components_which_shouldnt_be_included