From cab3d4e758e877307460b33fed46c6a6b2b326ee Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Tue, 9 Jan 2024 14:57:39 +0200 Subject: [PATCH] esp32: Fix ADC. Signed-off-by: IhorNehrutsa --- ports/esp32/adc.c | 6 ++++++ ports/esp32/adc.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/ports/esp32/adc.c b/ports/esp32/adc.c index c5886624ecb5a..094aa9f811d9b 100644 --- a/ports/esp32/adc.c +++ b/ports/esp32/adc.c @@ -62,11 +62,13 @@ 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); } + #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 for (adc_atten_t atten = ADC_ATTEN_DB_0; atten < ADC_ATTEN_MAX; atten++) { 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) { @@ -81,6 +83,7 @@ mp_int_t madcblock_read_helper(machine_adc_block_obj_t *self, adc_channel_t chan 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 CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 esp_adc_cal_characteristics_t *adc_chars = self->characteristics[atten]; if (adc_chars == NULL) { adc_chars = malloc(sizeof(esp_adc_cal_characteristics_t)); @@ -89,4 +92,7 @@ mp_int_t madcblock_read_uv_helper(machine_adc_block_obj_t *self, adc_channel_t c } mp_int_t uv = esp_adc_cal_raw_to_voltage(raw, adc_chars) * 1000; return uv; + #else + return 0; + #endif } diff --git a/ports/esp32/adc.h b/ports/esp32/adc.h index ae5c0d3c378c5..36c8882bc90fa 100644 --- a/ports/esp32/adc.h +++ b/ports/esp32/adc.h @@ -38,7 +38,9 @@ typedef struct _machine_adc_block_obj_t { adc_unit_t unit_id; mp_int_t bits; adc_bits_width_t width; + #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 esp_adc_cal_characteristics_t *characteristics[ADC_ATTEN_MAX]; + #endif } machine_adc_block_obj_t; typedef struct _machine_adc_obj_t {