ソースを参照

esp_adc: support adc calibration on esp32c2

Armando 3 年 前
コミット
ccfbba5994

+ 91 - 3
components/efuse/esp32c2/esp_efuse_rtc_calib.c

@@ -5,14 +5,102 @@
  */
 
 #include <esp_bit_defs.h>
+#include "esp_log.h"
 #include "esp_efuse.h"
 #include "esp_efuse_table.h"
+#include "esp_efuse_rtc_calib.h"
+#include "hal/adc_types.h"
 
 int esp_efuse_rtc_calib_get_ver(void)
 {
-    uint32_t result = 0;
-    esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MINOR, &result, ESP_EFUSE_BLK_VERSION_MINOR[0]->bit_count); // IDF-5366
-    return result;
+    uint32_t blk_ver_major = 0;
+    esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MAJOR, &blk_ver_major, ESP_EFUSE_BLK_VERSION_MAJOR[0]->bit_count); // IDF-5366
+
+    uint32_t cali_version = (blk_ver_major == 0) ? ESP_EFUSE_ADC_CALIB_VER : 0;
+    if (!cali_version) {
+        ESP_LOGW("eFuse", "calibration efuse version does not match, set default version to 0");
+    }
+
+    return cali_version;
+}
+
+uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
+{
+    assert(version == ESP_EFUSE_ADC_CALIB_VER);
+    assert(atten <= ADC_ATTEN_DB_11);
+    (void) adc_unit;
+
+    if (atten == ADC_ATTEN_DB_2_5 || atten == ADC_ATTEN_DB_6) {
+        /**
+         * - ESP32C2 only supports HW calibration on ADC_ATTEN_DB_0 and ADC_ATTEN_DB_11
+         * - For other attenuation, we just return default value, which is 0.
+         */
+        return 0;
+    }
+
+    int32_t adc_icode_diff_atten0 = 0;
+    int32_t adc_icode_diff_atten3 = 0;
+    int efuse_icode_bits = 0;
+
+    efuse_icode_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_INIT_CODE_ATTEN0);
+    ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_INIT_CODE_ATTEN0, &adc_icode_diff_atten0, efuse_icode_bits));
+    adc_icode_diff_atten0 = ((adc_icode_diff_atten0 & BIT(7)) != 0) ? -(adc_icode_diff_atten0 & 0x7f): adc_icode_diff_atten0;
+
+    efuse_icode_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_INIT_CODE_ATTEN3);
+    ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_INIT_CODE_ATTEN3, &adc_icode_diff_atten3, efuse_icode_bits));
+
+    ESP_EARLY_LOGV("eFuse", "adc_icode_diff_atten0: 0d%"PRId32", adc_icode_diff_atten3: 0d%"PRId32, adc_icode_diff_atten0, adc_icode_diff_atten3);
+
+    uint32_t init_code = 0;
+    if (atten == ADC_ATTEN_DB_0) {
+        init_code = adc_icode_diff_atten0 + 2160;
+    } else {
+        //ADC_ATTEN_DB_11
+        init_code = adc_icode_diff_atten3 + adc_icode_diff_atten0 + 2160;
+    }
+
+    return init_code;
+}
+
+esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, int atten, uint32_t *out_digi, uint32_t *out_vol_mv)
+{
+    assert(version == ESP_EFUSE_ADC_CALIB_VER);
+    assert(atten <= ADC_ATTEN_DB_11);
+    (void) adc_unit;
+
+    if (atten == ADC_ATTEN_DB_2_5 || atten == ADC_ATTEN_DB_6) {
+        /**
+         * - ESP32C2 only supports SW calibration on ADC_ATTEN_DB_0 and ADC_ATTEN_DB_11
+         * - For other attenuation, we need to return an error, informing upper layer SW calibration driver
+         *   to deal with the error.
+         */
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    int32_t adc_vol_diff_atten0 = 0;
+    int32_t adc_vol_diff_atten3 = 0;
+    int efuse_vol_bits = 0;
+
+    efuse_vol_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_CAL_VOL_ATTEN0);
+    ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_CAL_VOL_ATTEN0, &adc_vol_diff_atten0, efuse_vol_bits));
+    adc_vol_diff_atten0 = ((adc_vol_diff_atten0 & BIT(7)) != 0) ? -(adc_vol_diff_atten0 & 0x7f): adc_vol_diff_atten0;
+
+    efuse_vol_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_CAL_VOL_ATTEN3);
+    ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_CAL_VOL_ATTEN3, &adc_vol_diff_atten3, efuse_vol_bits));
+    adc_vol_diff_atten3 = ((adc_vol_diff_atten3 & BIT(5)) != 0) ? -(adc_vol_diff_atten3 & 0x1f): adc_vol_diff_atten3;
+
+    ESP_EARLY_LOGV("eFuse", "adc_vol_diff_atten0: 0d%"PRId32", adc_vol_diff_atten3: 0d%"PRId32, adc_vol_diff_atten0, adc_vol_diff_atten3);
+
+    if (atten == ADC_ATTEN_DB_0) {
+        *out_digi = adc_vol_diff_atten0 + 1540;
+        *out_vol_mv = 400;
+    } else {
+        //ADC_ATTEN_DB_11
+        *out_digi = adc_vol_diff_atten0 + 1540 - adc_vol_diff_atten3 - 123;
+        *out_vol_mv = 1370;
+    }
+
+    return ESP_OK;
 }
 
 esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal)

