From 9a5006e241cfb8ac905eb8e623ec0a1fe9f71a72 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Tue, 8 Dec 2020 17:30:32 -0500 Subject: [PATCH] Remove problematic calloc --- ports/esp32s2/common-hal/analogio/AnalogIn.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/esp32s2/common-hal/analogio/AnalogIn.c b/ports/esp32s2/common-hal/analogio/AnalogIn.c index bab1721ea7..f33439c4af 100644 --- a/ports/esp32s2/common-hal/analogio/AnalogIn.c +++ b/ports/esp32s2/common-hal/analogio/AnalogIn.c @@ -34,6 +34,8 @@ #include "shared-bindings/microcontroller/Pin.h" +#include + #define DEFAULT_VREF 1100 #define NO_OF_SAMPLES 64 #define ATTENUATION ADC_ATTEN_DB_11 @@ -66,11 +68,14 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { adc1_config_channel_atten((adc1_channel_t)self->pin->adc_channel, ATTENUATION); } else if (self->pin->adc_index == ADC_UNIT_2) { adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION); + } else { + mp_raise_ValueError(translate("Invalid Pin")); } // Automatically select calibration process depending on status of efuse - esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); - esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, adc_chars); + esp_adc_cal_characteristics_t adc_chars; + memset(&adc_chars, 0, sizeof(adc_chars)); + esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, &adc_chars); uint32_t adc_reading = 0; //Multisampling @@ -89,7 +94,7 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { adc_reading /= NO_OF_SAMPLES; // This corrects non-linear regions of the ADC range with a LUT, so it's a better reading than raw - uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); + uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, &adc_chars); return voltage * ((1 << 16) - 1)/3300; }