sar_periph_ctrl_common.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "soc/soc_caps.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "esp_private/sar_periph_ctrl.h"
  9. #include "esp_log.h"
  10. #if SOC_TEMP_SENSOR_SUPPORTED
  11. #include "hal/temperature_sensor_ll.h"
  12. #include "soc/temperature_sensor_periph.h"
  13. #include "soc/periph_defs.h"
  14. #include "esp_private/periph_ctrl.h"
  15. extern __attribute__((unused)) portMUX_TYPE rtc_spinlock;
  16. /*------------------------------------------------------------------------------------------------------------
  17. -----------------------------------------Temperature Sensor---------------------------------------------------
  18. ------------------------------------------------------------------------------------------------------------*/
  19. static const char *TAG_TSENS = "temperature_sensor";
  20. #define INT_NOT_USED 999999
  21. static int s_record_min = INT_NOT_USED;
  22. static int s_record_max = INT_NOT_USED;
  23. static int s_temperature_sensor_power_cnt;
  24. static uint8_t s_tsens_idx = 2; // Index for temperature attribute, set 2(middle) as default value
  25. void temperature_sensor_power_acquire(void)
  26. {
  27. portENTER_CRITICAL(&rtc_spinlock);
  28. s_temperature_sensor_power_cnt++;
  29. if (s_temperature_sensor_power_cnt == 1) {
  30. periph_module_enable(PERIPH_TEMPSENSOR_MODULE);
  31. periph_module_reset(PERIPH_TEMPSENSOR_MODULE);
  32. regi2c_saradc_enable();
  33. temperature_sensor_ll_clk_enable(true);
  34. temperature_sensor_ll_enable(true);
  35. }
  36. portEXIT_CRITICAL(&rtc_spinlock);
  37. }
  38. void temperature_sensor_power_release(void)
  39. {
  40. portENTER_CRITICAL(&rtc_spinlock);
  41. s_temperature_sensor_power_cnt--;
  42. /* Sanity check */
  43. if (s_temperature_sensor_power_cnt < 0) {
  44. portEXIT_CRITICAL(&rtc_spinlock);
  45. ESP_LOGE(TAG_TSENS, "%s called, but s_temperature_sensor_power_cnt == 0", __func__);
  46. abort();
  47. } else if (s_temperature_sensor_power_cnt == 0) {
  48. temperature_sensor_ll_clk_enable(false);
  49. temperature_sensor_ll_enable(false);
  50. regi2c_saradc_disable();
  51. periph_module_disable(PERIPH_TEMPSENSOR_MODULE);
  52. }
  53. portEXIT_CRITICAL(&rtc_spinlock);
  54. }
  55. static int temperature_sensor_get_raw_value(void)
  56. {
  57. int raw_value = temperature_sensor_ll_get_raw_value();
  58. return (TEMPERATURE_SENSOR_LL_ADC_FACTOR * raw_value - TEMPERATURE_SENSOR_LL_DAC_FACTOR * temperature_sensor_attributes[s_tsens_idx].offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR);
  59. }
  60. void temp_sensor_sync_tsens_idx(int tsens_idx)
  61. {
  62. s_tsens_idx = tsens_idx;
  63. }
  64. int16_t temp_sensor_get_raw_value(bool *range_changed)
  65. {
  66. portENTER_CRITICAL(&rtc_spinlock);
  67. int degree = temperature_sensor_get_raw_value();
  68. uint8_t temperature_dac;
  69. // 1. Check whether temperature value is in range
  70. if (s_record_min != INT_NOT_USED && degree >= s_record_min && degree <= s_record_max) {
  71. // If degree is in range, not needed to do any check to save time. Otherwise, choose proper range and record.
  72. if (range_changed != NULL) {
  73. *range_changed = false;
  74. }
  75. portEXIT_CRITICAL(&rtc_spinlock);
  76. return degree;
  77. }
  78. // 2. If temperature value is not in range, adjust to proper range
  79. if (degree >= temperature_sensor_attributes[1].range_max) {
  80. s_tsens_idx = 0;
  81. } else if (degree >= temperature_sensor_attributes[2].range_max && degree < temperature_sensor_attributes[1].range_max) {
  82. s_tsens_idx = 1;
  83. } else if (degree <= temperature_sensor_attributes[2].range_min && degree > temperature_sensor_attributes[3].range_min) {
  84. s_tsens_idx = 3;
  85. } else if (degree <= temperature_sensor_attributes[3].range_min) {
  86. s_tsens_idx = 4;
  87. } else {
  88. s_tsens_idx = 2;
  89. }
  90. ESP_EARLY_LOGD(TAG_TSENS, "range changed, change to index %d", s_tsens_idx);
  91. temperature_dac = temperature_sensor_attributes[s_tsens_idx].reg_val;
  92. s_record_min = temperature_sensor_attributes[s_tsens_idx].range_min;
  93. s_record_max = temperature_sensor_attributes[s_tsens_idx].range_max;
  94. temperature_sensor_ll_set_range(temperature_dac);
  95. // 3. Then, read value again
  96. // Before reading the temperature value, ticks need to be delayed, otherwise a wrong value will be returned.
  97. // As what has been recommended and tested, 300us is a good interval to get the correct value after adjust range.
  98. esp_rom_delay_us(300);
  99. degree = temperature_sensor_get_raw_value();
  100. if (range_changed != NULL) {
  101. *range_changed = true;
  102. }
  103. portEXIT_CRITICAL(&rtc_spinlock);
  104. return degree;
  105. }
  106. #endif