This commit is contained in:
2024-04-14 14:37:40 +02:00
parent 01e60734f5
commit cdaa4754b2
5 changed files with 447 additions and 337 deletions

View File

@@ -1,66 +1,91 @@
//! 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)]
#![feature(trait_alias)]
extern crate alloc;
use alloc::boxed::Box;
use embassy_executor::Executor;
use embassy_time::{Duration, Timer};
use esp32c3_hal::{clock::ClockControl, prelude::*, timer::TimerGroup, Rtc, embassy};
use esp32c3_hal::{
clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc, IO,
};
use esp_alloc::EspHeap;
use esp_backtrace as _;
use esp_hal_common::{Bank0GpioRegisterAccess, GpioPin, InputOutputAnalogPinType, Unknown, IO, pac::Peripherals};
use esp_hal_common::Rmt;
use esp_hal_common::{dma::TxChannel, gdma::Channel0};
use esp_hal_smartled::smartLedAdapter;
use esp_hal_smartled::*;
use smart_leds::SmartLedsWrite;
use smart_leds_trait::{RGB, RGBW, RGB8, White, RGBA};
use static_cell::StaticCell;
#[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;
esp_println::println!("Just woke up");
#[global_allocator]
static ALLOCATOR: EspHeap = EspHeap::empty();
trait MySmartLed {
fn set_color(&mut self, color: RGB8);
}
impl<TX: esp_hal_common::rmt::TxChannel<CH>, const CH: u8, const BUFFER_SIZE: usize> MySmartLed
for SmartLedsAdapter<TX, CH, BUFFER_SIZE>
{
fn set_color(&mut self, color: RGB8) {
if let Err(err) = self.write([color].into_iter()) {
esp_println::println!("{err:?}");
}
}
}
#[embassy_executor::task]
async fn run2() {
async fn blink(mut led: Box<dyn MySmartLed>) {
loop {
esp_println::println!("Bing!");
Timer::after(Duration::from_millis(5_000)).await;
}
}
#[embassy_executor::task]
async fn run_io(io: IO) {
let led = io.pins.gpio1;
blink_led(led).await;
}
async fn blink_led<const N: u8>(
pin: GpioPin<Unknown, Bank0GpioRegisterAccess, InputOutputAnalogPinType, N>,
) {
let mut led = pin.into_push_pull_output();
let mut on = true;
loop {
if on {
esp_println::println!("On!");
} else {
esp_println::println!("Off!");
for r in 0u8..255 {
Timer::after(Duration::from_millis(500)).await;
esp_println::println!("Bing! {r}");
let color = RGB8::new(r, r, r);
led.set_color(color);
}
Timer::after(Duration::from_millis(5_00)).await;
on = !on;
}
}
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
#[riscv_rt::entry]
#[entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
const HEAP_SIZE: usize = 2 * 1024;
extern "C" {
static _sheap: u8;
static _heap_size: u8;
}
unsafe {
let heap_bottom = &_sheap as *const u8 as usize;
let heap_size = &_heap_size as *const u8 as usize;
ALLOCATOR.init(heap_bottom as *mut u8, 1024);
}
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;
// Disable watchdog timers
@@ -69,20 +94,28 @@ fn main() -> ! {
wdt0.disable();
wdt1.disable();
// embassy::init(
// &clocks,
// esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
// );
embassy::init(&clocks, timer_group0.timer0);
embassy::init(
&clocks,
esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let rmt = Rmt::new(
peripherals.RMT,
80u32.MHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();
let mut led = <smartLedAdapter!(0, 1)>::new(rmt.channel0, io.pins.gpio7);
led.set_color(RGB8::new(0, 255, 255));
let led: Box<dyn MySmartLed> = Box::new(led);
let executor = EXECUTOR.init(Executor::new());
executor.run(move |spawner| {
spawner.spawn(run1()).ok();
spawner.spawn(run2()).ok();
let res = spawner.spawn(run_io(io));
esp_println::println!("{res:?}");
});
spawner.spawn(blink(led)).ok();
})
}