Commit 74d04c02 authored by Angus Gratton's avatar Angus Gratton Committed by Damien George

esp32/adc: Use new ADC calibration API in all cases.

Replaces the deprecated ESP32 calibration API with the "line" method
instead.

This work was funded through GitHub Sponsors.
Signed-off-by: default avatarAngus Gratton <angus@redyak.com.au>
parent 052693e4
......@@ -63,22 +63,6 @@ void madcblock_bits_helper(machine_adc_block_obj_t *self, mp_int_t bits) {
if (self->unit_id == ADC_UNIT_1) {
adc1_config_width(self->width);
}
for (adc_atten_t atten = ADC_ATTEN_DB_0; atten < ADC_ATTEN_MAX; atten++) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 1)
if (self->handle[atten] != NULL) {
adc_cali_curve_fitting_config_t cali_config = {
.unit_id = self->unit_id,
.atten = atten,
.bitwidth = self->width,
};
check_esp_err(adc_cali_create_scheme_curve_fitting(&cali_config, self->handle[atten]));
}
#else
if (self->characteristics[atten] != NULL) {
esp_adc_cal_characterize(self->unit_id, atten, self->width, DEFAULT_VREF, self->characteristics[atten]);
}
#endif
}
}
mp_int_t madcblock_read_helper(machine_adc_block_obj_t *self, adc_channel_t channel_id) {
......@@ -93,30 +77,34 @@ mp_int_t madcblock_read_helper(machine_adc_block_obj_t *self, adc_channel_t chan
return raw;
}
mp_int_t madcblock_read_uv_helper(machine_adc_block_obj_t *self, adc_channel_t channel_id, adc_atten_t atten) {
int raw = madcblock_read_helper(self, channel_id);
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 1)
adc_cali_handle_t *adc_handle = self->handle[atten];
if (adc_handle == NULL) {
adc_cali_curve_fitting_config_t cali_config = {
.unit_id = self->unit_id,
.atten = atten,
.bitwidth = self->width,
};
adc_handle = malloc(sizeof(adc_cali_handle_t));
check_esp_err(adc_cali_create_scheme_curve_fitting(&cali_config, adc_handle));
self->handle[atten] = adc_handle;
static esp_err_t ensure_adc_calibration(machine_adc_block_obj_t *self, adc_atten_t atten) {
if (self->handle[atten] != NULL) {
return ESP_OK;
}
int uv;
check_esp_err(adc_cali_raw_to_voltage(*adc_handle, raw, &uv));
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
adc_cali_curve_fitting_config_t cali_config = {
.unit_id = self->unit_id,
.atten = atten,
.bitwidth = self->width,
};
return adc_cali_create_scheme_curve_fitting(&cali_config, &self->handle[atten]);
#else
esp_adc_cal_characteristics_t *adc_chars = self->characteristics[atten];
if (adc_chars == NULL) {
adc_chars = malloc(sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_characterize(self->unit_id, atten, self->width, DEFAULT_VREF, adc_chars);
self->characteristics[atten] = adc_chars;
}
mp_int_t uv = esp_adc_cal_raw_to_voltage(raw, adc_chars);
adc_cali_line_fitting_config_t cali_config = {
.unit_id = self->unit_id,
.atten = atten,
.bitwidth = self->width,
};
return adc_cali_create_scheme_line_fitting(&cali_config, &self->handle[atten]);
#endif
}
mp_int_t madcblock_read_uv_helper(machine_adc_block_obj_t *self, adc_channel_t channel_id, adc_atten_t atten) {
int raw = madcblock_read_helper(self, channel_id);
int uv;
check_esp_err(ensure_adc_calibration(self, atten));
check_esp_err(adc_cali_raw_to_voltage(self->handle[atten], raw, &uv));
return (mp_int_t)uv * 1000;
}
......@@ -39,11 +39,7 @@ typedef struct _machine_adc_block_obj_t {
adc_unit_t unit_id;
mp_int_t bits;
adc_bits_width_t width;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 1)
adc_cali_handle_t *handle[ADC_ATTEN_MAX];
#else
esp_adc_cal_characteristics_t *characteristics[ADC_ATTEN_MAX];
#endif
adc_cali_handle_t handle[ADC_ATTEN_MAX];
} machine_adc_block_obj_t;
typedef struct _machine_adc_obj_t {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment