Skip to content

Conversation

@bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Oct 10, 2023

This makes the #[main] macro easily useable.

For RISC-V it just delegates to the existing macro and passes the entry.

For Xtensa it's basically an adaption of the code found in embassy-macro. Since we bring our own executor there it probably won't make sense to have it in embassy

For now, I haven't changed the examples - wanted to wait for a first review round

Here is the embassy_hello_world.rs adapted for S3:

//! embassy hello world
//!
//! This is an example of running the embassy executor with multiple tasks
//! concurrently.

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp32s3_hal::{
    clock::ClockControl,
    embassy::{self},
    peripherals::Peripherals,
    prelude::*,
};
use esp_backtrace as _;

#[embassy_executor::task]
async fn run1() {
    loop {
        esp_println::println!("Hello world from embassy using esp-hal-async!");
        Timer::after(Duration::from_millis(1_000)).await;
    }
}

#[embassy_executor::task]
async fn run2() {
    loop {
        esp_println::println!("Bing!");
        Timer::after(Duration::from_millis(5_000)).await;
    }
}

#[main]
async fn main(spawner: Spawner) {
    esp_println::println!("Init!");
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    #[cfg(feature = "embassy-time-systick")]
    embassy::init(
        &clocks,
        esp32s3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
    );

    #[cfg(feature = "embassy-time-timg0")]
    {
        let timer_group0 = esp32s3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks);
        embassy::init(&clocks, timer_group0.timer0);
    }

    spawner.spawn(run1()).ok();
    spawner.spawn(run2()).ok();

    loop {
        esp_println::println!("Tada!");
        Timer::after(Duration::from_millis(1_500)).await;
    }
}

It's really just a convenience - the current way of creating and using an executor can still be used unchanged.

@bugadani
Copy link
Contributor

bugadani commented Oct 10, 2023

I thought you must call embassy::init before creating the first executor. Won't this just immediately always panic because the executor can't allocate an alarm?

Nvm I checked now and I guess I just didn't understand how this works.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I've been meaning to do this for a while 😅. Looks good to me :)

@bjoernQ bjoernQ force-pushed the feature/embassy-main branch 3 times, most recently from 44b79a5 to 1574d06 Compare October 11, 2023 10:31
@bjoernQ bjoernQ force-pushed the feature/embassy-main branch from 1574d06 to a9ce8ac Compare October 11, 2023 10:40
@MabezDev MabezDev merged commit a0ebdf0 into main Oct 11, 2023
MabezDev pushed a commit to MabezDev/esp-hal that referenced this pull request Oct 20, 2023
@jessebraham jessebraham deleted the feature/embassy-main branch July 3, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants