temperature_sensor.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <string.h>
  9. #include "sdkconfig.h"
  10. #if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG
  11. // The local log level must be defined before including esp_log.h
  12. // Set the maximum log level for this source file
  13. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  14. #endif
  15. #include "esp_log.h"
  16. #include "sys/lock.h"
  17. #include "soc/rtc.h"
  18. #include "esp_check.h"
  19. #include "esp_types.h"
  20. #include "esp_heap_caps.h"
  21. #include "clk_ctrl_os.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "driver/temperature_sensor.h"
  24. #include "esp_efuse_rtc_calib.h"
  25. #include "esp_private/periph_ctrl.h"
  26. #include "hal/temperature_sensor_ll.h"
  27. #include "soc/temperature_sensor_periph.h"
  28. static const char *TAG = "temperature_sensor";
  29. typedef enum {
  30. TEMP_SENSOR_FSM_INIT,
  31. TEMP_SENSOR_FSM_ENABLE,
  32. } temp_sensor_fsm_t;
  33. static float s_deltaT = NAN; // unused number
  34. typedef struct temperature_sensor_obj_t temperature_sensor_obj_t;
  35. struct temperature_sensor_obj_t {
  36. const temperature_sensor_attribute_t *tsens_attribute;
  37. temp_sensor_fsm_t fsm;
  38. temperature_sensor_clk_src_t clk_src;
  39. };
  40. static temperature_sensor_attribute_t *s_tsens_attribute_copy;
  41. static int inline accuracy_compare(const void *p1, const void *p2)
  42. {
  43. return ((*(temperature_sensor_attribute_t *)p1).error_max < (*(temperature_sensor_attribute_t *)p2).error_max) ? -1 : 1;
  44. }
  45. static esp_err_t temperature_sensor_attribute_table_sort(void)
  46. {
  47. s_tsens_attribute_copy = (temperature_sensor_attribute_t *)heap_caps_malloc(sizeof(temperature_sensor_attributes), MALLOC_CAP_DEFAULT);
  48. ESP_RETURN_ON_FALSE(s_tsens_attribute_copy != NULL, ESP_ERR_NO_MEM, TAG, "No space for s_tsens_attribute_copy");
  49. for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
  50. s_tsens_attribute_copy[i] = temperature_sensor_attributes[i];
  51. }
  52. // Sort from small to large by error_max.
  53. qsort(s_tsens_attribute_copy, TEMPERATURE_SENSOR_ATTR_RANGE_NUM, sizeof(s_tsens_attribute_copy[0]), accuracy_compare);
  54. return ESP_OK;
  55. }
  56. static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_t tsens, const temperature_sensor_config_t *tsens_config)
  57. {
  58. for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
  59. if ((tsens_config->range_min >= s_tsens_attribute_copy[i].range_min) && (tsens_config->range_max <= s_tsens_attribute_copy[i].range_max)) {
  60. tsens->tsens_attribute = &s_tsens_attribute_copy[i];
  61. break;
  62. }
  63. }
  64. ESP_RETURN_ON_FALSE(tsens->tsens_attribute != NULL, ESP_ERR_INVALID_ARG, TAG, "Out of testing range");
  65. return ESP_OK;
  66. }
  67. esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_config, temperature_sensor_handle_t *ret_tsens)
  68. {
  69. #if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG
  70. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  71. #endif
  72. esp_err_t ret = ESP_OK;
  73. ESP_RETURN_ON_FALSE((tsens_config && ret_tsens), ESP_ERR_INVALID_ARG, TAG, "Invalid argument");
  74. ESP_RETURN_ON_FALSE((s_tsens_attribute_copy == NULL), ESP_ERR_INVALID_STATE, TAG, "Already installed");
  75. temperature_sensor_handle_t tsens = NULL;
  76. tsens = (temperature_sensor_obj_t *) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT);
  77. ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "no mem for temp sensor");
  78. tsens->clk_src = tsens_config->clk_src;
  79. periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
  80. periph_module_reset(PERIPH_TEMPSENSOR_MODULE);
  81. ESP_GOTO_ON_ERROR(temperature_sensor_attribute_table_sort(), err, TAG, "Table sort failed");
  82. ESP_GOTO_ON_ERROR(temperature_sensor_choose_best_range(tsens, tsens_config), err, TAG, "Cannot select the correct range");
  83. ESP_LOGI(TAG, "Range [%d°C ~ %d°C], error < %d°C",
  84. tsens->tsens_attribute->range_min,
  85. tsens->tsens_attribute->range_max,
  86. tsens->tsens_attribute->error_max);
  87. regi2c_saradc_enable();
  88. temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
  89. temperature_sensor_ll_enable(false); // disable the sensor by default
  90. tsens->fsm = TEMP_SENSOR_FSM_INIT;
  91. *ret_tsens = tsens;
  92. return ESP_OK;
  93. err:
  94. temperature_sensor_uninstall(tsens);
  95. return ret;
  96. }
  97. esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
  98. {
  99. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  100. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
  101. if (s_tsens_attribute_copy) {
  102. free(s_tsens_attribute_copy);
  103. }
  104. s_tsens_attribute_copy = NULL;
  105. regi2c_saradc_disable();
  106. periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
  107. free(tsens);
  108. return ESP_OK;
  109. }
  110. esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
  111. {
  112. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  113. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
  114. #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
  115. if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
  116. periph_rtc_dig_clk8m_enable();
  117. }
  118. #endif
  119. temperature_sensor_ll_clk_enable(true);
  120. temperature_sensor_ll_clk_sel(tsens->clk_src);
  121. temperature_sensor_ll_enable(true);
  122. tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
  123. return ESP_OK;
  124. }
  125. esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens)
  126. {
  127. ESP_RETURN_ON_FALSE(tsens, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  128. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
  129. temperature_sensor_ll_enable(false);
  130. #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
  131. if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
  132. periph_rtc_dig_clk8m_disable();
  133. }
  134. #endif
  135. tsens->fsm = TEMP_SENSOR_FSM_INIT;
  136. return ESP_OK;
  137. }
  138. static esp_err_t read_delta_t_from_efuse(void)
  139. {
  140. if (esp_efuse_rtc_calib_get_tsens_val(&s_deltaT) != ESP_OK) {
  141. ESP_LOGW(TAG, "Calibration failed");
  142. }
  143. ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
  144. return ESP_OK;
  145. }
  146. static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
  147. {
  148. if (isnan(s_deltaT)) { //suggests that the value is not initialized
  149. read_delta_t_from_efuse();
  150. }
  151. 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;
  152. return result;
  153. }
  154. esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius)
  155. {
  156. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has not been installed");
  157. ESP_RETURN_ON_FALSE(out_celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "Celsius points to nothing");
  158. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
  159. uint32_t tsens_out = temperature_sensor_ll_get_raw_value();
  160. ESP_LOGV(TAG, "tsens_out %"PRIu32, tsens_out);
  161. *out_celsius = parse_temp_sensor_raw_value(tsens_out, tsens->tsens_attribute->offset);
  162. if (*out_celsius < tsens->tsens_attribute->range_min || *out_celsius > tsens->tsens_attribute->range_max) {
  163. ESP_LOGW(TAG, "value out of range, probably invalid");
  164. return ESP_FAIL;
  165. }
  166. return ESP_OK;
  167. }