From fd3a1ffc21027f8c37745fa92780befb0b0abcd5 Mon Sep 17 00:00:00 2001 From: morris Date: Sat, 28 May 2022 17:02:07 +0800 Subject: [PATCH] mcpwm: deprecate legacy driver --- components/driver/Kconfig | 14 + .../{include => deprecated}/driver/mcpwm.h | 292 +---------------- .../deprecated/driver/mcpwm_types_legacy.h | 307 ++++++++++++++++++ .../{mcpwm.c => deprecated/mcpwm_legacy.c} | 37 ++- .../legacy_mcpwm_driver/CMakeLists.txt | 5 + .../test_apps/legacy_mcpwm_driver/README.md | 2 + .../legacy_mcpwm_driver/main/CMakeLists.txt | 7 + .../legacy_mcpwm_driver/main/test_app_main.c | 40 +++ .../main/test_legacy_mcpwm.c} | 3 - .../pytest_legacy_mcpwm.py | 21 ++ .../legacy_mcpwm_driver/sdkconfig.ci.release | 5 + .../legacy_mcpwm_driver/sdkconfig.defaults | 3 + 12 files changed, 436 insertions(+), 300 deletions(-) rename components/driver/{include => deprecated}/driver/mcpwm.h (59%) create mode 100644 components/driver/deprecated/driver/mcpwm_types_legacy.h rename components/driver/{mcpwm.c => deprecated/mcpwm_legacy.c} (97%) create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/CMakeLists.txt create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/README.md create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/main/CMakeLists.txt create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/main/test_app_main.c rename components/driver/{test/test_pwm.c => test_apps/legacy_mcpwm_driver/main/test_legacy_mcpwm.c} (99%) create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/pytest_legacy_mcpwm.py create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.ci.release create mode 100644 components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.defaults diff --git a/components/driver/Kconfig b/components/driver/Kconfig index bb46a084f6..8def437f13 100644 --- a/components/driver/Kconfig +++ b/components/driver/Kconfig @@ -352,6 +352,20 @@ menu "Driver Configurations" cache misses, and also be able to run whilst the cache is disabled. (e.g. SPI Flash write) + config MCPWM_SUPPRESS_DEPRECATE_WARN + bool "Suppress leagcy driver deprecated warning" + default n + help + Wether to suppress the deprecation warnings when using legacy MCPWM driver (driver/mcpwm.h). + If you want to continue using the legacy driver, and don't want to see related deprecation warnings, + you can enable this option. + + config MCPWM_ENABLE_DEBUG_LOG + bool "Enable debug log" + default n + help + Wether to enable the debug log message for MCPWM driver. + Note that, this option only controls the MCPWM driver log, won't affect other drivers. endmenu # MCPWM Configuration menu "I2S Configuration" diff --git a/components/driver/include/driver/mcpwm.h b/components/driver/deprecated/driver/mcpwm.h similarity index 59% rename from components/driver/include/driver/mcpwm.h rename to components/driver/deprecated/driver/mcpwm.h index 08f4adc8b1..e8ff38778f 100644 --- a/components/driver/include/driver/mcpwm.h +++ b/components/driver/deprecated/driver/mcpwm.h @@ -9,298 +9,16 @@ #include #include #include "esp_err.h" -#include "esp_bit_defs.h" -#include "esp_intr_alloc.h" -#include "soc/soc_caps.h" -#include "hal/mcpwm_types.h" +#include "driver/mcpwm_types_legacy.h" + +#if !CONFIG_MCPWM_SUPPRESS_DEPRECATE_WARN +#warning "legacy MCPWM driver is deprecated, please migrate to the new driver (include driver/mcpwm_prelude.h)" +#endif #ifdef __cplusplus extern "C" { #endif -/** - * @brief IO signals for the MCPWM - * - * - 6 MCPWM output pins that generate PWM signals - * - 3 MCPWM fault input pins to detect faults like overcurrent, overvoltage, etc. - * - 3 MCPWM sync input pins to synchronize MCPWM outputs signals - * - 3 MCPWM capture input pins to gather feedback from controlled motors, using e.g. hall sensors - */ -typedef enum { - MCPWM0A = 0, /*! +#include +#include "esp_bit_defs.h" +#include "soc/soc_caps.h" +#include "hal/mcpwm_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief IO signals for the MCPWM + * + * - 6 MCPWM output pins that generate PWM signals + * - 3 MCPWM fault input pins to detect faults like over-current, over-voltage, etc. + * - 3 MCPWM sync input pins to synchronize MCPWM outputs signals + * - 3 MCPWM capture input pins to gather feedback from controlled motors, using e.g. hall sensors + */ +typedef enum { + MCPWM0A = 0, /*!dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_LOW); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_NO_CHANGE); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, gen, MCPWM_ACTION_FORCE_HIGH); - } else if (duty_type == MCPWM_HAL_GENERATOR_MODE_FORCE_LOW) { + } else if (duty_type == MCPWM_DUTY_MODE_FORCE_LOW) { mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, gen, MCPWM_ACTION_FORCE_LOW); - } else if (duty_type == MCPWM_HAL_GENERATOR_MODE_FORCE_HIGH) { + } else if (duty_type == MCPWM_DUTY_MODE_FORCE_HIGH) { mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, gen, MCPWM_ACTION_FORCE_HIGH); @@ -345,11 +346,11 @@ esp_err_t mcpwm_set_duty_type(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, m mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_NO_CHANGE); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, gen, MCPWM_ACTION_FORCE_LOW); - } else if (duty_type == MCPWM_HAL_GENERATOR_MODE_FORCE_LOW) { + } else if (duty_type == MCPWM_DUTY_MODE_FORCE_LOW) { mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, gen, MCPWM_ACTION_FORCE_LOW); - } else if (duty_type == MCPWM_HAL_GENERATOR_MODE_FORCE_HIGH) { + } else if (duty_type == MCPWM_DUTY_MODE_FORCE_HIGH) { mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, gen, MCPWM_ACTION_FORCE_HIGH); @@ -364,14 +365,14 @@ esp_err_t mcpwm_set_duty_type(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, m mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, gen, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, gen, MCPWM_ACTION_FORCE_LOW); - } else if (duty_type == MCPWM_HAL_GENERATOR_MODE_FORCE_LOW) { + } else if (duty_type == MCPWM_DUTY_MODE_FORCE_LOW) { mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, gen, MCPWM_ACTION_FORCE_LOW); mcpwm_ll_generator_set_action_on_compare_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, gen, MCPWM_ACTION_FORCE_LOW); - } else if (duty_type == MCPWM_HAL_GENERATOR_MODE_FORCE_HIGH) { + } else if (duty_type == MCPWM_DUTY_MODE_FORCE_HIGH) { mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_FULL, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_HIGH); mcpwm_ll_generator_set_action_on_timer_event(hal->dev, op, gen, MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_ACTION_FORCE_HIGH); @@ -463,13 +464,13 @@ uint32_t mcpwm_get_duty_in_us(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, m esp_err_t mcpwm_set_signal_high(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_generator_t gen) { //the driver currently always use the timer x for operator x - return mcpwm_set_duty_type(mcpwm_num, timer_num, gen, MCPWM_HAL_GENERATOR_MODE_FORCE_HIGH); + return mcpwm_set_duty_type(mcpwm_num, timer_num, gen, MCPWM_DUTY_MODE_FORCE_HIGH); } esp_err_t mcpwm_set_signal_low(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_generator_t gen) { //the driver currently always use the timer x for operator x - return mcpwm_set_duty_type(mcpwm_num, timer_num, gen, MCPWM_HAL_GENERATOR_MODE_FORCE_LOW); + return mcpwm_set_duty_type(mcpwm_num, timer_num, gen, MCPWM_DUTY_MODE_FORCE_LOW); } esp_err_t mcpwm_carrier_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num) @@ -943,3 +944,19 @@ esp_err_t mcpwm_set_timer_sync_output(mcpwm_unit_t mcpwm_num, mcpwm_timer_t time mcpwm_critical_exit(mcpwm_num); return ESP_OK; } + +/** + * @brief This function will be called during start up, to check that this legacy mcpwm driver is not running along with the new MCPWM driver + */ +__attribute__((constructor)) +static void check_mcpwm_driver_conflict(void) +{ + // This function was declared as weak here. The new MCPWM driver has the implementation. + // So if the new MCPWM driver is not linked in, then `mcpwm_acquire_group_handle()` should be NULL at runtime. + extern __attribute__((weak)) void *mcpwm_acquire_group_handle(int group_id); + if ((void *)mcpwm_acquire_group_handle != NULL) { + ESP_EARLY_LOGE(TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver"); + abort(); + } + ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/mcpwm_prelude.h`"); +} diff --git a/components/driver/test_apps/legacy_mcpwm_driver/CMakeLists.txt b/components/driver/test_apps/legacy_mcpwm_driver/CMakeLists.txt new file mode 100644 index 0000000000..6a6fac6b51 --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/CMakeLists.txt @@ -0,0 +1,5 @@ +# This is the project CMakeLists.txt file for the test subproject +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(legacy_mcpwm_driver_test) diff --git a/components/driver/test_apps/legacy_mcpwm_driver/README.md b/components/driver/test_apps/legacy_mcpwm_driver/README.md new file mode 100644 index 0000000000..5ab630aafd --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/README.md @@ -0,0 +1,2 @@ +| Supported Targets | ESP32 | ESP32-S3 | +| ----------------- | ----- | -------- | diff --git a/components/driver/test_apps/legacy_mcpwm_driver/main/CMakeLists.txt b/components/driver/test_apps/legacy_mcpwm_driver/main/CMakeLists.txt new file mode 100644 index 0000000000..f5ad62dfcf --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/main/CMakeLists.txt @@ -0,0 +1,7 @@ +set(srcs "test_app_main.c" + "test_legacy_mcpwm.c") + +# In order for the cases defined by `TEST_CASE` to be linked into the final elf, +# the component can be registered as WHOLE_ARCHIVE +idf_component_register(SRCS ${srcs} + WHOLE_ARCHIVE) diff --git a/components/driver/test_apps/legacy_mcpwm_driver/main/test_app_main.c b/components/driver/test_apps/legacy_mcpwm_driver/main/test_app_main.c new file mode 100644 index 0000000000..027d2b69c0 --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/main/test_app_main.c @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "unity.h" +#include "unity_test_runner.h" +#include "esp_heap_caps.h" + +#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) +{ + unity_run_menu(); +} diff --git a/components/driver/test/test_pwm.c b/components/driver/test_apps/legacy_mcpwm_driver/main/test_legacy_mcpwm.c similarity index 99% rename from components/driver/test/test_pwm.c rename to components/driver/test_apps/legacy_mcpwm_driver/main/test_legacy_mcpwm.c index 0a3240b01c..72a41a7bc8 100644 --- a/components/driver/test/test_pwm.c +++ b/components/driver/test_apps/legacy_mcpwm_driver/main/test_legacy_mcpwm.c @@ -12,7 +12,6 @@ #include "hal/gpio_hal.h" #include "esp_rom_gpio.h" #include "esp_private/esp_clk.h" -#if SOC_MCPWM_SUPPORTED #include "soc/mcpwm_periph.h" #include "driver/pulse_cnt.h" #include "driver/mcpwm.h" @@ -568,5 +567,3 @@ TEST_CASE("MCPWM capture test", "[mcpwm]") } } } - -#endif // SOC_MCPWM_SUPPORTED diff --git a/components/driver/test_apps/legacy_mcpwm_driver/pytest_legacy_mcpwm.py b/components/driver/test_apps/legacy_mcpwm_driver/pytest_legacy_mcpwm.py new file mode 100644 index 0000000000..25da5646d2 --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/pytest_legacy_mcpwm.py @@ -0,0 +1,21 @@ +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: CC0-1.0 + +import pytest +from pytest_embedded import Dut + + +@pytest.mark.esp32 +@pytest.mark.esp32s3 +@pytest.mark.generic +@pytest.mark.parametrize( + 'config', + [ + 'release', + ], + indirect=True, +) +def test_legacy_mcpwm(dut: Dut) -> None: + dut.expect('Press ENTER to see the list of tests') + dut.write('*') + dut.expect_unity_test_output() diff --git a/components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.ci.release b/components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.ci.release new file mode 100644 index 0000000000..91d93f163e --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.ci.release @@ -0,0 +1,5 @@ +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 diff --git a/components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.defaults b/components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.defaults new file mode 100644 index 0000000000..4c808e5adc --- /dev/null +++ b/components/driver/test_apps/legacy_mcpwm_driver/sdkconfig.defaults @@ -0,0 +1,3 @@ +CONFIG_FREERTOS_HZ=1000 +CONFIG_ESP_TASK_WDT=n +CONFIG_MCPWM_SUPPRESS_DEPRECATE_WARN=y