From b3a74de31dd09d665721e0398763f7dc5e26c076 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 6 Dec 2023 10:57:32 -0500 Subject: [PATCH] use DMA in PIO when transfer size exceeds FIFO size --- ports/raspberrypi/common-hal/rp2pio/StateMachine.c | 7 +++++-- ports/raspberrypi/common-hal/rp2pio/StateMachine.h | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index 9034b642427e..643701c22ecc 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -348,6 +348,8 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, } else if (!tx_fifo) { join = PIO_FIFO_JOIN_RX; } + self->fifo_depth = (join == PIO_FIFO_JOIN_NONE) ? 4 : 8; + if (rx_fifo) { self->rx_dreq = pio_get_dreq(self->pio, self->state_machine, false); } @@ -688,8 +690,9 @@ static bool _transfer(rp2pio_statemachine_obj_t *self, // This implementation is based on SPI but varies because the tx and rx buffers // may be different lengths and occur at different times or speeds. - // Use DMA for large transfers if channels are available - const size_t dma_min_size_threshold = 32; + // Use DMA for large transfers if channels are available. + // Don't exceed FIFO size. + const size_t dma_min_size_threshold = self->fifo_depth; int chan_tx = -1; int chan_rx = -1; size_t len = MAX(out_len, in_len); diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h index 71f85b1f11d0..ddcd05c2d010 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h @@ -59,6 +59,7 @@ typedef struct { bool in_shift_right; bool user_interruptible; uint8_t offset; + uint8_t fifo_depth; // Either 4 if FIFOs are not joined, or 8 if they are. // dma-related items volatile int pending_buffers;