Skip to content

Error handling for DMA max transfer size set to 0 #201

@lonesometraveler

Description

@lonesometraveler

SpiDriver with a DMA max transfer size of 0 leads to panic.

let spi_driver =
            SpiDriver::new::<SPI2>(spi, sclk, serial_out, Some(serial_in), Dma::Auto(0))?;

The creation of the driver succeeds. But the panic occurs when the driver tries to process data in chunks by calling chunks or chunks_mut.

thread 'main' panicked at 'assertion failed: `(left != right)`
	 left: `0`,
	 right: `0`: chunks cannot have a size of zero', 

Setting the transfer size to 0 doesn't make sense. But mistakes happen. Wouldn't it be better if it panics earlier in max_tansfer_size with a message?
Maybe something like this?

impl Dma {
    pub const fn max_transfer_size(&self) -> usize {
        let max_transfer_size = match self {
            Dma::Disabled => TRANS_LEN,
            Dma::Channel1(size) | Dma::Channel2(size) | Dma::Auto(size) => *size,
        };
        match max_transfer_size {
            x if x % 4 != 0 => panic!("The max transfer size must be a multiple of 4"),
            x if x == 0 => panic!("The max transfer size must be greater than 0"),
            x if x > 4096 => 4096,
            _ => max_transfer_size,
        }
    }
}

Or assign a default value?

impl Dma {
    pub const fn max_transfer_size(&self) -> usize {
        let max_transfer_size = match self {
            Dma::Disabled => TRANS_LEN,
            Dma::Channel1(size) | Dma::Channel2(size) | Dma::Auto(size) => *size,
        };
        match max_transfer_size {
            x if x % 4 != 0 => panic!("The max transfer size must be a multiple of 4"),
            x if x == 0 => 4096,
            x if x > 4096 => 4096,
            _ => max_transfer_size,
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions