From 56350778531aca4909ba665e557d2b2afa284942 Mon Sep 17 00:00:00 2001 From: Milind Movasha Date: Fri, 17 Feb 2023 19:19:15 +0530 Subject: [PATCH 01/11] Espressif analogbufio implementation --- .../common-hal/analogbufio/BufferedIn.c | 283 ++++++++++++++++++ .../common-hal/analogbufio/BufferedIn.h | 42 +++ .../common-hal/analogbufio/__init__.c | 1 + ports/espressif/mpconfigport.mk | 1 + 4 files changed, 327 insertions(+) create mode 100644 ports/espressif/common-hal/analogbufio/BufferedIn.c create mode 100644 ports/espressif/common-hal/analogbufio/BufferedIn.h create mode 100644 ports/espressif/common-hal/analogbufio/__init__.c diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c new file mode 100644 index 0000000000..e26afb283e --- /dev/null +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -0,0 +1,283 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2023 Milind Movasha + * + * SPDX-License-Identifier: BSD-3-Clause + * + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "common-hal/analogbufio/BufferedIn.h" +#include "shared-bindings/analogbufio/BufferedIn.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared/runtime/interrupt_char.h" +#include "py/runtime.h" +#include +#include +#include "sdkconfig.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "driver/adc.h" + +//#define DEBUG_ANALOGBUFIO + +#define NUM_SAMPLES_PER_INTERRUPT 256 +#define NUM_ADC_CHANNELS 1 +#define DMA_BUFFER_SIZE 1024 +#define ATTENUATION ADC_ATTEN_DB_0 +#define ADC_READ_TIMEOUT_MS 2000 + +#if defined(CONFIG_IDF_TARGET_ESP32) +#define ADC_RESULT_BYTE 2 +#define ADC_CONV_LIMIT_EN 1 //For ESP32, this should always be set to 1 +#elif defined(CONFIG_IDF_TARGET_ESP32S2) +#define ADC_RESULT_BYTE 2 +#define ADC_CONV_LIMIT_EN 0 +#elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32H2) +#define ADC_RESULT_BYTE 4 +#define ADC_CONV_LIMIT_EN 0 +#elif defined(CONFIG_IDF_TARGET_ESP32S3) +#define ADC_RESULT_BYTE 4 +#define ADC_CONV_LIMIT_EN 0 +#endif + +static adc_digi_convert_mode_t convert_mode = ADC_CONV_SINGLE_UNIT_2; +static adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; +static uint8_t adc_channel = 0; + +void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *self, const mcu_pin_obj_t *pin, uint32_t sample_rate) { + uint16_t adc1_chan_mask = 0; + uint16_t adc2_chan_mask = 0; + + if( self->pin != NULL) { + mp_raise_ValueError(translate("ADC DMA already initialized")); + } + + output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; + if(pin->adc_index == ADC_UNIT_1) { + convert_mode = ADC_CONV_SINGLE_UNIT_1; + } else { + convert_mode = ADC_CONV_SINGLE_UNIT_2; + } + + if (pin->adc_index == NO_ADC || pin->adc_channel == NO_ADC_CHANNEL) { + raise_ValueError_invalid_pin(); + } + + adc_channel = pin->adc_channel; + + /* + * Chip version Conversion Mode Output Format Type + * ESP32 1 TYPE1 + * ESP32S2 1,2,BOTH,ALTER TYPE1, TYPE2 + * ESP32C3 ALTER TYPE2 + * ESP32S3 1,2,BOTH,ALTER TYPE2 + * ESP32H3 1,2,BOTH,ALTER TYPE2 + */ + +#if defined(CONFIG_IDF_TARGET_ESP32) + if(pin->adc_index != ADC_UNIT_1) { + mp_raise_ValueError(translate("ESP32 only supports ADC1 unit")); + } +#endif +#if defined(CONFIG_IDF_TARGET_ESP32S2) +#endif +#if defined(CONFIG_IDF_TARGET_ESP32C3) + //ESP32C3 only supports alter mode + convert_mode = ADC_CONV_ALTER_UNIT; + output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE2; +#endif +#if defined(CONFIG_IDF_TARGET_ESP32S3) +#endif +#if defined(CONFIG_IDF_TARGET_ESP32H2) +#endif + + self->pin = pin; + common_hal_mcu_pin_claim(pin); + + if(pin->adc_index == ADC_UNIT_1) { + adc1_chan_mask = 1 << pin->adc_channel; + } else { + adc2_chan_mask = 1 << pin->adc_channel; + } + + adc_digi_init_config_t adc_dma_config = { + .max_store_buf_size = DMA_BUFFER_SIZE, + .conv_num_each_intr = NUM_SAMPLES_PER_INTERRUPT, + .adc1_chan_mask = adc1_chan_mask, + .adc2_chan_mask = adc2_chan_mask, + }; + +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"pin:%d, ADC channel:%d, ADC index:%d, adc1_chan_mask:0x%x, adc2_chan_mask:0x%x\n",pin->number,pin->adc_channel,pin->adc_index,adc1_chan_mask,adc2_chan_mask); +#endif //DEBUG_ANALOGBUFIO + esp_err_t err = adc_digi_initialize(&adc_dma_config); + if(ESP_OK != err) { + common_hal_analogbufio_bufferedin_deinit(self); + mp_raise_ValueError_varg(translate("Unable to initialize ADC DMA controller, ErrorCode:%d"),err); + } + + adc_digi_configuration_t dig_cfg = { + .conv_limit_en = ADC_CONV_LIMIT_EN, + .conv_limit_num = 250, + .pattern_num = NUM_ADC_CHANNELS, + .sample_freq_hz = sample_rate, + .conv_mode = convert_mode, + .format = output_format, + }; + +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"conversion_mode:%d, format:%d, conv_limit_en:%d, sample_rate:%d\n",convert_mode,output_format,ADC_CONV_LIMIT_EN,sample_rate); +#endif //DEBUG_ANALOGBUFIO + + adc_digi_pattern_config_t adc_pattern[NUM_ADC_CHANNELS] = {0}; + adc_pattern[0].atten = ATTENUATION; + adc_pattern[0].channel = pin->adc_channel; + if(pin->adc_index == ADC_UNIT_1) { + adc_pattern[0].unit = 0; + } else { + adc_pattern[0].unit = 1; + } + adc_pattern[0].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH; + + dig_cfg.adc_pattern = adc_pattern; +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"adc_pattern[0].channel:%d, adc_pattern[0].unit:%d, adc_pattern[0].atten:%d\n",adc_pattern[0].channel,adc_pattern[0].unit,adc_pattern[0].atten); +#endif //DEBUG_ANALOGBUFIO + + err = adc_digi_controller_configure(&dig_cfg); + if(ESP_OK != err) { + common_hal_analogbufio_bufferedin_deinit(self); + mp_raise_ValueError_varg(translate("Unable to configure ADC DMA controller, ErrorCode:%d"),err); + } + err = adc_digi_start(); + if(ESP_OK != err) { + common_hal_analogbufio_bufferedin_deinit(self); + mp_raise_ValueError_varg(translate("Unable to start ADC DMA controller, ErrorCode:%d"),err); + } +} + +bool common_hal_analogbufio_bufferedin_deinited(analogbufio_bufferedin_obj_t *self) { + return self->pin == NULL; +} + +void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self) { + if (common_hal_analogbufio_bufferedin_deinited(self)) { + return; + } + + adc_digi_stop(); + adc_digi_deinitialize(); + + // Release ADC Pin + reset_pin_number(self->pin->number); + self->pin = NULL; +} + +static bool check_valid_data(const adc_digi_output_data_t *data) +{ + unsigned int unit = data->type2.unit; + if(output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE2) { + if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit)) return false; + if (adc_channel != data->type2.channel) return false; + } else { + if( convert_mode == ADC_CONV_SINGLE_UNIT_1 ) { + unit = 0; + } else { + unit = 1; + } + if (data->type1.channel >= SOC_ADC_CHANNEL_NUM(unit)) return false; + if (adc_channel != data->type1.channel) return false; + } + if (unit > 2) return false; + return true; +} + + +uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t *self, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample) { + uint8_t result[NUM_SAMPLES_PER_INTERRUPT] __attribute__ ((aligned (4))) = {0}; + uint32_t captured_samples = 0; + uint32_t captured_bytes = 0; + esp_err_t ret; + uint32_t ret_num = 0; + +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"Required bytes: %d\n",len); +#endif //DEBUG_ANALOGBUFIO + + while(captured_bytes < len) { + ret_num = 0; + ret = adc_digi_read_bytes(result, NUM_SAMPLES_PER_INTERRUPT, &ret_num, ADC_READ_TIMEOUT_MS); + + if (ret == ESP_OK) { + for(uint32_t i=0; itype1.data; + } else { + *(uint16_t *)(void *)&buffer[captured_bytes] = ((adc_digi_output_data_t *) (void *)&result[i])->type2.data; + } + } else { + if(output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1) { + *(uint32_t *)(void *)&buffer[captured_bytes] = ((adc_digi_output_data_t *) (void *)&result[i])->type1.data; + } else { + *(uint32_t *)(void *)&buffer[captured_bytes] = ((adc_digi_output_data_t *) (void *)&result[i])->type2.data; + } + } + captured_bytes += ADC_RESULT_BYTE; + captured_samples++; + } else { + return captured_samples; + } + } else { +#if defined(DEBUG_ANALOGBUFIO) + if(ADC_RESULT_BYTE == 2) { + mp_printf(&mp_plat_print,"Invalid sample received: 0x%0x\n",*(uint16_t *)(void *)&result[i]); + } else { + mp_printf(&mp_plat_print,"Invalid sample received: 0x%0x\n",*(uint32_t *)(void *)&result[i]); + } +#endif //DEBUG_ANALOGBUFIO + return captured_samples; + } + } + } else if (ret == ESP_ERR_TIMEOUT) { +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"ADC Timeout\n"); +#endif //DEBUG_ANALOGBUFIO + return captured_samples; + } else { +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"adc_digi_read_bytes failed error code:%d\n",ret); +#endif //DEBUG_ANALOGBUFIO + return captured_samples; + } + } +#if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"Captured bytes: %d\n",captured_bytes); +#endif //DEBUG_ANALOGBUFIO + return captured_samples; +} diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.h b/ports/espressif/common-hal/analogbufio/BufferedIn.h new file mode 100644 index 0000000000..0c9d7df3d7 --- /dev/null +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2023 Milind Movasha + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ESP32_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H +#define MICROPY_INCLUDED_ESP32_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H + +#include "common-hal/microcontroller/Pin.h" +#include "py/obj.h" + +// This is the analogbufio object +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + uint8_t chan; +} analogbufio_bufferedin_obj_t; + +#endif // MICROPY_INCLUDED_ESP32_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H diff --git a/ports/espressif/common-hal/analogbufio/__init__.c b/ports/espressif/common-hal/analogbufio/__init__.c new file mode 100644 index 0000000000..b6c74b985b --- /dev/null +++ b/ports/espressif/common-hal/analogbufio/__init__.c @@ -0,0 +1 @@ +// No analogbufio module functions. diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 460e651705..c82d622570 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -13,6 +13,7 @@ CIRCUITPY_FULL_BUILD ?= 1 # These modules are implemented in ports//common-hal: CIRCUITPY_ALARM ?= 1 CIRCUITPY_AUDIOBUSIO ?= 1 +CIRCUITPY_ANALOGBUFIO ?= 1 CIRCUITPY_AUDIOBUSIO_I2SOUT ?= 1 CIRCUITPY_AUDIOBUSIO_PDMIN ?= 0 CIRCUITPY_AUDIOCORE ?= 1 From 58532ece3fb216ab552c2fa8f4578ef395e9ace7 Mon Sep 17 00:00:00 2001 From: Milind Movasha Date: Fri, 17 Feb 2023 23:50:41 +0530 Subject: [PATCH 02/11] Moved CIRCUITPY_ANALOGBUFIO flag to correct place in alphabetical order --- ports/espressif/mpconfigport.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index c82d622570..79515619ea 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -12,8 +12,8 @@ CIRCUITPY_FULL_BUILD ?= 1 # These modules are implemented in ports//common-hal: CIRCUITPY_ALARM ?= 1 -CIRCUITPY_AUDIOBUSIO ?= 1 CIRCUITPY_ANALOGBUFIO ?= 1 +CIRCUITPY_AUDIOBUSIO ?= 1 CIRCUITPY_AUDIOBUSIO_I2SOUT ?= 1 CIRCUITPY_AUDIOBUSIO_PDMIN ?= 0 CIRCUITPY_AUDIOCORE ?= 1 From ead5751803ae2c770ec5923c5c996cb1f33aa930 Mon Sep 17 00:00:00 2001 From: Milind Movasha Date: Sat, 18 Feb 2023 14:23:49 +0530 Subject: [PATCH 03/11] Pre-commit: Done the formatting changes Pre-commit: Fixed compilation error for other ESP32C3/ESP32S3/ESP32H2 boards Review comment: Removed the self->pin NULL check Review comment: Using raise_ValueError_invalid_pin when adc_index is not ADC_UNIT1 for ESP32 Review comment: Optimized the code to set data in buffer from DMA results Fix: For ESP32C3 boards continuing collecting samples after channel mismatch as DMA runs in alternating UNIT mode Fix: For ESP32S3 and ESP32H2 setting conversion mode to type2 --- .../common-hal/analogbufio/BufferedIn.c | 161 +++++++++--------- 1 file changed, 83 insertions(+), 78 deletions(-) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index e26afb283e..0a88ccaba2 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -41,17 +41,17 @@ #include "freertos/semphr.h" #include "driver/adc.h" -//#define DEBUG_ANALOGBUFIO +// #define DEBUG_ANALOGBUFIO #define NUM_SAMPLES_PER_INTERRUPT 256 #define NUM_ADC_CHANNELS 1 -#define DMA_BUFFER_SIZE 1024 +#define DMA_BUFFER_SIZE 1024 #define ATTENUATION ADC_ATTEN_DB_0 #define ADC_READ_TIMEOUT_MS 2000 #if defined(CONFIG_IDF_TARGET_ESP32) #define ADC_RESULT_BYTE 2 -#define ADC_CONV_LIMIT_EN 1 //For ESP32, this should always be set to 1 +#define ADC_CONV_LIMIT_EN 1 // For ESP32, this should always be set to 1 #elif defined(CONFIG_IDF_TARGET_ESP32S2) #define ADC_RESULT_BYTE 2 #define ADC_CONV_LIMIT_EN 0 @@ -71,12 +71,8 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s uint16_t adc1_chan_mask = 0; uint16_t adc2_chan_mask = 0; - if( self->pin != NULL) { - mp_raise_ValueError(translate("ADC DMA already initialized")); - } - output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; - if(pin->adc_index == ADC_UNIT_1) { + if (pin->adc_index == ADC_UNIT_1) { convert_mode = ADC_CONV_SINGLE_UNIT_1; } else { convert_mode = ADC_CONV_SINGLE_UNIT_2; @@ -90,34 +86,37 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s /* * Chip version Conversion Mode Output Format Type - * ESP32 1 TYPE1 - * ESP32S2 1,2,BOTH,ALTER TYPE1, TYPE2 - * ESP32C3 ALTER TYPE2 - * ESP32S3 1,2,BOTH,ALTER TYPE2 - * ESP32H3 1,2,BOTH,ALTER TYPE2 + * ESP32 1 TYPE1 + * ESP32S2 1,2,BOTH,ALTER TYPE1, TYPE2 + * ESP32C3 ALTER TYPE2 + * ESP32S3 1,2,BOTH,ALTER TYPE2 + * ESP32H3 1,2,BOTH,ALTER TYPE2 */ -#if defined(CONFIG_IDF_TARGET_ESP32) - if(pin->adc_index != ADC_UNIT_1) { - mp_raise_ValueError(translate("ESP32 only supports ADC1 unit")); + #if defined(CONFIG_IDF_TARGET_ESP32) + if (pin->adc_index != ADC_UNIT_1) { + /* + * ESP32 only supports ADC1 unit + * https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf + * Table 29-3 + */ + raise_ValueError_invalid_pin(); } -#endif -#if defined(CONFIG_IDF_TARGET_ESP32S2) -#endif -#if defined(CONFIG_IDF_TARGET_ESP32C3) - //ESP32C3 only supports alter mode + #endif + + #if defined(CONFIG_IDF_TARGET_ESP32C3) + /* ESP32C3 only supports alter mode */ convert_mode = ADC_CONV_ALTER_UNIT; + #endif + + #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32H2) output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE2; -#endif -#if defined(CONFIG_IDF_TARGET_ESP32S3) -#endif -#if defined(CONFIG_IDF_TARGET_ESP32H2) -#endif + #endif self->pin = pin; common_hal_mcu_pin_claim(pin); - if(pin->adc_index == ADC_UNIT_1) { + if (pin->adc_index == ADC_UNIT_1) { adc1_chan_mask = 1 << pin->adc_channel; } else { adc2_chan_mask = 1 << pin->adc_channel; @@ -130,11 +129,11 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s .adc2_chan_mask = adc2_chan_mask, }; -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"pin:%d, ADC channel:%d, ADC index:%d, adc1_chan_mask:0x%x, adc2_chan_mask:0x%x\n",pin->number,pin->adc_channel,pin->adc_index,adc1_chan_mask,adc2_chan_mask); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO esp_err_t err = adc_digi_initialize(&adc_dma_config); - if(ESP_OK != err) { + if (ESP_OK != err) { common_hal_analogbufio_bufferedin_deinit(self); mp_raise_ValueError_varg(translate("Unable to initialize ADC DMA controller, ErrorCode:%d"),err); } @@ -148,14 +147,14 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s .format = output_format, }; -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"conversion_mode:%d, format:%d, conv_limit_en:%d, sample_rate:%d\n",convert_mode,output_format,ADC_CONV_LIMIT_EN,sample_rate); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO adc_digi_pattern_config_t adc_pattern[NUM_ADC_CHANNELS] = {0}; adc_pattern[0].atten = ATTENUATION; adc_pattern[0].channel = pin->adc_channel; - if(pin->adc_index == ADC_UNIT_1) { + if (pin->adc_index == ADC_UNIT_1) { adc_pattern[0].unit = 0; } else { adc_pattern[0].unit = 1; @@ -163,17 +162,17 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s adc_pattern[0].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH; dig_cfg.adc_pattern = adc_pattern; -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"adc_pattern[0].channel:%d, adc_pattern[0].unit:%d, adc_pattern[0].atten:%d\n",adc_pattern[0].channel,adc_pattern[0].unit,adc_pattern[0].atten); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO err = adc_digi_controller_configure(&dig_cfg); - if(ESP_OK != err) { + if (ESP_OK != err) { common_hal_analogbufio_bufferedin_deinit(self); mp_raise_ValueError_varg(translate("Unable to configure ADC DMA controller, ErrorCode:%d"),err); } err = adc_digi_start(); - if(ESP_OK != err) { + if (ESP_OK != err) { common_hal_analogbufio_bufferedin_deinit(self); mp_raise_ValueError_varg(translate("Unable to start ADC DMA controller, ErrorCode:%d"),err); } @@ -196,36 +195,46 @@ void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self self->pin = NULL; } -static bool check_valid_data(const adc_digi_output_data_t *data) -{ +static bool check_valid_data(const adc_digi_output_data_t *data) { unsigned int unit = data->type2.unit; - if(output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE2) { - if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit)) return false; - if (adc_channel != data->type2.channel) return false; + if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE2) { + if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit)) { + return false; + } + if (adc_channel != data->type2.channel) { + return false; + } } else { - if( convert_mode == ADC_CONV_SINGLE_UNIT_1 ) { + if ( convert_mode == ADC_CONV_SINGLE_UNIT_1 ) { unit = 0; } else { unit = 1; } - if (data->type1.channel >= SOC_ADC_CHANNEL_NUM(unit)) return false; - if (adc_channel != data->type1.channel) return false; + #if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) + if (data->type1.channel >= SOC_ADC_CHANNEL_NUM(unit)) { + return false; + } + if (adc_channel != data->type1.channel) { + return false; + } + #endif + } + if (unit > 2) { + return false; } - if (unit > 2) return false; return true; } - uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t *self, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample) { - uint8_t result[NUM_SAMPLES_PER_INTERRUPT] __attribute__ ((aligned (4))) = {0}; + uint8_t result[NUM_SAMPLES_PER_INTERRUPT] __attribute__ ((aligned(4))) = {0}; uint32_t captured_samples = 0; uint32_t captured_bytes = 0; esp_err_t ret; uint32_t ret_num = 0; -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"Required bytes: %d\n",len); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO while(captured_bytes < len) { ret_num = 0; @@ -233,51 +242,47 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t if (ret == ESP_OK) { for(uint32_t i=0; itype1.data; - } else { - *(uint16_t *)(void *)&buffer[captured_bytes] = ((adc_digi_output_data_t *) (void *)&result[i])->type2.data; - } + adc_digi_output_data_t *pResult = (adc_digi_output_data_t *) (void *)&result[i]; + if (check_valid_data(pResult)) { + if (captured_bytes < len) { + uint16_t *pBuffer = (uint16_t *)(void *)&buffer[captured_bytes]; + if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1) { + #if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) + *pBuffer = pResult->type1.data; + #endif } else { - if(output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1) { - *(uint32_t *)(void *)&buffer[captured_bytes] = ((adc_digi_output_data_t *) (void *)&result[i])->type1.data; - } else { - *(uint32_t *)(void *)&buffer[captured_bytes] = ((adc_digi_output_data_t *) (void *)&result[i])->type2.data; - } + *pBuffer = pResult->type2.data; } - captured_bytes += ADC_RESULT_BYTE; + captured_bytes += sizeof(uint16_t); captured_samples++; } else { return captured_samples; } } else { -#if defined(DEBUG_ANALOGBUFIO) - if(ADC_RESULT_BYTE == 2) { - mp_printf(&mp_plat_print,"Invalid sample received: 0x%0x\n",*(uint16_t *)(void *)&result[i]); - } else { - mp_printf(&mp_plat_print,"Invalid sample received: 0x%0x\n",*(uint32_t *)(void *)&result[i]); - } -#endif //DEBUG_ANALOGBUFIO + #if !defined(CONFIG_IDF_TARGET_ESP32C3) + // For all chips except for ESP32C3 we would receive samples only from one unit + // For ESP32C3 we may receive sample from alternating units and need to ignore them + #if defined(DEBUG_ANALOGBUFIO) + mp_printf(&mp_plat_print,"Invalid sample received: 0x%x\n",pResult->val); + #endif // DEBUG_ANALOGBUFIO return captured_samples; - } + #endif + } } } else if (ret == ESP_ERR_TIMEOUT) { -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"ADC Timeout\n"); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO return captured_samples; } else { -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"adc_digi_read_bytes failed error code:%d\n",ret); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO return captured_samples; } } -#if defined(DEBUG_ANALOGBUFIO) + #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"Captured bytes: %d\n",captured_bytes); -#endif //DEBUG_ANALOGBUFIO + #endif // DEBUG_ANALOGBUFIO return captured_samples; } From 4683e48337941a55a35b3b9f8ab2aee51a4ae4fc Mon Sep 17 00:00:00 2001 From: Milind Movasha Date: Sat, 18 Feb 2023 14:40:12 +0530 Subject: [PATCH 04/11] Further formatting changes related to whitespaces --- ports/espressif/common-hal/analogbufio/BufferedIn.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index 0a88ccaba2..e2928e5236 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -95,7 +95,7 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s #if defined(CONFIG_IDF_TARGET_ESP32) if (pin->adc_index != ADC_UNIT_1) { - /* + /* * ESP32 only supports ADC1 unit * https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf * Table 29-3 @@ -205,7 +205,7 @@ static bool check_valid_data(const adc_digi_output_data_t *data) { return false; } } else { - if ( convert_mode == ADC_CONV_SINGLE_UNIT_1 ) { + if (convert_mode == ADC_CONV_SINGLE_UNIT_1) { unit = 0; } else { unit = 1; @@ -236,12 +236,12 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t mp_printf(&mp_plat_print,"Required bytes: %d\n",len); #endif // DEBUG_ANALOGBUFIO - while(captured_bytes < len) { + while (captured_bytes < len) { ret_num = 0; ret = adc_digi_read_bytes(result, NUM_SAMPLES_PER_INTERRUPT, &ret_num, ADC_READ_TIMEOUT_MS); if (ret == ESP_OK) { - for(uint32_t i=0; i Date: Sat, 18 Feb 2023 14:45:37 +0530 Subject: [PATCH 05/11] Additional formatting change related to whitespaces --- ports/espressif/common-hal/analogbufio/BufferedIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index e2928e5236..582fe6e700 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -241,7 +241,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t ret = adc_digi_read_bytes(result, NUM_SAMPLES_PER_INTERRUPT, &ret_num, ADC_READ_TIMEOUT_MS); if (ret == ESP_OK) { - for (uint32_t i=0; i Date: Sat, 18 Feb 2023 14:49:11 +0530 Subject: [PATCH 06/11] Yet another formatting change related to whitespaces --- ports/espressif/common-hal/analogbufio/BufferedIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index 582fe6e700..db76069a69 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -242,7 +242,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t if (ret == ESP_OK) { for (uint32_t i = 0; i < ret_num; i += ADC_RESULT_BYTE) { - adc_digi_output_data_t *pResult = (adc_digi_output_data_t *) (void *)&result[i]; + adc_digi_output_data_t *pResult = (adc_digi_output_data_t *)(void *)&result[i]; if (check_valid_data(pResult)) { if (captured_bytes < len) { uint16_t *pBuffer = (uint16_t *)(void *)&buffer[captured_bytes]; From 556e2c915d6a399cb3210c4baf7984794233a469 Mon Sep 17 00:00:00 2001 From: Milind Movasha Date: Sat, 18 Feb 2023 20:42:31 +0530 Subject: [PATCH 07/11] Fix for pre-commit issue related to translation --- locale/circuitpython.pot | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 775a85ffe1..f3186a49c8 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -2162,6 +2162,11 @@ msgstr "" msgid "Unable to allocate the heap." msgstr "" +#: ports/espressif/common-hal/analogbufio/BufferedIn.c +#, c-format +msgid "Unable to configure ADC DMA controller, ErrorCode:%d" +msgstr "" + #: ports/espressif/common-hal/busio/I2C.c msgid "Unable to create lock" msgstr "" @@ -2180,10 +2185,20 @@ msgstr "" msgid "Unable to init parser" msgstr "" +#: ports/espressif/common-hal/analogbufio/BufferedIn.c +#, c-format +msgid "Unable to initialize ADC DMA controller, ErrorCode:%d" +msgstr "" + #: shared-module/displayio/OnDiskBitmap.c msgid "Unable to read color palette data" msgstr "" +#: ports/espressif/common-hal/analogbufio/BufferedIn.c +#, c-format +msgid "Unable to start ADC DMA controller, ErrorCode:%d" +msgstr "" + #: ports/espressif/common-hal/mdns/Server.c #: ports/raspberrypi/common-hal/mdns/Server.c msgid "Unable to start mDNS query" From 09f84e351318f9f3bf5bff6bb9664703ca743c78 Mon Sep 17 00:00:00 2001 From: Milind Date: Thu, 23 Feb 2023 13:32:48 +0530 Subject: [PATCH 08/11] Changed the code structure to start and stop dma from readinto function as per review comment to support one shot conversion mode for analogbufio Added check for verifying the buffer type passed to readinto is H --- locale/circuitpython.pot | 4 ++ .../common-hal/analogbufio/BufferedIn.c | 67 ++++++++++++------- .../common-hal/analogbufio/BufferedIn.h | 2 +- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index f3186a49c8..5977d4fc49 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -180,6 +180,10 @@ msgstr "" msgid "%q must be >= %d" msgstr "" +#: ports/espressif/common-hal/analogbufio/BufferedIn.c +msgid "%q must be a array of type 'H'" +msgstr "" + #: shared-bindings/analogbufio/BufferedIn.c msgid "%q must be a bytearray or array of type 'H' or 'B'" msgstr "" diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index db76069a69..5e34896b3e 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -63,27 +63,32 @@ #define ADC_CONV_LIMIT_EN 0 #endif -static adc_digi_convert_mode_t convert_mode = ADC_CONV_SINGLE_UNIT_2; -static adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; -static uint8_t adc_channel = 0; +static void start_dma(analogbufio_bufferedin_obj_t *self, adc_digi_convert_mode_t *convert_mode, adc_digi_output_format_t *output_format); +static void stop_dma(analogbufio_bufferedin_obj_t *self); void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *self, const mcu_pin_obj_t *pin, uint32_t sample_rate) { + self->pin = pin; + self->sample_rate = sample_rate; +} + +static void start_dma(analogbufio_bufferedin_obj_t *self, adc_digi_convert_mode_t *convert_mode, adc_digi_output_format_t *output_format) { uint16_t adc1_chan_mask = 0; uint16_t adc2_chan_mask = 0; - output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; + const mcu_pin_obj_t *pin = self->pin; + uint32_t sample_rate = self->sample_rate; + + *output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; if (pin->adc_index == ADC_UNIT_1) { - convert_mode = ADC_CONV_SINGLE_UNIT_1; + *convert_mode = ADC_CONV_SINGLE_UNIT_1; } else { - convert_mode = ADC_CONV_SINGLE_UNIT_2; + *convert_mode = ADC_CONV_SINGLE_UNIT_2; } if (pin->adc_index == NO_ADC || pin->adc_channel == NO_ADC_CHANNEL) { raise_ValueError_invalid_pin(); } - adc_channel = pin->adc_channel; - /* * Chip version Conversion Mode Output Format Type * ESP32 1 TYPE1 @@ -106,14 +111,13 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s #if defined(CONFIG_IDF_TARGET_ESP32C3) /* ESP32C3 only supports alter mode */ - convert_mode = ADC_CONV_ALTER_UNIT; + *convert_mode = ADC_CONV_ALTER_UNIT; #endif #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32H2) - output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE2; + *output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE2; #endif - self->pin = pin; common_hal_mcu_pin_claim(pin); if (pin->adc_index == ADC_UNIT_1) { @@ -143,12 +147,12 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s .conv_limit_num = 250, .pattern_num = NUM_ADC_CHANNELS, .sample_freq_hz = sample_rate, - .conv_mode = convert_mode, - .format = output_format, + .conv_mode = *convert_mode, + .format = *output_format, }; #if defined(DEBUG_ANALOGBUFIO) - mp_printf(&mp_plat_print,"conversion_mode:%d, format:%d, conv_limit_en:%d, sample_rate:%d\n",convert_mode,output_format,ADC_CONV_LIMIT_EN,sample_rate); + mp_printf(&mp_plat_print,"conversion_mode:%d, format:%d, conv_limit_en:%d, sample_rate:%d\n",*convert_mode,*output_format,ADC_CONV_LIMIT_EN,sample_rate); #endif // DEBUG_ANALOGBUFIO adc_digi_pattern_config_t adc_pattern[NUM_ADC_CHANNELS] = {0}; @@ -178,6 +182,13 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s } } +static void stop_dma(analogbufio_bufferedin_obj_t *self) { + adc_digi_stop(); + adc_digi_deinitialize(); + // Release ADC Pin + reset_pin_number(self->pin->number); +} + bool common_hal_analogbufio_bufferedin_deinited(analogbufio_bufferedin_obj_t *self) { return self->pin == NULL; } @@ -186,22 +197,16 @@ void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self if (common_hal_analogbufio_bufferedin_deinited(self)) { return; } - - adc_digi_stop(); - adc_digi_deinitialize(); - - // Release ADC Pin - reset_pin_number(self->pin->number); self->pin = NULL; } -static bool check_valid_data(const adc_digi_output_data_t *data) { +static bool check_valid_data(const adc_digi_output_data_t *data, const mcu_pin_obj_t *pin, adc_digi_convert_mode_t convert_mode, adc_digi_output_format_t output_format) { unsigned int unit = data->type2.unit; if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE2) { if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit)) { return false; } - if (adc_channel != data->type2.channel) { + if (pin->adc_channel != data->type2.channel) { return false; } } else { @@ -214,7 +219,7 @@ static bool check_valid_data(const adc_digi_output_data_t *data) { if (data->type1.channel >= SOC_ADC_CHANNEL_NUM(unit)) { return false; } - if (adc_channel != data->type1.channel) { + if (pin->adc_channel != data->type1.channel) { return false; } #endif @@ -231,6 +236,14 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t uint32_t captured_bytes = 0; esp_err_t ret; uint32_t ret_num = 0; + adc_digi_convert_mode_t convert_mode = ADC_CONV_SINGLE_UNIT_2; + adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; + + if (bytes_per_sample != 2) { + mp_raise_ValueError_varg(translate("%q must be a array of type 'H'"), MP_QSTR_buffer); + } + + start_dma(self, &convert_mode, &output_format); #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"Required bytes: %d\n",len); @@ -243,7 +256,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t if (ret == ESP_OK) { for (uint32_t i = 0; i < ret_num; i += ADC_RESULT_BYTE) { adc_digi_output_data_t *pResult = (adc_digi_output_data_t *)(void *)&result[i]; - if (check_valid_data(pResult)) { + if (check_valid_data(pResult, self->pin, convert_mode, output_format)) { if (captured_bytes < len) { uint16_t *pBuffer = (uint16_t *)(void *)&buffer[captured_bytes]; if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1) { @@ -256,6 +269,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t captured_bytes += sizeof(uint16_t); captured_samples++; } else { + stop_dma(self); return captured_samples; } } else { @@ -265,6 +279,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"Invalid sample received: 0x%x\n",pResult->val); #endif // DEBUG_ANALOGBUFIO + stop_dma(self); return captured_samples; #endif } @@ -273,14 +288,18 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"ADC Timeout\n"); #endif // DEBUG_ANALOGBUFIO + stop_dma(self); return captured_samples; } else { #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"adc_digi_read_bytes failed error code:%d\n",ret); #endif // DEBUG_ANALOGBUFIO + stop_dma(self); return captured_samples; } } + + stop_dma(self); #if defined(DEBUG_ANALOGBUFIO) mp_printf(&mp_plat_print,"Captured bytes: %d\n",captured_bytes); #endif // DEBUG_ANALOGBUFIO diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.h b/ports/espressif/common-hal/analogbufio/BufferedIn.h index 0c9d7df3d7..909455ca06 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.h +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.h @@ -36,7 +36,7 @@ typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; - uint8_t chan; + uint32_t sample_rate; } analogbufio_bufferedin_obj_t; #endif // MICROPY_INCLUDED_ESP32_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H From 9e73e8351aace95c6600345ca80134a7e13915de Mon Sep 17 00:00:00 2001 From: Milind Date: Thu, 23 Feb 2023 14:47:05 +0530 Subject: [PATCH 09/11] Calling stop_dma function while retruning errors from start_dma after configuring dma --- ports/espressif/common-hal/analogbufio/BufferedIn.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index 5e34896b3e..04595910ca 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -138,6 +138,7 @@ static void start_dma(analogbufio_bufferedin_obj_t *self, adc_digi_convert_mode_ #endif // DEBUG_ANALOGBUFIO esp_err_t err = adc_digi_initialize(&adc_dma_config); if (ESP_OK != err) { + stop_dma(self); common_hal_analogbufio_bufferedin_deinit(self); mp_raise_ValueError_varg(translate("Unable to initialize ADC DMA controller, ErrorCode:%d"),err); } @@ -172,11 +173,13 @@ static void start_dma(analogbufio_bufferedin_obj_t *self, adc_digi_convert_mode_ err = adc_digi_controller_configure(&dig_cfg); if (ESP_OK != err) { + stop_dma(self); common_hal_analogbufio_bufferedin_deinit(self); mp_raise_ValueError_varg(translate("Unable to configure ADC DMA controller, ErrorCode:%d"),err); } err = adc_digi_start(); if (ESP_OK != err) { + stop_dma(self); common_hal_analogbufio_bufferedin_deinit(self); mp_raise_ValueError_varg(translate("Unable to start ADC DMA controller, ErrorCode:%d"),err); } From 9058beb5737f1112d3dda43b18ec1c6634118f3e Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 23 Feb 2023 13:41:49 -0500 Subject: [PATCH 10/11] "a array" to "an array" --- locale/circuitpython.pot | 2 +- ports/espressif/common-hal/analogbufio/BufferedIn.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 5977d4fc49..096ef68ddc 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -181,7 +181,7 @@ msgid "%q must be >= %d" msgstr "" #: ports/espressif/common-hal/analogbufio/BufferedIn.c -msgid "%q must be a array of type 'H'" +msgid "%q must be an array of type 'H'" msgstr "" #: shared-bindings/analogbufio/BufferedIn.c diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index 04595910ca..7639ad03fe 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -243,7 +243,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; if (bytes_per_sample != 2) { - mp_raise_ValueError_varg(translate("%q must be a array of type 'H'"), MP_QSTR_buffer); + mp_raise_ValueError_varg(translate("%q must be an array of type 'H'"), MP_QSTR_buffer); } start_dma(self, &convert_mode, &output_format); From 8996fda0498b6914db6838bb8f5d717578482c2d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 23 Feb 2023 13:55:52 -0500 Subject: [PATCH 11/11] "an array" -> "array" --- locale/circuitpython.pot | 2 +- ports/espressif/common-hal/analogbufio/BufferedIn.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 096ef68ddc..e607d15af7 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -181,7 +181,7 @@ msgid "%q must be >= %d" msgstr "" #: ports/espressif/common-hal/analogbufio/BufferedIn.c -msgid "%q must be an array of type 'H'" +msgid "%q must be array of type 'H'" msgstr "" #: shared-bindings/analogbufio/BufferedIn.c diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index 7639ad03fe..f2ee8c19af 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -243,7 +243,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1; if (bytes_per_sample != 2) { - mp_raise_ValueError_varg(translate("%q must be an array of type 'H'"), MP_QSTR_buffer); + mp_raise_ValueError_varg(translate("%q must be array of type 'H'"), MP_QSTR_buffer); } start_dma(self, &convert_mode, &output_format);