Skip to content

Use only DMA0 for PIO background read/write #9980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions ports/raspberrypi/audio_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,28 +486,14 @@ void __not_in_flash_func(isr_dma_0)(void) {
dma->channels_to_load_mask |= mask;
background_callback_add(&dma->callback, dma_callback_fun, (void *)dma);
}
if (MP_STATE_PORT(background_pio)[i] != NULL) {
rp2pio_statemachine_obj_t *pio = MP_STATE_PORT(background_pio)[i];
rp2pio_statemachine_dma_complete_write(pio, i);
}
}
}

void __not_in_flash_func(isr_dma_1)(void) {
for (size_t i = 0; i < NUM_DMA_CHANNELS; i++) {
uint32_t mask = 1 << i;
if ((dma_hw->intr & mask) == 0) {
continue;
}
// acknowledge interrupt early. Doing so late means that you could lose an
// interrupt if the buffer is very small and the DMA operation
// completed by the time callback_add() / dma_complete() returned. This
// affected PIO continuous write more than audio.
dma_hw->ints1 = mask;
if (MP_STATE_PORT(background_pio)[i] != NULL) {
rp2pio_statemachine_obj_t *pio = MP_STATE_PORT(background_pio)[i];
if (MP_STATE_PORT(background_pio_read)[i] != NULL) {
rp2pio_statemachine_obj_t *pio = MP_STATE_PORT(background_pio_read)[i];
rp2pio_statemachine_dma_complete_read(pio, i);
}
if (MP_STATE_PORT(background_pio_write)[i] != NULL) {
rp2pio_statemachine_obj_t *pio = MP_STATE_PORT(background_pio_write)[i];
rp2pio_statemachine_dma_complete_write(pio, i);
}
}
}

Expand Down
17 changes: 9 additions & 8 deletions ports/raspberrypi/common-hal/rp2pio/StateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void rp2pio_statemachine_clear_dma_write(int pio_index, int sm) {
if (!dma_hw->inte0) {
irq_set_mask_enabled(1 << DMA_IRQ_0, false);
}
MP_STATE_PORT(background_pio)[channel_write] = NULL;
MP_STATE_PORT(background_pio_write)[channel_write] = NULL;
dma_channel_abort(channel_write);
dma_channel_unclaim(channel_write);
}
Expand All @@ -111,7 +111,7 @@ static void rp2pio_statemachine_clear_dma_read(int pio_index, int sm) {
if (!dma_hw->inte0) {
irq_set_mask_enabled(1 << DMA_IRQ_0, false);
}
MP_STATE_PORT(background_pio)[channel_read] = NULL;
MP_STATE_PORT(background_pio_read)[channel_read] = NULL;
dma_channel_abort(channel_read);
dma_channel_unclaim(channel_read);
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *

// Acknowledge any previous pending interrupt
dma_hw->ints0 |= 1u << channel_write;
MP_STATE_PORT(background_pio)[channel_write] = self;
MP_STATE_PORT(background_pio_write)[channel_write] = self;
dma_hw->inte0 |= 1u << channel_write;

irq_set_mask_enabled(1 << DMA_IRQ_0, true);
Expand Down Expand Up @@ -1392,10 +1392,10 @@ bool common_hal_rp2pio_statemachine_background_read(rp2pio_statemachine_obj_t *s

common_hal_mcu_disable_interrupts();
// Acknowledge any previous pending interrupt
dma_hw->ints1 |= 1u << channel_read;
MP_STATE_PORT(background_pio)[channel_read] = self;
dma_hw->inte1 |= 1u << channel_read;
irq_set_mask_enabled(1 << DMA_IRQ_1, true);
dma_hw->ints0 |= 1u << channel_read;
MP_STATE_PORT(background_pio_read)[channel_read] = self;
dma_hw->inte0 |= 1u << channel_read;
irq_set_mask_enabled(1 << DMA_IRQ_0, true);
dma_start_channel_mask((1u << channel_read));
common_hal_mcu_enable_interrupts();

Expand Down Expand Up @@ -1485,4 +1485,5 @@ mp_obj_t common_hal_rp2pio_statemachine_get_last_write(rp2pio_statemachine_obj_t

// Use a compile-time constant for MP_REGISTER_POINTER so the preprocessor will
// not split the expansion across multiple lines.
MP_REGISTER_ROOT_POINTER(mp_obj_t background_pio[enum_NUM_DMA_CHANNELS]);
MP_REGISTER_ROOT_POINTER(mp_obj_t background_pio_read[enum_NUM_DMA_CHANNELS]);
MP_REGISTER_ROOT_POINTER(mp_obj_t background_pio_write[enum_NUM_DMA_CHANNELS]);