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 
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <math.h>
18 #include "esp_types.h"
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/semphr.h"
21 #include "esp_log.h"
22 #include "soc/rtc_cntl_reg.h"
23 #include "soc/rtc_io_reg.h"
24 #include "soc/rtc_io_struct.h"
25 #include "soc/sens_reg.h"
26 #include "soc/sens_struct.h"
27 #include "driver/temp_sensor.h"
28 #include "regi2c_ctrl.h"
29 #include "esp_log.h"
30 #include "esp32s2/esp_efuse_rtc_table.h"
31 
32 static const char *TAG = "tsens";
33 
34 #define TSENS_CHECK(res, ret_val) ({                                    \
35     if (!(res)) {                                                       \
36         ESP_LOGE(TAG, "%s(%d)", __FUNCTION__, __LINE__);                \
37         return (ret_val);                                               \
38     }                                                                   \
39 })
40 #define TSENS_XPD_WAIT_DEFAULT 0xFF   /* Set wait cycle time(8MHz) from power up to reset enable. */
41 #define TSENS_ADC_FACTOR  (0.4386)
42 #define TSENS_DAC_FACTOR  (27.88)
43 #define TSENS_SYS_OFFSET  (20.52)
44 
45 typedef struct {
46     int index;
47     int offset;
48     int set_val;
49     int range_min;
50     int range_max;
51     int error_max;
52 } tsens_dac_offset_t;
53 
54 static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
55     /*     DAC     Offset reg_val  min  max  error */
56     {TSENS_DAC_L0,   -2,     5,    50,  125,   3},
57     {TSENS_DAC_L1,   -1,     7,    20,  100,   2},
58     {TSENS_DAC_L2,    0,    15,   -10,   80,   1},
59     {TSENS_DAC_L3,    1,    11,   -30,   50,   2},
60     {TSENS_DAC_L4,    2,    10,   -40,   20,   3},
61 };
62 
63 static SemaphoreHandle_t rtc_tsens_mux = NULL;
64 
65 static float s_deltaT = NAN; // Unused number
66 
temp_sensor_set_config(temp_sensor_config_t tsens)67 esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
68 {
69     CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PD_M);
70     SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PU_M);
71     CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
72     SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
73     REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val);
74     SENS.sar_tctrl.tsens_clk_div = tsens.clk_div;
75     SENS.sar_tctrl.tsens_power_up_force = 1;
76     SENS.sar_tctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT;
77     SENS.sar_tctrl2.tsens_xpd_force = 1;
78     SENS.sar_tctrl2.tsens_reset = 1;// Reset the temp sensor.
79     SENS.sar_tctrl2.tsens_reset = 0;// Clear the reset status.
80     ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C",
81              dac_offset[tsens.dac_offset].range_min,
82              dac_offset[tsens.dac_offset].range_max,
83              dac_offset[tsens.dac_offset].error_max);
84     return ESP_OK;
85 }
86 
temp_sensor_get_config(temp_sensor_config_t * tsens)87 esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)
88 {
89     TSENS_CHECK(tsens != NULL, ESP_ERR_INVALID_ARG);
90     CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PD_M);
91     SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PU_M);
92     CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
93     SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
94     tsens->dac_offset = REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC);
95     for (int i = TSENS_DAC_L0; i < TSENS_DAC_MAX; i++) {
96         if ((int)tsens->dac_offset == dac_offset[i].set_val) {
97             tsens->dac_offset = dac_offset[i].index;
98             break;
99         }
100     }
101     tsens->clk_div = SENS.sar_tctrl.tsens_clk_div;
102     return ESP_OK;
103 }
104 
temp_sensor_start(void)105 esp_err_t temp_sensor_start(void)
106 {
107     if (rtc_tsens_mux == NULL) {
108         rtc_tsens_mux = xSemaphoreCreateMutex();
109     }
110     TSENS_CHECK(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM);
111     SENS.sar_tctrl.tsens_dump_out = 0;
112     SENS.sar_tctrl2.tsens_clkgate_en = 1;
113     SENS.sar_tctrl.tsens_power_up = 1;
114     return ESP_OK;
115 }
116 
temp_sensor_stop(void)117 esp_err_t temp_sensor_stop(void)
118 {
119     SENS.sar_tctrl.tsens_power_up = 0;
120     SENS.sar_tctrl2.tsens_clkgate_en = 0;
121     if (rtc_tsens_mux != NULL) {
122         vSemaphoreDelete(rtc_tsens_mux);
123         rtc_tsens_mux = NULL;
124     }
125     return ESP_OK;
126 }
127 
temp_sensor_read_raw(uint32_t * tsens_out)128 esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
129 {
130     TSENS_CHECK(tsens_out != NULL, ESP_ERR_INVALID_ARG);
131     TSENS_CHECK(rtc_tsens_mux != NULL, ESP_ERR_INVALID_STATE);
132     xSemaphoreTake(rtc_tsens_mux, portMAX_DELAY);
133     SENS.sar_tctrl.tsens_dump_out = 1;
134     while (!SENS.sar_tctrl.tsens_ready);
135     *tsens_out = SENS.sar_tctrl.tsens_out;
136     SENS.sar_tctrl.tsens_dump_out = 0;
137     xSemaphoreGive(rtc_tsens_mux);
138     return ESP_OK;
139 }
140 
read_delta_t_from_efuse(void)141 static void read_delta_t_from_efuse(void)
142 {
143     uint32_t version = esp_efuse_rtc_table_read_calib_version();
144     if (version == 1 || version == 2) {
145         // fetch calibration value for temp sensor from eFuse
146         s_deltaT = esp_efuse_rtc_table_get_parsed_efuse_value(RTCCALIB_IDX_TMPSENSOR, false) / 10.0;
147     } else {
148         // no value to fetch, use 0.
149         s_deltaT = 0;
150     }
151     ESP_LOGD(TAG, "s_deltaT = %f\n", s_deltaT);
152 }
153 
parse_temp_sensor_raw_value(uint32_t tsens_raw,const int dac_offset)154 static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
155 {
156     if (isnan(s_deltaT)) { //suggests that the value is not initialized
157         read_delta_t_from_efuse();
158     }
159     float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT;
160     return result;
161 }
162 
temp_sensor_read_celsius(float * celsius)163 esp_err_t temp_sensor_read_celsius(float *celsius)
164 {
165     TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG);
166     temp_sensor_config_t tsens;
167     uint32_t tsens_out = 0;
168     esp_err_t ret = temp_sensor_get_config(&tsens);
169     if (ret == ESP_OK) {
170         ret = temp_sensor_read_raw(&tsens_out);
171         TSENS_CHECK(ret == ESP_OK, ret);
172         const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
173         *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset);
174         if (*celsius < dac->range_min || *celsius > dac->range_max) {
175             ESP_LOGW(TAG, "Exceeding the temperature range!");
176             ret = ESP_ERR_INVALID_STATE;
177         }
178     }
179     return ret;
180 }
181