|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: BSD-3-Clause |
| 9 | + * |
| 10 | + * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. |
| 11 | + * https://github.com/raspberrypi/pico-examples/blob/master/adc/dma_capture/dma_capture.c |
| 12 | + * |
| 13 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | + * of this software and associated documentation files (the "Software"), to deal |
| 15 | + * in the Software without restriction, including without limitation the rights |
| 16 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | + * copies of the Software, and to permit persons to whom the Software is |
| 18 | + * furnished to do so, subject to the following conditions: |
| 19 | + * |
| 20 | + * The above copyright notice and this permission notice shall be included in |
| 21 | + * all copies or substantial portions of the Software. |
| 22 | + * |
| 23 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 29 | + * THE SOFTWARE. |
| 30 | + */ |
| 31 | + |
| 32 | +#include <stdio.h> |
| 33 | +#include "common-hal/analogbufio/BufferedIn.h" |
| 34 | +#include "shared-bindings/analogbufio/BufferedIn.h" |
| 35 | +#include "shared-bindings/microcontroller/Pin.h" |
| 36 | +#include "py/runtime.h" |
| 37 | +#include "supervisor/shared/translate/translate.h" |
| 38 | +#include "src/rp2_common/hardware_adc/include/hardware/adc.h" |
| 39 | +#include "src/rp2_common/hardware_dma/include/hardware/dma.h" |
| 40 | +#include "src/common/pico_stdlib/include/pico/stdlib.h" |
| 41 | + |
| 42 | +#define ADC_FIRST_PIN_NUMBER 26 |
| 43 | +#define ADC_PIN_COUNT 4 |
| 44 | + |
| 45 | +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) { |
| 46 | + |
| 47 | + // Make sure pin number is in range for ADC |
| 48 | + if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) { |
| 49 | + raise_ValueError_invalid_pins(); |
| 50 | + } |
| 51 | + |
| 52 | + // Set pin and channel |
| 53 | + self->pin = pin; |
| 54 | + claim_pin(pin); |
| 55 | + |
| 56 | + // TODO: find a way to accept ADC4 for temperature |
| 57 | + self->chan = pin->number - ADC_FIRST_PIN_NUMBER; |
| 58 | + |
| 59 | + // Set buffer and length |
| 60 | + self->buffer = buffer; |
| 61 | + self->len = len; |
| 62 | + |
| 63 | + // Set sample rate - used in read |
| 64 | + self->bytes_per_sample = bytes_per_sample; |
| 65 | + self->sample_rate = sample_rate; |
| 66 | + |
| 67 | + // Init GPIO for analogue use: hi-Z, no pulls, disable digital input buffer. |
| 68 | + adc_init(); |
| 69 | + adc_gpio_init(pin->number); |
| 70 | + adc_select_input(self->chan); // chan = pin - 26 ?? |
| 71 | + |
| 72 | + // RP2040 Implementation Detail |
| 73 | + // Fills the supplied buffer with ADC values using DMA transfer. |
| 74 | + // If the buffer is 8-bit, then values are 8-bit shifted and error bit is off. |
| 75 | + // If buffer is 16-bit, then values are not shifted and error bit is present. |
| 76 | + // Number of transfers is always the number of samples which is the array |
| 77 | + // byte length divided by the bytes_per_sample. |
| 78 | + |
| 79 | + // self->bytes_per_sample == 1 |
| 80 | + uint dma_size = DMA_SIZE_8; |
| 81 | + bool show_error_bit = false; |
| 82 | + bool shift_sample_8_bits = true; |
| 83 | + if (self->bytes_per_sample == 2) { |
| 84 | + dma_size = DMA_SIZE_16; |
| 85 | + show_error_bit = true; |
| 86 | + shift_sample_8_bits = false; |
| 87 | + } |
| 88 | + |
| 89 | + // adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER); |
| 90 | + adc_fifo_setup( |
| 91 | + true, // Write each completed conversion to the sample FIFO |
| 92 | + true, // Enable DMA data request (DREQ) |
| 93 | + 1, // DREQ (and IRQ) asserted when at least 1 sample present |
| 94 | + show_error_bit, // See the ERR bit |
| 95 | + shift_sample_8_bits // Shift each sample to 8 bits when pushing to FIFO |
| 96 | + ); |
| 97 | + |
| 98 | + // Divisor of 0 -> full speed. Free-running capture with the divider is |
| 99 | + // equivalent to pressing the ADC_CS_START_ONCE button once per `div + 1` |
| 100 | + // cycles (div not necessarily an integer). Each conversion takes 96 |
| 101 | + // cycles, so in general you want a divider of 0 (hold down the button |
| 102 | + // continuously) or > 95 (take samples less frequently than 96 cycle |
| 103 | + // intervals). This is all timed by the 48 MHz ADC clock. |
| 104 | + // sample rate determines divisor, not zero. |
| 105 | + |
| 106 | + // sample_rate is forced to be >= 1 in shared-bindings |
| 107 | + adc_set_clkdiv((float)48000000.0 / (float)self->sample_rate); |
| 108 | + |
| 109 | + // Set up the DMA to start transferring data as soon as it appears in FIFO |
| 110 | + uint dma_chan = dma_claim_unused_channel(true); |
| 111 | + self->dma_chan = dma_chan; |
| 112 | + |
| 113 | + // Set Config |
| 114 | + self->cfg = dma_channel_get_default_config(dma_chan); |
| 115 | + |
| 116 | + // Reading from constant address, writing to incrementing byte addresses |
| 117 | + channel_config_set_transfer_data_size(&(self->cfg), dma_size); |
| 118 | + channel_config_set_read_increment(&(self->cfg), false); |
| 119 | + channel_config_set_write_increment(&(self->cfg), true); |
| 120 | + |
| 121 | + // Pace transfers based on availability of ADC samples |
| 122 | + channel_config_set_dreq(&(self->cfg), DREQ_ADC); |
| 123 | + |
| 124 | + // clear any previous activity |
| 125 | + adc_fifo_drain(); |
| 126 | + adc_run(false); |
| 127 | +} |
| 128 | + |
| 129 | +bool common_hal_analogbufio_bufferedin_deinited(analogbufio_bufferedin_obj_t *self) { |
| 130 | + return self->pin == NULL; |
| 131 | +} |
| 132 | + |
| 133 | +void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self) { |
| 134 | + if (common_hal_analogbufio_bufferedin_deinited(self)) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + // Release ADC Pin |
| 139 | + reset_pin_number(self->pin->number); |
| 140 | + self->pin = NULL; |
| 141 | + |
| 142 | + // Release DMA Channel |
| 143 | + dma_channel_unclaim(self->dma_chan); |
| 144 | +} |
| 145 | + |
| 146 | +void common_hal_analogbufio_bufferedin_read(analogbufio_bufferedin_obj_t *self) { |
| 147 | + |
| 148 | + uint32_t cdl = self->len / self->bytes_per_sample; |
| 149 | + |
| 150 | + dma_channel_configure(self->dma_chan, &(self->cfg), |
| 151 | + self->buffer, // dst |
| 152 | + &adc_hw->fifo, // src |
| 153 | + cdl, // transfer count |
| 154 | + true // start immediately |
| 155 | + ); |
| 156 | + |
| 157 | + // Start the ADC |
| 158 | + adc_run(true); |
| 159 | + |
| 160 | + // Once DMA finishes, stop any new conversions from starting, and clean up |
| 161 | + // the FIFO in case the ADC was still mid-conversion. |
| 162 | + dma_channel_wait_for_finish_blocking(self->dma_chan); |
| 163 | + |
| 164 | + // Clean up |
| 165 | + adc_run(false); |
| 166 | + adc_fifo_drain(); |
| 167 | +} |
0 commit comments