From 552f6af4a13527fc91943431226c5fb592cbe785 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sat, 6 Aug 2022 15:50:22 -0400 Subject: [PATCH 01/31] clean up --- ports/espressif/esp-idf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index d51f7d8821..ddb7ddbcb6 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit d51f7d882187afa4b39c2613fd0fe2ac2fea1145 +Subproject commit ddb7ddbcb613a582e0a91eda8b1d2510dd0a2d83 From f69939c49cee8b1bce827cd5a87848a9127460d8 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sat, 6 Aug 2022 16:22:50 -0400 Subject: [PATCH 02/31] First Commit for AnalogFastIn --- ports/raspberrypi/common-hal/analogio/AnalogFastIn.c | 1 + ports/raspberrypi/common-hal/analogio/AnalogFastIn.h | 1 + shared-bindings/analogio/AnalogFastIn.c | 1 + shared-bindings/analogio/AnalogFastIn.h | 1 + 4 files changed, 4 insertions(+) create mode 100644 ports/raspberrypi/common-hal/analogio/AnalogFastIn.c create mode 100644 ports/raspberrypi/common-hal/analogio/AnalogFastIn.h create mode 100644 shared-bindings/analogio/AnalogFastIn.c create mode 100644 shared-bindings/analogio/AnalogFastIn.h diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c new file mode 100644 index 0000000000..d916a669dd --- /dev/null +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c @@ -0,0 +1 @@ +/* AnalogFastIn.c */ diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h new file mode 100644 index 0000000000..e99192dfac --- /dev/null +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h @@ -0,0 +1 @@ +/* AnalogFastIn.h */ diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c new file mode 100644 index 0000000000..d916a669dd --- /dev/null +++ b/shared-bindings/analogio/AnalogFastIn.c @@ -0,0 +1 @@ +/* AnalogFastIn.c */ diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/analogio/AnalogFastIn.h new file mode 100644 index 0000000000..e99192dfac --- /dev/null +++ b/shared-bindings/analogio/AnalogFastIn.h @@ -0,0 +1 @@ +/* AnalogFastIn.h */ From 9e0c580d3df6ae0d038c7ff940308a89917d8c59 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Wed, 10 Aug 2022 09:42:24 -0400 Subject: [PATCH 03/31] AnalogFastIn --- .../common-hal/analogio/AnalogFastIn.c | 170 +++++++++++++++++- .../common-hal/analogio/AnalogFastIn.h | 72 +++++++- py/circuitpy_defns.mk | 1 + shared-bindings/analogio/AnalogFastIn.c | 163 ++++++++++++++++- shared-bindings/analogio/AnalogFastIn.h | 40 ++++- shared-bindings/analogio/__init__.c | 2 + 6 files changed, 444 insertions(+), 4 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c index d916a669dd..4acda3d4d5 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c @@ -1 +1,169 @@ -/* AnalogFastIn.c */ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries + * + * 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 "common-hal/analogio/AnalogFastIn.h" +#include "shared-bindings/analogio/AnalogFastIn.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "py/runtime.h" +#include "supervisor/shared/translate/translate.h" + +#include "src/rp2_common/hardware_adc/include/hardware/adc.h" +#include "src/rp2_common/hardware_dma/include/hardware/dma.h" + +// /sdk/src/rp2_common/hardware_dma/include/hardware/dma.h + +// ports/raspberrypi/ +#include "sdk/src/common/pico_stdlib/include/pico/stdlib.h" + + +#define ADC_FIRST_PIN_NUMBER 26 +#define ADC_PIN_COUNT 4 +// Channel 0 is GPIO26 +#define CAPTURE_CHANNEL 0 +#define CAPTURE_DEPTH 1000 +uint8_t capture_buf[CAPTURE_DEPTH]; + +// ADC unit8 or int8 ??? ---> unint16 +// +// uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { +// adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER); +// uint16_t value = adc_read(); +// +// // Stretch 12-bit ADC reading to 16-bit range +// return (value << 4) | (value >> 8); +// } +/* +typedef struct { + mp_obj_base_t base; + uint8_t number; +} mcu_pin_obj_t; +*/ + +// self->pin = pin; +// self->buffer = buffer; +// self->len = len; +// //self->bits_per_sample = bytes_per_sample * 8; +// self->samples_signed = samples_signed; +// self->sample_rate = sample_rate; + +void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { + + // Set pin and channel + self->pin = pin; + self->chan = pin->number - ADC_FIRST_PIN_NUMBER; + + // Checks on chan value here + + // Set buffer and length + self->buffer = buffer; + self->len = len; + + // checks on length here + + + // uint8_t bytes_per_sample + // Set sample rate + // self->bits_per_sample = bytes_per_sample * 8; + self->sample_rate = sample_rate; + + // Standard IO Init + stdio_init_all(); + + // Init GPIO for analogue use: hi-Z, no pulls, disable digital input buffer. + adc_init(); + adc_gpio_init(pin->number); + adc_select_input(self->chan); // chan = pin - 26 ?? + // adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER); + adc_fifo_setup( + true, // Write each completed conversion to the sample FIFO + true, // Enable DMA data request (DREQ) + 1, // DREQ (and IRQ) asserted when at least 1 sample present + false, // We won't see the ERR bit because of 8 bit reads; disable. // ?? + true // Shift each sample to 8 bits when pushing to FIFO // ?? + ); + + // Divisor of 0 -> full speed. Free-running capture with the divider is + // equivalent to pressing the ADC_CS_START_ONCE button once per `div + 1` + // cycles (div not necessarily an integer). Each conversion takes 96 + // cycles, so in general you want a divider of 0 (hold down the button + // continuously) or > 95 (take samples less frequently than 96 cycle + // intervals). This is all timed by the 48 MHz ADC clock. + // sample rate determines divisor, not zero. + adc_set_clkdiv(0); + + // sleep_ms(1000); + // Set up the DMA to start transferring data as soon as it appears in FIFO + uint dma_chan = dma_claim_unused_channel(true); + self->dma_chan = dma_chan; + dma_channel_config cfg = dma_channel_get_default_config(dma_chan); + + // Reading from constant address, writing to incrementing byte addresses + channel_config_set_transfer_data_size(&cfg, DMA_SIZE_8); + channel_config_set_read_increment(&cfg, false); + channel_config_set_write_increment(&cfg, true); + + // Pace transfers based on availability of ADC samples + channel_config_set_dreq(&cfg, DREQ_ADC); + + dma_channel_configure(dma_chan, &cfg, + capture_buf, // dst + &adc_hw->fifo, // src + self->len, // CAPTURE_DEPTH, // transfer count + true // start immediately + ); +} + + +bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self) { + return self->pin == NULL; +} + +void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self) { + if (common_hal_analogio_analogfastin_deinited(self)) { + return; + } + + reset_pin_number(self->pin->number); + self->pin = NULL; +} + +// ================================================================ +// capture() +// make this a bool so that later we can perform integrity checking +// ================================================================ +bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self) { + // uint16_t value = adc_read(); + // Stretch 12-bit ADC reading to 16-bit range + // return (value << 4) | (value >> 8); + adc_run(true); + // Once DMA finishes, stop any new conversions from starting, and clean up + // the FIFO in case the ADC was still mid-conversion. + dma_channel_wait_for_finish_blocking(self->dma_chan); + // printf("Capture finished\n"); + adc_run(false); + adc_fifo_drain(); + return true; +} diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h index e99192dfac..2853b4fe07 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h @@ -1 +1,71 @@ -/* AnalogFastIn.h */ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries + * + * 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_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H +#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +// We can extend the struct without impact to existing code +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + uint8_t *buffer; + uint32_t len; + uint8_t bytes_per_sample; + bool samples_signed; + uint32_t sample_rate; + uint8_t chan; + uint dma_chan; + // data_size = DMA_SIZE_8; // - default DMA_SIZE_8 + // data_size = DMA_SIZE_16; // - default DMA_SIZE_16 + // Either 12 bits in 16 or 12 over 2 bytes or truncate 12 to 8 in 8 + // Either B or H, default array.array("h", [0]*length) "h"==short (16 bits signed) +} analogio_analogfastin_obj_t; + +// void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) +/* +void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, + const mcu_pin_obj_t *pin, + uint8_t *buffer, + uint32_t len, + uint8_t bytes_per_sample, + bool samples_signed, + uint32_t sample_rate) { + self->pin = pin; + self->buffer = buffer; + self->len = len; + //self->bits_per_sample = bytes_per_sample * 8; + self->samples_signed = samples_signed; + self->sample_rate = sample_rate; +} +*/ + +void analogfastin_init(void); + +#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index af49c876ba..a0eb743dc7 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -390,6 +390,7 @@ SRC_COMMON_HAL_ALL = \ alarm/time/TimeAlarm.c \ alarm/touch/TouchAlarm.c \ analogio/AnalogIn.c \ + analogio/AnalogFastIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ audiobusio/I2SOut.c \ diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c index d916a669dd..d32e648af3 100644 --- a/shared-bindings/analogio/AnalogFastIn.c +++ b/shared-bindings/analogio/AnalogFastIn.c @@ -1 +1,162 @@ -/* AnalogFastIn.c */ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George + * + * 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 "shared/runtime/context_manager_helpers.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "py/nlr.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/analogio/AnalogFastIn.h" +#include "shared-bindings/util.h" + +// pin, buffer, rate + + + +STATIC mp_obj_t analogio_analogfastin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_pin, ARG_buffer, ARG_sample_rate }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Validate Pin + const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + + // Buffer Pointer defined and allocated by user + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); + + // signed or unsigned, byte per sample + bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h'; + uint8_t bytes_per_sample = 1; + + // Bytes Per Sample + if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { + bytes_per_sample = 2; + } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { + mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'")); + } + + // Validate sample rate here + uint32_t sample_rate = args[ARG_sample_rate].u_int; + + // Create local object + analogio_analogfastin_obj_t *self = m_new_obj(analogio_analogfastin_obj_t); + self->base.type = &analogio_analogfastin_type; + + // Call local intereface in ports/common-hal/analogio + common_hal_analogio_analogfastin_construct(self, + pin, + ((uint8_t *)bufinfo.buf), + bufinfo.len, + bytes_per_sample, + signed_samples, + sample_rate + ); + + return MP_OBJ_FROM_PTR(self); +} + +//| def deinit(self) -> None: +//| """Turn off the AnalogFastIn and release the pin for other use.""" +//| ... +//| +STATIC mp_obj_t analogio_analogfastin_deinit(mp_obj_t self_in) { + analogio_analogfastin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_analogio_analogfastin_deinit(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogfastin_deinit_obj, analogio_analogfastin_deinit); + +STATIC void check_for_deinit(analogio_analogfastin_obj_t *self) { + if (common_hal_analogio_analogfastin_deinited(self)) { + raise_deinited_error(); + } +} +//| def __enter__(self) -> AnalogFastIn: +//| """No-op used by Context Managers.""" +//| ... +//| +// Provided by context manager helper. + +//| def __exit__(self) -> None: +//| """Automatically deinitializes the hardware when exiting a context. See +//| :ref:`lifetime-and-contextmanagers` for more info.""" +//| ... +//| +STATIC mp_obj_t analogio_analogfastin___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_analogio_analogfastin_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogfastin___exit___obj, 4, 4, analogio_analogfastin___exit__); + +//| value: int +//| """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only) +//| +//| Even if the underlying analog to digital converter (ADC) is lower +//| resolution, the value is 16-bit.""" +//| +STATIC mp_obj_t analogio_analogfastin_obj_capture(mp_obj_t self_in) { + analogio_analogfastin_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogfastin_capture(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogfastin_capture_obj, analogio_analogfastin_obj_capture); + +// MP_PROPERTY_GETTER(analogio_analogfastin_value_obj, +// (mp_obj_t)&analogio_analogfastin_get_value_obj); + +//| reference_voltage: float +//| """The maximum voltage measurable (also known as the reference voltage) as a +//| `float` in Volts. Note the ADC value may not scale to the actual voltage linearly +//| at ends of the analog range.""" +//| + +STATIC const mp_rom_map_elem_t analogio_analogfastin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogfastin_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogfastin___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_capture), MP_ROM_PTR(&analogio_analogfastin_capture_obj)}, + +}; + +STATIC MP_DEFINE_CONST_DICT(analogio_analogfastin_locals_dict, analogio_analogfastin_locals_dict_table); + +const mp_obj_type_t analogio_analogfastin_type = { + { &mp_type_type }, + .name = MP_QSTR_AnalogFastIn, + .make_new = analogio_analogfastin_make_new, + .locals_dict = (mp_obj_t)&analogio_analogfastin_locals_dict, +}; diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/analogio/AnalogFastIn.h index e99192dfac..8e6f2070b6 100644 --- a/shared-bindings/analogio/AnalogFastIn.h +++ b/shared-bindings/analogio/AnalogFastIn.h @@ -1 +1,39 @@ -/* AnalogFastIn.h */ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George + * + * 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_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/analogio/AnalogFastIn.h" + +extern const mp_obj_type_t analogio_analogfastin_type; + +void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); +void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self); +bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self); +bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self); +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H__ diff --git a/shared-bindings/analogio/__init__.c b/shared-bindings/analogio/__init__.c index eb956d0eb7..0033b2a66d 100644 --- a/shared-bindings/analogio/__init__.c +++ b/shared-bindings/analogio/__init__.c @@ -32,6 +32,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/analogio/__init__.h" #include "shared-bindings/analogio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogFastIn.h" #include "shared-bindings/analogio/AnalogOut.h" //| """Analog hardware support @@ -70,6 +71,7 @@ STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) }, { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) }, + { MP_ROM_QSTR(MP_QSTR_AnalogFastIn), MP_ROM_PTR(&analogio_analogfastin_type) }, { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) }, }; From c3c1e307c47b38a5d3797f27a5c6497b1069c46f Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Thu, 11 Aug 2022 17:50:30 -0400 Subject: [PATCH 04/31] Sample Rate Enhancements --- locale/circuitpython.pot | 5 ++ .../common-hal/analogio/AnalogFastIn.c | 80 +++++++++---------- .../common-hal/analogio/AnalogFastIn.h | 4 +- shared-bindings/analogio/AnalogFastIn.c | 14 ++-- shared-bindings/analogio/AnalogFastIn.h | 2 +- 5 files changed, 54 insertions(+), 51 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index c69ea7bdfa..a2e2826cd1 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -3863,6 +3863,11 @@ msgstr "" msgid "rsplit(None,n)" msgstr "" +#: shared-bindings/analogio/AnalogFastIn.c +msgid "sample rate must be 1.0-500000.0 per second" +msgstr "" + +#: shared-bindings/analogio/AnalogFastIn.c #: shared-bindings/audiocore/RawSample.c msgid "" "sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or " diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c index 4acda3d4d5..1d08956bd4 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c @@ -29,16 +29,12 @@ #include "shared-bindings/microcontroller/Pin.h" #include "py/runtime.h" #include "supervisor/shared/translate/translate.h" - #include "src/rp2_common/hardware_adc/include/hardware/adc.h" #include "src/rp2_common/hardware_dma/include/hardware/dma.h" - // /sdk/src/rp2_common/hardware_dma/include/hardware/dma.h - -// ports/raspberrypi/ +// ports/raspberrypi/ #include "sdk/src/common/pico_stdlib/include/pico/stdlib.h" - #define ADC_FIRST_PIN_NUMBER 26 #define ADC_PIN_COUNT 4 // Channel 0 is GPIO26 @@ -46,30 +42,7 @@ #define CAPTURE_DEPTH 1000 uint8_t capture_buf[CAPTURE_DEPTH]; -// ADC unit8 or int8 ??? ---> unint16 -// -// uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { -// adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER); -// uint16_t value = adc_read(); -// -// // Stretch 12-bit ADC reading to 16-bit range -// return (value << 4) | (value >> 8); -// } -/* -typedef struct { - mp_obj_base_t base; - uint8_t number; -} mcu_pin_obj_t; -*/ - -// self->pin = pin; -// self->buffer = buffer; -// self->len = len; -// //self->bits_per_sample = bytes_per_sample * 8; -// self->samples_signed = samples_signed; -// self->sample_rate = sample_rate; - -void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { +void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate) { // Set pin and channel self->pin = pin; @@ -83,7 +56,6 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel // checks on length here - // uint8_t bytes_per_sample // Set sample rate // self->bits_per_sample = bytes_per_sample * 8; @@ -102,7 +74,7 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel true, // Enable DMA data request (DREQ) 1, // DREQ (and IRQ) asserted when at least 1 sample present false, // We won't see the ERR bit because of 8 bit reads; disable. // ?? - true // Shift each sample to 8 bits when pushing to FIFO // ?? + false // Shift each sample to 8 bits when pushing to FIFO // ?? ); // Divisor of 0 -> full speed. Free-running capture with the divider is @@ -112,28 +84,28 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel // continuously) or > 95 (take samples less frequently than 96 cycle // intervals). This is all timed by the 48 MHz ADC clock. // sample rate determines divisor, not zero. - adc_set_clkdiv(0); + + adc_set_clkdiv(48000000.0 / self->sample_rate); // sleep_ms(1000); // Set up the DMA to start transferring data as soon as it appears in FIFO uint dma_chan = dma_claim_unused_channel(true); self->dma_chan = dma_chan; - dma_channel_config cfg = dma_channel_get_default_config(dma_chan); + + // Set Config + self->cfg = dma_channel_get_default_config(dma_chan); // Reading from constant address, writing to incrementing byte addresses - channel_config_set_transfer_data_size(&cfg, DMA_SIZE_8); - channel_config_set_read_increment(&cfg, false); - channel_config_set_write_increment(&cfg, true); + channel_config_set_transfer_data_size(&(self->cfg), DMA_SIZE_16); + channel_config_set_read_increment(&(self->cfg), false); + channel_config_set_write_increment(&(self->cfg), true); // Pace transfers based on availability of ADC samples - channel_config_set_dreq(&cfg, DREQ_ADC); + channel_config_set_dreq(&(self->cfg), DREQ_ADC); - dma_channel_configure(dma_chan, &cfg, - capture_buf, // dst - &adc_hw->fifo, // src - self->len, // CAPTURE_DEPTH, // transfer count - true // start immediately - ); + // clear any previous activity + adc_fifo_drain(); + adc_run(false); } @@ -146,8 +118,12 @@ void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self) return; } + // Release ADC Pin reset_pin_number(self->pin->number); self->pin = NULL; + + // Release DMA Channel + dma_channel_unclaim(self->dma_chan); } // ================================================================ @@ -155,15 +131,31 @@ void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self) // make this a bool so that later we can perform integrity checking // ================================================================ bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self) { + + + // CONSIDER THESE ISSUES // uint16_t value = adc_read(); // Stretch 12-bit ADC reading to 16-bit range // return (value << 4) | (value >> 8); + + uint32_t cdl = self->len / 2 - 1; + dma_channel_configure(self->dma_chan, &(self->cfg), + self->buffer, // dst + &adc_hw->fifo, // src + cdl, // CAPTURE_DEPTH, // transfer count + true // start immediately + ); + + // Start the ADC adc_run(true); + // Once DMA finishes, stop any new conversions from starting, and clean up // the FIFO in case the ADC was still mid-conversion. dma_channel_wait_for_finish_blocking(self->dma_chan); - // printf("Capture finished\n"); + + // Clean up adc_run(false); adc_fifo_drain(); + return true; } diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h index 2853b4fe07..3b8b8f0da9 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h @@ -28,6 +28,7 @@ #define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H #include "common-hal/microcontroller/Pin.h" +#include "src/rp2_common/hardware_dma/include/hardware/dma.h" #include "py/obj.h" @@ -39,9 +40,10 @@ typedef struct { uint32_t len; uint8_t bytes_per_sample; bool samples_signed; - uint32_t sample_rate; + mp_float_t sample_rate; uint8_t chan; uint dma_chan; + dma_channel_config cfg; // data_size = DMA_SIZE_8; // - default DMA_SIZE_8 // data_size = DMA_SIZE_16; // - default DMA_SIZE_16 // Either 12 bits in 16 or 12 over 2 bytes or truncate 12 to 8 in 8 diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c index d32e648af3..4fda809e0b 100644 --- a/shared-bindings/analogio/AnalogFastIn.c +++ b/shared-bindings/analogio/AnalogFastIn.c @@ -37,15 +37,18 @@ #include "shared-bindings/util.h" // pin, buffer, rate - - +STATIC void validate_rate(mp_float_t rate) { + if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) { + mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second")); + } +} STATIC mp_obj_t analogio_analogfastin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} }, + { MP_QSTR_sample_rate, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -69,7 +72,8 @@ STATIC mp_obj_t analogio_analogfastin_make_new(const mp_obj_type_t *type, size_t } // Validate sample rate here - uint32_t sample_rate = args[ARG_sample_rate].u_int; + mp_float_t sample_rate = mp_obj_get_float(args[ARG_sample_rate].u_obj); + validate_rate(sample_rate); // Create local object analogio_analogfastin_obj_t *self = m_new_obj(analogio_analogfastin_obj_t); diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/analogio/AnalogFastIn.h index 8e6f2070b6..816eaa0d42 100644 --- a/shared-bindings/analogio/AnalogFastIn.h +++ b/shared-bindings/analogio/AnalogFastIn.h @@ -32,7 +32,7 @@ extern const mp_obj_type_t analogio_analogfastin_type; -void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); +void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate); void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self); bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self); bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self); From 35aa8e440f66e40b9dab17c32f296b367e943788 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Thu, 11 Aug 2022 18:02:05 -0400 Subject: [PATCH 05/31] Fixing commits after non rebase --- frozen/circuitpython-stage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozen/circuitpython-stage b/frozen/circuitpython-stage index 9a8338b3bd..f993d5fac6 160000 --- a/frozen/circuitpython-stage +++ b/frozen/circuitpython-stage @@ -1 +1 @@ -Subproject commit 9a8338b3bdaeac9eeb5b74d147107c67db33fdac +Subproject commit f993d5fac69f3a0cfa33988268666c462b72c0ec From 81c7ba899d90aa06b752398b6eb7eca72b09f5d4 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Fri, 12 Aug 2022 13:57:45 -0400 Subject: [PATCH 06/31] Copyright Update/Acknowledgements --- .../common-hal/analogio/AnalogFastIn.c | 4 ++- .../common-hal/analogio/AnalogFastIn.h | 26 +++---------------- shared-bindings/analogio/AnalogFastIn.c | 3 ++- shared-bindings/analogio/AnalogFastIn.h | 3 ++- 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c index 1d08956bd4..d1145bb4fb 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c @@ -3,7 +3,9 @@ * * The MIT License (MIT) * - * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * Taken from AnalogIn by Scott Shawcroft for Adafruit Industries + * Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h index 3b8b8f0da9..87508949e6 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h @@ -3,7 +3,9 @@ * * The MIT License (MIT) * - * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * Taken from AnalogIn by Scott Shawcroft for Adafruit Industries + * Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -44,30 +46,8 @@ typedef struct { uint8_t chan; uint dma_chan; dma_channel_config cfg; - // data_size = DMA_SIZE_8; // - default DMA_SIZE_8 - // data_size = DMA_SIZE_16; // - default DMA_SIZE_16 - // Either 12 bits in 16 or 12 over 2 bytes or truncate 12 to 8 in 8 - // Either B or H, default array.array("h", [0]*length) "h"==short (16 bits signed) } analogio_analogfastin_obj_t; -// void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) -/* -void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, - const mcu_pin_obj_t *pin, - uint8_t *buffer, - uint32_t len, - uint8_t bytes_per_sample, - bool samples_signed, - uint32_t sample_rate) { - self->pin = pin; - self->buffer = buffer; - self->len = len; - //self->bits_per_sample = bytes_per_sample * 8; - self->samples_signed = samples_signed; - self->sample_rate = sample_rate; -} -*/ - void analogfastin_init(void); #endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c index 4fda809e0b..602abf67ed 100644 --- a/shared-bindings/analogio/AnalogFastIn.c +++ b/shared-bindings/analogio/AnalogFastIn.c @@ -3,7 +3,8 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * Taken from AnalogIn by Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/analogio/AnalogFastIn.h index 816eaa0d42..04ab0644f1 100644 --- a/shared-bindings/analogio/AnalogFastIn.h +++ b/shared-bindings/analogio/AnalogFastIn.h @@ -3,7 +3,8 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * Taken from AnalogIn by Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From 8cd12b2478f8bf95a18794f46618a47b2a53ac50 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Fri, 12 Aug 2022 16:01:58 -0400 Subject: [PATCH 07/31] Copyright Change --- ports/raspberrypi/common-hal/analogio/AnalogFastIn.c | 2 +- ports/raspberrypi/common-hal/analogio/AnalogFastIn.h | 2 +- shared-bindings/analogio/AnalogFastIn.c | 2 +- shared-bindings/analogio/AnalogFastIn.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c index d1145bb4fb..68ca742cdd 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. * Taken from AnalogIn by Scott Shawcroft for Adafruit Industries * Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd. * diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h index 87508949e6..343579fb47 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. * Taken from AnalogIn by Scott Shawcroft for Adafruit Industries * Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd. * diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c index 602abf67ed..3eb6ce5d1d 100644 --- a/shared-bindings/analogio/AnalogFastIn.c +++ b/shared-bindings/analogio/AnalogFastIn.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. * Taken from AnalogIn by Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/analogio/AnalogFastIn.h index 04ab0644f1..e2b83647c7 100644 --- a/shared-bindings/analogio/AnalogFastIn.h +++ b/shared-bindings/analogio/AnalogFastIn.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2022 Lee A. Atkinson + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. * Taken from AnalogIn by Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy From 4542c801b04a773b0a3daa9bff2e319d0c45fb07 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sun, 14 Aug 2022 13:03:33 -0400 Subject: [PATCH 08/31] Tidying up loose endson draft PR --- .../common-hal/analogio/AnalogFastIn.c | 7 ++-- ports/raspberrypi/mpconfigport.mk | 3 ++ py/circuitpy_defns.mk | 2 ++ shared-bindings/analogio/AnalogFastIn.c | 32 ++++++++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c index 68ca742cdd..bc72e940d1 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c +++ b/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c @@ -39,15 +39,12 @@ #define ADC_FIRST_PIN_NUMBER 26 #define ADC_PIN_COUNT 4 -// Channel 0 is GPIO26 -#define CAPTURE_CHANNEL 0 -#define CAPTURE_DEPTH 1000 -uint8_t capture_buf[CAPTURE_DEPTH]; void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate) { // Set pin and channel self->pin = pin; + claim_pin(pin); self->chan = pin->number - ADC_FIRST_PIN_NUMBER; // Checks on chan value here @@ -70,6 +67,7 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel adc_init(); adc_gpio_init(pin->number); adc_select_input(self->chan); // chan = pin - 26 ?? + // adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER); adc_fifo_setup( true, // Write each completed conversion to the sample FIFO @@ -108,6 +106,7 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel // clear any previous activity adc_fifo_drain(); adc_run(false); + } diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 1c4de50c00..1ef4fa45ff 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -26,6 +26,9 @@ CIRCUITPY_NVM = 1 CIRCUITPY_PULSEIO ?= 1 CIRCUITPY_WATCHDOG ?= 1 +# Use of analogio +CIRCUITPYTHON_ANALOGFASTIN = 1 + # Audio via PWM CIRCUITPY_AUDIOIO = 0 CIRCUITPY_AUDIOBUSIO ?= 1 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 7ee6c17588..5c5f05ca3a 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -390,7 +390,9 @@ SRC_COMMON_HAL_ALL = \ alarm/time/TimeAlarm.c \ alarm/touch/TouchAlarm.c \ analogio/AnalogIn.c \ +#ifdef CIRCUITPYTHON_ANALOGFASTIN analogio/AnalogFastIn.c \ +#endif analogio/AnalogOut.c \ analogio/__init__.c \ audiobusio/I2SOut.c \ diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c index 3eb6ce5d1d..e489fa497e 100644 --- a/shared-bindings/analogio/AnalogFastIn.c +++ b/shared-bindings/analogio/AnalogFastIn.c @@ -37,7 +37,37 @@ #include "shared-bindings/analogio/AnalogFastIn.h" #include "shared-bindings/util.h" -// pin, buffer, rate + + +//| class AnalogFastIn: +//| """Read analog voltage levels quickly using DMA Capture""" +//| +//| def __init__(self, pin: microcontroller.Pin, buffer: ReadableBuffer, *, sample_rate: int = 500000) -> None: +//| """Use the AnalogFastIn on the given pin. Fill the given buffer from ADC read values at the supplied +/// sample_rate. +/// +//| :param ~microcontroller.Pin pin: the pin to read from""" +//| :param ~circuitpython_typing.WriteableBuffer buffer: A buffer for samples +//| :param int sample_rate: The desired playback sample rate +/// +//| Usage:: +/// +//| import board +//| import analogio +//| import array +/// +//| length = 1000 +//| mybuffer = array.array("H", [0] * length) +//| fadc = analogio.AnalogFastIn(board.GP26, mybuffer) +//| fadc.capture() +//| fadc.deinit() +//| for i in range(length): +//| print(i, mybuffer[i]) +/// +/// (Future) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting. +/// """ +//| ... +/// STATIC void validate_rate(mp_float_t rate) { if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) { mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second")); From b2c6bcdf6d421295936f569786d191eb26d3c8e1 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sun, 14 Aug 2022 14:22:22 -0400 Subject: [PATCH 09/31] Fixing py/circuitpy_defns.mk --- py/circuitpy_defns.mk | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 5c5f05ca3a..5fc6b1fa16 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -390,9 +390,6 @@ SRC_COMMON_HAL_ALL = \ alarm/time/TimeAlarm.c \ alarm/touch/TouchAlarm.c \ analogio/AnalogIn.c \ -#ifdef CIRCUITPYTHON_ANALOGFASTIN - analogio/AnalogFastIn.c \ -#endif analogio/AnalogOut.c \ analogio/__init__.c \ audiobusio/I2SOut.c \ @@ -482,6 +479,13 @@ SRC_C += \ endif +ifeq ($(CIRCUITPYTHON_ANALOGFASTIN),1) +# Needed for AnalogFastIn +SRC_COMMON_HAL_ALL += \ + analogio/AnalogFastIn.c \ + +endif + SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL)) From c53c1c0927601209fec40fbb5ed77f086a9fd7ca Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sun, 14 Aug 2022 18:49:59 -0400 Subject: [PATCH 10/31] Adding py/circuitpy_mpconfig.mk --- py/circuitpy_mpconfig.mk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 90bf0ce9e8..f391128fd3 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -65,6 +65,9 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM) CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) +CIRCUITPY_ANALOGFASTIN ?= 0 +CFLAGS += -DCIRCUITPY_ANALOGFASTIN=$(CIRCUITPY_ANALOGFASTIN) + CIRCUITPY_ATEXIT ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_ATEXIT=$(CIRCUITPY_ATEXIT) From 3a45a8eae6882e2efb1b27ea1132037c84cb2fbb Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Mon, 15 Aug 2022 07:52:34 -0400 Subject: [PATCH 11/31] Adding ifdef in __init__.c --- shared-bindings/analogio/__init__.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/shared-bindings/analogio/__init__.c b/shared-bindings/analogio/__init__.c index 0033b2a66d..c4cca3c51d 100644 --- a/shared-bindings/analogio/__init__.c +++ b/shared-bindings/analogio/__init__.c @@ -32,9 +32,12 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/analogio/__init__.h" #include "shared-bindings/analogio/AnalogIn.h" -#include "shared-bindings/analogio/AnalogFastIn.h" #include "shared-bindings/analogio/AnalogOut.h" +#ifdef CIRCUITPY_ANALOGFASTIN +#include "shared-bindings/analogio/AnalogFastIn.h" +#endif + //| """Analog hardware support //| //| The `analogio` module contains classes to provide access to analog IO @@ -71,8 +74,10 @@ STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) }, { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) }, - { MP_ROM_QSTR(MP_QSTR_AnalogFastIn), MP_ROM_PTR(&analogio_analogfastin_type) }, { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) }, + #ifdef CIRCUITPY_ANALOGFASTIN + { MP_ROM_QSTR(MP_QSTR_AnalogFastIn), MP_ROM_PTR(&analogio_analogfastin_type) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(analogio_module_globals, analogio_module_globals_table); From 1836a14fc6ced2847fdc424619a8f3d0825e4598 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Mon, 15 Aug 2022 10:23:16 -0400 Subject: [PATCH 12/31] Adding ifdef in AnalogFastIn.h --- shared-bindings/analogio/AnalogFastIn.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/analogio/AnalogFastIn.h index e2b83647c7..072f56b976 100644 --- a/shared-bindings/analogio/AnalogFastIn.h +++ b/shared-bindings/analogio/AnalogFastIn.h @@ -29,6 +29,7 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H #include "common-hal/microcontroller/Pin.h" +#ifdef CIRCUITPY_ANALOGFASTIN #include "common-hal/analogio/AnalogFastIn.h" extern const mp_obj_type_t analogio_analogfastin_type; @@ -37,4 +38,5 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self); bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self); bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self); +#endif #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H__ From f91af513b7349c10733f8779e6913cca18fb10db Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Thu, 18 Aug 2022 16:23:17 -0400 Subject: [PATCH 13/31] Introduced odule adcbuffer / removed analogio/AnalogFastIn --- locale/circuitpython.pot | 14 +- .../BufferedInput.c} | 53 ++--- .../BufferedInput.h} | 21 +- .../common-hal/adcbuffer/__init__.c | 1 + ports/raspberrypi/mpconfigport.mk | 4 +- py/circuitpy_defns.mk | 9 +- py/circuitpy_mpconfig.mk | 4 +- shared-bindings/adcbuffer/BufferdInput.c | 197 ++++++++++++++++++ .../BufferdInput.h} | 24 +-- shared-bindings/adcbuffer/__init__.c | 84 ++++++++ shared-bindings/adcbuffer/__init__.h | 34 +++ shared-bindings/analogio/AnalogFastIn.c | 197 ------------------ shared-bindings/analogio/__init__.c | 7 - 13 files changed, 387 insertions(+), 262 deletions(-) rename ports/raspberrypi/common-hal/{analogio/AnalogFastIn.c => adcbuffer/BufferedInput.c} (74%) rename ports/raspberrypi/common-hal/{analogio/AnalogFastIn.h => adcbuffer/BufferedInput.h} (71%) create mode 100644 ports/raspberrypi/common-hal/adcbuffer/__init__.c create mode 100644 shared-bindings/adcbuffer/BufferdInput.c rename shared-bindings/{analogio/AnalogFastIn.h => adcbuffer/BufferdInput.h} (61%) create mode 100644 shared-bindings/adcbuffer/__init__.c create mode 100644 shared-bindings/adcbuffer/__init__.h delete mode 100644 shared-bindings/analogio/AnalogFastIn.c diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index a2e2826cd1..3f97fb8b1d 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -398,12 +398,10 @@ msgstr "" msgid "All CAN peripherals are in use" msgstr "" -#: ports/espressif/common-hal/busio/I2C.c ports/nrf/common-hal/busio/I2C.c -msgid "All I2C peripherals are in use" -msgstr "" - +#: ports/espressif/common-hal/busio/I2C.c #: ports/espressif/common-hal/i2ctarget/I2CTarget.c -msgid "All I2C targets are in use" +#: ports/nrf/common-hal/busio/I2C.c +msgid "All I2C peripherals are in use" msgstr "" #: ports/espressif/common-hal/countio/Counter.c @@ -1061,6 +1059,7 @@ msgid "I2C init error" msgstr "" #: ports/raspberrypi/common-hal/busio/I2C.c +#: ports/raspberrypi/common-hal/i2ctarget/I2CTarget.c msgid "I2C peripheral in use" msgstr "" @@ -1592,6 +1591,7 @@ msgid "Only one TouchAlarm can be set in deep sleep." msgstr "" #: ports/espressif/common-hal/i2ctarget/I2CTarget.c +#: ports/raspberrypi/common-hal/i2ctarget/I2CTarget.c msgid "Only one address is allowed" msgstr "" @@ -3863,11 +3863,11 @@ msgstr "" msgid "rsplit(None,n)" msgstr "" -#: shared-bindings/analogio/AnalogFastIn.c +#: shared-bindings/adcbuffer/BufferdInput.c msgid "sample rate must be 1.0-500000.0 per second" msgstr "" -#: shared-bindings/analogio/AnalogFastIn.c +#: shared-bindings/adcbuffer/BufferdInput.c #: shared-bindings/audiocore/RawSample.c msgid "" "sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or " diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c similarity index 74% rename from ports/raspberrypi/common-hal/analogio/AnalogFastIn.c rename to ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index bc72e940d1..1f187bcc26 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -4,8 +4,13 @@ * The MIT License (MIT) * * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. - * Taken from AnalogIn by Scott Shawcroft for Adafruit Industries - * Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd. + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. + * https://github.com/raspberrypi/pico-examples/blob/master/adc/dma_capture/dma_capture.c * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,38 +31,47 @@ * THE SOFTWARE. */ -#include "common-hal/analogio/AnalogFastIn.h" -#include "shared-bindings/analogio/AnalogFastIn.h" +#include "common-hal/adcbuffer/Bufferedinput.h" +#include "shared-bindings/adcbuffer/Bufferedinput.h" #include "shared-bindings/microcontroller/Pin.h" #include "py/runtime.h" #include "supervisor/shared/translate/translate.h" #include "src/rp2_common/hardware_adc/include/hardware/adc.h" #include "src/rp2_common/hardware_dma/include/hardware/dma.h" -// /sdk/src/rp2_common/hardware_dma/include/hardware/dma.h -// ports/raspberrypi/ -#include "sdk/src/common/pico_stdlib/include/pico/stdlib.h" +#include "src/common/pico_stdlib/include/pico/stdlib.h" #define ADC_FIRST_PIN_NUMBER 26 #define ADC_PIN_COUNT 4 -void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate) { +void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate) { // Set pin and channel self->pin = pin; claim_pin(pin); + + // validate pin number + if (pin->number < ADC_FIRST_PIN_NUMBER) { + and(pin->number >= ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) { + raise_ValueError_invalid_pins(); + } + } + // TODO: find a wat to accept ADC4 for temperature self->chan = pin->number - ADC_FIRST_PIN_NUMBER; - // Checks on chan value here + // TODO: Checks on chan value here // Set buffer and length self->buffer = buffer; self->len = len; - // checks on length here + // TODO: checks on length here // uint8_t bytes_per_sample // Set sample rate // self->bits_per_sample = bytes_per_sample * 8; + // TODO: Possibly check Rate values here, already u_int + // NOTE: Anything over 500000 for RP2040 will not + // exceed DMA conversion sampling rate. self->sample_rate = sample_rate; // Standard IO Init @@ -110,12 +124,12 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel } -bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self) { +bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self) { return self->pin == NULL; } -void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self) { - if (common_hal_analogio_analogfastin_deinited(self)) { +void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self) { + if (common_hal_adcbuffer_bufferedinput_deinited(self)) { return; } @@ -127,19 +141,10 @@ void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self) dma_channel_unclaim(self->dma_chan); } -// ================================================================ -// capture() -// make this a bool so that later we can perform integrity checking -// ================================================================ -bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self) { +bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { + // uint32_t cdl = self->len / 2 - 1; - // CONSIDER THESE ISSUES - // uint16_t value = adc_read(); - // Stretch 12-bit ADC reading to 16-bit range - // return (value << 4) | (value >> 8); - - uint32_t cdl = self->len / 2 - 1; dma_channel_configure(self->dma_chan, &(self->cfg), self->buffer, // dst &adc_hw->fifo, // src diff --git a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h similarity index 71% rename from ports/raspberrypi/common-hal/analogio/AnalogFastIn.h rename to ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h index 343579fb47..7fd8f50653 100644 --- a/ports/raspberrypi/common-hal/analogio/AnalogFastIn.h +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h @@ -4,8 +4,13 @@ * The MIT License (MIT) * * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. - * Taken from AnalogIn by Scott Shawcroft for Adafruit Industries - * Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd. + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. + * https://github.com/raspberrypi/pico-examples/blob/master/adc/dma_capture/dma_capture.c * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,15 +31,15 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H -#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H +#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H +#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H #include "common-hal/microcontroller/Pin.h" #include "src/rp2_common/hardware_dma/include/hardware/dma.h" #include "py/obj.h" -// We can extend the struct without impact to existing code +// This is the adcbuffer object typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -46,8 +51,8 @@ typedef struct { uint8_t chan; uint dma_chan; dma_channel_config cfg; -} analogio_analogfastin_obj_t; +} adcbuffer_bufferedinput_obj_t; -void analogfastin_init(void); +void bufferedinput_init(void); -#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H +#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H diff --git a/ports/raspberrypi/common-hal/adcbuffer/__init__.c b/ports/raspberrypi/common-hal/adcbuffer/__init__.c new file mode 100644 index 0000000000..eea58c77d6 --- /dev/null +++ b/ports/raspberrypi/common-hal/adcbuffer/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 3d275b04cb..ba6869169f 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -26,8 +26,8 @@ CIRCUITPY_NVM = 1 CIRCUITPY_PULSEIO ?= 1 CIRCUITPY_WATCHDOG ?= 1 -# Use of analogio -CIRCUITPYTHON_ANALOGFASTIN = 1 +# Use of adcbuffer +CIRCUITPYTHON_ADCBUFFER = 1 # Audio via PWM CIRCUITPY_AUDIOIO = 0 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 5fc6b1fa16..b23016c7a7 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -102,6 +102,9 @@ endif ### # Select which builtin modules to compile and include. +ifeq ($(CIRCUITPY_ADCBUFFER),1) +SRC_PATTERNS += adcbuffer/% +endif ifeq ($(CIRCUITPY_AESIO),1) SRC_PATTERNS += aesio/% endif @@ -479,10 +482,10 @@ SRC_C += \ endif -ifeq ($(CIRCUITPYTHON_ANALOGFASTIN),1) -# Needed for AnalogFastIn +ifeq ($(CIRCUITPYTHON_ADCBUFFER),1) +# Needed for ADCBUFFER SRC_COMMON_HAL_ALL += \ - analogio/AnalogFastIn.c \ + adcbuffer/BufferedInput.c \ endif diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index f391128fd3..1d4a5765f8 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -65,8 +65,8 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM) CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) -CIRCUITPY_ANALOGFASTIN ?= 0 -CFLAGS += -DCIRCUITPY_ANALOGFASTIN=$(CIRCUITPY_ANALOGFASTIN) +CIRCUITPY_ADCBUFFER ?= 0 +CFLAGS += -DCIRCUITPY_ADCBUFFER=$(CIRCUITPY_ADCBUFFER) CIRCUITPY_ATEXIT ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_ATEXIT=$(CIRCUITPY_ATEXIT) diff --git a/shared-bindings/adcbuffer/BufferdInput.c b/shared-bindings/adcbuffer/BufferdInput.c new file mode 100644 index 0000000000..e47f5e5d7d --- /dev/null +++ b/shared-bindings/adcbuffer/BufferdInput.c @@ -0,0 +1,197 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. + * + * TODO: Based on analogio/AnalogIn.c from Scott Shaw + * + * 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 "shared/runtime/context_manager_helpers.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "py/nlr.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/adcbuffer/BufferedInput.h" +#include "shared-bindings/util.h" + +/// class BufferedInput: +/// """Input analog voltage level to supplied buffer using DMA Capture""" +/// +/// def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None: +/// """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied +/// sample_rate. +/// +/// :param ~microcontroller.Pin pin: the pin to read from""" +/// :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples +/// :param ~int sample_rate: rate: The desired playback sample rate +/// +/// Usage:: +/// +/// import board +/// import adcbuffer +/// import array +/// +/// length = 1000 +/// mybuffer = array.array("H", [0] * length) +/// rate = 500000 +/// adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate) +/// adcbuf.readmultiple() +/// adcbuf.deinit() +/// for i in range(length): +/// print(i, mybuffer[i]) +/// +/// (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting. +/// (TODO) Provide mechanism to read CPU Temperature +/// """ +/// ... +/// +STATIC void validate_rate(mp_float_t rate) { + if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) { + mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second")); + } +} + +STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_pin, ARG_buffer, ARG_sample_rate }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_sample_rate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_int = 500000} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Validate Pin + const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + + // Buffer Pointer defined and allocated by user + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); + + // signed or unsigned, byte per sample + bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h'; + uint8_t bytes_per_sample = 1; + + // Bytes Per Sample + if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { + bytes_per_sample = 2; + } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { + mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'")); + } + + // Validate sample rate here + mp_float_t sample_rate = mp_obj_get_float(args[ARG_sample_rate].u_obj); + validate_rate(sample_rate); + + // Create local object + adcbuffer_bufferedinput_obj_t *self = m_new_obj(adcbuffer_bufferedinput_obj_t); + self->base.type = &adcbuffer_bufferedinput_type; + + // Call local intereface in ports/common-hal/adcbuffer + common_hal_adcbuffer_bufferedinput_construct(self, + pin, + ((uint8_t *)bufinfo.buf), + bufinfo.len, + bytes_per_sample, + signed_samples, + sample_rate + ); + + return MP_OBJ_FROM_PTR(self); +} + +/// def deinit(self) -> None: +/// """Turn off the BufferedInput and release the pin for other use.""" +/// ... +/// +STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) { + adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_adcbuffer_bufferedinput_deinit(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_deinit_obj, adcbuffer_bufferedinput_deinit); + +STATIC void check_for_deinit(adcbuffer_bufferedinput_obj_t *self) { + if (common_hal_adcbuffer_bufferedinput_deinited(self)) { + raise_deinited_error(); + } +} +/// def __enter__(self) -> BufferedInput: +/// """No-op used by Context Managers.""" +/// ... +/// +/// Provided by context manager helper. +/// +/// def __exit__(self) -> None: +/// """Automatically deinitializes the hardware when exiting a context. See +/// :ref:`lifetime-and-contextmanagers` for more info.""" +/// ... +/// +STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_adcbuffer_bufferedinput_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__); + +/// value: int +/// """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only) +/// +/// Even if the underlying analog to digital converter (ADC) is lower +/// resolution, the value is 16-bit.""" +/// +STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { + adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + return MP_OBJ_NEW_SMALL_INT(common_hal_adcbuffer_bufferedinput_readmultiple(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple); + +/// MP_PROPERTY_GETTER(adcbuffer_bufferedinput_value_obj, +/// (mp_obj_t)&adcbuffer_bufferedinput_get_value_obj); +/// +/// reference_voltage: float +/// """The maximum voltage measurable (also known as the reference voltage) as a +/// `float` in Volts. Note the ADC value may not scale to the actual voltage linearly +/// at ends of the analog range.""" +/// + +STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_readmultiple), MP_ROM_PTR(&adcbuffer_bufferedinput_readmultiple_obj)}, + +}; + +STATIC MP_DEFINE_CONST_DICT(adcbuffer_bufferedinput_locals_dict, adcbuffer_bufferedinput_locals_dict_table); + +const mp_obj_type_t adcbuffer_bufferedinput_type = { + { &mp_type_type }, + .name = MP_QSTR_BufferedInput, + .make_new = adcbuffer_bufferedinput_make_new, + .locals_dict = (mp_obj_t)&adcbuffer_bufferedinput_locals_dict, +}; diff --git a/shared-bindings/analogio/AnalogFastIn.h b/shared-bindings/adcbuffer/BufferdInput.h similarity index 61% rename from shared-bindings/analogio/AnalogFastIn.h rename to shared-bindings/adcbuffer/BufferdInput.h index 072f56b976..24012eadfb 100644 --- a/shared-bindings/analogio/AnalogFastIn.h +++ b/shared-bindings/adcbuffer/BufferdInput.h @@ -4,7 +4,6 @@ * The MIT License (MIT) * * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. - * Taken from AnalogIn by Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,18 +24,19 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H #include "common-hal/microcontroller/Pin.h" -#ifdef CIRCUITPY_ANALOGFASTIN -#include "common-hal/analogio/AnalogFastIn.h" +#include "common-hal/adcbuffer/Bufferedinput.h" -extern const mp_obj_type_t analogio_analogfastin_type; +// #ifdef CIRCUITPY_BUFFEREDINPUT #endif -void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate); -void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self); -bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self); -bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self); -#endif -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H__ +extern const mp_obj_type_t adcbuffer_bufferedinput_type; + +void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate); +void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self); +bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self); +bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H__ diff --git a/shared-bindings/adcbuffer/__init__.c b/shared-bindings/adcbuffer/__init__.c new file mode 100644 index 0000000000..3f81153500 --- /dev/null +++ b/shared-bindings/adcbuffer/__init__.c @@ -0,0 +1,84 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. + * + * 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 "py/obj.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/adcbuffer/__init__.h" +#include "shared-bindings/adcbuffer/BufferedInput.h" + +// #ifdef CIRCUITPY_BUFFEREDINPUT#endif + +//| """Analog buffered hardware support +//| +//| The `adcbuffer` module contains classes to provide access to analog-to-digital +//| conversion and digital-to-analog (DAC) for multiple value transfer. +//| +//| All classes change hardware state and should be deinitialized when they +//| are no longer needed if the program continues after use. To do so, either +//| call :py:meth:`!deinit` or use a context manager. See +//| :ref:`lifetime-and-contextmanagers` for more info. +//| +//| For example:: +//| +//| import adcbuffer +/// import array +//| from board import * +//| +/// length = 5000000 +/// mybuffer = array.array("H", [0] * length) +//| adcbuf_obj = adcbuffer.BufferdInPut(GP26, mybuffer, length) +//| adcbuffer.readmultiple() +//| print(*mybuffer) +//| adcbuf_obj.deinit() +//| +//| This example will initialize the the device, read and fill +//| :py:data:`~adcbuffer.BufferdInPut` to mybuffer and then +//| :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional +//| because CircuitPython will do it automatically after the program finishes. +//| +//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials +//| Learn guide `_ +//| +//| TODO: For more information on using `adcbuffer`, see `this additional Learn guide +//| `_ +//| """ +//| + +STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) }, + { MP_ROM_QSTR(MP_QSTR_Bufferedinput), MP_ROM_PTR(&adcbuffer_bufferedinput_type) }, + // #ifdef CIRCUITPY_BUFFEREDINPUT #endif +}; + +STATIC MP_DEFINE_CONST_DICT(adcbuffer_module_globals, adcbuffer_module_globals_table); + +const mp_obj_module_t adcbuffer_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&adcbuffer_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_adcbuffer, adcbuffer_module, CIRCUITPY_ADCBUFFER); diff --git a/shared-bindings/adcbuffer/__init__.h b/shared-bindings/adcbuffer/__init__.h new file mode 100644 index 0000000000..c271d71273 --- /dev/null +++ b/shared-bindings/adcbuffer/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. + * + * 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_SHARED_BINDINGS_ADCBUFFER___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H + +#include "py/obj.h" + +// Nothing now. + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H diff --git a/shared-bindings/analogio/AnalogFastIn.c b/shared-bindings/analogio/AnalogFastIn.c deleted file mode 100644 index e489fa497e..0000000000 --- a/shared-bindings/analogio/AnalogFastIn.c +++ /dev/null @@ -1,197 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. - * Taken from AnalogIn by Damien P. George - * - * 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 "shared/runtime/context_manager_helpers.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "py/nlr.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/analogio/AnalogFastIn.h" -#include "shared-bindings/util.h" - - - -//| class AnalogFastIn: -//| """Read analog voltage levels quickly using DMA Capture""" -//| -//| def __init__(self, pin: microcontroller.Pin, buffer: ReadableBuffer, *, sample_rate: int = 500000) -> None: -//| """Use the AnalogFastIn on the given pin. Fill the given buffer from ADC read values at the supplied -/// sample_rate. -/// -//| :param ~microcontroller.Pin pin: the pin to read from""" -//| :param ~circuitpython_typing.WriteableBuffer buffer: A buffer for samples -//| :param int sample_rate: The desired playback sample rate -/// -//| Usage:: -/// -//| import board -//| import analogio -//| import array -/// -//| length = 1000 -//| mybuffer = array.array("H", [0] * length) -//| fadc = analogio.AnalogFastIn(board.GP26, mybuffer) -//| fadc.capture() -//| fadc.deinit() -//| for i in range(length): -//| print(i, mybuffer[i]) -/// -/// (Future) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting. -/// """ -//| ... -/// -STATIC void validate_rate(mp_float_t rate) { - if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) { - mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second")); - } -} - -STATIC mp_obj_t analogio_analogfastin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_pin, ARG_buffer, ARG_sample_rate }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_sample_rate, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - // Validate Pin - const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); - - // Buffer Pointer defined and allocated by user - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); - - // signed or unsigned, byte per sample - bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h'; - uint8_t bytes_per_sample = 1; - - // Bytes Per Sample - if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { - bytes_per_sample = 2; - } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { - mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'")); - } - - // Validate sample rate here - mp_float_t sample_rate = mp_obj_get_float(args[ARG_sample_rate].u_obj); - validate_rate(sample_rate); - - // Create local object - analogio_analogfastin_obj_t *self = m_new_obj(analogio_analogfastin_obj_t); - self->base.type = &analogio_analogfastin_type; - - // Call local intereface in ports/common-hal/analogio - common_hal_analogio_analogfastin_construct(self, - pin, - ((uint8_t *)bufinfo.buf), - bufinfo.len, - bytes_per_sample, - signed_samples, - sample_rate - ); - - return MP_OBJ_FROM_PTR(self); -} - -//| def deinit(self) -> None: -//| """Turn off the AnalogFastIn and release the pin for other use.""" -//| ... -//| -STATIC mp_obj_t analogio_analogfastin_deinit(mp_obj_t self_in) { - analogio_analogfastin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_analogio_analogfastin_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogfastin_deinit_obj, analogio_analogfastin_deinit); - -STATIC void check_for_deinit(analogio_analogfastin_obj_t *self) { - if (common_hal_analogio_analogfastin_deinited(self)) { - raise_deinited_error(); - } -} -//| def __enter__(self) -> AnalogFastIn: -//| """No-op used by Context Managers.""" -//| ... -//| -// Provided by context manager helper. - -//| def __exit__(self) -> None: -//| """Automatically deinitializes the hardware when exiting a context. See -//| :ref:`lifetime-and-contextmanagers` for more info.""" -//| ... -//| -STATIC mp_obj_t analogio_analogfastin___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_analogio_analogfastin_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogfastin___exit___obj, 4, 4, analogio_analogfastin___exit__); - -//| value: int -//| """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only) -//| -//| Even if the underlying analog to digital converter (ADC) is lower -//| resolution, the value is 16-bit.""" -//| -STATIC mp_obj_t analogio_analogfastin_obj_capture(mp_obj_t self_in) { - analogio_analogfastin_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogfastin_capture(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogfastin_capture_obj, analogio_analogfastin_obj_capture); - -// MP_PROPERTY_GETTER(analogio_analogfastin_value_obj, -// (mp_obj_t)&analogio_analogfastin_get_value_obj); - -//| reference_voltage: float -//| """The maximum voltage measurable (also known as the reference voltage) as a -//| `float` in Volts. Note the ADC value may not scale to the actual voltage linearly -//| at ends of the analog range.""" -//| - -STATIC const mp_rom_map_elem_t analogio_analogfastin_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogfastin_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogfastin___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_capture), MP_ROM_PTR(&analogio_analogfastin_capture_obj)}, - -}; - -STATIC MP_DEFINE_CONST_DICT(analogio_analogfastin_locals_dict, analogio_analogfastin_locals_dict_table); - -const mp_obj_type_t analogio_analogfastin_type = { - { &mp_type_type }, - .name = MP_QSTR_AnalogFastIn, - .make_new = analogio_analogfastin_make_new, - .locals_dict = (mp_obj_t)&analogio_analogfastin_locals_dict, -}; diff --git a/shared-bindings/analogio/__init__.c b/shared-bindings/analogio/__init__.c index c4cca3c51d..eb956d0eb7 100644 --- a/shared-bindings/analogio/__init__.c +++ b/shared-bindings/analogio/__init__.c @@ -34,10 +34,6 @@ #include "shared-bindings/analogio/AnalogIn.h" #include "shared-bindings/analogio/AnalogOut.h" -#ifdef CIRCUITPY_ANALOGFASTIN -#include "shared-bindings/analogio/AnalogFastIn.h" -#endif - //| """Analog hardware support //| //| The `analogio` module contains classes to provide access to analog IO @@ -75,9 +71,6 @@ STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) }, { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) }, { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) }, - #ifdef CIRCUITPY_ANALOGFASTIN - { MP_ROM_QSTR(MP_QSTR_AnalogFastIn), MP_ROM_PTR(&analogio_analogfastin_type) }, - #endif }; STATIC MP_DEFINE_CONST_DICT(analogio_module_globals, analogio_module_globals_table); From d3c3c9eac4b517df363ff5e2c5369500469ec2c5 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Thu, 18 Aug 2022 19:10:16 -0400 Subject: [PATCH 14/31] Tidy a few things --- .../common-hal/adcbuffer/BufferedInput.c | 4 +- .../common-hal/adcbuffer/__init__.c | 2 +- py/circuitpy_defns.mk | 11 +-- py/circuitpy_mpconfig.mk | 2 +- shared-bindings/adcbuffer/BufferdInput.h | 2 +- shared-bindings/adcbuffer/__init__.c | 68 +++++++++---------- 6 files changed, 45 insertions(+), 44 deletions(-) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index 1f187bcc26..d4ae246089 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -31,8 +31,8 @@ * THE SOFTWARE. */ -#include "common-hal/adcbuffer/Bufferedinput.h" -#include "shared-bindings/adcbuffer/Bufferedinput.h" +#include "common-hal/adcbuffer/BufferedInput.h" +#include "shared-bindings/adcbuffer/BufferedInput.h" #include "shared-bindings/microcontroller/Pin.h" #include "py/runtime.h" #include "supervisor/shared/translate/translate.h" diff --git a/ports/raspberrypi/common-hal/adcbuffer/__init__.c b/ports/raspberrypi/common-hal/adcbuffer/__init__.c index eea58c77d6..3473642698 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/__init__.c +++ b/ports/raspberrypi/common-hal/adcbuffer/__init__.c @@ -1 +1 @@ -// No analogio module functions. +// No adcbuffer module functions. diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index b23016c7a7..515b5bf04c 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -387,6 +387,7 @@ SRC_COMMON_HAL_ALL = \ _bleio/__init__.c \ _pew/PewPew.c \ _pew/__init__.c \ + adcbuffer/BufferedInput.c \ alarm/SleepMemory.c \ alarm/__init__.c \ alarm/pin/PinAlarm.c \ @@ -482,12 +483,12 @@ SRC_C += \ endif -ifeq ($(CIRCUITPYTHON_ADCBUFFER),1) +#ifeq ($(CIRCUITPYTHON_ADCBUFFER),1) # Needed for ADCBUFFER -SRC_COMMON_HAL_ALL += \ - adcbuffer/BufferedInput.c \ - -endif +#SRC_COMMON_HAL_ALL += \ +# adcbuffer/BufferedInput.c \ +# +#endif SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL)) diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 1d4a5765f8..8738966a43 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -65,7 +65,7 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM) CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) -CIRCUITPY_ADCBUFFER ?= 0 +CIRCUITPY_ADCBUFFER ?= 1 CFLAGS += -DCIRCUITPY_ADCBUFFER=$(CIRCUITPY_ADCBUFFER) CIRCUITPY_ATEXIT ?= $(CIRCUITPY_FULL_BUILD) diff --git a/shared-bindings/adcbuffer/BufferdInput.h b/shared-bindings/adcbuffer/BufferdInput.h index 24012eadfb..593eee493c 100644 --- a/shared-bindings/adcbuffer/BufferdInput.h +++ b/shared-bindings/adcbuffer/BufferdInput.h @@ -28,7 +28,7 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H #include "common-hal/microcontroller/Pin.h" -#include "common-hal/adcbuffer/Bufferedinput.h" +#include "common-hal/adcbuffer/BufferedInput.h" // #ifdef CIRCUITPY_BUFFEREDINPUT #endif diff --git a/shared-bindings/adcbuffer/__init__.c b/shared-bindings/adcbuffer/__init__.c index 3f81153500..34b87125ce 100644 --- a/shared-bindings/adcbuffer/__init__.c +++ b/shared-bindings/adcbuffer/__init__.c @@ -30,47 +30,47 @@ #include "shared-bindings/adcbuffer/__init__.h" #include "shared-bindings/adcbuffer/BufferedInput.h" -// #ifdef CIRCUITPY_BUFFEREDINPUT#endif +/// #ifdef CIRCUITPY_BUFFEREDINPUT#endif -//| """Analog buffered hardware support -//| -//| The `adcbuffer` module contains classes to provide access to analog-to-digital -//| conversion and digital-to-analog (DAC) for multiple value transfer. -//| -//| All classes change hardware state and should be deinitialized when they -//| are no longer needed if the program continues after use. To do so, either -//| call :py:meth:`!deinit` or use a context manager. See -//| :ref:`lifetime-and-contextmanagers` for more info. -//| -//| For example:: -//| -//| import adcbuffer +/// """Analog buffered hardware support +/// +/// The `adcbuffer` module contains classes to provide access to analog-to-digital +/// conversion and digital-to-analog (DAC) for multiple value transfer. +/// +/// All classes change hardware state and should be deinitialized when they +/// are no longer needed if the program continues after use. To do so, either +/// call :py:meth:`!deinit` or use a context manager. See +/// :ref:`lifetime-and-contextmanagers` for more info. +/// +/// For example:: +/// +/// import adcbuffer /// import array -//| from board import * -//| +/// from board import * +/// /// length = 5000000 /// mybuffer = array.array("H", [0] * length) -//| adcbuf_obj = adcbuffer.BufferdInPut(GP26, mybuffer, length) -//| adcbuffer.readmultiple() -//| print(*mybuffer) -//| adcbuf_obj.deinit() -//| -//| This example will initialize the the device, read and fill -//| :py:data:`~adcbuffer.BufferdInPut` to mybuffer and then -//| :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional -//| because CircuitPython will do it automatically after the program finishes. -//| -//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials -//| Learn guide `_ -//| -//| TODO: For more information on using `adcbuffer`, see `this additional Learn guide -//| `_ -//| """ -//| +/// adcbuf_obj = adcbuffer.BufferedInPut(GP26, mybuffer, length) +/// adcbuffer.readmultiple() +/// print(*mybuffer) +/// adcbuf_obj.deinit() +/// +/// This example will initialize the the device, read and fill +/// :py:data:`~adcbuffer.BufferedInPut` to mybuffer and then +/// :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional +/// because CircuitPython will do it automatically after the program finishes. +/// +/// TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials +/// Learn guide `_ +/// +/// TODO: For more information on using `adcbuffer`, see `this additional Learn guide +/// `_ +/// """ +/// STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) }, - { MP_ROM_QSTR(MP_QSTR_Bufferedinput), MP_ROM_PTR(&adcbuffer_bufferedinput_type) }, + { MP_ROM_QSTR(MP_QSTR_BufferedInput), MP_ROM_PTR(&adcbuffer_bufferedinput_type) }, // #ifdef CIRCUITPY_BUFFEREDINPUT #endif }; From 68671399d16a0cd2f185df6ca9242642e286ec8e Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Fri, 19 Aug 2022 10:51:13 -0400 Subject: [PATCH 15/31] More formating --- ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c | 9 ++++----- .../adcbuffer/{BufferdInput.c => BufferedInput.c} | 0 .../adcbuffer/{BufferdInput.h => BufferedInput.h} | 0 3 files changed, 4 insertions(+), 5 deletions(-) rename shared-bindings/adcbuffer/{BufferdInput.c => BufferedInput.c} (100%) rename shared-bindings/adcbuffer/{BufferdInput.h => BufferedInput.h} (100%) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index d4ae246089..1e37e640f3 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -50,11 +50,10 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t claim_pin(pin); // validate pin number - if (pin->number < ADC_FIRST_PIN_NUMBER) { - and(pin->number >= ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) { - raise_ValueError_invalid_pins(); - } + if (pin->number < ADC_FIRST_PIN_NUMBER && pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) { + raise_ValueError_invalid_pins(); } + // TODO: find a wat to accept ADC4 for temperature self->chan = pin->number - ADC_FIRST_PIN_NUMBER; @@ -123,7 +122,6 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t } - bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self) { return self->pin == NULL; } @@ -144,6 +142,7 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { // uint32_t cdl = self->len / 2 - 1; + uint32_t cdl = self->len; dma_channel_configure(self->dma_chan, &(self->cfg), self->buffer, // dst diff --git a/shared-bindings/adcbuffer/BufferdInput.c b/shared-bindings/adcbuffer/BufferedInput.c similarity index 100% rename from shared-bindings/adcbuffer/BufferdInput.c rename to shared-bindings/adcbuffer/BufferedInput.c diff --git a/shared-bindings/adcbuffer/BufferdInput.h b/shared-bindings/adcbuffer/BufferedInput.h similarity index 100% rename from shared-bindings/adcbuffer/BufferdInput.h rename to shared-bindings/adcbuffer/BufferedInput.h From 61591ac136cd8c1160aabe9ed0ad674eeb0c56ef Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Fri, 19 Aug 2022 13:17:37 -0400 Subject: [PATCH 16/31] adding __init.c to circuitpy_defs.mk --- py/circuitpy_defns.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 515b5bf04c..b295647da6 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -388,6 +388,7 @@ SRC_COMMON_HAL_ALL = \ _pew/PewPew.c \ _pew/__init__.c \ adcbuffer/BufferedInput.c \ + adcbuffer/__init__.c \ alarm/SleepMemory.c \ alarm/__init__.c \ alarm/pin/PinAlarm.c \ From f5655dd48cf367a907ae9d2700746ad5cedbf497 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sun, 21 Aug 2022 11:44:40 -0400 Subject: [PATCH 17/31] Fixing Build errors/ DMA Xfer Rate --- .../common-hal/adcbuffer/BufferedInput.c | 26 ++++++++++++------- .../common-hal/adcbuffer/BufferedInput.h | 2 +- py/circuitpy_mpconfig.mk | 2 +- shared-bindings/adcbuffer/BufferedInput.c | 11 ++------ shared-bindings/adcbuffer/BufferedInput.h | 2 +- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index 1e37e640f3..50e043f58a 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -43,7 +43,7 @@ #define ADC_FIRST_PIN_NUMBER 26 #define ADC_PIN_COUNT 4 -void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate) { +void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { // Set pin and channel self->pin = pin; @@ -65,9 +65,10 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t // TODO: checks on length here - // uint8_t bytes_per_sample // Set sample rate - // self->bits_per_sample = bytes_per_sample * 8; + // NOTE: bits_per_sample = bytes_per_sample * 8; + self->bytes_per_sample = bytes_per_sample; + // TODO: Possibly check Rate values here, already u_int // NOTE: Anything over 500000 for RP2040 will not // exceed DMA conversion sampling rate. @@ -86,8 +87,8 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t true, // Write each completed conversion to the sample FIFO true, // Enable DMA data request (DREQ) 1, // DREQ (and IRQ) asserted when at least 1 sample present - false, // We won't see the ERR bit because of 8 bit reads; disable. // ?? - false // Shift each sample to 8 bits when pushing to FIFO // ?? + true, // We will see the ERR bit because of 12 bit reads; enabled. + false // Do not shift each sample to 8 bits when pushing to FIFO ); // Divisor of 0 -> full speed. Free-running capture with the divider is @@ -98,7 +99,7 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t // intervals). This is all timed by the 48 MHz ADC clock. // sample rate determines divisor, not zero. - adc_set_clkdiv(48000000.0 / self->sample_rate); + adc_set_clkdiv((float)48000000.0 / (float)self->sample_rate); // sleep_ms(1000); // Set up the DMA to start transferring data as soon as it appears in FIFO @@ -109,7 +110,11 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t self->cfg = dma_channel_get_default_config(dma_chan); // Reading from constant address, writing to incrementing byte addresses - channel_config_set_transfer_data_size(&(self->cfg), DMA_SIZE_16); + if (self->bytes_per_sample == 1) { + channel_config_set_transfer_data_size(&(self->cfg), DMA_SIZE_8); + } else if (self->bytes_per_sample == 2) { + channel_config_set_transfer_data_size(&(self->cfg), DMA_SIZE_16); + } channel_config_set_read_increment(&(self->cfg), false); channel_config_set_write_increment(&(self->cfg), true); @@ -141,13 +146,14 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { - // uint32_t cdl = self->len / 2 - 1; - uint32_t cdl = self->len; + // uint32_t cdl = self->len/2 + 1; + // uint32_t cdl = 3*self->len/4; //BAD + uint32_t cdl = self->len * self->bytes_per_sample; dma_channel_configure(self->dma_chan, &(self->cfg), self->buffer, // dst &adc_hw->fifo, // src - cdl, // CAPTURE_DEPTH, // transfer count + cdl, // transfer count true // start immediately ); diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h index 7fd8f50653..292fecc8a4 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h @@ -47,7 +47,7 @@ typedef struct { uint32_t len; uint8_t bytes_per_sample; bool samples_signed; - mp_float_t sample_rate; + uint32_t sample_rate; uint8_t chan; uint dma_chan; dma_channel_config cfg; diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 8738966a43..1d4a5765f8 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -65,7 +65,7 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM) CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) -CIRCUITPY_ADCBUFFER ?= 1 +CIRCUITPY_ADCBUFFER ?= 0 CFLAGS += -DCIRCUITPY_ADCBUFFER=$(CIRCUITPY_ADCBUFFER) CIRCUITPY_ATEXIT ?= $(CIRCUITPY_FULL_BUILD) diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/adcbuffer/BufferedInput.c index e47f5e5d7d..67647e680d 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/adcbuffer/BufferedInput.c @@ -68,18 +68,12 @@ /// """ /// ... /// -STATIC void validate_rate(mp_float_t rate) { - if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) { - mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second")); - } -} - STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_sample_rate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_int = 500000} }, + { MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500000} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -103,8 +97,7 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size } // Validate sample rate here - mp_float_t sample_rate = mp_obj_get_float(args[ARG_sample_rate].u_obj); - validate_rate(sample_rate); + uint32_t sample_rate = (uint32_t)mp_arg_validate_int_range(args[ARG_sample_rate].u_int, 1, 500000, MP_QSTR_sample_rate); // Create local object adcbuffer_bufferedinput_obj_t *self = m_new_obj(adcbuffer_bufferedinput_obj_t); diff --git a/shared-bindings/adcbuffer/BufferedInput.h b/shared-bindings/adcbuffer/BufferedInput.h index 593eee493c..8cd88fed21 100644 --- a/shared-bindings/adcbuffer/BufferedInput.h +++ b/shared-bindings/adcbuffer/BufferedInput.h @@ -34,7 +34,7 @@ extern const mp_obj_type_t adcbuffer_bufferedinput_type; -void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate); +void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self); bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self); bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self); From e23b621d69a0aad69689d6e06037c63b7b9ec81d Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Mon, 22 Aug 2022 18:55:55 -0400 Subject: [PATCH 18/31] Fixing 8 bit and 12 bit conversion --- .../common-hal/adcbuffer/BufferedInput.c | 32 +++++++++++-------- ports/raspberrypi/mpconfigport.mk | 4 +-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index 50e043f58a..2f558ef256 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -31,6 +31,7 @@ * THE SOFTWARE. */ +#include #include "common-hal/adcbuffer/BufferedInput.h" #include "shared-bindings/adcbuffer/BufferedInput.h" #include "shared-bindings/microcontroller/Pin.h" @@ -46,6 +47,7 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { // Set pin and channel + self->pin = pin; claim_pin(pin); @@ -82,13 +84,23 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t adc_gpio_init(pin->number); adc_select_input(self->chan); // chan = pin - 26 ?? + // self->bytes_per_sample == 1 + uint dma_size = DMA_SIZE_8; + bool show_error_bit = false; + bool shift_sample_8_bits = true; + if (self->bytes_per_sample == 2) { + dma_size = DMA_SIZE_16; + show_error_bit = true; + shift_sample_8_bits = false; + } + // adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER); adc_fifo_setup( - true, // Write each completed conversion to the sample FIFO - true, // Enable DMA data request (DREQ) - 1, // DREQ (and IRQ) asserted when at least 1 sample present - true, // We will see the ERR bit because of 12 bit reads; enabled. - false // Do not shift each sample to 8 bits when pushing to FIFO + true, // Write each completed conversion to the sample FIFO + true, // Enable DMA data request (DREQ) + 1, // DREQ (and IRQ) asserted when at least 1 sample present + show_error_bit, // See the ERR bit on 8 bit bit reads + shift_sample_8_bits // Shift each sample to 8 bits when pushing to FIFO ); // Divisor of 0 -> full speed. Free-running capture with the divider is @@ -110,11 +122,7 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t self->cfg = dma_channel_get_default_config(dma_chan); // Reading from constant address, writing to incrementing byte addresses - if (self->bytes_per_sample == 1) { - channel_config_set_transfer_data_size(&(self->cfg), DMA_SIZE_8); - } else if (self->bytes_per_sample == 2) { - channel_config_set_transfer_data_size(&(self->cfg), DMA_SIZE_16); - } + channel_config_set_transfer_data_size(&(self->cfg), dma_size); channel_config_set_read_increment(&(self->cfg), false); channel_config_set_write_increment(&(self->cfg), true); @@ -146,9 +154,7 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { - // uint32_t cdl = self->len/2 + 1; - // uint32_t cdl = 3*self->len/4; //BAD - uint32_t cdl = self->len * self->bytes_per_sample; + uint32_t cdl = self->len / self->bytes_per_sample; dma_channel_configure(self->dma_chan, &(self->cfg), self->buffer, // dst diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index ba6869169f..3e55aa2c38 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -27,8 +27,8 @@ CIRCUITPY_PULSEIO ?= 1 CIRCUITPY_WATCHDOG ?= 1 # Use of adcbuffer -CIRCUITPYTHON_ADCBUFFER = 1 - +#CIRCUITPYTHON_ADCBUFFER = 1 +CIRCUITPY_ADCBUFFER = 1 # Audio via PWM CIRCUITPY_AUDIOIO = 0 CIRCUITPY_AUDIOBUSIO ?= 1 From 6fd08483e28c8eee82137e86c52b97308e1a3828 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Wed, 24 Aug 2022 17:41:51 -0400 Subject: [PATCH 19/31] Tidying code for PR/ Minor Issues --- .../common-hal/adcbuffer/BufferedInput.c | 26 ++-- ports/raspberrypi/mpconfigport.mk | 2 +- py/circuitpy_defns.mk | 8 -- shared-bindings/adcbuffer/BufferedInput.c | 112 ++++++++---------- shared-bindings/adcbuffer/BufferedInput.h | 2 - shared-bindings/adcbuffer/__init__.c | 72 ++++++----- 6 files changed, 95 insertions(+), 127 deletions(-) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index 2f558ef256..50f2dc5d1a 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -46,34 +46,24 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { - // Set pin and channel - - self->pin = pin; - claim_pin(pin); - - // validate pin number + // Make sure pin number is in range for ADC if (pin->number < ADC_FIRST_PIN_NUMBER && pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) { raise_ValueError_invalid_pins(); } - // TODO: find a wat to accept ADC4 for temperature - self->chan = pin->number - ADC_FIRST_PIN_NUMBER; + // Set pin and channel + self->pin = pin; + claim_pin(pin); - // TODO: Checks on chan value here + // TODO: find a way to accept ADC4 for temperature + self->chan = pin->number - ADC_FIRST_PIN_NUMBER; // Set buffer and length self->buffer = buffer; self->len = len; - // TODO: checks on length here - - // Set sample rate - // NOTE: bits_per_sample = bytes_per_sample * 8; + // Set sample rate - used in readmultiple self->bytes_per_sample = bytes_per_sample; - - // TODO: Possibly check Rate values here, already u_int - // NOTE: Anything over 500000 for RP2040 will not - // exceed DMA conversion sampling rate. self->sample_rate = sample_rate; // Standard IO Init @@ -111,9 +101,9 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t // intervals). This is all timed by the 48 MHz ADC clock. // sample rate determines divisor, not zero. + // sample_rate is forced to be >= 1 in shared-bindings adc_set_clkdiv((float)48000000.0 / (float)self->sample_rate); - // sleep_ms(1000); // Set up the DMA to start transferring data as soon as it appears in FIFO uint dma_chan = dma_claim_unused_channel(true); self->dma_chan = dma_chan; diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 3e55aa2c38..d33f3640c7 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -27,8 +27,8 @@ CIRCUITPY_PULSEIO ?= 1 CIRCUITPY_WATCHDOG ?= 1 # Use of adcbuffer -#CIRCUITPYTHON_ADCBUFFER = 1 CIRCUITPY_ADCBUFFER = 1 + # Audio via PWM CIRCUITPY_AUDIOIO = 0 CIRCUITPY_AUDIOBUSIO ?= 1 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index b295647da6..43a6932f57 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -484,14 +484,6 @@ SRC_C += \ endif -#ifeq ($(CIRCUITPYTHON_ADCBUFFER),1) -# Needed for ADCBUFFER -#SRC_COMMON_HAL_ALL += \ -# adcbuffer/BufferedInput.c \ -# -#endif - - SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL)) # These don't have corresponding files in each port but are still located in diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/adcbuffer/BufferedInput.c index 67647e680d..99da047e53 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/adcbuffer/BufferedInput.c @@ -37,37 +37,37 @@ #include "shared-bindings/adcbuffer/BufferedInput.h" #include "shared-bindings/util.h" -/// class BufferedInput: -/// """Input analog voltage level to supplied buffer using DMA Capture""" -/// -/// def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None: -/// """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied -/// sample_rate. -/// -/// :param ~microcontroller.Pin pin: the pin to read from""" -/// :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples -/// :param ~int sample_rate: rate: The desired playback sample rate -/// -/// Usage:: -/// -/// import board -/// import adcbuffer -/// import array -/// -/// length = 1000 -/// mybuffer = array.array("H", [0] * length) -/// rate = 500000 -/// adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate) -/// adcbuf.readmultiple() -/// adcbuf.deinit() -/// for i in range(length): -/// print(i, mybuffer[i]) -/// -/// (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting. -/// (TODO) Provide mechanism to read CPU Temperature -/// """ -/// ... -/// +//| class BufferedInput: +//| """Input analog voltage level to supplied buffer using DMA Capture""" +//| +//| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None: +//| """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied +//| sample_rate. +//| +//| :param ~microcontroller.Pin pin: the pin to read from""" +//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples +//| :param ~int sample_rate: rate: The desired playback sample rate +//| +//| Usage:: +//| +//| import board +//| import adcbuffer +//| import array +//| +//| length = 1000 +//| mybuffer = array.array("H", [0] * length) +//| rate = 500000 +//| adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate) +//| adcbuf.readmultiple() +//| adcbuf.deinit() +//| for i in range(length): +//| print(i, mybuffer[i]) +//| +//| (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting. +//| (TODO) Provide mechanism to read CPU Temperature +//| """ +//| ... +//| STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; static const mp_arg_t allowed_args[] = { @@ -116,10 +116,10 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size return MP_OBJ_FROM_PTR(self); } -/// def deinit(self) -> None: -/// """Turn off the BufferedInput and release the pin for other use.""" -/// ... -/// +//| def deinit(self) -> None: +//| """Turn off the BufferedInput and release the pin for other use.""" +//| ... +//| STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) { adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_adcbuffer_bufferedinput_deinit(self); @@ -132,17 +132,14 @@ STATIC void check_for_deinit(adcbuffer_bufferedinput_obj_t *self) { raise_deinited_error(); } } -/// def __enter__(self) -> BufferedInput: -/// """No-op used by Context Managers.""" -/// ... -/// -/// Provided by context manager helper. -/// -/// def __exit__(self) -> None: -/// """Automatically deinitializes the hardware when exiting a context. See -/// :ref:`lifetime-and-contextmanagers` for more info.""" -/// ... -/// + +//| Provided by context manager helper. +//| +//| def __exit__(self) -> None: +//| """Automatically deinitializes the hardware when exiting a context. See +//| :ref:`lifetime-and-contextmanagers` for more info.""" +//| ... +//| STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; common_hal_adcbuffer_bufferedinput_deinit(args[0]); @@ -150,12 +147,14 @@ STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t * } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__); -/// value: int -/// """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only) -/// -/// Even if the underlying analog to digital converter (ADC) is lower -/// resolution, the value is 16-bit.""" -/// +//| value: --> None +//| """Fills the supplied buffer with ADC values using DMA transfer. +//| If the buffer is 8-bit, then values are 8-bit shifted and error bit is off. +//| If buffer is 16-bit, then values are not shifted and error bit is present. +//| Number of transfers is always the number of samples which is the array +//| byte length divided by the byte_per_sample. """ +//| ... +//| STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); @@ -163,15 +162,6 @@ STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple); -/// MP_PROPERTY_GETTER(adcbuffer_bufferedinput_value_obj, -/// (mp_obj_t)&adcbuffer_bufferedinput_get_value_obj); -/// -/// reference_voltage: float -/// """The maximum voltage measurable (also known as the reference voltage) as a -/// `float` in Volts. Note the ADC value may not scale to the actual voltage linearly -/// at ends of the analog range.""" -/// - STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, diff --git a/shared-bindings/adcbuffer/BufferedInput.h b/shared-bindings/adcbuffer/BufferedInput.h index 8cd88fed21..42b9f94558 100644 --- a/shared-bindings/adcbuffer/BufferedInput.h +++ b/shared-bindings/adcbuffer/BufferedInput.h @@ -30,8 +30,6 @@ #include "common-hal/microcontroller/Pin.h" #include "common-hal/adcbuffer/BufferedInput.h" -// #ifdef CIRCUITPY_BUFFEREDINPUT #endif - extern const mp_obj_type_t adcbuffer_bufferedinput_type; void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); diff --git a/shared-bindings/adcbuffer/__init__.c b/shared-bindings/adcbuffer/__init__.c index 34b87125ce..4d5fefc26e 100644 --- a/shared-bindings/adcbuffer/__init__.c +++ b/shared-bindings/adcbuffer/__init__.c @@ -30,43 +30,41 @@ #include "shared-bindings/adcbuffer/__init__.h" #include "shared-bindings/adcbuffer/BufferedInput.h" -/// #ifdef CIRCUITPY_BUFFEREDINPUT#endif - -/// """Analog buffered hardware support -/// -/// The `adcbuffer` module contains classes to provide access to analog-to-digital -/// conversion and digital-to-analog (DAC) for multiple value transfer. -/// -/// All classes change hardware state and should be deinitialized when they -/// are no longer needed if the program continues after use. To do so, either -/// call :py:meth:`!deinit` or use a context manager. See -/// :ref:`lifetime-and-contextmanagers` for more info. -/// -/// For example:: -/// -/// import adcbuffer -/// import array -/// from board import * -/// -/// length = 5000000 -/// mybuffer = array.array("H", [0] * length) -/// adcbuf_obj = adcbuffer.BufferedInPut(GP26, mybuffer, length) -/// adcbuffer.readmultiple() -/// print(*mybuffer) -/// adcbuf_obj.deinit() -/// -/// This example will initialize the the device, read and fill -/// :py:data:`~adcbuffer.BufferedInPut` to mybuffer and then -/// :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional -/// because CircuitPython will do it automatically after the program finishes. -/// -/// TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials -/// Learn guide `_ -/// -/// TODO: For more information on using `adcbuffer`, see `this additional Learn guide -/// `_ -/// """ -/// +//| """Analog buffered hardware support +//| +//| The `adcbuffer` module contains classes to provide access to analog-to-digital +//| conversion and digital-to-analog (DAC) for multiple value transfer. +//| +//| All classes change hardware state and should be deinitialized when they +//| are no longer needed if the program continues after use. To do so, either +//| call :py:meth:`!deinit` or use a context manager. See +//| :ref:`lifetime-and-contextmanagers` for more info. +//| +//| For example:: +//| +//| import adcbuffer +//| import array +//| from board import * +//| +//| length = 5000000 +//| mybuffer = array.array("H", [0] * length) +//| adcbuf_obj = adcbuffer.BufferedInput(GP26, mybuffer, length) +//| adcbuffer.readmultiple() +//| print(*mybuffer) +//| adcbuf_obj.deinit() +//| +//| This example will initialize the the device, read and fill +//| :py:data:`~adcbuffer.BufferedInPut` to mybuffer and then +//| :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional +//| because CircuitPython will do it automatically after the program finishes. +//| +//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials +//| Learn guide `_ +//| +//| TODO: For more information on using `adcbuffer`, see `this additional Learn guide +//| `_ +//| """ +//| STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) }, From 532ae2a39dfb10ad3fe9b56c0f12fcbb9fac52dc Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Thu, 25 Aug 2022 11:57:07 -0400 Subject: [PATCH 20/31] Fixing Document issues --- shared-bindings/adcbuffer/BufferedInput.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/adcbuffer/BufferedInput.c index 99da047e53..f84a5c6069 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/adcbuffer/BufferedInput.c @@ -63,10 +63,10 @@ //| for i in range(length): //| print(i, mybuffer[i]) //| -//| (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting. -//| (TODO) Provide mechanism to read CPU Temperature -//| """ -//| ... +//| (TODO) The reference voltage varies by platform so use +//| ``reference_voltage`` to read the configured setting. +//| (TODO) Provide mechanism to read CPU Temperature.""" +//| ... //| STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; @@ -152,7 +152,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, //| If the buffer is 8-bit, then values are 8-bit shifted and error bit is off. //| If buffer is 16-bit, then values are not shifted and error bit is present. //| Number of transfers is always the number of samples which is the array -//| byte length divided by the byte_per_sample. """ +//| byte length divided by the bytes_per_sample.""" //| ... //| STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { From 7cd969e23675061415d9da3e7f2b85968f01f32b Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Thu, 25 Aug 2022 17:09:50 -0400 Subject: [PATCH 21/31] Fixing document triple quotes --- shared-bindings/adcbuffer/BufferedInput.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/adcbuffer/BufferedInput.c index f84a5c6069..25e1b75cd7 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/adcbuffer/BufferedInput.c @@ -44,7 +44,7 @@ //| """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied //| sample_rate. //| -//| :param ~microcontroller.Pin pin: the pin to read from""" +//| :param ~microcontroller.Pin pin: the pin to read from //| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples //| :param ~int sample_rate: rate: The desired playback sample rate //| @@ -147,13 +147,12 @@ STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t * } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__); -//| value: --> None -//| """Fills the supplied buffer with ADC values using DMA transfer. +//| value: None +//| """Fills the supplied buffer with ADC values using DMA transfer. //| If the buffer is 8-bit, then values are 8-bit shifted and error bit is off. //| If buffer is 16-bit, then values are not shifted and error bit is present. //| Number of transfers is always the number of samples which is the array //| byte length divided by the bytes_per_sample.""" -//| ... //| STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); From 04af01a672879f58623c3696abb5f68842b03311 Mon Sep 17 00:00:00 2001 From: Lee Atkinson Date: Sat, 27 Aug 2022 14:47:12 -0400 Subject: [PATCH 22/31] Pin Validation fixed/readmultiple return fixed --- ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c | 7 ++----- shared-bindings/adcbuffer/BufferedInput.c | 3 ++- shared-bindings/adcbuffer/BufferedInput.h | 2 +- shared-bindings/adcbuffer/__init__.c | 1 - 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index 50f2dc5d1a..b42bed2c0c 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -47,7 +47,7 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { // Make sure pin number is in range for ADC - if (pin->number < ADC_FIRST_PIN_NUMBER && pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) { + if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) { raise_ValueError_invalid_pins(); } @@ -122,7 +122,6 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t // clear any previous activity adc_fifo_drain(); adc_run(false); - } bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self) { @@ -142,7 +141,7 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se dma_channel_unclaim(self->dma_chan); } -bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { +void common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { uint32_t cdl = self->len / self->bytes_per_sample; @@ -163,6 +162,4 @@ bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj // Clean up adc_run(false); adc_fifo_drain(); - - return true; } diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/adcbuffer/BufferedInput.c index 25e1b75cd7..da7cb9525a 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/adcbuffer/BufferedInput.c @@ -157,7 +157,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - return MP_OBJ_NEW_SMALL_INT(common_hal_adcbuffer_bufferedinput_readmultiple(self)); + common_hal_adcbuffer_bufferedinput_readmultiple(self); + return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple); diff --git a/shared-bindings/adcbuffer/BufferedInput.h b/shared-bindings/adcbuffer/BufferedInput.h index 42b9f94558..25411a3d7c 100644 --- a/shared-bindings/adcbuffer/BufferedInput.h +++ b/shared-bindings/adcbuffer/BufferedInput.h @@ -35,6 +35,6 @@ extern const mp_obj_type_t adcbuffer_bufferedinput_type; void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self); bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self); -bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self); +void common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self); #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H__ diff --git a/shared-bindings/adcbuffer/__init__.c b/shared-bindings/adcbuffer/__init__.c index 4d5fefc26e..447f01ea15 100644 --- a/shared-bindings/adcbuffer/__init__.c +++ b/shared-bindings/adcbuffer/__init__.c @@ -69,7 +69,6 @@ STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) }, { MP_ROM_QSTR(MP_QSTR_BufferedInput), MP_ROM_PTR(&adcbuffer_bufferedinput_type) }, - // #ifdef CIRCUITPY_BUFFEREDINPUT #endif }; STATIC MP_DEFINE_CONST_DICT(adcbuffer_module_globals, adcbuffer_module_globals_table); From d7a1db5b87d484631016ec40e44b5847b7dc3de1 Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Sun, 4 Sep 2022 15:57:59 -0400 Subject: [PATCH 23/31] Changes per review --- .../common-hal/adcbuffer/BufferedInput.c | 11 ++++- shared-bindings/adcbuffer/BufferedInput.c | 41 ++++++++++--------- shared-bindings/adcbuffer/BufferedInput.h | 2 +- shared-bindings/adcbuffer/__init__.c | 10 ++--- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c index b42bed2c0c..1050cf7e57 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c @@ -62,7 +62,7 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t self->buffer = buffer; self->len = len; - // Set sample rate - used in readmultiple + // Set sample rate - used in read self->bytes_per_sample = bytes_per_sample; self->sample_rate = sample_rate; @@ -74,6 +74,13 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t adc_gpio_init(pin->number); adc_select_input(self->chan); // chan = pin - 26 ?? + // RP2040 Implementation Detail + // Fills the supplied buffer with ADC values using DMA transfer. + // If the buffer is 8-bit, then values are 8-bit shifted and error bit is off. + // If buffer is 16-bit, then values are not shifted and error bit is present. + // Number of transfers is always the number of samples which is the array + // byte length divided by the bytes_per_sample. + // self->bytes_per_sample == 1 uint dma_size = DMA_SIZE_8; bool show_error_bit = false; @@ -141,7 +148,7 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se dma_channel_unclaim(self->dma_chan); } -void common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) { +void common_hal_adcbuffer_bufferedinput_read(adcbuffer_bufferedinput_obj_t *self) { uint32_t cdl = self->len / self->bytes_per_sample; diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/adcbuffer/BufferedInput.c index da7cb9525a..3941d1b5b7 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/adcbuffer/BufferedInput.c @@ -38,15 +38,19 @@ #include "shared-bindings/util.h" //| class BufferedInput: -//| """Input analog voltage level to supplied buffer using DMA Capture""" +//| """Capture multiple analog voltage levels to the supplied buffer""" //| //| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None: -//| """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied -//| sample_rate. +//| """Create a `BufferedInput` on the given pin. ADC values will be read +//| into the given buffer at the supplied sample_rate. Depending on the +//| buffer typecode, 'b', 'B', 'h', 'H', samples are 8-bit byte-arrays or +//| 16-bit half-words and are signed or unsigned. +//| The ADC most significant bits of the ADC are kept. Please see: +//| `https://docs.circuitpython.org/en/latest/docs/library/array.html` //| //| :param ~microcontroller.Pin pin: the pin to read from //| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples -//| :param ~int sample_rate: rate: The desired playback sample rate +//| :param ~int sample_rate: rate: sampling frequency, in samples per second //| //| Usage:: //| @@ -58,7 +62,7 @@ //| mybuffer = array.array("H", [0] * length) //| rate = 500000 //| adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate) -//| adcbuf.readmultiple() +//| adcbuf.read() //| adcbuf.deinit() //| for i in range(length): //| print(i, mybuffer[i]) @@ -93,7 +97,7 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { bytes_per_sample = 2; } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { - mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'")); + mp_raise_ValueError_varg(translate("%q must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); } // Validate sample rate here @@ -117,7 +121,7 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size } //| def deinit(self) -> None: -//| """Turn off the BufferedInput and release the pin for other use.""" +//| """Shut down the `BufferedInput` and release the pin for other use.""" //| ... //| STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) { @@ -147,26 +151,23 @@ STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t * } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__); -//| value: None -//| """Fills the supplied buffer with ADC values using DMA transfer. -//| If the buffer is 8-bit, then values are 8-bit shifted and error bit is off. -//| If buffer is 16-bit, then values are not shifted and error bit is present. -//| Number of transfers is always the number of samples which is the array -//| byte length divided by the bytes_per_sample.""" +//| +//| def read(self) -> None: +//| """Fills the provided buffer with ADC voltage values.""" //| -STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) { +STATIC mp_obj_t adcbuffer_bufferedinput_obj_read(mp_obj_t self_in) { adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - common_hal_adcbuffer_bufferedinput_readmultiple(self); + common_hal_adcbuffer_bufferedinput_read(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple); +MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_read_obj, adcbuffer_bufferedinput_obj_read); STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_readmultiple), MP_ROM_PTR(&adcbuffer_bufferedinput_readmultiple_obj)}, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&adcbuffer_bufferedinput_read_obj)}, }; diff --git a/shared-bindings/adcbuffer/BufferedInput.h b/shared-bindings/adcbuffer/BufferedInput.h index 25411a3d7c..8529dc24d1 100644 --- a/shared-bindings/adcbuffer/BufferedInput.h +++ b/shared-bindings/adcbuffer/BufferedInput.h @@ -35,6 +35,6 @@ extern const mp_obj_type_t adcbuffer_bufferedinput_type; void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self); bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self); -void common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self); +void common_hal_adcbuffer_bufferedinput_read(adcbuffer_bufferedinput_obj_t *self); #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H__ diff --git a/shared-bindings/adcbuffer/__init__.c b/shared-bindings/adcbuffer/__init__.c index 447f01ea15..e88287a4b2 100644 --- a/shared-bindings/adcbuffer/__init__.c +++ b/shared-bindings/adcbuffer/__init__.c @@ -48,15 +48,13 @@ //| //| length = 5000000 //| mybuffer = array.array("H", [0] * length) -//| adcbuf_obj = adcbuffer.BufferedInput(GP26, mybuffer, length) -//| adcbuffer.readmultiple() +//| adc_in = adcbuffer.BufferedInput(GP26, mybuffer, length) +//| adcbuffer.read() //| print(*mybuffer) -//| adcbuf_obj.deinit() +//| adc_in.deinit() //| //| This example will initialize the the device, read and fill -//| :py:data:`~adcbuffer.BufferedInPut` to mybuffer and then -//| :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional -//| because CircuitPython will do it automatically after the program finishes. +//| :py:data:`~adcbuffer.BufferedInPut` to mybuffer //| //| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials //| Learn guide `_ From a1856ea3e92186a7791fd5f9bd724ef608fe08cc Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Mon, 5 Sep 2022 12:11:49 -0400 Subject: [PATCH 24/31] Renaming module from adcbuffer to analogbufio --- .../common-hal/adcbuffer/__init__.c | 1 - .../BufferedIn.c} | 14 ++-- .../BufferedIn.h} | 12 ++-- .../common-hal/analogbufio/__init__.c | 1 + ports/raspberrypi/mpconfigport.mk | 4 +- py/circuitpy_defns.mk | 10 +-- py/circuitpy_mpconfig.mk | 6 +- .../BufferedIn.c} | 68 +++++++++---------- .../BufferedIn.h} | 18 ++--- .../{adcbuffer => analogbufio}/__init__.c | 38 +++++------ .../{adcbuffer => analogbufio}/__init__.h | 6 +- 11 files changed, 89 insertions(+), 89 deletions(-) delete mode 100644 ports/raspberrypi/common-hal/adcbuffer/__init__.c rename ports/raspberrypi/common-hal/{adcbuffer/BufferedInput.c => analogbufio/BufferedIn.c} (90%) rename ports/raspberrypi/common-hal/{adcbuffer/BufferedInput.h => analogbufio/BufferedIn.h} (86%) create mode 100644 ports/raspberrypi/common-hal/analogbufio/__init__.c rename shared-bindings/{adcbuffer/BufferedInput.c => analogbufio/BufferedIn.c} (68%) rename shared-bindings/{adcbuffer/BufferedInput.h => analogbufio/BufferedIn.h} (62%) rename shared-bindings/{adcbuffer => analogbufio}/__init__.c (64%) rename shared-bindings/{adcbuffer => analogbufio}/__init__.h (87%) diff --git a/ports/raspberrypi/common-hal/adcbuffer/__init__.c b/ports/raspberrypi/common-hal/adcbuffer/__init__.c deleted file mode 100644 index 3473642698..0000000000 --- a/ports/raspberrypi/common-hal/adcbuffer/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No adcbuffer module functions. diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c similarity index 90% rename from ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c rename to ports/raspberrypi/common-hal/analogbufio/BufferedIn.c index 1050cf7e57..7ca37934aa 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c @@ -32,8 +32,8 @@ */ #include -#include "common-hal/adcbuffer/BufferedInput.h" -#include "shared-bindings/adcbuffer/BufferedInput.h" +#include "common-hal/analogbufio/BufferedIn.h" +#include "shared-bindings/analogbufio/BufferedIn.h" #include "shared-bindings/microcontroller/Pin.h" #include "py/runtime.h" #include "supervisor/shared/translate/translate.h" @@ -44,7 +44,7 @@ #define ADC_FIRST_PIN_NUMBER 26 #define ADC_PIN_COUNT 4 -void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { +void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate) { // Make sure pin number is in range for ADC if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) { @@ -131,12 +131,12 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t adc_run(false); } -bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self) { +bool common_hal_analogbufio_bufferedin_deinited(analogbufio_bufferedin_obj_t *self) { return self->pin == NULL; } -void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self) { - if (common_hal_adcbuffer_bufferedinput_deinited(self)) { +void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self) { + if (common_hal_analogbufio_bufferedin_deinited(self)) { return; } @@ -148,7 +148,7 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se dma_channel_unclaim(self->dma_chan); } -void common_hal_adcbuffer_bufferedinput_read(adcbuffer_bufferedinput_obj_t *self) { +void common_hal_analogbufio_bufferedin_read(analogbufio_bufferedin_obj_t *self) { uint32_t cdl = self->len / self->bytes_per_sample; diff --git a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h similarity index 86% rename from ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h rename to ports/raspberrypi/common-hal/analogbufio/BufferedIn.h index 292fecc8a4..71077e4432 100644 --- a/ports/raspberrypi/common-hal/adcbuffer/BufferedInput.h +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h @@ -31,15 +31,15 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H -#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H +#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H +#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H #include "common-hal/microcontroller/Pin.h" #include "src/rp2_common/hardware_dma/include/hardware/dma.h" #include "py/obj.h" -// This is the adcbuffer object +// This is the analogbufio object typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -51,8 +51,8 @@ typedef struct { uint8_t chan; uint dma_chan; dma_channel_config cfg; -} adcbuffer_bufferedinput_obj_t; +} analogbuf_bufferedin_obj_t; -void bufferedinput_init(void); +void bufferedin_init(void); -#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H +#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGBUFIO_BUFFEREDIN_H diff --git a/ports/raspberrypi/common-hal/analogbufio/__init__.c b/ports/raspberrypi/common-hal/analogbufio/__init__.c new file mode 100644 index 0000000000..b6c74b985b --- /dev/null +++ b/ports/raspberrypi/common-hal/analogbufio/__init__.c @@ -0,0 +1 @@ +// No analogbufio module functions. diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index d33f3640c7..fa1f0c153d 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -26,8 +26,8 @@ CIRCUITPY_NVM = 1 CIRCUITPY_PULSEIO ?= 1 CIRCUITPY_WATCHDOG ?= 1 -# Use of adcbuffer -CIRCUITPY_ADCBUFFER = 1 +# Use of analogbufio +CIRCUITPY_ANALOGBUFIO = 1 # Audio via PWM CIRCUITPY_AUDIOIO = 0 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 43a6932f57..4d5d6c78d3 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -102,15 +102,15 @@ endif ### # Select which builtin modules to compile and include. -ifeq ($(CIRCUITPY_ADCBUFFER),1) -SRC_PATTERNS += adcbuffer/% -endif ifeq ($(CIRCUITPY_AESIO),1) SRC_PATTERNS += aesio/% endif ifeq ($(CIRCUITPY_ALARM),1) SRC_PATTERNS += alarm/% endif +ifeq ($(CIRCUITPY_ANALOGBUFIO),1) +SRC_PATTERNS += analogbufio/% +endif ifeq ($(CIRCUITPY_ANALOGIO),1) SRC_PATTERNS += analogio/% endif @@ -387,13 +387,13 @@ SRC_COMMON_HAL_ALL = \ _bleio/__init__.c \ _pew/PewPew.c \ _pew/__init__.c \ - adcbuffer/BufferedInput.c \ - adcbuffer/__init__.c \ alarm/SleepMemory.c \ alarm/__init__.c \ alarm/pin/PinAlarm.c \ alarm/time/TimeAlarm.c \ alarm/touch/TouchAlarm.c \ + analogbufio/BufferedIn.c \ + analogbufio/__init__.c \ analogio/AnalogIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 1d4a5765f8..a227738519 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -62,12 +62,12 @@ CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO) CIRCUITPY_ALARM ?= 0 CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM) +CIRCUITPY_ANALOGBUFIO ?= 0 +CFLAGS += -DCIRCUITPY_ANALOGBUFIO=$(CIRCUITPY_ANALOGBUFIO) + CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) -CIRCUITPY_ADCBUFFER ?= 0 -CFLAGS += -DCIRCUITPY_ADCBUFFER=$(CIRCUITPY_ADCBUFFER) - CIRCUITPY_ATEXIT ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_ATEXIT=$(CIRCUITPY_ATEXIT) diff --git a/shared-bindings/adcbuffer/BufferedInput.c b/shared-bindings/analogbufio/BufferedIn.c similarity index 68% rename from shared-bindings/adcbuffer/BufferedInput.c rename to shared-bindings/analogbufio/BufferedIn.c index 3941d1b5b7..7c5b195994 100644 --- a/shared-bindings/adcbuffer/BufferedInput.c +++ b/shared-bindings/analogbufio/BufferedIn.c @@ -34,14 +34,14 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/adcbuffer/BufferedInput.h" +#include "shared-bindings/analogbufio/BufferedIn.h" #include "shared-bindings/util.h" -//| class BufferedInput: +//| class BufferedIn: //| """Capture multiple analog voltage levels to the supplied buffer""" //| //| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None: -//| """Create a `BufferedInput` on the given pin. ADC values will be read +//| """Create a `BufferedIn` on the given pin. ADC values will be read //| into the given buffer at the supplied sample_rate. Depending on the //| buffer typecode, 'b', 'B', 'h', 'H', samples are 8-bit byte-arrays or //| 16-bit half-words and are signed or unsigned. @@ -55,13 +55,13 @@ //| Usage:: //| //| import board -//| import adcbuffer +//| import analogbufio //| import array //| //| length = 1000 -//| mybuffer = array.array("H", [0] * length) +//| mybuffer = array.array("H", 0x0000 for i in range(length)) //| rate = 500000 -//| adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate) +//| adcbuf = analogbufio.BufferedIn(board.GP26, mybuffer, rate) //| adcbuf.read() //| adcbuf.deinit() //| for i in range(length): @@ -72,7 +72,7 @@ //| (TODO) Provide mechanism to read CPU Temperature.""" //| ... //| -STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, @@ -104,11 +104,11 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size uint32_t sample_rate = (uint32_t)mp_arg_validate_int_range(args[ARG_sample_rate].u_int, 1, 500000, MP_QSTR_sample_rate); // Create local object - adcbuffer_bufferedinput_obj_t *self = m_new_obj(adcbuffer_bufferedinput_obj_t); - self->base.type = &adcbuffer_bufferedinput_type; + analogbufio_bufferedin_obj_t *self = m_new_obj(analogbufio_bufferedin_obj_t); + self->base.type = &analogbufio_bufferedin_type; - // Call local intereface in ports/common-hal/adcbuffer - common_hal_adcbuffer_bufferedinput_construct(self, + // Call local intereface in ports/common-hal/analogbufio + common_hal_analogbufio_bufferedin_construct(self, pin, ((uint8_t *)bufinfo.buf), bufinfo.len, @@ -121,18 +121,18 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size } //| def deinit(self) -> None: -//| """Shut down the `BufferedInput` and release the pin for other use.""" +//| """Shut down the `BufferedIn` and release the pin for other use.""" //| ... //| -STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) { - adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_adcbuffer_bufferedinput_deinit(self); +STATIC mp_obj_t analogbufio_bufferedin_deinit(mp_obj_t self_in) { + analogbufio_bufferedin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_analogbufio_bufferedin_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_deinit_obj, adcbuffer_bufferedinput_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_deinit_obj, analogbufio_bufferedin_deinit); -STATIC void check_for_deinit(adcbuffer_bufferedinput_obj_t *self) { - if (common_hal_adcbuffer_bufferedinput_deinited(self)) { +STATIC void check_for_deinit(analogbufio_bufferedin_obj_t *self) { + if (common_hal_analogbufio_bufferedin_deinited(self)) { raise_deinited_error(); } } @@ -144,38 +144,38 @@ STATIC void check_for_deinit(adcbuffer_bufferedinput_obj_t *self) { //| :ref:`lifetime-and-contextmanagers` for more info.""" //| ... //| -STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t analogbufio_bufferedin___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_adcbuffer_bufferedinput_deinit(args[0]); + common_hal_analogbufio_bufferedin_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogbufio_bufferedin___exit___obj, 4, 4, analogbufio_bufferedin___exit__); //| //| def read(self) -> None: //| """Fills the provided buffer with ADC voltage values.""" //| -STATIC mp_obj_t adcbuffer_bufferedinput_obj_read(mp_obj_t self_in) { - adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t analogbufio_bufferedin_obj_read(mp_obj_t self_in) { + analogbufio_bufferedin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - common_hal_adcbuffer_bufferedinput_read(self); + common_hal_analogbufio_bufferedin_read(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_read_obj, adcbuffer_bufferedinput_obj_read); +MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_read_obj, analogbufio_bufferedin_obj_read); -STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) }, +STATIC const mp_rom_map_elem_t analogbufio_bufferedin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogbufio_bufferedin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&adcbuffer_bufferedinput_read_obj)}, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogbufio_bufferedin___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&analogbufio_bufferedin_read_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(adcbuffer_bufferedinput_locals_dict, adcbuffer_bufferedinput_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(analogbufio_bufferedin_locals_dict, analogbufio_bufferedin_locals_dict_table); -const mp_obj_type_t adcbuffer_bufferedinput_type = { +const mp_obj_type_t analogbufio_bufferedin_type = { { &mp_type_type }, - .name = MP_QSTR_BufferedInput, - .make_new = adcbuffer_bufferedinput_make_new, - .locals_dict = (mp_obj_t)&adcbuffer_bufferedinput_locals_dict, + .name = MP_QSTR_BufferedIn, + .make_new = analogbufio_bufferedin_make_new, + .locals_dict = (mp_obj_t)&analogbufio_bufferedin_locals_dict, }; diff --git a/shared-bindings/adcbuffer/BufferedInput.h b/shared-bindings/analogbufio/BufferedIn.h similarity index 62% rename from shared-bindings/adcbuffer/BufferedInput.h rename to shared-bindings/analogbufio/BufferedIn.h index 8529dc24d1..7d59720cb4 100644 --- a/shared-bindings/adcbuffer/BufferedInput.h +++ b/shared-bindings/analogbufio/BufferedIn.h @@ -24,17 +24,17 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO_BUFFEREDIN_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO_BUFFEREDIN_H #include "common-hal/microcontroller/Pin.h" -#include "common-hal/adcbuffer/BufferedInput.h" +#include "common-hal/analogbufio/BufferedIn.h" -extern const mp_obj_type_t adcbuffer_bufferedinput_type; +extern const mp_obj_type_t analogbufio_bufferedin_type; -void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); -void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self); -bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self); -void common_hal_adcbuffer_bufferedinput_read(adcbuffer_bufferedinput_obj_t *self); +void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate); +void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self); +bool common_hal_analogbufio_bufferedin_deinited(analogbufio_bufferedin_obj_t *self); +void common_hal_analogbufio_bufferedin_read(analogbufio_bufferedin_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO_BUFFEREDIN_H__ diff --git a/shared-bindings/adcbuffer/__init__.c b/shared-bindings/analogbufio/__init__.c similarity index 64% rename from shared-bindings/adcbuffer/__init__.c rename to shared-bindings/analogbufio/__init__.c index e88287a4b2..b47fe4b897 100644 --- a/shared-bindings/adcbuffer/__init__.c +++ b/shared-bindings/analogbufio/__init__.c @@ -27,12 +27,12 @@ #include "py/obj.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/adcbuffer/__init__.h" -#include "shared-bindings/adcbuffer/BufferedInput.h" +#include "shared-bindings/analogbufio/__init__.h" +#include "shared-bindings/analogbufio/BufferedIn.h" -//| """Analog buffered hardware support +//| """Analog Buffered IO Hardware Support //| -//| The `adcbuffer` module contains classes to provide access to analog-to-digital +//| The `analogbufio` module contains classes to provide access to analog-to-digital //| conversion and digital-to-analog (DAC) for multiple value transfer. //| //| All classes change hardware state and should be deinitialized when they @@ -42,38 +42,38 @@ //| //| For example:: //| -//| import adcbuffer +//| import analogbufio //| import array //| from board import * //| //| length = 5000000 -//| mybuffer = array.array("H", [0] * length) -//| adc_in = adcbuffer.BufferedInput(GP26, mybuffer, length) -//| adcbuffer.read() +//| mybuffer = array.array("H", 0x0000 for i in range(length)) +//| adc_in = analogbufio.BufferedIn(GP26, mybuffer, length) +//| analogbufio.read() //| print(*mybuffer) //| adc_in.deinit() //| //| This example will initialize the the device, read and fill -//| :py:data:`~adcbuffer.BufferedInPut` to mybuffer +//| :py:data:`~analogbufio.BufferedIn` to mybuffer //| -//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials -//| Learn guide `_ +//| TODO: For the essentials of `analogbufio`, see the `CircuitPython Essentials +//| Learn guide `_ //| -//| TODO: For more information on using `adcbuffer`, see `this additional Learn guide +//| TODO: For more information on using `analogbufio`, see `this additional Learn guide //| `_ //| """ //| -STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) }, - { MP_ROM_QSTR(MP_QSTR_BufferedInput), MP_ROM_PTR(&adcbuffer_bufferedinput_type) }, +STATIC const mp_rom_map_elem_t analogbufio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogbufio) }, + { MP_ROM_QSTR(MP_QSTR_BufferedIn), MP_ROM_PTR(&analogbufio_bufferedin_type) }, }; -STATIC MP_DEFINE_CONST_DICT(adcbuffer_module_globals, adcbuffer_module_globals_table); +STATIC MP_DEFINE_CONST_DICT(analogbufio_module_globals, analogbufio_module_globals_table); -const mp_obj_module_t adcbuffer_module = { +const mp_obj_module_t analogbufio_module = { .base = { &mp_type_module }, - .globals = (mp_obj_dict_t *)&adcbuffer_module_globals, + .globals = (mp_obj_dict_t *)&analogbufio_module_globals, }; -MP_REGISTER_MODULE(MP_QSTR_adcbuffer, adcbuffer_module, CIRCUITPY_ADCBUFFER); +MP_REGISTER_MODULE(MP_QSTR_analogbufio, analogbufio_module, CIRCUITPY_ANALOGBUFIO); diff --git a/shared-bindings/adcbuffer/__init__.h b/shared-bindings/analogbufio/__init__.h similarity index 87% rename from shared-bindings/adcbuffer/__init__.h rename to shared-bindings/analogbufio/__init__.h index c271d71273..d66dd4e8d6 100644 --- a/shared-bindings/adcbuffer/__init__.h +++ b/shared-bindings/analogbufio/__init__.h @@ -24,11 +24,11 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H -#define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO___INIT___H #include "py/obj.h" // Nothing now. -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO___INIT___H From 5498b3ac918272b308f941bbf2c51bc90deaabf1 Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Mon, 5 Sep 2022 14:22:08 -0400 Subject: [PATCH 25/31] fix to analogbufio BufferedIn.h --- ports/raspberrypi/common-hal/analogbufio/BufferedIn.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h index 71077e4432..b38bfc8738 100644 --- a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h @@ -51,7 +51,7 @@ typedef struct { uint8_t chan; uint dma_chan; dma_channel_config cfg; -} analogbuf_bufferedin_obj_t; +} analogbufio_bufferedin_obj_t; void bufferedin_init(void); From 25078a24a3a1dd53f9c7a96386b9b995e4d67a75 Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Tue, 6 Sep 2022 18:58:57 -0400 Subject: [PATCH 26/31] Attempt to fix Build-Docs and Pre-Commit --- .../common-hal/analogbufio/BufferedIn.c | 2 +- shared-bindings/analogbufio/BufferedIn.c | 82 ++++++++++--------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c index 7ca37934aa..bd3ad8e93a 100644 --- a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c @@ -80,7 +80,7 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s // If buffer is 16-bit, then values are not shifted and error bit is present. // Number of transfers is always the number of samples which is the array // byte length divided by the bytes_per_sample. - + // self->bytes_per_sample == 1 uint dma_size = DMA_SIZE_8; bool show_error_bit = false; diff --git a/shared-bindings/analogbufio/BufferedIn.c b/shared-bindings/analogbufio/BufferedIn.c index 7c5b195994..1d452a4200 100644 --- a/shared-bindings/analogbufio/BufferedIn.c +++ b/shared-bindings/analogbufio/BufferedIn.c @@ -38,46 +38,47 @@ #include "shared-bindings/util.h" //| class BufferedIn: -//| """Capture multiple analog voltage levels to the supplied buffer""" +//| """Capture multiple analog voltage levels to the supplied buffer //| +//| Usage:: +//| +//| import board +//| import analogbufio +//| import array +//| +//| length = 1000 +//| mybuffer = array.array("H", 0x0000 for i in range(length)) +//| rate = 500000 +//| adcbuf = analogbufio.BufferedIn(board.GP26, mybuffer, rate) +//| adcbuf.read() +//| adcbuf.deinit() +//| for i in range(length): +//| print(i, mybuffer[i]) +//| +//| (TODO) The reference voltage varies by platform so use +//| ``reference_voltage`` to read the configured setting. +//| (TODO) Provide mechanism to read CPU Temperature.""" +//| + //| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None: //| """Create a `BufferedIn` on the given pin. ADC values will be read //| into the given buffer at the supplied sample_rate. Depending on the //| buffer typecode, 'b', 'B', 'h', 'H', samples are 8-bit byte-arrays or //| 16-bit half-words and are signed or unsigned. -//| The ADC most significant bits of the ADC are kept. Please see: -//| `https://docs.circuitpython.org/en/latest/docs/library/array.html` +//| The ADC most significant bits of the ADC are kept. (See +//| https://docs.circuitpython.org/en/latest/docs/library/array.html) //| //| :param ~microcontroller.Pin pin: the pin to read from //| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples -//| :param ~int sample_rate: rate: sampling frequency, in samples per second -//| -//| Usage:: -//| -//| import board -//| import analogbufio -//| import array -//| -//| length = 1000 -//| mybuffer = array.array("H", 0x0000 for i in range(length)) -//| rate = 500000 -//| adcbuf = analogbufio.BufferedIn(board.GP26, mybuffer, rate) -//| adcbuf.read() -//| adcbuf.deinit() -//| for i in range(length): -//| print(i, mybuffer[i]) -//| -//| (TODO) The reference voltage varies by platform so use -//| ``reference_voltage`` to read the configured setting. -//| (TODO) Provide mechanism to read CPU Temperature.""" -//| ... +//| :param ~int sample_rate: rate: sampling frequency, in samples per second""" +//| ... //| STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500000} }, + { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500000} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -95,7 +96,7 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_ // Bytes Per Sample if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { - bytes_per_sample = 2; + bytes_per_sample = 2; } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { mp_raise_ValueError_varg(translate("%q must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); } @@ -109,13 +110,13 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_ // Call local intereface in ports/common-hal/analogbufio common_hal_analogbufio_bufferedin_construct(self, - pin, - ((uint8_t *)bufinfo.buf), - bufinfo.len, - bytes_per_sample, - signed_samples, - sample_rate - ); + pin, + ((uint8_t *)bufinfo.buf), + bufinfo.len, + bytes_per_sample, + signed_samples, + sample_rate + ); return MP_OBJ_FROM_PTR(self); } @@ -133,12 +134,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_deinit_obj, analogbufio_buffere STATIC void check_for_deinit(analogbufio_bufferedin_obj_t *self) { if (common_hal_analogbufio_bufferedin_deinited(self)) { - raise_deinited_error(); + raise_deinited_error(); } } - -//| Provided by context manager helper. +//| def __enter__(self) -> AnalogIn: +//| """No-op used by Context Managers.""" +//| ... //| +// Provided by context manager helper. + //| def __exit__(self) -> None: //| """Automatically deinitializes the hardware when exiting a context. See //| :ref:`lifetime-and-contextmanagers` for more info.""" @@ -151,9 +155,9 @@ STATIC mp_obj_t analogbufio_bufferedin___exit__(size_t n_args, const mp_obj_t *a } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogbufio_bufferedin___exit___obj, 4, 4, analogbufio_bufferedin___exit__); -//| //| def read(self) -> None: //| """Fills the provided buffer with ADC voltage values.""" +//| ... //| STATIC mp_obj_t analogbufio_bufferedin_obj_read(mp_obj_t self_in) { analogbufio_bufferedin_obj_t *self = MP_OBJ_TO_PTR(self_in); From 51dab921ea9e1e7d598b36ae32f3ad1b61c1a0c5 Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Tue, 6 Sep 2022 19:09:00 -0400 Subject: [PATCH 27/31] Bad object reference --- shared-bindings/analogbufio/BufferedIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/analogbufio/BufferedIn.c b/shared-bindings/analogbufio/BufferedIn.c index 1d452a4200..e18aaaa23a 100644 --- a/shared-bindings/analogbufio/BufferedIn.c +++ b/shared-bindings/analogbufio/BufferedIn.c @@ -137,7 +137,7 @@ STATIC void check_for_deinit(analogbufio_bufferedin_obj_t *self) { raise_deinited_error(); } } -//| def __enter__(self) -> AnalogIn: +//| def __enter__(self) -> BufferedIn: //| """No-op used by Context Managers.""" //| ... //| From a87d3bd89e1edcba1bb458fcd8d72f2a19799106 Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Tue, 6 Sep 2022 19:40:20 -0400 Subject: [PATCH 28/31] Bad quotes --- shared-bindings/analogbufio/BufferedIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/analogbufio/BufferedIn.c b/shared-bindings/analogbufio/BufferedIn.c index e18aaaa23a..05529ed64c 100644 --- a/shared-bindings/analogbufio/BufferedIn.c +++ b/shared-bindings/analogbufio/BufferedIn.c @@ -98,7 +98,7 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_ if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { bytes_per_sample = 2; } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { - mp_raise_ValueError_varg(translate("%q must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); + mp_raise_ValueError_varg(translate("%q ``must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); } // Validate sample rate here From c1f57c6ceb326ab069ae82506a4fbfab265cec67 Mon Sep 17 00:00:00 2001 From: latkinso42 Date: Wed, 7 Sep 2022 18:53:35 -0400 Subject: [PATCH 29/31] Pushing for resolution --- locale/circuitpython.pot | 9 +++--- .../common-hal/analogbufio/BufferedIn.c | 2 +- shared-bindings/analogbufio/BufferedIn.c | 28 +++++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 4193094968..e799b131e6 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -76,6 +76,10 @@ msgstr "" msgid "%q" msgstr "" +#: shared-bindings/analogbufio/BufferedIn.c +msgid "%q ``must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'" +msgstr "" + #: shared-bindings/microcontroller/Pin.c msgid "%q and %q contain duplicate pins" msgstr "" @@ -3869,11 +3873,6 @@ msgstr "" msgid "rsplit(None,n)" msgstr "" -#: shared-bindings/adcbuffer/BufferdInput.c -msgid "sample rate must be 1.0-500000.0 per second" -msgstr "" - -#: shared-bindings/adcbuffer/BufferdInput.c #: shared-bindings/audiocore/RawSample.c msgid "" "sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or " diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c index bd3ad8e93a..f5f9dbde33 100644 --- a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c @@ -96,7 +96,7 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s true, // Write each completed conversion to the sample FIFO true, // Enable DMA data request (DREQ) 1, // DREQ (and IRQ) asserted when at least 1 sample present - show_error_bit, // See the ERR bit on 8 bit bit reads + show_error_bit, // See the ERR bit shift_sample_8_bits // Shift each sample to 8 bits when pushing to FIFO ); diff --git a/shared-bindings/analogbufio/BufferedIn.c b/shared-bindings/analogbufio/BufferedIn.c index 05529ed64c..a0a0f27161 100644 --- a/shared-bindings/analogbufio/BufferedIn.c +++ b/shared-bindings/analogbufio/BufferedIn.c @@ -76,9 +76,9 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_buffer, ARG_sample_rate }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500000} }, + { MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500000} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -86,7 +86,7 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_ // Validate Pin const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); - // Buffer Pointer defined and allocated by user + // Buffer defined and allocated by user mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); @@ -96,9 +96,9 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_ // Bytes Per Sample if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') { - bytes_per_sample = 2; + bytes_per_sample = 2; } else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { - mp_raise_ValueError_varg(translate("%q ``must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); + mp_raise_ValueError_varg(translate("%q ``must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); } // Validate sample rate here @@ -110,13 +110,13 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_ // Call local intereface in ports/common-hal/analogbufio common_hal_analogbufio_bufferedin_construct(self, - pin, - ((uint8_t *)bufinfo.buf), - bufinfo.len, - bytes_per_sample, - signed_samples, - sample_rate - ); + pin, + ((uint8_t *)bufinfo.buf), + bufinfo.len, + bytes_per_sample, + signed_samples, + sample_rate + ); return MP_OBJ_FROM_PTR(self); } @@ -134,7 +134,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_deinit_obj, analogbufio_buffere STATIC void check_for_deinit(analogbufio_bufferedin_obj_t *self) { if (common_hal_analogbufio_bufferedin_deinited(self)) { - raise_deinited_error(); + raise_deinited_error(); } } //| def __enter__(self) -> BufferedIn: From 82694b726584b90e269962467ce609c4229c05c9 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 15 Sep 2022 20:31:08 -0400 Subject: [PATCH 30/31] remove extraneous copyrights --- ports/raspberrypi/common-hal/analogbufio/BufferedIn.c | 5 ----- ports/raspberrypi/common-hal/analogbufio/BufferedIn.h | 2 -- 2 files changed, 7 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c index f5f9dbde33..951c07af03 100644 --- a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c @@ -4,8 +4,6 @@ * The MIT License (MIT) * * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. - * Copyright (c) 2013, 2014 Damien P. George - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries * * SPDX-License-Identifier: BSD-3-Clause * @@ -66,9 +64,6 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s self->bytes_per_sample = bytes_per_sample; self->sample_rate = sample_rate; - // Standard IO Init - stdio_init_all(); - // Init GPIO for analogue use: hi-Z, no pulls, disable digital input buffer. adc_init(); adc_gpio_init(pin->number); diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h index b38bfc8738..8b183a1d70 100644 --- a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h @@ -4,8 +4,6 @@ * The MIT License (MIT) * * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. - * Copyright (c) 2013, 2014 Damien P. George - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries * * SPDX-License-Identifier: BSD-3-Clause * From 0ab4f12dc4bc81b5dfb26cae3942c4f4b231f83b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 15 Sep 2022 20:36:29 -0400 Subject: [PATCH 31/31] fix some submodules commit skew --- frozen/circuitpython-stage | 2 +- ports/espressif/esp-idf | 2 +- ports/espressif/esp32-camera | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frozen/circuitpython-stage b/frozen/circuitpython-stage index 9a8338b3bd..4124dfbdaa 160000 --- a/frozen/circuitpython-stage +++ b/frozen/circuitpython-stage @@ -1 +1 @@ -Subproject commit 9a8338b3bdaeac9eeb5b74d147107c67db33fdac +Subproject commit 4124dfbdaadce1966f457d7d6c6984e9832999bf diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index ddb7ddbcb6..d51f7d8821 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit ddb7ddbcb613a582e0a91eda8b1d2510dd0a2d83 +Subproject commit d51f7d882187afa4b39c2613fd0fe2ac2fea1145 diff --git a/ports/espressif/esp32-camera b/ports/espressif/esp32-camera index 28804391c0..54c3f61c86 160000 --- a/ports/espressif/esp32-camera +++ b/ports/espressif/esp32-camera @@ -1 +1 @@ -Subproject commit 28804391c002f6a3ea5ce6a55aee3b191be3ecde +Subproject commit 54c3f61c864c3d3ffd779042454554045b9dd9d2