feat(hal): graudate the ana_cmpr hal driver into a new component

This commit is contained in:
laokaiyao
2025-11-12 16:57:43 +08:00
committed by morris
parent 5e249409ff
commit 79d6e573e6
18 changed files with 116 additions and 18 deletions
@@ -19,5 +19,6 @@ endif()
idf_component_register(SRCS ${srcs} idf_component_register(SRCS ${srcs}
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
PRIV_REQUIRES "${priv_requires}" PRIV_REQUIRES "${priv_requires}"
REQUIRES esp_hal_ana_cmpr
LDFRAGMENTS "linker.lf" LDFRAGMENTS "linker.lf"
) )
@@ -24,7 +24,7 @@
#include "driver/ana_cmpr_types.h" #include "driver/ana_cmpr_types.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "hal/ana_cmpr_ll.h" #include "hal/ana_cmpr_ll.h"
#include "soc/ana_cmpr_periph.h" #include "hal/ana_cmpr_periph.h"
#define TAG "ana_cmpr" #define TAG "ana_cmpr"
@@ -9,9 +9,17 @@ project(test_ana_cmpr)
idf_build_get_property(elf EXECUTABLE) idf_build_get_property(elf EXECUTABLE)
if(CONFIG_COMPILER_DUMP_RTL_FILES) 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 add_custom_target(check_test_app_sections ALL
COMMAND ${PYTHON} $ENV{IDF_PATH}/tools/ci/check_callgraph.py 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 --elf-file ${CMAKE_BINARY_DIR}/test_ana_cmpr.elf
find-refs find-refs
--from-sections=.iram0.text --from-sections=.iram0.text
@@ -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)
+72
View File
@@ -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
@@ -1,10 +1,10 @@
/* /*
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * 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" #include "soc/ana_cmpr_struct.h"
const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = {
@@ -28,7 +28,6 @@ extern "C" {
#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))
/** /**
* @brief Enable analog comparator * @brief Enable analog comparator
* *
@@ -1,10 +1,10 @@
/* /*
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * 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" #include "soc/ana_cmpr_struct.h"
const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = {
@@ -28,7 +28,6 @@ extern "C" {
#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))
/** /**
* @brief Enable analog comparator * @brief Enable analog comparator
* *
@@ -1,10 +1,10 @@
/* /*
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * 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" #include "soc/ana_cmpr_struct.h"
const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = {
@@ -1,10 +1,10 @@
/* /*
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * 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" #include "soc/ana_cmpr_struct.h"
const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = { const ana_cmpr_periph_t ana_cmpr_periph[SOC_ANA_CMPR_NUM] = {
@@ -28,7 +28,6 @@ extern "C" {
#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))
/** /**
* @brief Enable analog comparator * @brief Enable analog comparator
* *
@@ -1,7 +1,7 @@
/* /*
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * 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 #pragma once
-4
View File
@@ -68,10 +68,6 @@ if(CONFIG_SOC_ADC_SUPPORTED)
list(APPEND srcs "${target_folder}/adc_periph.c") list(APPEND srcs "${target_folder}/adc_periph.c")
endif() endif()
if(CONFIG_SOC_ANA_CMPR_SUPPORTED)
list(APPEND srcs "${target_folder}/ana_cmpr_periph.c")
endif()
if(CONFIG_SOC_DEBUG_PROBE_SUPPORTED) if(CONFIG_SOC_DEBUG_PROBE_SUPPORTED)
list(APPEND srcs "${target_folder}/debug_probe_periph.c") list(APPEND srcs "${target_folder}/debug_probe_periph.c")
endif() endif()
@@ -29,6 +29,9 @@ foreach(hal_dir ${esp_hal_component_dirs})
endif() endif()
endforeach() 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. # These components are currently included into "G1" build, but shouldn't.
# After removing the extra dependencies, remove the components from this list as well. # After removing the extra dependencies, remove the components from this list as well.
set(extra_components_which_shouldnt_be_included set(extra_components_which_shouldnt_be_included