rtc_temperature_legacy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "esp_private/regi2c_ctrl.h"
  15. #include "soc/regi2c_saradc.h"
  16. #include "esp_log.h"
  17. #include "esp_efuse_rtc_calib.h"
  18. #include "hal/temperature_sensor_ll.h"
  19. #include "driver/temp_sensor_types_legacy.h"
  20. #include "esp_private/periph_ctrl.h"
  21. #include "esp_private/sar_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. temp_sensor_sync_tsens_idx(tsens.dac_offset);
  58. temperature_sensor_ll_set_range(dac_offset[tsens.dac_offset].reg_val);
  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. temperature_sensor_power_acquire();
  87. temperature_sensor_ll_clk_sel(TEMPERATURE_SENSOR_CLK_SRC_DEFAULT);
  88. tsens_hw_state = TSENS_HW_STATE_STARTED;
  89. return err;
  90. }
  91. esp_err_t temp_sensor_stop(void)
  92. {
  93. temperature_sensor_power_release();
  94. tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
  95. return ESP_OK;
  96. }
  97. esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
  98. {
  99. ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens_out specified");
  100. *tsens_out = temperature_sensor_ll_get_raw_value();
  101. return ESP_OK;
  102. }
  103. static esp_err_t read_delta_t_from_efuse(void)
  104. {
  105. ESP_RETURN_ON_ERROR(esp_efuse_rtc_calib_get_tsens_val(&s_deltaT), TAG, "Calibration error");
  106. ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
  107. return ESP_OK;
  108. }
  109. static float parse_temp_sensor_raw_value(uint32_t tsens_raw)
  110. {
  111. if (isnan(s_deltaT)) { //suggests that the value is not initialized
  112. read_delta_t_from_efuse();
  113. }
  114. float result = tsens_raw - s_deltaT / 10.0;
  115. return result;
  116. }
  117. esp_err_t temp_sensor_read_celsius(float *celsius)
  118. {
  119. ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
  120. if (tsens_hw_state != TSENS_HW_STATE_STARTED) {
  121. ESP_LOGE(TAG, "Has not been started");
  122. return ESP_ERR_INVALID_STATE;
  123. }
  124. temp_sensor_config_t tsens;
  125. temp_sensor_get_config(&tsens);
  126. bool range_changed;
  127. uint16_t tsens_out = temp_sensor_get_raw_value(&range_changed);
  128. *celsius = parse_temp_sensor_raw_value(tsens_out);
  129. if (*celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) {
  130. ESP_LOGE(TAG, "Exceeding temperature measure range.");
  131. return ESP_ERR_INVALID_STATE;
  132. }
  133. if (range_changed) {
  134. temp_sensor_get_config(&tsens);
  135. }
  136. return ESP_OK;
  137. }
  138. /**
  139. * @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
  140. */
  141. __attribute__((constructor))
  142. static void check_legacy_temp_sensor_driver_conflict(void)
  143. {
  144. // This function was declared as weak here. temperature_sensor driver has one implementation.
  145. // So if temperature_sensor driver is not linked in, then `temperature_sensor_install()` should be NULL at runtime.
  146. extern __attribute__((weak)) esp_err_t temperature_sensor_install(const void *tsens_config, void **ret_tsens);
  147. if ((void *)temperature_sensor_install != NULL) {
  148. ESP_EARLY_LOGE(TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver");
  149. abort();
  150. }
  151. ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/temperature_sensor.h`");
  152. }