summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2024-07-23 11:40:45 +1000
committerDamien George <damien@micropython.org>2024-08-14 15:57:26 +1000
commit74d04c026220b301a1966dbb0d6064d06132999e (patch)
tree3cd461f9c158834b55e57fff45b39d1f43b5a4cb
parent052693e4495354daabb4d61e97121283334c6665 (diff)
downloadmicropython-74d04c026220b301a1966dbb0d6064d06132999e.tar.gz
micropython-74d04c026220b301a1966dbb0d6064d06132999e.zip
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: Angus Gratton <angus@redyak.com.au>
-rw-r--r--ports/esp32/adc.c64
-rw-r--r--ports/esp32/adc.h6
2 files changed, 27 insertions, 43 deletions
diff --git a/ports/esp32/adc.c b/ports/esp32/adc.c
index 7c9e0cfad6..91db9ec525 100644
--- a/ports/esp32/adc.c
+++ b/ports/esp32/adc.c
@@ -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;
}
diff --git a/ports/esp32/adc.h b/ports/esp32/adc.h
index 61771255d3..5688e0a29a 100644
--- a/ports/esp32/adc.h
+++ b/ports/esp32/adc.h
@@ -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 {