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_table.h"
22 
23 #ifdef __ZEPHYR__
24 #include <zephyr/kernel.h>
25 #endif /* __ZEPHYR__ */
26 
27 static const char *TAG = "tsens";
28 
29 #define TSENS_XPD_WAIT_DEFAULT 0xFF   /* Set wait cycle time(8MHz) from power up to reset enable. */
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 set_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     esp_err_t err = ESP_OK;
71     if (tsens_hw_state == TSENS_HW_STATE_STARTED) {
72         ESP_LOGE(TAG, "Do not configure the temp sensor when it's running!");
73         err = ESP_ERR_INVALID_STATE;
74     }
75     CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PD_M);
76     SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PU_M);
77     CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
78     SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
79     REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val);
80     SENS.sar_tctrl.tsens_clk_div = tsens.clk_div;
81     SENS.sar_tctrl.tsens_power_up_force = 1;
82     SENS.sar_tctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT;
83     SENS.sar_tctrl2.tsens_xpd_force = 1;
84     SENS.sar_tctrl2.tsens_reset = 1;// Reset the temp sensor.
85     SENS.sar_tctrl2.tsens_reset = 0;// Clear the reset status.
86     ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C",
87              dac_offset[tsens.dac_offset].range_min,
88              dac_offset[tsens.dac_offset].range_max,
89              dac_offset[tsens.dac_offset].error_max);
90     tsens_hw_state = TSENS_HW_STATE_CONFIGURED;
91     return err;
92 }
93 
temp_sensor_get_config(temp_sensor_config_t * tsens)94 esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)
95 {
96     ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified");
97     CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PD_M);
98     SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PU_M);
99     CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M);
100     SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M);
101     tsens->dac_offset = REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC);
102     for (int i = TSENS_DAC_L0; i < TSENS_DAC_MAX; i++) {
103         if ((int)tsens->dac_offset == dac_offset[i].set_val) {
104             tsens->dac_offset = dac_offset[i].index;
105             break;
106         }
107     }
108     tsens->clk_div = SENS.sar_tctrl.tsens_clk_div;
109     return ESP_OK;
110 }
111 
temp_sensor_start(void)112 esp_err_t temp_sensor_start(void)
113 {
114     esp_err_t err = ESP_OK;
115     if (tsens_hw_state != TSENS_HW_STATE_CONFIGURED) {
116         ESP_LOGE(TAG, "Temperature sensor is already running or not be configured");
117         err = ESP_ERR_INVALID_STATE;
118     }
119 #ifndef __ZEPHYR__
120     if (rtc_tsens_mux == NULL) {
121         rtc_tsens_mux = xSemaphoreCreateMutex();
122     }
123     ESP_RETURN_ON_FALSE(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM, TAG, "failed to create mutex");
124 #endif /* __ZEPHYR__ */
125     SENS.sar_tctrl.tsens_dump_out = 0;
126     SENS.sar_tctrl2.tsens_clkgate_en = 1;
127     SENS.sar_tctrl.tsens_power_up = 1;
128     tsens_hw_state = TSENS_HW_STATE_STARTED;
129     return err;
130 }
131 
temp_sensor_stop(void)132 esp_err_t temp_sensor_stop(void)
133 {
134     SENS.sar_tctrl.tsens_power_up = 0;
135     SENS.sar_tctrl2.tsens_clkgate_en = 0;
136 #ifndef __ZEPHYR__
137     if (rtc_tsens_mux != NULL) {
138         vSemaphoreDelete(rtc_tsens_mux);
139         rtc_tsens_mux = NULL;
140     }
141 #endif /* __ZEPHYR__ */
142     return ESP_OK;
143 }
144 
temp_sensor_read_raw(uint32_t * tsens_out)145 esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)
146 {
147     ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens_out specified");
148 #ifndef __ZEPHYR__
149     ESP_RETURN_ON_FALSE(rtc_tsens_mux != NULL, ESP_ERR_INVALID_STATE, TAG, "mutex not ready");
150     xSemaphoreTake(rtc_tsens_mux, portMAX_DELAY);
151 #else
152     k_mutex_lock(&rtc_tsens_mux, K_FOREVER);
153 #endif /* __ZEPHYR__ */
154     SENS.sar_tctrl.tsens_dump_out = 1;
155     while (!SENS.sar_tctrl.tsens_ready);
156     *tsens_out = SENS.sar_tctrl.tsens_out;
157     SENS.sar_tctrl.tsens_dump_out = 0;
158 #ifndef __ZEPHYR__
159     xSemaphoreGive(rtc_tsens_mux);
160 #else
161     k_mutex_unlock(&rtc_tsens_mux);
162 #endif /* __ZEPHYR__ */
163     return ESP_OK;
164 }
165 
read_delta_t_from_efuse(void)166 static void read_delta_t_from_efuse(void)
167 {
168     uint32_t version = esp_efuse_rtc_table_read_calib_version();
169     if (version == 1 || version == 2) {
170         // fetch calibration value for temp sensor from eFuse
171         s_deltaT = esp_efuse_rtc_table_get_parsed_efuse_value(RTCCALIB_IDX_TMPSENSOR, false) / 10.0f;
172     } else {
173         // no value to fetch, use 0.
174         s_deltaT = 0;
175     }
176     ESP_LOGD(TAG, "s_deltaT = %f\n", s_deltaT);
177 }
178 
parse_temp_sensor_raw_value(uint32_t tsens_raw,const int dac_offset)179 static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset)
180 {
181     if (isnan(s_deltaT)) { //suggests that the value is not initialized
182         read_delta_t_from_efuse();
183     }
184     float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT;
185     return result;
186 }
187 
temp_sensor_read_celsius(float * celsius)188 esp_err_t temp_sensor_read_celsius(float *celsius)
189 {
190     ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing");
191     temp_sensor_config_t tsens;
192     uint32_t tsens_out = 0;
193     esp_err_t ret = temp_sensor_get_config(&tsens);
194     if (ret == ESP_OK) {
195         ret = temp_sensor_read_raw(&tsens_out);
196         ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data");
197         const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset];
198         *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset);
199         if (*celsius < dac->range_min || *celsius > dac->range_max) {
200             ESP_LOGW(TAG, "Exceeding the temperature range!");
201             ret = ESP_ERR_INVALID_STATE;
202         }
203     }
204     return ret;
205 }
206