STM32 Embassy using peripherals in tasks

⚓ Rust    📅 2025-12-24    👤 surdeus    👁️ 11      

surdeus

Warning

This post was published 119 days ago. The information described in this article may have changed.

Hello!
I'm trying to learn rust for embed. I'm struggle with using multiple tasks that uses peripheral.

I can't understand how to share embassy_stm32::Peripherals between tasks.

For example embassy_stm32::Peripherals::take is private. cortex_m::Peripherals::take().unwrap() do not have PA6 or other pins.

I can't send spi to spawner like spawner.spawn(gyro_task(spi)).unwrap(); as task can't be generic and I don't want to have long code for task arguments and want some flexibility.

Usage of something like

static mut PERIPHERALS:Peripherals=embassy_stm32::init(Config::default());
// another task
let mut led = Output::new(PERIPHERALS.PA4, Level::Low, Speed::Low);

will produce error
move occurs because PERIPHERALS.PA4has typePA4, which does not implement the 'Copy' trait
How to share SPI if, for example I have multiple devices on one bus?
I understand how to do such task in C with ease. But what is proper way in rust+embassy if you don't want your code to be hardcoded for SPI1 PA3 etc?

1 post - 1 participant

Read full topic

🏷️ Rust_feed