rtc_temperature_legacy.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9. #include "sdkconfig.h"
  10. #include "esp_types.h"
  11. #include "esp_log.h"
  12. #include "esp_check.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "soc/rtc_cntl_reg.h"
  15. #include "esp_private/regi2c_ctrl.h"
  16. #include "soc/regi2c_saradc.h"
  17. #include "esp_log.h"
  18. #include "esp_efuse_rtc_calib.h"
  19. #include "hal/temperature_sensor_ll.h"
  20. #include "driver/temp_sensor_types_legacy.h"
  21. #include "esp_private/periph_ctrl.h"
  22. static const char *TAG = "tsens";
  23. #define TSENS_ADC_FACTOR (0.4386)
  24. #define TSENS_DAC_FACTOR (27.88)
  25. #define TSENS_SYS_OFFSET (20.52)
  26. typedef struct {
  27. int index;
  28. int offset;
  29. int reg_val;
  30. int range_min;
  31. int range_max;
  32. int error_max;
  33. } tsens_dac_offset_t;
  34. static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
  35. /* DAC Offset reg_val min max error */
  36. {TSENS_DAC_L0, -2, 5, 50, 125, 3},
  37. {TSENS_DAC_L1, -1, 7, 20, 100, 2},
  38. {TSENS_DAC_L2, 0, 15, -10, 80, 1},
  39. {TSENS_DAC_L3, 1, 11, -30, 50, 2},
  40. {TSENS_DAC_L4, 2, 10, -40, 20, 3},
  41. };
  42. typedef enum {
  43. TSENS_HW_STATE_UNCONFIGURED,
  44. TSENS_HW_STATE_CONFIGURED,
  45. TSENS_HW_STATE_STARTED,
  46. } tsens_hw_state_t;
  47. static tsens_hw_state_t tsens_hw_state = TSENS_HW_STATE_UNCONFIGURED;
  48. static float s_deltaT = NAN; // Unused number
  49. esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
  50. {
  51. esp_err_t err = ESP_OK;
  52. if (tsens_hw_state == TSENS_HW_STATE_STARTED) {
  53. ESP_LOGE(TAG, "Do not configure the temp sensor when it's running!");
  54. err = ESP_ERR_INVALID_STATE;
  55. }
  56. temperature_sensor_ll_set_clk_div(tsens.clk_div);
  57. temperature_sensor_ll_set_range(dac_offset[tsens.dac_offset].reg_val);
  58. temperature_sensor_ll_enable(true);
  59. ESP_LOGI(TAG, "Config range [%d°C ~ %d°C], error < %d°C",
  60. dac_offset[tsens.dac_offset].range_min,
  61. dac_offset[tsens.dac_offset].range_max,
  62. dac_offset[tsens.dac_offset].error_max);
  63. tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
  64. return err;
  65. }
  66. esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)
  67. {
  68. ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified");
  69. tsens->dac_offset = temperature_sensor_ll_get_offset();
  70. for (int i = TSENS_DAC_L0; i < TSENS_DAC_MAX; i++) {
  71. if ((int)tsens->dac_offset == dac_offset[i].reg_val) {
  72. tsens->dac_offset = dac_offset[i].index;
  73. break;
  74. }
  75. }
  76. tsens->clk_div = temperature_sensor_ll_get_clk_div();
  77. return ESP_OK;
  78. }
  79. esp_err_t temp_sensor_start(void)
  80. {
  81. esp_err_t err = ESP_OK;
  82. if (tsens_hw_state != TSENS_HW_STATE_CONFIGURED) {
  83. ESP_LOGE(TAG, "Is already running or not be configured");
  84. err = ESP_ERR_INVALID_STATE;
  85. }
  86. regi2c_saradc_enable();
  87. periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
  88. temperature_sensor_ll_enable(true);
  89. temperature_sensor_ll_clk_enable(true);
  90. temperature_sensor_ll_clk_sel(TEMPERATURE_SENSOR_CLK_SRC_DEFAULT);
  91. tsens_hw_state = TSENS_HW_STATE_STARTED;
  92. return err;
  93. }
  94. esp_err_t temp_sensor_stop(void)
  95. {
  96. regi2c_saradc_disable();
  97. temperature_sensor_ll_enable(false);
  98. tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
  99. return ESP_OK;
  100. }
  101. esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
  102. {
  103. ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens_out specified");
  104. *tsens_out = temperature_sensor_ll_get_raw_value();
  105. return ESP_OK;
  106. }
  107. static esp_err_t read_delta_t_from_efuse(void)
  108. {
  109. ESP_RETURN_ON_ERROR(esp_efuse_rtc_calib_get_tsens_val(&s_deltaT), TAG, "Calibration error");
  110. ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
  111. return ESP_OK;
  112. }
  113. static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
  114. {
  115. if (isnan(s_deltaT)) { //suggests that the value is not initialized
  116. read_delta_t_from_efuse();
  117. }
  118. float result = (TEMPERATURE_SENSOR_LL_ADC_FACTOR * (float)tsens_raw - TEMPERATURE_SENSOR_LL_DAC_FACTOR * dac_offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR) - s_deltaT / 10.0;
  119. return result;
  120. }
  121. esp_err_t temp_sensor_read_celsius(float *celsius)
  122. {
  123. ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
  124. if (tsens_hw_state != TSENS_HW_STATE_STARTED) {
  125. ESP_LOGE(TAG, "Has not been started");
  126. return ESP_ERR_INVALID_STATE;
  127. }
  128. temp_sensor_config_t tsens;
  129. uint32_t tsens_out = 0;
  130. temp_sensor_get_config(&tsens);
  131. temp_sensor_read_raw(&tsens_out);
  132. ESP_LOGV(TAG, "tsens_out %"PRIu32, tsens_out);
  133. const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
  134. *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset);
  135. if (*celsius < dac->range_min || *celsius > dac->range_max) {
  136. ESP_LOGW(TAG, "Exceeding the temperature range!");
  137. return ESP_ERR_INVALID_STATE;
  138. }
  139. return ESP_OK;
  140. }
  141. /**
  142. * @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
  143. */
  144. __attribute__((constructor))
  145. static void check_legacy_temp_sensor_driver_conflict(void)
  146. {
  147. // This function was declared as weak here. temperature_sensor driver has one implementation.
  148. // So if temperature_sensor driver is not linked in, then `temperature_sensor_install()` should be NULL at runtime.
  149. extern __attribute__((weak)) esp_err_t temperature_sensor_install(const void *tsens_config, void **ret_tsens);
  150. if ((void *)temperature_sensor_install != NULL) {
  151. ESP_EARLY_LOGE(TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver");
  152. abort();
  153. }
  154. ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/temperature_sensor.h`");
  155. }