temperature_sensor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 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 "temperature_sensor_private.h"
  27. #include "hal/temperature_sensor_ll.h"
  28. #include "soc/temperature_sensor_periph.h"
  29. #include "esp_memory_utils.h"
  30. #include "esp_private/sar_periph_ctrl.h"
  31. static const char *TAG = "temperature_sensor";
  32. static float s_deltaT = NAN; // unused number
  33. #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  34. static int8_t s_temperature_regval_2_celsius(temperature_sensor_handle_t tsens, uint8_t regval);
  35. #endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  36. static temperature_sensor_attribute_t *s_tsens_attribute_copy;
  37. static int inline accuracy_compare(const void *p1, const void *p2)
  38. {
  39. return ((*(temperature_sensor_attribute_t *)p1).error_max < (*(temperature_sensor_attribute_t *)p2).error_max) ? -1 : 1;
  40. }
  41. static esp_err_t temperature_sensor_attribute_table_sort(void)
  42. {
  43. s_tsens_attribute_copy = (temperature_sensor_attribute_t *)heap_caps_malloc(sizeof(temperature_sensor_attributes), TEMPERATURE_SENSOR_MEM_ALLOC_CAPS);
  44. ESP_RETURN_ON_FALSE(s_tsens_attribute_copy != NULL, ESP_ERR_NO_MEM, TAG, "No space for s_tsens_attribute_copy");
  45. for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
  46. s_tsens_attribute_copy[i] = temperature_sensor_attributes[i];
  47. }
  48. // Sort from small to large by error_max.
  49. qsort(s_tsens_attribute_copy, TEMPERATURE_SENSOR_ATTR_RANGE_NUM, sizeof(s_tsens_attribute_copy[0]), accuracy_compare);
  50. return ESP_OK;
  51. }
  52. static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_t tsens, const temperature_sensor_config_t *tsens_config)
  53. {
  54. for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
  55. if ((tsens_config->range_min >= s_tsens_attribute_copy[i].range_min) && (tsens_config->range_max <= s_tsens_attribute_copy[i].range_max)) {
  56. tsens->tsens_attribute = &s_tsens_attribute_copy[i];
  57. break;
  58. }
  59. temp_sensor_sync_tsens_idx(i);
  60. }
  61. ESP_RETURN_ON_FALSE(tsens->tsens_attribute != NULL, ESP_ERR_INVALID_ARG, TAG, "Out of testing range");
  62. return ESP_OK;
  63. }
  64. #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  65. static void IRAM_ATTR temperature_sensor_isr(void *arg)
  66. {
  67. temperature_sensor_ll_clear_intr();
  68. bool cbs_yield = false;
  69. temperature_sensor_handle_t tsens = (temperature_sensor_handle_t) arg;
  70. temperature_val_intr_condition_t intr_condition = (temperature_sensor_ll_get_wakeup_reason() == 1 ? TEMPERATURE_VAL_HIGHER_THAN_HIGH_THRESHOLD: TEMPERATURE_VAL_LOWER_THAN_LOW_THRESHOLD);
  71. temperature_sensor_threshold_event_data_t data = {
  72. .celsius_value = s_temperature_regval_2_celsius(tsens, temperature_sensor_ll_get_raw_value()),
  73. .intr_condition = intr_condition,
  74. };
  75. if (tsens->threshold_cbs) {
  76. if (tsens->threshold_cbs(tsens, &data, tsens->cb_user_arg)) {
  77. cbs_yield = true;
  78. }
  79. }
  80. if (cbs_yield) {
  81. portYIELD_FROM_ISR();
  82. }
  83. }
  84. #endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  85. esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_config, temperature_sensor_handle_t *ret_tsens)
  86. {
  87. #if CONFIG_TEMP_SENSOR_ENABLE_DEBUG_LOG
  88. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  89. #endif
  90. esp_err_t ret = ESP_OK;
  91. ESP_RETURN_ON_FALSE((tsens_config && ret_tsens), ESP_ERR_INVALID_ARG, TAG, "Invalid argument");
  92. ESP_RETURN_ON_FALSE((s_tsens_attribute_copy == NULL), ESP_ERR_INVALID_STATE, TAG, "Already installed");
  93. temperature_sensor_handle_t tsens = NULL;
  94. tsens = (temperature_sensor_obj_t *) heap_caps_calloc(1, sizeof(temperature_sensor_obj_t), MALLOC_CAP_DEFAULT);
  95. ESP_GOTO_ON_FALSE(tsens != NULL, ESP_ERR_NO_MEM, err, TAG, "no mem for temp sensor");
  96. tsens->clk_src = tsens_config->clk_src;
  97. ESP_GOTO_ON_ERROR(temperature_sensor_attribute_table_sort(), err, TAG, "Table sort failed");
  98. ESP_GOTO_ON_ERROR(temperature_sensor_choose_best_range(tsens, tsens_config), err, TAG, "Cannot select the correct range");
  99. ESP_LOGI(TAG, "Range [%d°C ~ %d°C], error < %d°C",
  100. tsens->tsens_attribute->range_min,
  101. tsens->tsens_attribute->range_max,
  102. tsens->tsens_attribute->error_max);
  103. temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val);
  104. tsens->fsm = TEMP_SENSOR_FSM_INIT;
  105. *ret_tsens = tsens;
  106. return ESP_OK;
  107. err:
  108. temperature_sensor_uninstall(tsens);
  109. return ret;
  110. }
  111. esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens)
  112. {
  113. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  114. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
  115. if (s_tsens_attribute_copy) {
  116. free(s_tsens_attribute_copy);
  117. }
  118. s_tsens_attribute_copy = NULL;
  119. #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  120. temperature_sensor_ll_enable_intr(false);
  121. if (tsens->temp_sensor_isr_handle) {
  122. ESP_RETURN_ON_ERROR(esp_intr_free(tsens->temp_sensor_isr_handle), TAG, "uninstall interrupt service failed");
  123. }
  124. #endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  125. free(tsens);
  126. return ESP_OK;
  127. }
  128. static esp_err_t s_update_tsens_attribute(temperature_sensor_handle_t tsens)
  129. {
  130. uint32_t dac;
  131. ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified");
  132. dac = temperature_sensor_ll_get_offset();
  133. for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) {
  134. if (dac == s_tsens_attribute_copy[i].reg_val) {
  135. tsens->tsens_attribute = &s_tsens_attribute_copy[i];
  136. break;
  137. }
  138. }
  139. return ESP_OK;
  140. }
  141. esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens)
  142. {
  143. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  144. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "tsens not in init state");
  145. #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
  146. if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
  147. periph_rtc_dig_clk8m_enable();
  148. }
  149. #endif
  150. #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  151. temperature_sensor_ll_wakeup_enable(true);
  152. temperature_sensor_ll_sample_enable(true);
  153. #endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  154. temperature_sensor_ll_clk_sel(tsens->clk_src);
  155. temperature_sensor_power_acquire();
  156. tsens->fsm = TEMP_SENSOR_FSM_ENABLE;
  157. return ESP_OK;
  158. }
  159. esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens)
  160. {
  161. ESP_RETURN_ON_FALSE(tsens, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  162. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
  163. temperature_sensor_power_release();
  164. #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  165. temperature_sensor_ll_wakeup_enable(false);
  166. temperature_sensor_ll_sample_enable(false);
  167. #endif
  168. #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC
  169. if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) {
  170. periph_rtc_dig_clk8m_disable();
  171. }
  172. #endif
  173. tsens->fsm = TEMP_SENSOR_FSM_INIT;
  174. return ESP_OK;
  175. }
  176. static esp_err_t read_delta_t_from_efuse(void)
  177. {
  178. if (esp_efuse_rtc_calib_get_tsens_val(&s_deltaT) != ESP_OK) {
  179. ESP_LOGW(TAG, "Calibration failed");
  180. }
  181. ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT);
  182. return ESP_OK;
  183. }
  184. static float parse_temp_sensor_raw_value(int16_t tsens_raw)
  185. {
  186. if (isnan(s_deltaT)) { //suggests that the value is not initialized
  187. read_delta_t_from_efuse();
  188. }
  189. float result = tsens_raw - s_deltaT / 10.0;
  190. return result;
  191. }
  192. esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius)
  193. {
  194. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has not been installed");
  195. ESP_RETURN_ON_FALSE(out_celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "Celsius points to nothing");
  196. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet");
  197. bool range_changed;
  198. int16_t tsens_out = temp_sensor_get_raw_value(&range_changed);
  199. *out_celsius = parse_temp_sensor_raw_value(tsens_out);
  200. if (*out_celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *out_celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) {
  201. ESP_LOGE(TAG, "Exceeding temperature measure range.");
  202. return ESP_ERR_INVALID_STATE;
  203. }
  204. if (range_changed) {
  205. s_update_tsens_attribute(tsens);
  206. }
  207. return ESP_OK;
  208. }
  209. #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
  210. static uint8_t s_temperature_celsius_2_regval(temperature_sensor_handle_t tsens, int8_t celsius)
  211. {
  212. return (uint8_t)((celsius + TEMPERATURE_SENSOR_LL_OFFSET_FACTOR + TEMPERATURE_SENSOR_LL_DAC_FACTOR * tsens->tsens_attribute->offset)/TEMPERATURE_SENSOR_LL_ADC_FACTOR);
  213. }
  214. IRAM_ATTR static int8_t s_temperature_regval_2_celsius(temperature_sensor_handle_t tsens, uint8_t regval)
  215. {
  216. return TEMPERATURE_SENSOR_LL_ADC_FACTOR * regval - TEMPERATURE_SENSOR_LL_DAC_FACTOR * tsens->tsens_attribute->offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR;
  217. }
  218. esp_err_t temperature_sensor_set_absolute_threshold(temperature_sensor_handle_t tsens, const temperature_sensor_abs_threshold_config_t *abs_cfg)
  219. {
  220. esp_err_t ret = ESP_OK;
  221. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Temperature sensor has not been installed");
  222. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in init state");
  223. ESP_RETURN_ON_FALSE(abs_cfg, ESP_ERR_INVALID_ARG, TAG, "Invalid callback configuration");
  224. temperature_sensor_ll_set_sample_rate(0xffff);
  225. temperature_sensor_ll_wakeup_mode(TEMPERATURE_SENSOR_LL_WAKE_ABSOLUTE);
  226. temperature_sensor_ll_set_th_high_val(s_temperature_celsius_2_regval(tsens, abs_cfg->high_threshold));
  227. temperature_sensor_ll_set_th_low_val(s_temperature_celsius_2_regval(tsens, abs_cfg->low_threshold));
  228. return ret;
  229. }
  230. esp_err_t temperature_sensor_set_delta_threshold(temperature_sensor_handle_t tsens, const temperature_sensor_delta_threshold_config_t *delta_cfg)
  231. {
  232. esp_err_t ret = ESP_OK;
  233. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Temperature sensor has not been installed");
  234. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in init state");
  235. ESP_RETURN_ON_FALSE(delta_cfg, ESP_ERR_INVALID_ARG, TAG, "Invalid callback configuration");
  236. temperature_sensor_ll_set_sample_rate(0xffff);
  237. temperature_sensor_ll_wakeup_mode(TEMPERATURE_SENSOR_LL_WAKE_DELTA);
  238. temperature_sensor_ll_set_th_high_val((uint8_t)(delta_cfg->increase_delta / TEMPERATURE_SENSOR_LL_ADC_FACTOR));
  239. temperature_sensor_ll_set_th_low_val((uint8_t)(delta_cfg->decrease_delta / TEMPERATURE_SENSOR_LL_ADC_FACTOR));
  240. return ret;
  241. }
  242. esp_err_t temperature_sensor_register_callbacks(temperature_sensor_handle_t tsens, const temperature_sensor_event_callbacks_t *cbs, void *user_arg)
  243. {
  244. esp_err_t ret = ESP_OK;
  245. ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Temperature sensor has not been installed");
  246. ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "temperature sensor is not in init state");
  247. ESP_RETURN_ON_FALSE(cbs, ESP_ERR_INVALID_ARG, TAG, "callback group pointer is invalid");
  248. #if CONFIG_TEMP_SENSOR_ISR_IRAM_SAFE
  249. if (cbs->on_threshold) {
  250. ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_threshold), ESP_ERR_INVALID_ARG, TAG, "threshold callback not in IRAM");
  251. }
  252. if (user_arg) {
  253. ESP_RETURN_ON_FALSE(esp_ptr_internal(user_arg), ESP_ERR_INVALID_ARG, TAG, "user argument not in internal RAM");
  254. }
  255. #endif
  256. int isr_flags = TEMPERATURE_SENSOR_INTR_ALLOC_FLAGS;
  257. #if SOC_ADC_TEMPERATURE_SHARE_INTR
  258. isr_flags |= ESP_INTR_FLAG_SHARED;
  259. #endif
  260. // lazy install interrupt service.
  261. if (!tsens->temp_sensor_isr_handle) {
  262. ret = esp_intr_alloc_intrstatus(ETS_APB_ADC_INTR_SOURCE, isr_flags,
  263. (uint32_t)temperature_sensor_ll_get_intr_status(),
  264. TEMPERATURE_SENSOR_LL_INTR_MASK, temperature_sensor_isr, tsens, &tsens->temp_sensor_isr_handle);
  265. }
  266. if (cbs->on_threshold != NULL) {
  267. temperature_sensor_ll_enable_intr(true);
  268. temperature_sensor_ll_clear_intr();
  269. tsens->threshold_cbs = cbs->on_threshold;
  270. tsens->cb_user_arg = user_arg;
  271. } else {
  272. temperature_sensor_ll_enable_intr(false);
  273. }
  274. return ret;
  275. }
  276. #endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT