diff options
| author | Dan Halbert <halbert@adafruit.com> | 2020-12-15 16:31:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-15 16:31:46 -0500 |
| commit | a2d2d699e4d431d4459eae172444bd569559ff02 (patch) | |
| tree | ec70bd9ad59fb43655854be9e2e06f20c5b1c6c0 | |
| parent | afcc00fed4355f9fa8c3da4c28858e6bff21bf4d (diff) | |
| parent | 3ad4b12ce141806e1aaee524a4e3c818ecd3d0d8 (diff) | |
Merge pull request #3810 from hierophect/esp-analog-hang
ESP32-S2: Remove calloc in AnalogIn
| -rw-r--r-- | locale/circuitpython.pot | 6 | ||||
| -rw-r--r-- | ports/esp32s2/common-hal/analogio/AnalogIn.c | 13 |
2 files changed, 14 insertions, 5 deletions
diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 93c8edc44..d64cff185 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-12-14 11:48-0500\n" +"POT-Creation-Date: 2020-12-14 12:59-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" @@ -1080,6 +1080,10 @@ msgstr "" msgid "Invalid PWM frequency" msgstr "" +#: ports/esp32s2/common-hal/analogio/AnalogIn.c +msgid "Invalid Pin" +msgstr "" + #: py/moduerrno.c shared-module/rgbmatrix/RGBMatrix.c msgid "Invalid argument" msgstr "" diff --git a/ports/esp32s2/common-hal/analogio/AnalogIn.c b/ports/esp32s2/common-hal/analogio/AnalogIn.c index bab1721ea..b2c09fc07 100644 --- a/ports/esp32s2/common-hal/analogio/AnalogIn.c +++ b/ports/esp32s2/common-hal/analogio/AnalogIn.c @@ -34,8 +34,10 @@ #include "shared-bindings/microcontroller/Pin.h" +#include <string.h> + #define DEFAULT_VREF 1100 -#define NO_OF_SAMPLES 64 +#define NO_OF_SAMPLES 2 #define ATTENUATION ADC_ATTEN_DB_11 #define DATA_WIDTH ADC_WIDTH_BIT_13 @@ -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; } |
