Skip to content

Commit

Permalink
Merge pull request #801 from Daft-Freak/pico-extras-dma-sm-alloc
Browse files Browse the repository at this point in the history
Avoid DMA channel/PIO SM conflicts from scanvideo/audio
  • Loading branch information
Gadgetoid authored May 17, 2023
2 parents 517fa76 + df0272b commit f3333f8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 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
10 changes: 10 additions & 0 deletions 32blit-pico/display_scanvideo.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "display.hpp"

#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "pico/time.h"
#include "pico/scanvideo.h"
#include "pico/scanvideo/composable_scanline.h"
Expand Down Expand Up @@ -43,7 +44,12 @@ static void fill_scanline_buffer(struct scanvideo_scanline_buffer *buffer) {
}

void init_display() {
// channel 0 get claimed later, channel 3 doesn't get claimed, but does get used
// reserve them so out claims don't conflict
dma_claim_mask(1 << 0 | 1 << 3);

// PIO SMs that get claimed later
pio_claim_sm_mask(pio0, 1 << 0 | 1 << 3);
}

void update_display(uint32_t time) {
Expand All @@ -56,6 +62,10 @@ void update_display(uint32_t time) {
}

void init_display_core1() {
dma_unclaim_mask(1 << 0 | 1 << 3);
pio_sm_unclaim(pio0, 0);
pio_sm_unclaim(pio0, 3);

// no mode switching yet
#if ALLOW_HIRES
#if DISPLAY_HEIGHT == 160 // extra middle mode
Expand Down
3 changes: 1 addition & 2 deletions 32blit-pico/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ int main() {
init_input();
init_fs();
init_usb();
init_audio();

#if defined(ENABLE_CORE1)
multicore_launch_core1(core1_main);
Expand All @@ -192,8 +193,6 @@ int main() {
blit::render = ::render;
blit::update = ::update;

init_audio();

// user init
::init();

Expand Down

0 comments on commit f3333f8

Please sign in to comment.