diff --git a/chip/stm32/dma.c b/chip/stm32/dma.c index 8325d2edba..fe5bdd55d6 100644 --- a/chip/stm32/dma.c +++ b/chip/stm32/dma.c @@ -97,6 +97,13 @@ int dma_start_rx(unsigned channel, unsigned count, void *periph, return 0; } +int dma_bytes_done(struct dma_channel *chan, int orig_count) +{ + if (!(REG32(&chan->ccr) & DMA_EN)) + return 0; + return orig_count - REG32(&chan->cndtr); +} + /* Hide this code behind an undefined CONFIG for now */ #ifdef CONFIG_DMA_TEST diff --git a/chip/stm32/dma.h b/chip/stm32/dma.h index 81a4e1edd9..caf6c6228a 100644 --- a/chip/stm32/dma.h +++ b/chip/stm32/dma.h @@ -117,6 +117,19 @@ int dma_start_rx(unsigned channel, unsigned count, void *periph, */ void dma_disable(unsigned channel); +/** + * Get the number of bytes available to read, or number of bytes written + * + * Since the DMA controller counts downwards, if we know the starting value + * we can work out how many bytes have been completed so far. + * + * @param chan DMA channel to check (use dma_get_channel()) + * @param orig_count Original number of bytes requested on channel + * @return number of bytes completed on a channel, or 0 if this channel is + * not enabled + */ +int dma_bytes_done(struct dma_channel *chan, int orig_count); + /** * Start a previously-prepared dma channel *