From a7d380c80a2686426fe1d84a69cf5388a77846c9 Mon Sep 17 00:00:00 2001 From: morris Date: Fri, 8 Apr 2022 15:11:59 +0800 Subject: [PATCH] driver: a better way to avoid new/old driver coexistence --- components/driver/CMakeLists.txt | 1 - components/driver/deprecated/pcnt_legacy.c | 13 ++++++----- .../deprecated/rtc_temperature_legacy.c | 13 ++++++----- components/driver/deprecated/timer_legacy.c | 11 +++++---- components/driver/gptimer.c | 17 -------------- components/driver/legacy_new_driver_coexist.c | 23 ------------------- components/driver/pulse_cnt.c | 17 -------------- components/driver/temperature_sensor.c | 16 +------------ 8 files changed, 21 insertions(+), 90 deletions(-) delete mode 100644 components/driver/legacy_new_driver_coexist.c diff --git a/components/driver/CMakeLists.txt b/components/driver/CMakeLists.txt index 89f347f637..d22d3f09af 100644 --- a/components/driver/CMakeLists.txt +++ b/components/driver/CMakeLists.txt @@ -5,7 +5,6 @@ set(srcs "gptimer.c" "i2c.c" "ledc.c" - "legacy_new_driver_coexist.c" "rtc_io.c" "rtc_module.c" "sdspi_crc.c" diff --git a/components/driver/deprecated/pcnt_legacy.c b/components/driver/deprecated/pcnt_legacy.c index 4b962aaee0..a71048f1b0 100644 --- a/components/driver/deprecated/pcnt_legacy.c +++ b/components/driver/deprecated/pcnt_legacy.c @@ -31,7 +31,7 @@ #define PCNT_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux) #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux) -static const char *TAG = "pcnt"; +static const char *TAG = "pcnt(legacy)"; #define PCNT_CHECK(a, str, ret_val) ESP_RETURN_ON_FALSE(a, ret_val, TAG, "%s", str) @@ -552,11 +552,12 @@ void pcnt_isr_service_uninstall(void) __attribute__((constructor)) static void check_pcnt_driver_conflict(void) { - extern int pcnt_driver_init_count; - pcnt_driver_init_count++; - if (pcnt_driver_init_count > 1) { - ESP_EARLY_LOGE(TAG, "CONFLICT! The pulse_cnt driver can't work along with the legacy pcnt driver"); + // This function was declared as weak here. pulse_cnt driver has one implementation. + // So if pulse_cnt driver is not linked in, then `pcnt_new_unit` should be NULL at runtime. + extern __attribute__((weak)) esp_err_t pcnt_new_unit(const void *config, void **ret_unit); + if (pcnt_new_unit != NULL) { + ESP_EARLY_LOGE(TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver"); abort(); } - ESP_EARLY_LOGW(TAG, "legacy pcnt driver is deprecated, please migrate to use driver/pulse_cnt.h"); + ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/pulse_cnt.h`"); } diff --git a/components/driver/deprecated/rtc_temperature_legacy.c b/components/driver/deprecated/rtc_temperature_legacy.c index 75078fd742..46cb5632b3 100644 --- a/components/driver/deprecated/rtc_temperature_legacy.c +++ b/components/driver/deprecated/rtc_temperature_legacy.c @@ -7,6 +7,7 @@ #include #include #include +#include "sdkconfig.h" #include "esp_types.h" #include "esp_log.h" #include "esp_check.h" @@ -18,7 +19,6 @@ #include "hal/temperature_sensor_ll.h" #include "driver/temp_sensor_types_legacy.h" #include "esp_private/periph_ctrl.h" -#include "sdkconfig.h" static const char *TAG = "tsens"; @@ -158,11 +158,12 @@ esp_err_t temp_sensor_read_celsius(float *celsius) __attribute__((constructor)) static void check_legacy_temp_sensor_driver_conflict(void) { - extern int temp_sensor_driver_init_count; - temp_sensor_driver_init_count++; - if (temp_sensor_driver_init_count > 1) { - ESP_EARLY_LOGE(TAG, "CONFLICT! The legacy temp sensor driver can't work along with the new temperature driver"); + // This function was declared as weak here. temperature_sensor driver has one implementation. + // So if temperature_sensor driver is not linked in, then `temperature_sensor_install()` should be NULL at runtime. + extern __attribute__((weak)) esp_err_t temperature_sensor_install(const void *tsens_config, void **ret_tsens); + if (temperature_sensor_install != NULL) { + ESP_EARLY_LOGE(TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver"); abort(); } - ESP_EARLY_LOGW(TAG, "legacy temp sensor driver is deprecated, please migrate to use driver/temperature_sensor.h"); + ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/temperature_sensor.h`"); } diff --git a/components/driver/deprecated/timer_legacy.c b/components/driver/deprecated/timer_legacy.c index 1e8f3937cf..6713b69216 100644 --- a/components/driver/deprecated/timer_legacy.c +++ b/components/driver/deprecated/timer_legacy.c @@ -480,11 +480,12 @@ esp_err_t IRAM_ATTR timer_spinlock_give(timer_group_t group_num) __attribute__((constructor)) static void check_legacy_timer_driver_conflict(void) { - extern int timer_group_driver_init_count; - timer_group_driver_init_count++; - if (timer_group_driver_init_count > 1) { - ESP_EARLY_LOGE(TIMER_TAG, "CONFLICT! The legacy timer group driver can't work along with the gptimer driver"); + // This function was declared as weak here. gptimer driver has one implementation. + // So if gptimer driver is not linked in, then `gptimer_new_timer()` should be NULL at runtime. + extern __attribute__((weak)) esp_err_t gptimer_new_timer(const void *config, void **ret_timer); + if (gptimer_new_timer != NULL) { + ESP_EARLY_LOGE(TIMER_TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver"); abort(); } - ESP_EARLY_LOGW(TIMER_TAG, "legacy timer group driver is deprecated, please migrate to use driver/gptimer.h"); + ESP_EARLY_LOGW(TIMER_TAG, "legacy driver is deprecated, please migrate to `driver/gptimer.h`"); } diff --git a/components/driver/gptimer.c b/components/driver/gptimer.c index 362b05f4e5..1b7b37d17a 100644 --- a/components/driver/gptimer.c +++ b/components/driver/gptimer.c @@ -504,20 +504,3 @@ esp_err_t gptimer_get_pm_lock(gptimer_handle_t timer, esp_pm_lock_handle_t *ret_ *ret_pm_lock = timer->pm_lock; return ESP_OK; } - -/** - * @brief This function will be called during start up, to check that gptimer driver is not running along with the legacy timer group driver - */ -__attribute__((constructor)) -static void check_gptimer_driver_conflict(void) -{ -#if CONFIG_GPTIMER_ENABLE_DEBUG_LOG - esp_log_level_set(TAG, ESP_LOG_DEBUG); -#endif - extern int timer_group_driver_init_count; - timer_group_driver_init_count++; - if (timer_group_driver_init_count > 1) { - ESP_EARLY_LOGE(TAG, "CONFLICT! The gptimer driver can't work along with the legacy timer group driver"); - abort(); - } -} diff --git a/components/driver/legacy_new_driver_coexist.c b/components/driver/legacy_new_driver_coexist.c deleted file mode 100644 index 427809d7e7..0000000000 --- a/components/driver/legacy_new_driver_coexist.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @brief This count is used to prevent the coexistence of - * the legacy timer group driver (deprecated/driver/timer.h) and the new gptimer driver (driver/gptimer.h). - */ -int timer_group_driver_init_count = 0; - -/** - * @brief This count is used to prevent the coexistence of - * the legacy pcnt driver (deprecated/driver/pcnt.h) and the new pulse_cnt driver (driver/pulse_cnt.h). - */ -int pcnt_driver_init_count = 0; - -/** - * @brief This count is used to prevent the coexistence of - * the legacy temperature sensor driver (deprecated/driver/temp_sensor.h) and the new temperature sensor driver (driver/temperature_sensor.h). - */ -int temp_sensor_driver_init_count = 0; diff --git a/components/driver/pulse_cnt.c b/components/driver/pulse_cnt.c index ada6015881..7ee93309df 100644 --- a/components/driver/pulse_cnt.c +++ b/components/driver/pulse_cnt.c @@ -724,20 +724,3 @@ IRAM_ATTR static void pcnt_default_isr(void *args) portYIELD_FROM_ISR(); } } - -/** - * @brief This function will be called during start up, to check that pulse_cnt driver is not running along with the legacy pcnt driver - */ -__attribute__((constructor)) -static void check_pulse_cnt_driver_conflict(void) -{ -#if CONFIG_PCNT_ENABLE_DEBUG_LOG - esp_log_level_set(TAG, ESP_LOG_DEBUG); -#endif - extern int pcnt_driver_init_count; - pcnt_driver_init_count++; - if (pcnt_driver_init_count > 1) { - ESP_EARLY_LOGE(TAG, "CONFLICT! The pulse_cnt driver can't work along with the legacy pcnt driver"); - abort(); - } -} diff --git a/components/driver/temperature_sensor.c b/components/driver/temperature_sensor.c index a16f1c74f6..acec435cfc 100644 --- a/components/driver/temperature_sensor.c +++ b/components/driver/temperature_sensor.c @@ -83,10 +83,10 @@ static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_config, temperature_sensor_handle_t *ret_tsens) { - esp_err_t ret = ESP_OK; #if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG esp_log_level_set(TAG, ESP_LOG_DEBUG); #endif + esp_err_t ret = ESP_OK; ESP_RETURN_ON_FALSE((tsens_config && ret_tsens), ESP_ERR_INVALID_ARG, TAG, "Invalid argument"); ESP_RETURN_ON_FALSE((s_tsens_attribute_copy == NULL), ESP_ERR_INVALID_STATE, TAG, "Already installed"); temperature_sensor_handle_t tsens; @@ -198,17 +198,3 @@ esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, floa } return ESP_OK; } - -/** - * @brief This function will be called during start up, to check the new temperature driver is not running along with the legacy temp sensor driver - */ -__attribute__((constructor)) -static void check_temperature_driver_conflict(void) -{ - extern int temp_sensor_driver_init_count; - temp_sensor_driver_init_count++; - if (temp_sensor_driver_init_count > 1) { - ESP_EARLY_LOGE(TAG, "CONFLICT! The temperature driver can't work along with the legacy temp sensor driver"); - abort(); - } -}