|
| 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 "freertos/FreeRTOS.h" |
| 12 | +#include "freertos/semphr.h" |
| 13 | +#include "esp_log.h" |
| 14 | +#include "esp_check.h" |
| 15 | +#include "soc/rtc_cntl_reg.h" |
| 16 | +#include "soc/rtc_io_reg.h" |
| 17 | +#include "soc/rtc_io_struct.h" |
| 18 | +#include "soc/sens_reg.h" |
| 19 | +#include "soc/sens_struct.h" |
| 20 | +#include "driver/temp_sensor.h" |
| 21 | +#include "regi2c_ctrl.h" |
| 22 | +#include "esp_log.h" |
| 23 | +#include "esp_efuse_rtc_calib.h" |
| 24 | + |
| 25 | +static const char *TAG = "tsens"; |
| 26 | + |
| 27 | +#define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */ |
| 28 | +#define TSENS_ADC_FACTOR (0.4386) |
| 29 | +#define TSENS_DAC_FACTOR (27.88) |
| 30 | +#define TSENS_SYS_OFFSET (20.52) |
| 31 | + |
| 32 | +typedef struct { |
| 33 | + int index; |
| 34 | + int offset; |
| 35 | + int set_val; |
| 36 | + int range_min; |
| 37 | + int range_max; |
| 38 | + int error_max; |
| 39 | +} tsens_dac_offset_t; |
| 40 | + |
| 41 | +static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { |
| 42 | + /* DAC Offset reg_val min max error */ |
| 43 | + {TSENS_DAC_L0, -2, 5, 50, 125, 3}, |
| 44 | + {TSENS_DAC_L1, -1, 7, 20, 100, 2}, |
| 45 | + {TSENS_DAC_L2, 0, 15, -10, 80, 1}, |
| 46 | + {TSENS_DAC_L3, 1, 11, -30, 50, 2}, |
| 47 | + {TSENS_DAC_L4, 2, 10, -40, 20, 3}, |
| 48 | +}; |
| 49 | + |
| 50 | +typedef enum { |
| 51 | + TSENS_HW_STATE_UNCONFIGURED, |
| 52 | + TSENS_HW_STATE_CONFIGURED, |
| 53 | + TSENS_HW_STATE_STARTED, |
| 54 | +} tsens_hw_state_t; |
| 55 | + |
| 56 | +static tsens_hw_state_t tsens_hw_state = TSENS_HW_STATE_UNCONFIGURED; |
| 57 | + |
| 58 | +static float s_deltaT = NAN; // Unused number |
| 59 | + |
| 60 | +esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens) |
| 61 | +{ |
| 62 | + esp_err_t err = ESP_OK; |
| 63 | + if (tsens_hw_state == TSENS_HW_STATE_STARTED) { |
| 64 | + ESP_LOGE(TAG, "Do not configure the temp sensor when it's running!"); |
| 65 | + err = ESP_ERR_INVALID_STATE; |
| 66 | + } |
| 67 | + CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M); |
| 68 | + SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); |
| 69 | + REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val); |
| 70 | + SENS.sar_tctrl.tsens_clk_div = tsens.clk_div; |
| 71 | + SENS.sar_tctrl.tsens_power_up_force = 1; |
| 72 | + SENS.sar_tctrl.tsens_power_up = 1; |
| 73 | + SENS.sar_tctrl2.tsens_xpd_force = 1; |
| 74 | + ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C", |
| 75 | + dac_offset[tsens.dac_offset].range_min, |
| 76 | + dac_offset[tsens.dac_offset].range_max, |
| 77 | + dac_offset[tsens.dac_offset].error_max); |
| 78 | + tsens_hw_state = TSENS_HW_STATE_CONFIGURED; |
| 79 | + return err; |
| 80 | +} |
| 81 | + |
| 82 | +esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens) |
| 83 | +{ |
| 84 | + ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified"); |
| 85 | + CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M); |
| 86 | + SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); |
| 87 | + tsens->dac_offset = REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC); |
| 88 | + for (int i = TSENS_DAC_L0; i < TSENS_DAC_MAX; i++) { |
| 89 | + if ((int)tsens->dac_offset == dac_offset[i].set_val) { |
| 90 | + tsens->dac_offset = dac_offset[i].index; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + tsens->clk_div = SENS.sar_tctrl.tsens_clk_div; |
| 95 | + return ESP_OK; |
| 96 | +} |
| 97 | + |
| 98 | +esp_err_t temp_sensor_start(void) |
| 99 | +{ |
| 100 | + esp_err_t err = ESP_OK; |
| 101 | + if (tsens_hw_state != TSENS_HW_STATE_CONFIGURED) { |
| 102 | + ESP_LOGE(TAG, "Temperature sensor is already running or not be configured"); |
| 103 | + err = ESP_ERR_INVALID_STATE; |
| 104 | + } |
| 105 | + SENS.sar_tctrl.tsens_dump_out = 0; |
| 106 | + SENS.sar_tctrl.tsens_power_up = 1; |
| 107 | + SENS.sar_peri_clk_gate_conf.tsens_clk_en = 1; |
| 108 | + tsens_hw_state = TSENS_HW_STATE_STARTED; |
| 109 | + return err; |
| 110 | +} |
| 111 | + |
| 112 | +esp_err_t temp_sensor_stop(void) |
| 113 | +{ |
| 114 | + SENS.sar_tctrl.tsens_power_up = 0; |
| 115 | + tsens_hw_state = TSENS_HW_STATE_CONFIGURED; |
| 116 | + return ESP_OK; |
| 117 | +} |
| 118 | + |
| 119 | +esp_err_t temp_sensor_read_raw(uint32_t *tsens_out) |
| 120 | +{ |
| 121 | + ESP_RETURN_ON_FALSE(tsens_out != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens_out specified"); |
| 122 | + SENS.sar_tctrl.tsens_dump_out = 1; |
| 123 | + while (!SENS.sar_tctrl.tsens_ready); |
| 124 | + *tsens_out = SENS.sar_tctrl.tsens_out; |
| 125 | + SENS.sar_tctrl.tsens_dump_out = 0; |
| 126 | + return ESP_OK; |
| 127 | +} |
| 128 | + |
| 129 | +static void read_delta_t_from_efuse(void) |
| 130 | +{ |
| 131 | + uint32_t version = esp_efuse_rtc_calib_get_ver(); |
| 132 | + if (version == 1) { |
| 133 | + // fetch calibration value for temp sensor from eFuse |
| 134 | + s_deltaT = esp_efuse_rtc_calib_get_cal_temp(version); |
| 135 | + } else { |
| 136 | + // no value to fetch, use 0. |
| 137 | + s_deltaT = 0; |
| 138 | + } |
| 139 | + ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT); |
| 140 | +} |
| 141 | + |
| 142 | +static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset) |
| 143 | +{ |
| 144 | + if (isnan(s_deltaT)) { //suggests that the value is not initialized |
| 145 | + read_delta_t_from_efuse(); |
| 146 | + } |
| 147 | + float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT; |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +esp_err_t temp_sensor_read_celsius(float *celsius) |
| 152 | +{ |
| 153 | + ESP_RETURN_ON_FALSE(celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "celsius points to nothing"); |
| 154 | + temp_sensor_config_t tsens; |
| 155 | + uint32_t tsens_out = 0; |
| 156 | + esp_err_t ret = temp_sensor_get_config(&tsens); |
| 157 | + if (ret == ESP_OK) { |
| 158 | + ret = temp_sensor_read_raw(&tsens_out); |
| 159 | + ESP_RETURN_ON_FALSE(ret == ESP_OK, ret, TAG, "failed to read raw data"); |
| 160 | + const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset]; |
| 161 | + *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset); |
| 162 | + if (*celsius < dac->range_min || *celsius > dac->range_max) { |
| 163 | + ESP_LOGW(TAG, "Exceeding the temperature range!"); |
| 164 | + ret = ESP_ERR_INVALID_STATE; |
| 165 | + } |
| 166 | + } |
| 167 | + return ret; |
| 168 | +} |
0 commit comments