+ 34 - 0
components/efuse/esp32c2/include/esp_efuse_rtc_calib.h

@@ -11,6 +11,40 @@
 extern "C" {
 #endif
 
+//This is the ADC calibration value version burnt in efuse
+#define ESP_EFUSE_ADC_CALIB_VER     1
+
+/**
+ * @brief Get the RTC calibration efuse version
+ *
+ * @return Version of the stored efuse
+ */
+int esp_efuse_rtc_calib_get_ver(void);
+
+/**
+ * @brief Get the init code in the efuse, for the corresponding attenuation.
+ *
+ * @param version       Version of the stored efuse
+ * @param adc_unit      ADC unit
+ * @param atten         Attenuation of the init code
+ * @return The init code stored in efuse
+ */
+uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten);
+
+/**
+ * @brief Get the calibration digits stored in the efuse, and the corresponding voltage.
+ *
+ * @param version       Version of the stored efuse
+ * @param adc_unit      ADC unit
+ * @param atten         Attenuation to use
+ * @param out_digi      Output buffer of the digits
+ * @param out_vol_mv    Output of the voltage, in mV
+ * @return
+ *      - ESP_ERR_INVALID_ARG: If `ADC_ATTEN_DB_2_5` or `ADC_ATTEN_DB_6` is used
+ *      - ESP_OK: if success
+ */
+esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, int atten, uint32_t *out_digi, uint32_t *out_vol_mv);
+
 /**
  * @brief Get the temperature sensor calibration number delta_T stored in the efuse.
  *

+ 133 - 0
components/esp_adc/esp32c2/adc_cali_line_fitting.c

@@ -0,0 +1,133 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include "esp_types.h"
+#include "esp_err.h"
+#include "esp_log.h"
+#include "esp_check.h"
+#include "esp_heap_caps.h"
+#include "esp_efuse_rtc_calib.h"
+#include "soc/soc_caps.h"
+#include "esp_adc/adc_cali_scheme.h"
+#include "adc_cali_interface.h"
+
+
+/**
+ * This file contains Line Fitting Calibration Scheme for ESP32C2.
+ *
+ * If ESP EFuse Line Fitting Calibration Scheme on future chips are similar to the scheme in this file, we can:
+ *
+ * 1. Rename this file to `adc_cali_line_fitting_v2.c`, as the Line Fitting Scheme on ESP32 and ESP32S2 are different to this.
+ * 2. Move this file to common directory
+ * 3. Still support `ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED`
+ * 4. Add a new internal maccro `ADC_CALI_SCHEME_LINE_FITTING_V2_SUPPORTED`
+ * 5. Only build this file, when `ADC_CALI_SCHEME_LINE_FITTING_V2_SUPPORTED == true`
+ */
+
+
+// coeff_a is actually a float number
+// it is scaled to put them into uint32_t so that the headers do not have to be changed
+static const int coeff_a_scaling = 65536;
+const static char *TAG = "adc_cali";
+
+typedef struct {
+    adc_unit_t unit_id;
+    adc_atten_t atten;
+    uint32_t coeff_a;    ///< Gradient of ADC-Voltage characteristics
+    uint32_t coeff_b;    ///< Offset of ADC-Voltage characteristics
+} cali_chars_line_fitting_t;
+
+
+/* ------------------------ Interface Functions --------------------------- */
+static esp_err_t cali_raw_to_voltage(void *arg, int raw, int *voltage);
+static esp_err_t check_valid(const adc_cali_line_fitting_config_t *config);
+
+/* ------------------------- Public API ------------------------------------- */
+esp_err_t adc_cali_create_scheme_line_fitting(const adc_cali_line_fitting_config_t *config, adc_cali_handle_t *ret_handle)
+{
+    esp_err_t ret = ESP_OK;
+    ESP_RETURN_ON_FALSE(config && ret_handle, ESP_ERR_INVALID_ARG, TAG, "invalid arg: null pointer");
+    ret = check_valid(config);
+    if (ret != ESP_OK) {
+        return ret;
+    }
+
+    //current version only accepts encoding version: `ESP_EFUSE_ADC_CALIB_VER`.
+    uint8_t adc_cali_version = esp_efuse_rtc_calib_get_ver();
+    ESP_RETURN_ON_FALSE(adc_cali_version == ESP_EFUSE_ADC_CALIB_VER, ESP_ERR_NOT_SUPPORTED, TAG, "Calibration required eFuse bits not burnt");
+
+    adc_cali_scheme_t *scheme = (adc_cali_scheme_t *)heap_caps_calloc(1, sizeof(adc_cali_scheme_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
+    ESP_RETURN_ON_FALSE(scheme, ESP_ERR_NO_MEM, TAG, "no mem for adc calibration scheme");
+
+    cali_chars_line_fitting_t *chars = (cali_chars_line_fitting_t *)heap_caps_calloc(1, sizeof(cali_chars_line_fitting_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
+    ESP_GOTO_ON_FALSE(chars, ESP_ERR_NO_MEM, err, TAG, "no memory for the calibration characteristics");
+
+    scheme->raw_to_voltage = cali_raw_to_voltage;
+    scheme->ctx = chars;
+
+    chars->unit_id = config->unit_id;
+    chars->atten = config->atten;
+
+    uint32_t voltage_mv = 0;
+    uint32_t digi_val = 0;
+    esp_efuse_rtc_calib_get_cal_voltage(adc_cali_version, chars->unit_id, chars->atten, &digi_val, &voltage_mv);
+    assert(ret == ESP_OK);
+    chars->coeff_a = coeff_a_scaling * voltage_mv / digi_val;
+    chars->coeff_b = 0;
+    ESP_LOGV(TAG, "Calib V1, Cal Voltage = %"PRId32", Digi out = %"PRId32", Coef_a = %"PRId32"\n", voltage_mv, digi_val, chars->coeff_a);
+
+    *ret_handle = scheme;
+
+    return ESP_OK;
+
+err:
+    if (scheme) {
+        free(scheme);
+    }
+    return ret;
+}
+
+esp_err_t adc_cali_delete_scheme_line_fitting(adc_cali_handle_t handle)
+{
+    ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
+
+    free(handle->ctx);
+    handle->ctx = NULL;
+
+    free(handle);
+    handle = NULL;
+
+    return ESP_OK;
+}
+
+
+/* ------------------------ Interface Functions --------------------------- */
+static esp_err_t cali_raw_to_voltage(void *arg, int raw, int *voltage)
+{
+    //pointers are checked in the upper layer
+
+    cali_chars_line_fitting_t *ctx = arg;
+    *voltage = raw * ctx->coeff_a / coeff_a_scaling + ctx->coeff_b;
+
+    return ESP_OK;
+}
+
+static esp_err_t check_valid(const adc_cali_line_fitting_config_t *config)
+{
+    ESP_RETURN_ON_FALSE(config->unit_id < SOC_ADC_PERIPH_NUM, ESP_ERR_INVALID_ARG, TAG, "invalid ADC unit");
+    ESP_RETURN_ON_FALSE((config->atten == ADC_ATTEN_DB_0 || config->atten == ADC_ATTEN_DB_11), ESP_ERR_NOT_SUPPORTED, TAG, "only ADC_ATTEN_DB_0 and ADC_ATTEN_DB_11 are supported");
+
+    bool available_oneshot_bitwidth = (config->bitwidth >= SOC_ADC_RTC_MIN_BITWIDTH && config->bitwidth <= SOC_ADC_RTC_MAX_BITWIDTH);
+    bool available_dma_bitwidth = (config->bitwidth >= SOC_ADC_DIGI_MIN_BITWIDTH && config->bitwidth <= SOC_ADC_DIGI_MAX_BITWIDTH);
+    bool default_bitwidth_mark = (config->bitwidth == ADC_BITWIDTH_DEFAULT);
+    bool available_bitwidth = (available_oneshot_bitwidth || available_dma_bitwidth || default_bitwidth_mark);
+    ESP_RETURN_ON_FALSE(available_bitwidth, ESP_ERR_INVALID_ARG, TAG, "invalid bitwidth");
+
+    return ESP_OK;
+}

+ 1 - 1
components/esp_adc/esp32c2/include/adc_cali_schemes.h

@@ -12,4 +12,4 @@
  * @brief Supported calibration schemes
  */
 
-//No scheme supported
+#define ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED    1

+ 5 - 1
components/hal/adc_hal_common.c

@@ -92,7 +92,9 @@ void adc_hal_calibration_init(adc_unit_t adc_n)
     adc_ll_calibration_init(adc_n);
 }
 
-static uint32_t s_previous_init_code[SOC_ADC_PERIPH_NUM] = {-1, -1};
+static uint32_t s_previous_init_code[SOC_ADC_PERIPH_NUM] = {
+    [0 ... (SOC_ADC_PERIPH_NUM - 1)] = -1,
+};
 
 void adc_hal_set_calibration_param(adc_unit_t adc_n, uint32_t param)
 {
@@ -145,10 +147,12 @@ static uint32_t read_cal_channel(adc_unit_t adc_n)
 
 uint32_t adc_hal_self_calibration(adc_unit_t adc_n, adc_atten_t atten, bool internal_gnd)
 {
+#if SOC_ADC_ARBITER_SUPPORTED
     if (adc_n == ADC_UNIT_2) {
         adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
         adc_hal_arbiter_config(&config);
     }
+#endif // #if SOC_ADC_ARBITER_SUPPORTED
 
     cal_setup(adc_n, atten);
 

+ 17 - 40
components/hal/esp32c2/include/hal/adc_ll.h

@@ -133,7 +133,6 @@ static inline void adc_ll_digi_output_invert(adc_unit_t adc_n, bool inv_en)
  */
 static inline void adc_ll_digi_controller_clk_div(uint32_t div_num, uint32_t div_b, uint32_t div_a)
 {
-    abort();  //TODO IDF-3908
     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.saradc_apb_adc_clkm_conf, saradc_reg_clkm_div_num, div_num);
     APB_SARADC.saradc_apb_adc_clkm_conf.saradc_reg_clkm_div_b = div_b;
     APB_SARADC.saradc_apb_adc_clkm_conf.saradc_reg_clkm_div_a = div_a;
@@ -316,12 +315,8 @@ static inline void adc_ll_set_controller(adc_unit_t adc_n, adc_ll_controller_t c
 __attribute__((always_inline))
 static inline void adc_ll_calibration_init(adc_unit_t adc_n)
 {
-    abort();  //TODO IDF-3908
-    // if (adc_n == ADC_UNIT_1) {
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_DREF_ADDR, 1);
-    // } else {
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_DREF_ADDR, 1);
-    // }
+    (void)adc_n;
+    REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_DREF_ADDR, 1);
 }
 
 /**
@@ -330,27 +325,18 @@ static inline void adc_ll_calibration_init(adc_unit_t adc_n)
  * @note  Different ADC units and different attenuation options use different calibration data (initial data).
  *
  * @param adc_n ADC index number.
- * @param channel adc channel number.
  * @param internal_gnd true:  Disconnect from the IO port and use the internal GND as the calibration voltage.
  *                     false: Use IO external voltage as calibration voltage.
  */
-static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, adc_channel_t channel, bool internal_gnd)
+static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, bool internal_gnd)
 {
-    abort();  //TODO IDF-3908
-    // /* Enable/disable internal connect GND (for calibration). */
-    // if (adc_n == ADC_UNIT_1) {
-    //     if (internal_gnd) {
-    //         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 1);
-    //     } else {
-    //         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
-    //     }
-    // } else {
-    //     if (internal_gnd) {
-    //         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 1);
-    //     } else {
-    //         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
-    //     }
-    // }
+    (void)adc_n;
+    /* Enable/disable internal connect GND (for calibration). */
+    if (internal_gnd) {
+        REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 1);
+    } else {
+        REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
+    }
 }
 
 /**
@@ -360,12 +346,8 @@ static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, adc_channel_t ch
  */
 static inline void adc_ll_calibration_finish(adc_unit_t adc_n)
 {
-    abort();  //TODO IDF-3908
-    // if (adc_n == ADC_UNIT_1) {
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
-    // } else {
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
-    // }
+    (void)adc_n;
+    REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
 }
 
 /**
@@ -378,16 +360,11 @@ static inline void adc_ll_calibration_finish(adc_unit_t adc_n)
 __attribute__((always_inline))
 static inline void adc_ll_set_calibration_param(adc_unit_t adc_n, uint32_t param)
 {
-    abort();  //TODO IDF-3908
-    // uint8_t msb = param >> 8;
-    // uint8_t lsb = param & 0xFF;
-    // if (adc_n == ADC_UNIT_1) {
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, msb);
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, lsb);
-    // } else {
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_HIGH_ADDR, msb);
-    //     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_LOW_ADDR, lsb);
-    // }
+    (void)adc_n;
+    uint8_t msb = param >> 8;
+    uint8_t lsb = param & 0xFF;
+    REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, msb);
+    REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, lsb);
 }
 
 /*---------------------------------------------------------------

+ 4 - 0
components/soc/esp32c2/include/soc/Kconfig.soc_caps.in

@@ -147,6 +147,10 @@ config SOC_RTC_SLOW_CLOCK_SUPPORT_8MD256
     bool
     default y
 
+config SOC_ADC_CALIBRATION_V1_SUPPORTED
+    bool
+    default y
+
 config SOC_BROWNOUT_RESET_SUPPORTED
     bool
     default y

+ 3 - 0
components/soc/esp32c2/include/soc/soc_caps.h

@@ -73,6 +73,9 @@
 #define SOC_ADC_RTC_MAX_BITWIDTH                (12)
 #define SOC_RTC_SLOW_CLOCK_SUPPORT_8MD256       (1)
 
+/*!< Calibration */
+#define SOC_ADC_CALIBRATION_V1_SUPPORTED        (1) /*!< support HW offset calibration version 1*/
+
 /*-------------------------- BROWNOUT CAPS -----------------------------------*/
 #define SOC_BROWNOUT_RESET_SUPPORTED 1