rtc_tempsensor.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <esp_types.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/semphr.h"
  19. #include "esp_log.h"
  20. #include "soc/rtc_io_reg.h"
  21. #include "soc/rtc_io_struct.h"
  22. #include "soc/sens_reg.h"
  23. #include "soc/sens_struct.h"
  24. #include "temp_sensor.h"
  25. #include "esp32s2beta/rom/ets_sys.h"
  26. static const char *TAG = "tsens";
  27. #define TSENS_CHECK(res, ret_val) ({ \
  28. if (!(res)) { \
  29. ESP_LOGE(TAG, "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__); \
  30. return (ret_val); \
  31. } \
  32. })
  33. #define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */
  34. #define TSENS_ADC_FACTOR (0.4386)
  35. #define TSENS_DAC_FACTOR (27.88)
  36. #define TSENS_SYS_OFFSET (20.52)
  37. typedef struct {
  38. int index;
  39. int offset;
  40. int set_val;
  41. int range_min;
  42. int range_max;
  43. int error_max;
  44. } tsens_dac_offset_t;
  45. static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
  46. /* DAC Offset reg_val min max error */
  47. {TSENS_DAC_L0, -2, 5, 50, 125, 3},
  48. {TSENS_DAC_L1, -1, 7, 20, 100, 2},
  49. {TSENS_DAC_L2, 0, 15, -10, 80, 1},
  50. {TSENS_DAC_L3, 1, 11, -30, 50, 2},
  51. {TSENS_DAC_L4, 2, 10, -40, 20, 3},
  52. };
  53. static SemaphoreHandle_t rtc_tsens_mux = NULL;
  54. esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
  55. {
  56. SENS.sar_tctrl.tsens_dac = dac_offset[tsens.dac_offset].set_val;
  57. SENS.sar_tctrl.tsens_clk_div = tsens.clk_div;
  58. SENS.sar_tctrl.tsens_power_up_force = 1;
  59. SENS.sar_tctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT;
  60. SENS.sar_tctrl2.tsens_xpd_force = 1;
  61. SENS.sar_tctrl2.tsens_reset = 1;// Reset the temp sensor.
  62. SENS.sar_tctrl2.tsens_reset = 0;// Clear the reset status.
  63. ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C",
  64. dac_offset[tsens.dac_offset].range_min,
  65. dac_offset[tsens.dac_offset].range_max,
  66. dac_offset[tsens.dac_offset].error_max);
  67. return ESP_OK;
  68. }
  69. esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)
  70. {
  71. TSENS_CHECK(tsens != NULL, ESP_ERR_INVALID_ARG);
  72. tsens->dac_offset = SENS.sar_tctrl.tsens_dac;
  73. for(int i=TSENS_DAC_L0; i<TSENS_DAC_MAX; i++) {
  74. if(tsens->dac_offset == dac_offset[i].set_val) {
  75. tsens->dac_offset = dac_offset[i].index;
  76. break;
  77. }
  78. }
  79. tsens->clk_div = SENS.sar_tctrl.tsens_clk_div;
  80. return ESP_OK;
  81. }
  82. esp_err_t temp_sensor_start(void)
  83. {
  84. if (rtc_tsens_mux == NULL) {
  85. rtc_tsens_mux = xSemaphoreCreateMutex();
  86. }
  87. TSENS_CHECK(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM);
  88. SENS.sar_tctrl.tsens_dump_out = 0;
  89. SENS.sar_tctrl2.tsens_clkgate_en = 1;
  90. SENS.sar_tctrl.tsens_power_up = 1;
  91. return ESP_OK;
  92. }
  93. esp_err_t temp_sensor_stop(void)
  94. {
  95. SENS.sar_tctrl.tsens_power_up = 0;
  96. SENS.sar_tctrl2.tsens_clkgate_en = 0;
  97. if (rtc_tsens_mux != NULL) {
  98. vSemaphoreDelete(rtc_tsens_mux);
  99. rtc_tsens_mux = NULL;
  100. }
  101. return ESP_OK;
  102. }
  103. esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
  104. {
  105. TSENS_CHECK(tsens_out != NULL, ESP_ERR_INVALID_ARG);
  106. TSENS_CHECK(rtc_tsens_mux != NULL, ESP_ERR_INVALID_STATE);
  107. xSemaphoreTake(rtc_tsens_mux, portMAX_DELAY);
  108. SENS.sar_tctrl.tsens_dump_out = 1;
  109. while (!SENS.sar_tctrl.tsens_ready);
  110. *tsens_out = SENS.sar_tctrl.tsens_out;
  111. SENS.sar_tctrl.tsens_dump_out = 0;
  112. xSemaphoreGive(rtc_tsens_mux);
  113. return ESP_OK;
  114. }
  115. esp_err_t temp_sensor_read_celsius(float *celsius)
  116. {
  117. TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG);
  118. temp_sensor_config_t tsens;
  119. uint32_t tsens_out = 0;
  120. esp_err_t ret = temp_sensor_get_config(&tsens);
  121. if (ret == ESP_OK) {
  122. ret = temp_sensor_read_raw(&tsens_out);
  123. TSENS_CHECK(ret == ESP_OK, ret);
  124. const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
  125. *celsius = (TSENS_ADC_FACTOR * (float)tsens_out - TSENS_DAC_FACTOR * dac->offset - TSENS_SYS_OFFSET);
  126. if (*celsius < dac->range_min || *celsius > dac->range_max) {
  127. ESP_LOGW(TAG, "Exceeding the temperature range!");
  128. ret = ESP_ERR_INVALID_STATE;
  129. }
  130. }
  131. return ret;
  132. }