Skip to content

Commit

Permalink
pico: automatically allocate dma channels/pio sms for audio
Browse files Browse the repository at this point in the history
audio setup claims the channel passed in so we claim an unused channel/sm, then immediately unclaim it
  • Loading branch information
Daft-Freak committed Feb 26, 2023
1 parent e371c16 commit df0272b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 13 additions & 2 deletions 32blit-pico/audio_i2s.cpp
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
Expand Down
14 changes: 12 additions & 2 deletions 32blit-pico/audio_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
};
Expand Down

0 comments on commit df0272b

Please sign in to comment.