From fe0b6120513d12b95de5c54da5dc1bdcbccb1f08 Mon Sep 17 00:00:00 2001 From: tomcombriat Date: Tue, 10 Jan 2023 10:11:07 +0100 Subject: [PATCH] Async adc readings on rp2040 on DMA_IRQ_0 with shared handler --- MozziGuts_impl_RP2040.hpp | 9 +++++---- README.md | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/MozziGuts_impl_RP2040.hpp b/MozziGuts_impl_RP2040.hpp index 35bf36edf..096a292ca 100644 --- a/MozziGuts_impl_RP2040.hpp +++ b/MozziGuts_impl_RP2040.hpp @@ -102,14 +102,15 @@ void setupMozziADC(int8_t speed) { ); // we want notification, when a sample has arrived - dma_channel_set_irq1_enabled(dma_chan, true); - irq_add_shared_handler(DMA_IRQ_1, rp2040_adc_queue_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); - irq_set_enabled(DMA_IRQ_1, true); + dma_channel_set_irq0_enabled(dma_chan, true); + irq_add_shared_handler(DMA_IRQ_0, rp2040_adc_queue_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); + irq_set_enabled(DMA_IRQ_0, true); dma_channel_start(dma_chan); } void rp2040_adc_queue_handler() { - dma_hw->ints0 = 1u << rp2040_adc_dma_chan; // clear interrupt flag + if (!dma_channel_get_irq0_status(rp2040_adc_dma_chan)) return; // shared handler may get called on unrelated events + dma_channel_acknowledge_irq0(rp2040_adc_dma_chan); // clear interrupt flag //adc_run(false); // adc not running continuous //adc_fifo_drain(); // no need to drain fifo, the dma transfer did that dma_channel_set_trans_count(rp2040_adc_dma_chan, 1, true); // set up for another read diff --git a/README.md b/README.md index b1b125dc5..52912c6eb 100644 --- a/README.md +++ b/README.md @@ -291,13 +291,13 @@ on the RP2040 SDK API. Tested on a Pi Pico. - Wavetables and samples are not kept in progmem on this platform. While apparently speed (of the external flash) is not much of an issue, the data always seems to be copied into RAM, anyway. - Currently, two audio output modes exist (configurable in AudioConfigRP2040.h) in addition to using an user-defined `audioOutput` function, with the default being PWM_VIA_BARE_CHIP: - PWM_VIA_BARE_CHIP: PWM audio output on pin 0, by default, with 11 bits default output resolution - - One timer interrupt (DMA_IRQ_1) and one DMA channel are claimed (number not hardcoded). + - One non-exclusive timer interrupt (DMA_IRQ_0) and one DMA channel are claimed (number not hardcoded). - HIFI_MODE not yet implemented (although that should not be too hard to do). - EXTERNAL_DAC_VIA_I2S: I2S output to be connected to an external DAC - 16 bits resolution by default (configurable in AudioConfigRP2040.h), 8, 16, 24 (left aligned) and 32 resolution are available. - Both plain I2S and LSBJ_FORMAT (for the PT8211 for instance) are available (configurable in AudioConfigRP2040.h), default is LSBJ. - Outputs pins can be configured in AudioConfigRP2040.h. Default is BCK: 20, WS: 21, DATA: 22. - - Two timer interrupts (DMA_IRQ_0 and DMA_IRQ_1) and two DMA channels are claimed (numbers not hardcoded). + - One non-exclusive timer interrupts (DMA_IRQ_0) and two DMA channels are claimed (numbers not hardcoded). - At the time of writing, LSBJ is only available with github arduino-pico core. - Note that AUDIO_INPUT and mozziAnalogRead() return values in the RP2040's full ADC resolution of 0-4095 rather than AVR's 0-1023. - twi_nonblock is not ported