refactor(ana_cmpr): make analog comparator driver as component

This commit is contained in:
laokaiyao
2023-11-09 16:54:18 +08:00
parent 82a110c7f3
commit bc0201d6f0
34 changed files with 82 additions and 62 deletions

View File

@@ -1,13 +1,5 @@
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
components/driver/test_apps/analog_comparator:
disable:
- if: SOC_ANA_CMPR_SUPPORTED != 1
disable_test:
- if: IDF_TARGET == "esp32p4"
temporary: true
reason: not supported yet
components/driver/test_apps/dac_test_apps/dac:
disable:
- if: SOC_DAC_SUPPORTED != 1

View File

@@ -1,21 +0,0 @@
# This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.16)
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
set(COMPONENTS main)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(test_ana_cmpr)
if(CONFIG_COMPILER_DUMP_RTL_FILES)
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/driver/,${CMAKE_BINARY_DIR}/esp-idf/hal/
--elf-file ${CMAKE_BINARY_DIR}/test_ana_cmpr.elf
find-refs
--from-sections=.iram0.text
--to-sections=.flash.text
--exit-code
DEPENDS ${elf}
)
endif()

View File

@@ -1,2 +0,0 @@
| Supported Targets | ESP32-H2 | ESP32-P4 |
| ----------------- | -------- | -------- |

View File

@@ -1,12 +0,0 @@
set(srcs "test_app_main.c"
"test_ana_cmpr_common.c"
"test_ana_cmpr.c")
if(CONFIG_ANA_CMPR_ISR_IRAM_SAFE)
list(APPEND srcs "test_ana_cmpr_iram.c")
endif()
idf_component_register(SRCS ${srcs}
INCLUDE_DIRS "."
PRIV_REQUIRES unity driver
WHOLE_ARCHIVE)

View File

@@ -1,87 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 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]")
{
ana_cmpr_handle_t cmpr = NULL;
ana_cmpr_config_t config = {
.unit = SOC_ANA_CMPR_NUM, // Set a wrong unit
.clk_src = ANA_CMPR_CLK_SRC_DEFAULT,
.ref_src = ANA_CMPR_REF_SRC_INTERNAL,
.cross_type = ANA_CMPR_CROSS_ANY,
};
/* Allocate a wrong unit */
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, ana_cmpr_new_unit(&config, &cmpr));
/* Allocate a correct unit */
config.unit = 0;
TEST_ESP_OK(ana_cmpr_new_unit(&config, &cmpr));
/* Try to allocate a existed unit */
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, ana_cmpr_new_unit(&config, &cmpr));
/* Set the internal reference before enable */
ana_cmpr_internal_ref_config_t ref_cfg = {
.ref_volt = ANA_CMPR_REF_VOLT_50_PCT_VDD,
};
TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
/* Enable the unit */
TEST_ESP_OK(ana_cmpr_enable(cmpr));
/* Set the internal reference after enable */
ref_cfg.ref_volt = ANA_CMPR_REF_VOLT_30_PCT_VDD;
TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
/* Try tp delete unit after enable */
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, ana_cmpr_del_unit(cmpr));
/* Disable the unit */
TEST_ESP_OK(ana_cmpr_disable(cmpr));
/* Try to delete the unit with a wrong handle */
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, ana_cmpr_del_unit((void *)&cmpr));
/* Delete the unit */
TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
/* Try to set internal reference for a external unit */
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_OK(ana_cmpr_del_unit(cmpr));
}
TEST_CASE("ana_cmpr_internal_reference", "[ana_cmpr]")
{
int src_chan = test_init_src_chan_gpio();
uint32_t cnt = 0;
ana_cmpr_handle_t cmpr = NULL;
ana_cmpr_config_t config = {
.unit = 0,
.clk_src = ANA_CMPR_CLK_SRC_DEFAULT,
.ref_src = ANA_CMPR_REF_SRC_INTERNAL,
.cross_type = ANA_CMPR_CROSS_ANY,
.flags.io_loop_back = 1,
};
TEST_ESP_OK(ana_cmpr_new_unit(&config, &cmpr));
ana_cmpr_internal_ref_config_t ref_cfg = {
.ref_volt = ANA_CMPR_REF_VOLT_50_PCT_VDD,
};
TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
ana_cmpr_debounce_config_t dbc_cfg = {
.wait_us = 10.0,
};
TEST_ESP_OK(ana_cmpr_set_debounce(cmpr, &dbc_cfg));
ana_cmpr_event_callbacks_t cbs = {
.on_cross = test_ana_cmpr_on_cross_callback,
};
TEST_ESP_OK(ana_cmpr_register_event_callbacks(cmpr, &cbs, &cnt));
TEST_ESP_OK(ana_cmpr_enable(cmpr));
cnt = 0;
for (int i = 1; i <= 10; i++) {
test_simulate_src_signal(src_chan, i % 2);
esp_rom_delay_us(100);
TEST_ASSERT(cnt == i);
}
TEST_ESP_OK(ana_cmpr_disable(cmpr));
TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
}

