Rust Embassy: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 13: Line 13:
esp-generate --chip esp32 esp-blink
esp-generate --chip esp32 esp-blink
</syntaxhighlight>
</syntaxhighlight>
=Blinky=
=Template=
The template provides this
The template provides this
<syntaxhighlight lang="rs">
<syntaxhighlight lang="rs">
Line 51: Line 51:
</syntaxhighlight>
</syntaxhighlight>
Could not find and led on my board so I used pin 23.
Could not find and led on my board so I used pin 23.
=Tasks=
We need to create and executor which I assume is the same as the scheduler.
<syntaxhighlight lang="rs">
    static EXECUTOR: StaticCell<Executor> = StaticCell::new();
    let executor = EXECUTOR.init(Executor::new());
    executor.run(|spawner| {
        // Then add the tasks
        spawner.spawn(blink_me(led)).unwrap();
    });
</syntaxhighlight>
I guess this is really easy once you know
<syntaxhighlight lang="rs">
#[embassy_executor::task]
async fn blink_me(mut led: Output<'static>) {
    loop {
        led.toggle();
        Timer::after(Duration::from_millis(330)).await;
    }
}
</syntaxhighlight>

Revision as of 00:51, 31 December 2024

Introduction

After spending some time on RTOS I wanted to compare using Rust to do the same thing. Embassy seem more used than the non-std rust. In fact getting the bluetooth to work seem too hard

Getting Started

For the older wroom-32 boards, they use xtensa chips. For this we need to install the toolchain

cargo install espup
espup install # To install Espressif Rust ecosystem
. $HOME/export-esp.sh # This add stuff to the path

Once done we can create a project with

cargo install esp-generate
esp-generate --chip esp32 esp-blink

Template

The template provides this

#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::prelude::*;
use log::info;

#[main]
async fn main(spawner: Spawner) {
    let peripherals = esp_hal::init({
        let mut config = esp_hal::Config::default();
        config.cpu_clock = CpuClock::max();
        config
    });

    esp_println::logger::init_logger_from_env();

    let timer0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1);
    esp_hal_embassy::init(timer0.timer0);

    info!("Embassy initialized!");

    // TODO: Spawn some tasks
    let _ = spawner;

    loop {
        info!("Hello world!");
        Timer::after(Duration::from_secs(1)).await;
    }
}

Could not find and led on my board so I used pin 23.

Tasks

We need to create and executor which I assume is the same as the scheduler.

    static EXECUTOR: StaticCell<Executor> = StaticCell::new();
    let executor = EXECUTOR.init(Executor::new());

    executor.run(|spawner| {
        // Then add the tasks
        spawner.spawn(blink_me(led)).unwrap();
    });

I guess this is really easy once you know

#[embassy_executor::task]
async fn blink_me(mut led: Output<'static>) {
    loop {
        led.toggle();
        Timer::after(Duration::from_millis(330)).await;
    }
}