temperature_sensor.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. static const char *TAG = "temperature_sensor";
  28. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  29. #define TEMPERATURE_SENSOR_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  30. #define TEMPERATURE_SENSOR_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  31. typedef enum {
  32. TEMP_SENSOR_FSM_INIT,
  33. TEMP_SENSOR_FSM_ENABLE,
  34. } temp_sensor_fsm_t;
  35. static float s_deltaT = NAN; // unused number
  36. typedef struct temperature_sensor_obj_t temperature_sensor_obj_t;
  37. struct temperature_sensor_obj_t {
  38. const temp_sensor_ll_attribute_t *tsens_attribute;
  39. temp_sensor_fsm_t fsm;
  40. temperature_sensor_clk_src_t clk_src;
  41. };
  42. static temp_sensor_ll_attribute_t *s_tsens_attribute_copy;
  43. static int inline accuracy_compare(const void *p1, const void *p2)
  44. {
  45. return ((*(temp_sensor_ll_attribute_t *)p1).error_max < (*(temp_sensor_ll_attribute_t *)p2).error_max) ? -1 : 1;
  46. }
  47. static esp_err_t temperature_sensor_attribute_table_sort(void)
  48. {
  49. s_tsens_attribute_copy = (temp_sensor_ll_attribute_t *)heap_caps_malloc(sizeof(temp_sensor_ll_attributes), MALLOC_CAP_DEFAULT);
  50. ESP_RETURN_ON_FALSE(s_tsens_attribute_copy != NULL, ESP_ERR_NO_MEM, TAG, "No space for s_tsens_attribute_copy");
  51. for (int i = 0 ; i < TEMPERATURE_SENSOR_LL_RANGE_NUM; i++) {
  52. s_tsens_attribute_copy[i] = temp_sensor_ll_attributes[i];
  53. }
  54. // Sort from small to large by error_max.
  55. qsort(s_tsens_attribute_copy, TEMPERATURE_SENSOR_LL_RANGE_NUM, sizeof(s_tsens_attribute_copy[0]), accuracy_compare);
  56. return ESP_OK;
  57. }
  58. static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_t tsens, const temperature_sensor_config_t *tsens_config)
  59. {
  60. for (int i = 0 ; i < TEMPERATURE_SENSOR_LL_RANGE_NUM; i++) {
  61. if ((tsens_config->range_min >= s_tsens_attribute_copy[i].range_min) && (tsens_config->range_max <= s_tsens_attribute_copy[i].range_max)) {
  62. tsens->tsens_attribute = &s_tsens_attribute_copy[i];
  63. break;
  64. }
  65. }
  66. ESP_RETURN_ON_FALSE(tsens->tsens_attribute != NULL, ESP_ERR_INVALID_ARG, TAG, "Out of testing range");
  67. return ESP_OK;
  68. }
  69. esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_config, temperature_sensor_handle_t *ret_tsens)
  70. {
  71. #if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG
  72. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  73. #endif
  74. esp_err_t ret = ESP_OK;
  75. ESP_RETURN_ON_FALSE((tsens_config && ret_tsens), ESP_ERR_INVALID_ARG, TAG, "Invalid argument");
  76. ESP_RETURN_ON_FALSE((s_tsens_attribute_copy == NULL), ESP_ERR_INVALID_STATE, TAG, "Already installed");
  77. temperature_sensor_handle_t tsens = NULL;
  78. tsens = (temperature_sensor_obj_t *) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT);
  79. ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "no mem for temp sensor");
  80. tsens->clk_src = tsens_config->clk_src;
  81. periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
  82. periph_module_reset(PERIPH_TEMPSENSOR_MODULE);
  83. ESP_GOTO_ON_ERROR(temperature_sensor_attribute_table_sort(), err, TAG, "Table sort failed");
  84. ESP_GOTO_ON_ERROR(temperature_sensor_choose_best_range(tsens, tsens_config), err, TAG, "Cannot select the correct range");
  85. ESP_LOGI(TAG, "Range [%d°C ~ %d°C], error < %d°C",
  86. tsens->tsens_attribute->range_min,
  87. tsens->tsens_attribute->range_max,
  88. tsens->tsens_attribute->error_max);
  89. TEMPERATURE_SENSOR_ENTER_CRITICAL();
  90. temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
  91. temperature_sensor_ll_enable(false); // disable the sensor by default
  92. TEMPERATURE_SENSOR_EXIT_CRITICAL();
  93. tsens->fsm = TEMP_SENSOR_FSM_INIT;
  94. *ret_tsens = tsens;
  95. return ESP_OK;
  96. err:
  97. temperature_sensor_uninstall(tsens);
  98. return ret;
  99. }
  100. esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
  101. {
  102. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  103. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
  104. if (s_tsens_attribute_copy) {
  105. free(s_tsens_attribute_copy);
  106. }
  107. s_tsens_attribute_copy = NULL;
  108. periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
  109. free(tsens);
  110. return ESP_OK;
  111. }
  112. esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
  113. {
  114. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  115. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
  116. #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
  117. if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
  118. periph_rtc_dig_clk8m_enable();
  119. }
  120. #endif
  121. temperature_sensor_ll_clk_enable(true);
  122. temperature_sensor_ll_clk_sel(tsens->clk_src);
  123. temperature_sensor_ll_enable(true);
  124. tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
  125. return ESP_OK;
  126. }
  127. esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens)
  128. {
  129. ESP_RETURN_ON_FALSE(tsens, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  130. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
  131. temperature_sensor_ll_enable(false);
  132. #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
  133. if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
  134. periph_rtc_dig_clk8m_disable();
  135. }
  136. #endif
  137. tsens->fsm = TEMP_SENSOR_FSM_INIT;
  138. return ESP_OK;
  139. }
  140. static esp_err_t read_delta_t_from_efuse(void)
  141. {
  142. if (esp_efuse_rtc_calib_get_tsens_val(&s_deltaT) != ESP_OK) {
  143. ESP_LOGW(TAG, "Calibration failed");
  144. }
  145. ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
  146. return ESP_OK;
  147. }
  148. static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
  149. {
  150. if (isnan(s_deltaT)) { //suggests that the value is not initialized
  151. read_delta_t_from_efuse();
  152. }
  153. 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;
  154. return result;
  155. }
  156. esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius)
  157. {
  158. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has not been installed");
  159. ESP_RETURN_ON_FALSE(out_celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "Celsius points to nothing");
  160. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
  161. uint32_t tsens_out = temperature_sensor_ll_get_raw_value();
  162. ESP_LOGV(TAG, "tsens_out %d", tsens_out);
  163. *out_celsius = parse_temp_sensor_raw_value(tsens_out, tsens->tsens_attribute->offset);
  164. if (*out_celsius < tsens->tsens_attribute->range_min || *out_celsius > tsens->tsens_attribute->range_max) {
  165. ESP_LOGW(TAG, "value out of range, probably invalid");
  166. return ESP_FAIL;
  167. }
  168. return ESP_OK;
  169. }