Created
February 20, 2026 08:07
-
-
Save direvius/241d08d696b7b07212f4f84d103ecf98 to your computer and use it in GitHub Desktop.
Embassy STM32 SPI slave hanging example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![no_std] | |
| #![no_main] | |
| use defmt::{info, error}; | |
| use embassy_executor::Spawner; | |
| use embassy_stm32::mode::Async; | |
| use embassy_stm32::spi::mode::Slave; | |
| use embassy_stm32::time::mhz; | |
| use embassy_stm32::{Config, spi}; | |
| use embassy_stm32::{bind_interrupts, dma, peripherals}; | |
| use embassy_time::{Duration, Timer, with_timeout}; | |
| use {defmt_rtt as _, panic_probe as _}; | |
| bind_interrupts!(struct Irqs { | |
| DMA2_STREAM2 => dma::InterruptHandler<peripherals::DMA2_CH2>; | |
| DMA2_STREAM3 => dma::InterruptHandler<peripherals::DMA2_CH3>; | |
| }); | |
| #[embassy_executor::task] | |
| async fn slave_task(mut spi_slave: spi::Spi<'static, Async, Slave>) -> ! { | |
| info!("Receiver started"); | |
| loop { | |
| let mut rx_buf = [0u8; 20]; | |
| info!("Gonna read"); | |
| // this one hangs: | |
| match with_timeout(Duration::from_millis(100), spi_slave.read(&mut rx_buf)).await { | |
| Ok(Ok(_)) => info!("OK"), | |
| Ok(Err(e)) => error!("SPI error: {}", e), | |
| Err(_) => error!("Timeout"), | |
| } | |
| info!("Receive: {:?}", rx_buf); | |
| } | |
| } | |
| #[embassy_executor::task] | |
| async fn master_task() -> ! { | |
| info!("Just wondering"); | |
| loop { | |
| info!("Alive"); | |
| Timer::after(Duration::from_millis(1000)).await; | |
| } | |
| } | |
| #[embassy_executor::main] | |
| async fn main(spawner: Spawner) -> ! { | |
| let config = Config::default(); | |
| let p = embassy_stm32::init(config); | |
| let mut tx_config = spi::Config::default(); | |
| tx_config.frequency = mhz(1); | |
| let rx_config = tx_config.clone(); | |
| spawner.spawn(master_task()).unwrap(); | |
| let spi_slave: spi::Spi<'_, Async, spi::mode::Slave> = spi::Spi::new_slave( | |
| p.SPI1, p.PA5, p.PA7, p.PA6, p.PA4, p.DMA2_CH3, p.DMA2_CH2, Irqs, rx_config, | |
| ); | |
| spawner.spawn(slave_task(spi_slave)).unwrap(); | |
| loop { | |
| defmt::info!("Main"); | |
| Timer::after(Duration::from_millis(500)).await; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment