diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index fd6df1cafe..30971fd9b1 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -79,6 +79,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 "" diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c new file mode 100644 index 0000000000..951c07af03 --- /dev/null +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c @@ -0,0 +1,167 @@ +/* + * 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. + * + * 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 + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "common-hal/analogbufio/BufferedIn.h" +#include "shared-bindings/analogbufio/BufferedIn.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "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" +#include "src/common/pico_stdlib/include/pico/stdlib.h" + +#define ADC_FIRST_PIN_NUMBER 26 +#define ADC_PIN_COUNT 4 + +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)) { + raise_ValueError_invalid_pins(); + } + + // Set pin and channel + self->pin = pin; + claim_pin(pin); + + // 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; + + // Set sample rate - used in read + self->bytes_per_sample = bytes_per_sample; + self->sample_rate = sample_rate; + + // 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 ?? + + // 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; + 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 + show_error_bit, // See the ERR bit + 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 + // 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. + + // sample_rate is forced to be >= 1 in shared-bindings + adc_set_clkdiv((float)48000000.0 / (float)self->sample_rate); + + // 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; + + // 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(&(self->cfg), dma_size); + 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(&(self->cfg), DREQ_ADC); + + // clear any previous activity + adc_fifo_drain(); + adc_run(false); +} + +bool common_hal_analogbufio_bufferedin_deinited(analogbufio_bufferedin_obj_t *self) { + return self->pin == NULL; +} + +void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self) { + if (common_hal_analogbufio_bufferedin_deinited(self)) { + return; + } + + // Release ADC Pin + reset_pin_number(self->pin->number); + self->pin = NULL; + + // Release DMA Channel + dma_channel_unclaim(self->dma_chan); +} + +void common_hal_analogbufio_bufferedin_read(analogbufio_bufferedin_obj_t *self) { + + 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, // 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); + + // Clean up + adc_run(false); + adc_fifo_drain(); +} diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h new file mode 100644 index 0000000000..8b183a1d70 --- /dev/null +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.h @@ -0,0 +1,56 @@ +/* + * 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. + * + * 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 + * 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_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 analogbufio object +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; + dma_channel_config cfg; +} analogbufio_bufferedin_obj_t; + +void bufferedin_init(void); + +#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 ac11bafcb6..fa1f0c153d 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 analogbufio +CIRCUITPY_ANALOGBUFIO = 1 + # Audio via PWM CIRCUITPY_AUDIOIO = 0 CIRCUITPY_AUDIOBUSIO ?= 1 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index a7b51a879f..80377f1a65 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -109,6 +109,9 @@ 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 @@ -391,6 +394,8 @@ SRC_COMMON_HAL_ALL = \ 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 \ @@ -481,7 +486,6 @@ SRC_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/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index ad41efee6b..0f4269c522 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -62,6 +62,9 @@ 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) diff --git a/shared-bindings/analogbufio/BufferedIn.c b/shared-bindings/analogbufio/BufferedIn.c new file mode 100644 index 0000000000..a0a0f27161 --- /dev/null +++ b/shared-bindings/analogbufio/BufferedIn.c @@ -0,0 +1,185 @@ +/* + * 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/analogbufio/BufferedIn.h" +#include "shared-bindings/util.h" + +//| class BufferedIn: +//| """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. (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""" +//| ... +//| +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_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 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_varg(translate("%q ``must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer); + } + + // Validate sample rate here + 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 + 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/analogbufio + common_hal_analogbufio_bufferedin_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: +//| """Shut down the `BufferedIn` and release the pin for other use.""" +//| ... +//| +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(analogbufio_bufferedin_deinit_obj, analogbufio_bufferedin_deinit); + +STATIC void check_for_deinit(analogbufio_bufferedin_obj_t *self) { + if (common_hal_analogbufio_bufferedin_deinited(self)) { + raise_deinited_error(); + } +} +//| def __enter__(self) -> BufferedIn: +//| """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 analogbufio_bufferedin___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_analogbufio_bufferedin_deinit(args[0]); + return mp_const_none; +} +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); + check_for_deinit(self); + common_hal_analogbufio_bufferedin_read(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_read_obj, analogbufio_bufferedin_obj_read); + +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(&analogbufio_bufferedin___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&analogbufio_bufferedin_read_obj)}, + +}; + +STATIC MP_DEFINE_CONST_DICT(analogbufio_bufferedin_locals_dict, analogbufio_bufferedin_locals_dict_table); + +const mp_obj_type_t analogbufio_bufferedin_type = { + { &mp_type_type }, + .name = MP_QSTR_BufferedIn, + .make_new = analogbufio_bufferedin_make_new, + .locals_dict = (mp_obj_t)&analogbufio_bufferedin_locals_dict, +}; diff --git a/shared-bindings/analogbufio/BufferedIn.h b/shared-bindings/analogbufio/BufferedIn.h new file mode 100644 index 0000000000..7d59720cb4 --- /dev/null +++ b/shared-bindings/analogbufio/BufferedIn.h @@ -0,0 +1,40 @@ +/* + * 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. + * + * 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_ANALOGBUFIO_BUFFEREDIN_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO_BUFFEREDIN_H + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/analogbufio/BufferedIn.h" + +extern const mp_obj_type_t analogbufio_bufferedin_type; + +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_ANALOGBUFIO_BUFFEREDIN_H__ diff --git a/shared-bindings/analogbufio/__init__.c b/shared-bindings/analogbufio/__init__.c new file mode 100644 index 0000000000..b47fe4b897 --- /dev/null +++ b/shared-bindings/analogbufio/__init__.c @@ -0,0 +1,79 @@ +/* + * 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/analogbufio/__init__.h" +#include "shared-bindings/analogbufio/BufferedIn.h" + +//| """Analog Buffered IO Hardware Support +//| +//| 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 +//| 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 analogbufio +//| import array +//| from board import * +//| +//| length = 5000000 +//| 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:`~analogbufio.BufferedIn` to mybuffer +//| +//| TODO: For the essentials of `analogbufio`, see the `CircuitPython Essentials +//| Learn guide `_ +//| +//| TODO: For more information on using `analogbufio`, see `this additional Learn guide +//| `_ +//| """ +//| + +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(analogbufio_module_globals, analogbufio_module_globals_table); + +const mp_obj_module_t analogbufio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&analogbufio_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_analogbufio, analogbufio_module, CIRCUITPY_ANALOGBUFIO); diff --git a/shared-bindings/analogbufio/__init__.h b/shared-bindings/analogbufio/__init__.h new file mode 100644 index 0000000000..d66dd4e8d6 --- /dev/null +++ b/shared-bindings/analogbufio/__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_ANALOGBUFIO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO___INIT___H + +#include "py/obj.h" + +// Nothing now. + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGBUFIO___INIT___H