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"));