Skip to content
xoviat edited this page Mar 27, 2021 · 16 revisions

Hello, World

To begin this example, you should:

  • Have an MCU that embassy supports. We currently support stm32f4, stm32l0, and nrf series MCUs. We aim to support all common stm32 families.
  • Know how to compile, load, and debug a rust program on an embedded target.
  • Have an embedded probe that your IDE supports.
  • Have the nightly rust compiler with the applicable eabi target, for example thumbv7em-none-eabi.

If you're not familiar with embedded development, consult the rust embedded development book.

Because embassy uses some unstable features to support async, embassy requires the rust nightly compiler. Even though embassy currently requires the nightly compiler, you can be confident that software written with embassy will continue to be supported into the future as the required compiler features become stabilized.

The main Function

To begin, let's create our main function:

#[embassy::main(use_hse = 48)]
async fn main(spawner: Spawner) {
    let dp = embassy_stm32::Peripherals::take().unwrap();
    let clocks = dp.clocks;

    spawner.spawn(run1()).unwrap();
}

First, we the write main function and add an embassy::main attribute. The embassy::main attribute sets-up a simple clock and executor configuration using the specified arguments. In this case we use a high-speed external oscillator running at 48 Mhz. Then, we take our device peripherals with embassy_stm32::Peripherals::take(). This replaces the standard pac::Peripherals::take() call, because embassy takes some of the peripherals during set-up. Finally, we use the spawner to spawn a task.

Declaring a task

Every task is declared with the task macro, as such. Every task is a standard async function defined with the task attribute. Only functions defined with the task attribute can be spawned with the spawner. Tasks can accept any number of arguments, but they cannot be defined with generic parameters.

#[task]
async fn run2() {
    loop {
        info!("tick");
        Timer::after(Duration::from_ticks(13000 as u64)).await;
    }
}
Clone this wiki locally