Skip to content

Commit

Permalink
esp32: Fix ADC.
Browse files Browse the repository at this point in the history
Signed-off-by: IhorNehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa authored and IhorNehrutsa committed Jan 9, 2024
1 parent da7f408 commit cab3d4e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ports/esp32/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions ports/esp32/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cab3d4e

Please sign in to comment.