Переглянути джерело

temp_sensor: avoid coexistence of new and legacy driver

morris 3 роки тому
батько
коміт
21e29f285f

+ 1 - 1
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"

+ 1 - 0
components/driver/deprecated/driver/temp_sensor.h

@@ -7,6 +7,7 @@
 #pragma once
 
 #include <stdint.h>
+#include "sdkconfig.h"
 #include "esp_err.h"
 #include "driver/temp_sensor_types_legacy.h"
 

+ 9 - 3
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

+ 6 - 0
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;

+ 15 - 0
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");
+}

+ 17 - 3
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();
+    }
+}