esp-hal/esp32s3-hal/examples/embassy_serial.rs
Scott Mabin 1731169112
Async serial write (#510)
* Initial async_{write|flush} implementations

- ESP32C3 + UART0 example

* Support UART1 & UART2

* Add examples for all chips

* reduce number of wakers depending on uart count
2023-05-05 09:05:15 -07:00

84 lines
2.2 KiB
Rust

//! embassy serial
//!
//! This is an example of running the embassy executor and asynchronously
//! writing to a uart.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Executor;
use embassy_time::{Duration, Timer};
use esp32s3_hal::{
clock::ClockControl,
embassy,
peripherals::{Interrupt, Peripherals, UART0},
prelude::*,
timer::TimerGroup,
Rtc,
Uart,
};
use esp_backtrace as _;
use static_cell::StaticCell;
#[embassy_executor::task]
async fn run(mut uart: Uart<'static, UART0>) {
loop {
embedded_hal_async::serial::Write::write(&mut uart, b"Hello async write!!!\r\n")
.await
.unwrap();
embedded_hal_async::serial::Write::flush(&mut uart)
.await
.unwrap();
Timer::after(Duration::from_millis(1_000)).await;
}
}
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
#[entry]
fn main() -> ! {
esp_println::println!("Init!");
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,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;
// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
#[cfg(feature = "embassy-time-systick")]
embassy::init(
&clocks,
esp32s3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);
#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);
let uart0 = Uart::new(peripherals.UART0, &mut system.peripheral_clock_control);
esp32s3_hal::interrupt::enable(Interrupt::UART0, esp32s3_hal::Priority::Priority1).unwrap();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(run(uart0)).ok();
});
}