View File

@@ -1,43 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "esp_attr.h"
#include "unity.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "soc/soc_caps.h"
#include "driver/ana_cmpr.h"
/**
* @brief Test default on cross callback
*
* @param cmpr Analog Comparator handle
* @param edata Event data
* @param user_ctx User context, need to input a unint32_t counter
* @return
* - true Need to yield
* - false Don't need yield
*/
bool test_ana_cmpr_on_cross_callback(ana_cmpr_handle_t cmpr, const ana_cmpr_cross_event_data_t *edata, void *user_ctx);
/**
* @brief Initialize Analog Comparator source channel GPIO
*
* @return
* - int Source channel GPIO number
*/
int test_init_src_chan_gpio(void);
/**
* @brief Simulate source channel signal
*
* @param src_chan Source channel GPIO number
* @param val 0 to set low, others to set high
*/
void test_simulate_src_signal(int src_chan, uint32_t val);

View File

@@ -1,39 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "test_ana_cmpr.h"
#include "hal/gpio_ll.h"
#include "driver/gpio.h"
#include "esp_attr.h"
bool IRAM_ATTR test_ana_cmpr_on_cross_callback(ana_cmpr_handle_t cmpr, const ana_cmpr_cross_event_data_t *edata, void *user_ctx)
{
uint32_t *count = (uint32_t *)user_ctx;
(*count)++;
return false;
}
int test_init_src_chan_gpio(void)
{
int src_chan_num = -1;
TEST_ESP_OK(ana_cmpr_get_gpio(0, ANA_CMPR_SOURCE_CHAN, &src_chan_num));
TEST_ASSERT(src_chan_num > 0);
gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = (1ULL << src_chan_num),
.pull_down_en = false,
.pull_up_en = false,
};
TEST_ESP_OK(gpio_config(&io_conf));
TEST_ESP_OK(gpio_set_level(src_chan_num, 0));
return src_chan_num;
}
void IRAM_ATTR test_simulate_src_signal(int src_chan, uint32_t val)
{
gpio_set_level(src_chan, val);
}

View File

