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) },