From 21e29f285fcf06f030ec887ebb47b52de65416c7 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 8 Mar 2022 11:53:22 +0800 Subject: [PATCH] temp_sensor: avoid coexistence of new and legacy driver --- components/driver/Kconfig | 2 +- .../driver/deprecated/driver/temp_sensor.h | 1 + .../include/driver/temperature_sensor.h | 12 ++++++++--- components/driver/legacy_new_driver_coexist.c | 6 ++++++ components/driver/rtc_temperature_legacy.c | 15 ++++++++++++++ components/driver/temperature_sensor.c | 20 ++++++++++++++++--- 6 files changed, 49 insertions(+), 7 deletions(-) diff --git a/components/driver/Kconfig b/components/driver/Kconfig index 0f98cdb998..42308eced6 100644 --- a/components/driver/Kconfig +++ b/components/driver/Kconfig @@ -146,7 +146,7 @@ menu "Driver configurations" endmenu # TWAI Configuration menu "Temperature sensor Configuration" - visible if SOC_TEMP_SENSOR_SUPPORTED + depends on SOC_TEMP_SENSOR_SUPPORTED config TEMP_SENSOR_SUPPRESS_DEPRECATE_WARN bool "Suppress legacy driver deprecated warning" diff --git a/components/driver/deprecated/driver/temp_sensor.h b/components/driver/deprecated/driver/temp_sensor.h index c5a28600df..546a9cc43d 100644 --- a/components/driver/deprecated/driver/temp_sensor.h +++ b/components/driver/deprecated/driver/temp_sensor.h @@ -7,6 +7,7 @@ #pragma once #include +#include "sdkconfig.h" #include "esp_err.h" #include "driver/temp_sensor_types_legacy.h" diff --git a/components/driver/include/driver/temperature_sensor.h b/components/driver/include/driver/temperature_sensor.h index fee0f2bb36..8e2bd083fc 100644 --- a/components/driver/include/driver/temperature_sensor.h +++ b/components/driver/include/driver/temperature_sensor.h @@ -31,9 +31,15 @@ typedef struct { temperature_sensor_clk_src_t clk_src; // the clock source of the temperature sensor. } temperature_sensor_config_t; -#define TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(min, max) {.range_min = min, \ - .range_max = max, \ - } +/** + * @brief temperature_sensor_config_t default constructure + */ +#define TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(min, max) \ + { \ + .range_min = min, \ + .range_max = max, \ + .clk_src = TEMPERATURE_SENSOR_CLK_SRC_DEFAULT, \ + } /** * @brief Install temperature sensor driver diff --git a/components/driver/legacy_new_driver_coexist.c b/components/driver/legacy_new_driver_coexist.c index 0bbdcf5c5b..427809d7e7 100644 --- a/components/driver/legacy_new_driver_coexist.c +++ b/components/driver/legacy_new_driver_coexist.c @@ -15,3 +15,9 @@ int timer_group_driver_init_count = 0; * 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/rtc_temperature_legacy.c b/components/driver/rtc_temperature_legacy.c index a3f9f33443..87ecd407ec 100644 --- a/components/driver/rtc_temperature_legacy.c +++ b/components/driver/rtc_temperature_legacy.c @@ -150,3 +150,18 @@ esp_err_t temp_sensor_read_celsius(float *celsius) } return ESP_OK; } + +/** + * @brief This function will be called during start up, to check that this legacy temp sensor driver is not running along with the new driver + */ +__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"); + abort(); + } + ESP_EARLY_LOGW(TAG, "legacy temp sensor driver is deprecated, please migrate to use driver/temperature_sensor.h"); +} diff --git a/components/driver/temperature_sensor.c b/components/driver/temperature_sensor.c index dad90224c1..a16f1c74f6 100644 --- a/components/driver/temperature_sensor.c +++ b/components/driver/temperature_sensor.c @@ -54,12 +54,12 @@ static temp_sensor_ll_attribute_t *s_tsens_attribute_copy; static int inline accuracy_compare(const void *p1, const void *p2) { - return ((*(temp_sensor_ll_attribute_t*)p1).error_max < (*(temp_sensor_ll_attribute_t*)p2).error_max) ? -1 : 1; + return ((*(temp_sensor_ll_attribute_t *)p1).error_max < (*(temp_sensor_ll_attribute_t *)p2).error_max) ? -1 : 1; } static esp_err_t temperature_sensor_attribute_table_sort(void) { - s_tsens_attribute_copy = (temp_sensor_ll_attribute_t*)heap_caps_malloc(sizeof(temp_sensor_ll_attributes), MALLOC_CAP_DEFAULT); + s_tsens_attribute_copy = (temp_sensor_ll_attribute_t *)heap_caps_malloc(sizeof(temp_sensor_ll_attributes), MALLOC_CAP_DEFAULT); ESP_RETURN_ON_FALSE(s_tsens_attribute_copy != NULL, ESP_ERR_NO_MEM, TAG, "No space for s_tsens_attribute_copy"); for (int i = 0 ; i < TEMPERATURE_SENSOR_LL_RANGE_NUM; i++) { s_tsens_attribute_copy[i] = temp_sensor_ll_attributes[i]; @@ -90,7 +90,7 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co 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; - tsens = (temperature_sensor_obj_t*) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT); + tsens = (temperature_sensor_obj_t *) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT); ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "install fail..."); tsens->clk_src = tsens_config->clk_src; @@ -198,3 +198,17 @@ 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(); + } +}