From 772c9f951b5ee292089670d9b489314d75f8a92a Mon Sep 17 00:00:00 2001 From: Felipe Balbi Date: Tue, 16 Dec 2025 11:28:31 -0800 Subject: [iMXRT] dma: fix transfer count computation count should be defined as the integer div_round_up of buffer length and word size, otherwise transferring small buffers will cause a panic due to underflow: if we let from = [0u32; 1]; then calling dma::write() will result in: let count = ((1 / 4 as usize) - 1) = 0 - 1 // underflow Rounding up results in 1 - 1 as expected. --- embassy-imxrt/src/dma.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/embassy-imxrt/src/dma.rs b/embassy-imxrt/src/dma.rs index 76c9953c1..eaa09870d 100644 --- a/embassy-imxrt/src/dma.rs +++ b/embassy-imxrt/src/dma.rs @@ -71,7 +71,7 @@ pub(crate) unsafe fn init() { /// /// SAFETY: Slice must point to a valid location reachable by DMA. pub unsafe fn read<'a, C: Channel, W: Word>(ch: Peri<'a, C>, from: *const W, to: *mut [W]) -> Transfer<'a, C> { - let count = ((to.len() / W::size() as usize) - 1) as isize; + let count = (to.len().div_ceil(W::size() as usize) - 1) as isize; copy_inner( ch, @@ -89,7 +89,7 @@ pub unsafe fn read<'a, C: Channel, W: Word>(ch: Peri<'a, C>, from: *const W, to: /// /// SAFETY: Slice must point to a valid location reachable by DMA. pub unsafe fn write<'a, C: Channel, W: Word>(ch: Peri<'a, C>, from: *const [W], to: *mut W) -> Transfer<'a, C> { - let count = ((from.len() / W::size() as usize) - 1) as isize; + let count = (from.len().div_ceil(W::size() as usize) - 1) as isize; copy_inner( ch, @@ -111,7 +111,7 @@ pub unsafe fn copy<'a, C: Channel, W: Word>(ch: Peri<'a, C>, from: &[W], to: &mu let to_len = to.len(); assert_eq!(from_len, to_len); - let count = ((from_len / W::size() as usize) - 1) as isize; + let count = (from_len.div_ceil(W::size() as usize) - 1) as isize; copy_inner( ch, -- cgit