embassy-rs/embassy · error

Invalid word size

Error message

Invalid word size

What it means

This From<WordSize> impl converts the crate's WordSize enum into the GPDMA hardware DW register value. WordSize has more variants than GPDMA supports (e.g. DoubleWord sizes on other DMA IP), and any such variant reaches a catch-all arm that panics.

Solutions

  1. Only use WordSize::OneByte, TwoBytes, or FourBytes with GPDMA transfers
  2. Match on WordSize yourself before converting and return a proper error for unsupported sizes
  3. Check the width requirements of the peripheral; shrink the transfer to a supported word size

Example fix

// before
let dw: pac::gpdma::vals::Dw = WordSize::EightBytes.into(); // panics
// after
let ws = match req.word_size { WordSize::OneByte | WordSize::TwoBytes | WordSize::FourBytes => req.word_size, _ => panic!("gpdma supports at most 4-byte words") };
let dw: pac::gpdma::vals::Dw = ws.into();
Defensive patterns

Strategy: validation

Validate before calling

fn gpdma_word_ok(ws: WordSize) -> bool { matches!(ws, WordSize::OneByte | WordSize::TwoBytes | WordSize::FourBytes) }

Type guard

fn as_gpdma_word(ws: WordSize) -> Option<WordSize> { matches!(ws, WordSize::OneByte | WordSize::TwoBytes | WordSize::FourBytes).then_some(ws) }

Try / catch

// Panic-based conversion: check the variant before .into().
let dw = match ws { WordSize::OneByte | WordSize::TwoBytes | WordSize::FourBytes => ws.into(), _ => return Err(Error::InvalidWordSize) };

Prevention

When it happens

Trigger: Converting a WordSize variant larger than FourBytes (e.g. WordSize::EightBytes / DoubleWord) into pac::gpdma::vals::Dw via .into() when configuring a GPDMA transfer.

Common situations: Generic transfer code that takes WordSize from a user or another driver where 8-byte words are legal on other STM32 DMA peripherals but not on GPDMA.

Related errors


AI-assisted analysis of embassy-rs/embassy@463a07b963 (2026-09-10). Data as JSON: /api/errors/92d8138f61abe47a. Report an issue: GitHub.

Appendix: source

Thrown at embassy-stm32/src/dma/gpdma/mod.rs:456

            packing: vals::Pam::Pack,

            #[cfg(not(stm32c5))]
            burst_length: Burst::_1Beats,
            request_mode: RequestMode::Burst,
            transfer_complete_mode: TransferCompleteMode::EachBlock,
            trigger: None,
        }
    }
}

#[cfg(gpdma)]
impl From<WordSize> for pac::gpdma::vals::Dw {
    fn from(raw: WordSize) -> Self {
        match raw {
            WordSize::OneByte => Self::Byte,
            WordSize::TwoBytes => Self::HalfWord,
            WordSize::FourBytes => Self::Word,
            _ => panic!("Invalid word size"),
        }
    }
}

#[cfg(gpdma)]
impl From<pac::gpdma::vals::Dw> for WordSize {
    fn from(raw: pac::gpdma::vals::Dw) -> Self {
        match raw {
            pac::gpdma::vals::Dw::Byte => Self::OneByte,
            pac::gpdma::vals::Dw::HalfWord => Self::TwoBytes,
            pac::gpdma::vals::Dw::Word => Self::FourBytes,
            _ => panic!("Invalid word size"),
        }
    }
}

#[cfg(lpdma)]
impl From<WordSize> for pac::lpdma::vals::Dw {

View on GitHub (pinned to 463a07b963)