2022-08-10 09:42:24 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2022-08-12 16:01:58 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
|
2022-08-18 16:23:17 -04:00
|
|
|
* 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
|
2022-08-10 09:42:24 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-08-22 18:55:55 -04:00
|
|
|
#include <stdio.h>
|
2022-08-18 19:10:16 -04:00
|
|
|
#include "common-hal/adcbuffer/BufferedInput.h"
|
|
|
|
#include "shared-bindings/adcbuffer/BufferedInput.h"
|
2022-08-10 09:42:24 -04:00
|
|
|
#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"
|
2022-08-18 16:23:17 -04:00
|
|
|
#include "src/common/pico_stdlib/include/pico/stdlib.h"
|
2022-08-10 09:42:24 -04:00
|
|
|
|
|
|
|
#define ADC_FIRST_PIN_NUMBER 26
|
|
|
|
#define ADC_PIN_COUNT 4
|
|
|
|
|
2022-08-21 11:44:40 -04:00
|
|
|
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) {
|
2022-08-10 09:42:24 -04:00
|
|
|
|
2022-08-24 17:41:51 -04:00
|
|
|
// Make sure pin number is in range for ADC
|
2022-08-19 10:51:13 -04:00
|
|
|
if (pin->number < ADC_FIRST_PIN_NUMBER && pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) {
|
|
|
|
raise_ValueError_invalid_pins();
|
2022-08-18 16:23:17 -04:00
|
|
|
}
|
2022-08-19 10:51:13 -04:00
|
|
|
|
2022-08-24 17:41:51 -04:00
|
|
|
// Set pin and channel
|
|
|
|
self->pin = pin;
|
|
|
|
claim_pin(pin);
|
2022-08-10 09:42:24 -04:00
|
|
|
|
2022-08-24 17:41:51 -04:00
|
|
|
// TODO: find a way to accept ADC4 for temperature
|
|
|
|
self->chan = pin->number - ADC_FIRST_PIN_NUMBER;
|
2022-08-10 09:42:24 -04:00
|
|
|
|
|
|
|
// Set buffer and length
|
|
|
|
self->buffer = buffer;
|
|
|
|
self->len = len;
|
|
|
|
|
2022-08-24 17:41:51 -04:00
|
|
|
// Set sample rate - used in readmultiple
|
2022-08-21 11:44:40 -04:00
|
|
|
self->bytes_per_sample = bytes_per_sample;
|
2022-08-10 09:42:24 -04:00
|
|
|
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 ??
|
2022-08-14 13:03:33 -04:00
|
|
|
|
2022-08-22 18:55:55 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:42:24 -04:00
|
|
|
// adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER);
|
|
|
|
adc_fifo_setup(
|
2022-08-22 18:55:55 -04:00
|
|
|
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
|
2022-08-10 09:42:24 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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.
|
2022-08-11 17:50:30 -04:00
|
|
|
|
2022-08-24 17:41:51 -04:00
|
|
|
// sample_rate is forced to be >= 1 in shared-bindings
|
2022-08-21 11:44:40 -04:00
|
|
|
adc_set_clkdiv((float)48000000.0 / (float)self->sample_rate);
|
2022-08-10 09:42:24 -04:00
|
|
|
|
|
|
|
// 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;
|
2022-08-11 17:50:30 -04:00
|
|
|
|
|
|
|
// Set Config
|
|
|
|
self->cfg = dma_channel_get_default_config(dma_chan);
|
2022-08-10 09:42:24 -04:00
|
|
|
|
|
|
|
// Reading from constant address, writing to incrementing byte addresses
|
2022-08-22 18:55:55 -04:00
|
|
|
channel_config_set_transfer_data_size(&(self->cfg), dma_size);
|
2022-08-11 17:50:30 -04:00
|
|
|
channel_config_set_read_increment(&(self->cfg), false);
|
|
|
|
channel_config_set_write_increment(&(self->cfg), true);
|
2022-08-10 09:42:24 -04:00
|
|
|
|
|
|
|
// Pace transfers based on availability of ADC samples
|
2022-08-11 17:50:30 -04:00
|
|
|
channel_config_set_dreq(&(self->cfg), DREQ_ADC);
|
2022-08-10 09:42:24 -04:00
|
|
|
|
2022-08-11 17:50:30 -04:00
|
|
|
// clear any previous activity
|
|
|
|
adc_fifo_drain();
|
|
|
|
adc_run(false);
|
2022-08-14 13:03:33 -04:00
|
|
|
|
2022-08-10 09:42:24 -04:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:23:17 -04:00
|
|
|
bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self) {
|
2022-08-10 09:42:24 -04:00
|
|
|
return self->pin == NULL;
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:23:17 -04:00
|
|
|
void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self) {
|
|
|
|
if (common_hal_adcbuffer_bufferedinput_deinited(self)) {
|
2022-08-10 09:42:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:50:30 -04:00
|
|
|
// Release ADC Pin
|
2022-08-10 09:42:24 -04:00
|
|
|
reset_pin_number(self->pin->number);
|
|
|
|
self->pin = NULL;
|
2022-08-11 17:50:30 -04:00
|
|
|
|
|
|
|
// Release DMA Channel
|
|
|
|
dma_channel_unclaim(self->dma_chan);
|
2022-08-10 09:42:24 -04:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:23:17 -04:00
|
|
|
bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) {
|
2022-08-11 17:50:30 -04:00
|
|
|
|
2022-08-22 18:55:55 -04:00
|
|
|
uint32_t cdl = self->len / self->bytes_per_sample;
|
2022-08-11 17:50:30 -04:00
|
|
|
|
|
|
|
dma_channel_configure(self->dma_chan, &(self->cfg),
|
|
|
|
self->buffer, // dst
|
|
|
|
&adc_hw->fifo, // src
|
2022-08-21 11:44:40 -04:00
|
|
|
cdl, // transfer count
|
2022-08-11 17:50:30 -04:00
|
|
|
true // start immediately
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start the ADC
|
2022-08-10 09:42:24 -04:00
|
|
|
adc_run(true);
|
2022-08-11 17:50:30 -04:00
|
|
|
|
2022-08-10 09:42:24 -04:00
|
|
|
// 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);
|
2022-08-11 17:50:30 -04:00
|
|
|
|
|
|
|
// Clean up
|
2022-08-10 09:42:24 -04:00
|
|
|
adc_run(false);
|
|
|
|
adc_fifo_drain();
|
2022-08-11 17:50:30 -04:00
|
|
|
|
2022-08-10 09:42:24 -04:00
|
|
|
return true;
|
|
|
|
}
|