From df0272b7465777a17813a7b4d93ba9d51354d7ab Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sun, 26 Feb 2023 19:28:33 +0000 Subject: [PATCH] pico: automatically allocate dma channels/pio sms for audio audio setup claims the channel passed in so we claim an unused channel/sm, then immediately unclaim it --- 32blit-pico/audio_i2s.cpp | 15 +++++++++++++-- 32blit-pico/audio_pwm.cpp | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/32blit-pico/audio_i2s.cpp b/32blit-pico/audio_i2s.cpp index b78c17278..79bef69e2 100644 --- a/32blit-pico/audio_i2s.cpp +++ b/32blit-pico/audio_i2s.cpp @@ -1,11 +1,15 @@ #include "audio.hpp" #include "config.h" +#include "hardware/dma.h" +#include "hardware/pio.h" #include "pico/audio_i2s.h" #define AUDIO_SAMPLE_FREQ 44100 #include "audio/audio.hpp" +#define audio_pio __CONCAT(pio, PICO_AUDIO_I2S_PIO) + static audio_buffer_pool *audio_pool = nullptr; void init_audio() { @@ -23,11 +27,18 @@ void init_audio() { struct audio_buffer_pool *producer_pool = audio_new_producer_pool(&producer_format, 4, 441); const struct audio_format *output_format; + uint8_t dma_channel = dma_claim_unused_channel(true); + uint8_t pio_sm = pio_claim_unused_sm(audio_pio, true); + + // audio_i2s_setup claims + dma_channel_unclaim(dma_channel); + pio_sm_unclaim(audio_pio, pio_sm); + struct audio_i2s_config config = { .data_pin = PICO_AUDIO_I2S_DATA_PIN, .clock_pin_base = PICO_AUDIO_I2S_CLOCK_PIN_BASE, - .dma_channel = 1, - .pio_sm = 1, + .dma_channel = dma_channel, + .pio_sm = pio_sm, }; output_format = audio_i2s_setup(&audio_format, &config); diff --git a/32blit-pico/audio_pwm.cpp b/32blit-pico/audio_pwm.cpp index 87028b9c9..76d8d12e9 100644 --- a/32blit-pico/audio_pwm.cpp +++ b/32blit-pico/audio_pwm.cpp @@ -3,12 +3,15 @@ #include "pico/audio_pwm.h" #include "hardware/clocks.h" +#include "hardware/dma.h" #include "hardware/pio.h" #define AUDIO_SAMPLE_FREQ 22050 #include "audio/audio.hpp" +#define audio_pio __CONCAT(pio, PICO_AUDIO_I2S_PIO) + static audio_buffer_pool *audio_pool = nullptr; void init_audio() { @@ -26,11 +29,18 @@ void init_audio() { struct audio_buffer_pool *producer_pool = audio_new_producer_pool(&producer_format, 4, 441); const struct audio_format *output_format; + uint8_t dma_channel = dma_claim_unused_channel(true); + uint8_t pio_sm = pio_claim_unused_sm(audio_pio, true); + + // audio_i2s_setup claims + dma_channel_unclaim(dma_channel); + pio_sm_unclaim(audio_pio, pio_sm); + struct audio_pwm_channel_config audio_pwm_config = { .core = { .base_pin = PICO_AUDIO_PWM_MONO_PIN, - .dma_channel = 1, - .pio_sm = 1, + .dma_channel = dma_channel, + .pio_sm = pio_sm, }, .pattern = 3, };