1 /*
2  * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <math.h>
10 #include "esp_types.h"
11 #include "esp_log.h"
12 #include "esp_check.h"
13 #include "soc/rtc_cntl_reg.h"
14 #include "soc/rtc_io_reg.h"
15 #include "soc/rtc_io_struct.h"
16 #include "soc/sens_reg.h"
17 #include "soc/sens_struct.h"
18 #include "driver/temp_sensor.h"
19 #include "regi2c_ctrl.h"
20 #include "esp_log.h"
21 #include "esp_efuse_rtc_calib.h"
22 #include "hal/misc.h"
23 
24 #ifdef __ZEPHYR__
25 #include <zephyr/kernel.h>
26 #endif /* __ZEPHYR__ */
27 
28 static const char *TAG = "tsens";
29 
30 #define TSENS_ADC_FACTOR  (0.4386f)
31 #define TSENS_DAC_FACTOR  (27.88f)
32 #define TSENS_SYS_OFFSET  (20.52f)
33 
34 typedef struct {
35     int index;
36     int offset;
37     int reg_val;
38     int range_min;
39     int range_max;
40     int error_max;
41 } tsens_dac_offset_t;
42 
43 static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = {
44     /*     DAC     Offset reg_val  min  max  error */
45     {TSENS_DAC_L0,   -2,     5,    50,  125,   3},
46     {TSENS_DAC_L1,   -1,     7,    20,  100,   2},
47     {TSENS_DAC_L2,    0,    15,   -10,   80,   1},
48     {TSENS_DAC_L3,    1,    11,   -30,   50,   2},
49     {TSENS_DAC_L4,    2,    10,   -40,   20,   3},
50 };
51 
52 typedef enum {
53     TSENS_HW_STATE_UNCONFIGURED,
54     TSENS_HW_STATE_CONFIGURED,
55     TSENS_HW_STATE_STARTED,
56 } tsens_hw_state_t;
57 
58 static tsens_hw_state_t tsens_hw_state = TSENS_HW_STATE_UNCONFIGURED;
59 
60 #ifdef __ZEPHYR__
61 K_MUTEX_DEFINE(rtc_tsens_mux);
62 #else
63 static SemaphoreHandle_t rtc_tsens_mux = NULL;
64 #endif /* __ZEPHYR__ */
65 
66 static float s_deltaT = NAN; // Unused number
67 
temp_sensor_set_config(temp_sensor_config_t tsens)68 esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)
69 {
70     if (tsens_hw_state == TSENS_HW_STATE_STARTED) {
71         ESP_LOGE(TAG, "Do not configure the temp sensor when it's running!");
72         return ESP_ERR_INVALID_STATE;
73     }
74     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_clk_div, tsens.clk_div);
75     REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].reg_val);
76     tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
77     return ESP_OK;
78 }
79 
temp_sensor_get_config(temp_sensor_config_t * tsens)80 esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)
81 {
82     ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified");
83 
84     tsens->dac_offset = REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC);
85     for (int i = TSENS_DAC_L0; i < TSENS_DAC_MAX; i++) {
86         if ((int)tsens->dac_offset == dac_offset[i].reg_val) {
87             tsens->dac_offset = dac_offset[i].index;
88             break;
89         }
90     }
91     HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_tctrl, tsens_clk_div);
92     return ESP_OK;
93 }
94 
temp_sensor_start(void)95 esp_err_t temp_sensor_start(void)
96 {
97     if (tsens_hw_state != TSENS_HW_STATE_CONFIGURED) {
98         ESP_LOGE(TAG, "Is already running or not be configured");
99         return ESP_ERR_INVALID_STATE;
100     }
101 #ifndef __ZEPHYR__
102     if (rtc_tsens_mux == NULL) {
103         rtc_tsens_mux = xSemaphoreCreateMutex();
104     }
105     ESP_RETURN_ON_FALSE(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM, TAG, "failed to create mutex");
106 #endif /* __ZEPHYR__ */
107     CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
108     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_peri_clk_gate_conf, tsens_clk_en, 1);
109     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_power_up_force, 1);
110     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl2, tsens_xpd_force, 1);
111     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_power_up, 1);
112     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_dump_out, 0);
113     tsens_hw_state = TSENS_HW_STATE_STARTED;
114     return ESP_OK;
115 }
116 
temp_sensor_stop(void)117 esp_err_t temp_sensor_stop(void)
118 {
119     CLEAR_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
120     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_power_up_force, 0);
121     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl2, tsens_xpd_force, 0);
122     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_power_up, 0);
123 #ifndef __ZEPHYR__
124     if (rtc_tsens_mux != NULL) {
125         vSemaphoreDelete(rtc_tsens_mux);
126         rtc_tsens_mux = NULL;
127     }
128 #endif /* __ZEPHYR__ */
129     tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
130     return ESP_OK;
131 }
132 
temp_sensor_read_raw(uint32_t * tsens_out)133 esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
134 {
135     ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens_out specified");
136 #ifndef __ZEPHYR__
137     ESP_RETURN_ON_FALSE(rtc_tsens_mux != NULL, ESP_ERR_INVALID_STATE, TAG, "mutex not ready");
138     xSemaphoreTake(rtc_tsens_mux, portMAX_DELAY);
139 #else
140     k_mutex_lock(&rtc_tsens_mux, K_FOREVER);
141 #endif /* __ZEPHYR__ */
142     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_dump_out, 1);
143     while (!HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_tctrl, tsens_ready));
144     *tsens_out = HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_tctrl, tsens_out);
145     HAL_FORCE_MODIFY_U32_REG_FIELD(SENS.sar_tctrl, tsens_dump_out, 0);
146 #ifndef __ZEPHYR__
147     xSemaphoreGive(rtc_tsens_mux);
148 #else
149     k_mutex_unlock(&rtc_tsens_mux);
150 #endif /* __ZEPHYR__ */
151     return ESP_OK;
152 }
153 
read_delta_t_from_efuse(void)154 static void read_delta_t_from_efuse(void)
155 {
156     uint32_t version = esp_efuse_rtc_calib_get_ver();
157     if (version == 1) {
158         // fetch calibration value for temp sensor from eFuse
159         s_deltaT = esp_efuse_rtc_calib_get_cal_temp(version) / 10.0f;
160     } else {
161         // no value to fetch, use 0.
162         s_deltaT = 0;
163     }
164     ESP_LOGD(TAG, "s_deltaT = %f\n", s_deltaT);
165 }
166 
parse_temp_sensor_raw_value(uint32_t tsens_raw,const int dac_offset)167 static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
168 {
169     if (isnan(s_deltaT)) { //suggests that the value is not initialized
170         read_delta_t_from_efuse();
171     }
172     float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT;
173     return result;
174 }
175 
temp_sensor_read_celsius(float * celsius)176 esp_err_t temp_sensor_read_celsius(float *celsius)
177 {
178     ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
179     if (tsens_hw_state != TSENS_HW_STATE_STARTED) {
180         ESP_LOGE(TAG, "Has not been started");
181         return ESP_ERR_INVALID_STATE;
182     }
183     temp_sensor_config_t tsens;
184     uint32_t tsens_out = 0;
185     esp_err_t ret = temp_sensor_get_config(&tsens);
186     if (ret == ESP_OK) {
187         ret = temp_sensor_read_raw(&tsens_out);
188         ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data");
189         const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
190         *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset);
191         if (*celsius < dac->range_min || *celsius > dac->range_max) {
192             ESP_LOGW(TAG, "Exceeding the temperature range!");
193             ret = ESP_ERR_INVALID_STATE;
194         }
195     }
196     return ret;
197 }
198 
199