@@ -1,74 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 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 {
ana_cmpr_handle_t handle;
uint32_t count;
int src_chan;
} test_ana_cmpr_data_t;
static void IRAM_ATTR test_ana_cmpr_iram_safety(void *args)
{
test_ana_cmpr_data_t *data = (test_ana_cmpr_data_t *)args;
ana_cmpr_internal_ref_config_t ref_cfg = {
.ref_volt = ANA_CMPR_REF_VOLT_50_PCT_VDD,
};
ana_cmpr_set_internal_reference(data->handle, &ref_cfg);
ana_cmpr_debounce_config_t dbc_cfg = {
.wait_us = 1,
};
ana_cmpr_set_debounce(data->handle, &dbc_cfg);
data->count = 0;
for (int i = 1; i <= 10; i++) {
test_simulate_src_signal(data->src_chan, i % 2);
esp_rom_delay_us(100);
}
ana_cmpr_set_cross_type(data->handle, ANA_CMPR_CROSS_POS);
}
TEST_CASE("ana_cmpr_internal_reference_iram_safe", "[ana_cmpr]")
{
test_ana_cmpr_data_t test_data = {
.handle = NULL,
.count = 0,
.src_chan = -1,
};
test_data.src_chan = test_init_src_chan_gpio();
ana_cmpr_handle_t cmpr = NULL;
ana_cmpr_config_t config = {
.unit = 0,
.clk_src = ANA_CMPR_CLK_SRC_DEFAULT,
.ref_src = ANA_CMPR_REF_SRC_INTERNAL,
.cross_type = ANA_CMPR_CROSS_ANY,
.flags.io_loop_back = 1,
};
TEST_ESP_OK(ana_cmpr_new_unit(&config, &cmpr));
test_data.handle = cmpr;
ana_cmpr_internal_ref_config_t ref_cfg = {
.ref_volt = ANA_CMPR_REF_VOLT_50_PCT_VDD,
};
TEST_ESP_OK(ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
ana_cmpr_debounce_config_t dbc_cfg = {
.wait_us = 10,
};
TEST_ESP_OK(ana_cmpr_set_debounce(cmpr, &dbc_cfg));
ana_cmpr_event_callbacks_t cbs = {
.on_cross = test_ana_cmpr_on_cross_callback,
};
TEST_ESP_OK(ana_cmpr_register_event_callbacks(cmpr, &cbs, &test_data.count));
TEST_ESP_OK(ana_cmpr_enable(cmpr));
unity_utils_run_cache_disable_stub(test_ana_cmpr_iram_safety, &test_data);
TEST_ASSERT_EQUAL_INT(test_data.count, 10);
TEST_ESP_OK(ana_cmpr_disable(cmpr));
TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
}

View File

@@ -1,54 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include "unity_test_runner.h"
#include "esp_heap_caps.h"
// Some resources are lazy allocated in analog comparator driver, the threshold is left for that case
#define TEST_MEMORY_LEAK_THRESHOLD (-300)
static size_t before_free_8bit;
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);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
}
void setUp(void)
{
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
}
void tearDown(void)
{
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
check_leak(before_free_8bit, after_free_8bit, "8BIT");
check_leak(before_free_32bit, after_free_32bit, "32BIT");
}
void app_main(void)
{
// _ _ _ _ ____ __ __ ____ ____ _____ _
// / \ | \ | | / \ / ___| \/ | _ \| _ \ |_ _|__ ___| |_
// / _ \ | \| | / _ \ | | | |\/| | |_) | |_) | | |/ _ \/ __| __|
// / ___ \| |\ |/ ___ \ | |___| | | | __/| _ < | | __/\__ \ |_
// /_/ \_\_| \_/_/ \_\ \____|_| |_|_| |_| \_\ |_|\___||___/\__|
printf(" _ _ _ _ ____ __ __ ____ ____ _____ _ \n");
printf(" / \\ | \\ | | / \\ / ___| \\/ | _ \\| _ \\ |_ _|__ ___| |_ \n");
printf(" / _ \\ | \\| | / _ \\ | | | |\\/| | |_) | |_) | | |/ _ \\/ __| __|\n");
printf(" / ___ \\| |\\ |/ ___ \\ | |___| | | | __/| _ < | | __/\\__ \\ |_ \n");
printf(" /_/ \\_\\_| \\_/_/ \\_\\ \\____|_| |_|_| |_| \\_\\ |_|\\___||___/\\__|\n");
printf("\n");
unity_run_menu();
}

View File

@@ -1,19 +0,0 @@
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import pytest
from pytest_embedded import Dut
@pytest.mark.esp32h2
@pytest.mark.generic
@pytest.mark.parametrize(
'config',
[
'iram_safe',
'release',
],
indirect=True,
)
def test_ana_cmpr(dut: Dut) -> None:
dut.run_all_single_board_cases()

View File

@@ -1,7 +0,0 @@
CONFIG_COMPILER_DUMP_RTL_FILES=y
CONFIG_ANA_CMPR_ISR_IRAM_SAFE=y
CONFIG_ANA_CMPR_CTRL_FUNC_IN_IRAM=y
CONFIG_GPIO_CTRL_FUNC_IN_IRAM=y
CONFIG_COMPILER_OPTIMIZATION_NONE=y
# place non-ISR FreeRTOS functions in Flash
CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y

View File

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

View File

@@ -1,2 +0,0 @@
CONFIG_FREERTOS_HZ=1000
CONFIG_ESP_TASK_WDT=n