Add embassy_serial and embassy_wait examples for ESP32-H2 (#569)

* Add `embassy_{serial,wait}` examples for ESP32-H2

* Update the CHANGELOG
This commit is contained in:
Jesse Braham 2023-05-31 07:17:39 -07:00 committed by GitHub
parent 67e9c60a23
commit e096bca561
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 182 additions and 2 deletions

View File

@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add all `SPI` examples for the ESP32-H2 (#549)
- Add initial support for ADC in ESP32-H2 (#564)
- Simplify the `Delay` driver, derive `Clone` and `Copy` (#568)
- Add `embassy_serial` and `embassy_wait` examples for ESP32-H2 (#569)
### Changed

View File

@ -76,11 +76,19 @@ required-features = ["embassy"]
[[example]]
name = "embassy_i2c"
required-features = ["embassy", "async", "embassy-time-systick"]
required-features = ["async", "embassy", "embassy-time-systick"]
[[example]]
name = "embassy_serial"
required-features = ["async", "embassy"]
[[example]]
name = "embassy_spi"
required-features = ["embassy", "async"]
required-features = ["async", "embassy"]
[[example]]
name = "embassy_wait"
required-features = ["async", "embassy"]
[[example]]
name = "interrupt_preemption"

View File

@ -0,0 +1,83 @@
//! 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 esp32h2_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.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
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,
esp32h2_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);
esp32h2_hal::interrupt::enable(Interrupt::UART0, esp32h2_hal::Priority::Priority1).unwrap();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(run(uart0)).ok();
});
}

View File

@ -0,0 +1,88 @@
//! embassy wait
//!
//! This is an example of asynchronously `Wait`ing for a pin state to change.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Executor;
use embassy_time::{Duration, Timer};
use embedded_hal_async::digital::Wait;
use esp32h2_hal::{
clock::ClockControl,
embassy,
gpio::{Gpio9, Input, PullDown},
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
IO,
};
use esp_backtrace as _;
use static_cell::StaticCell;
#[embassy_executor::task]
async fn ping(mut pin: Gpio9<Input<PullDown>>) {
loop {
esp_println::println!("Waiting...");
pin.wait_for_rising_edge().await.unwrap();
esp_println::println!("Ping!");
Timer::after(Duration::from_millis(100)).await;
}
}
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
#[entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let mut system = peripherals.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
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,
esp32h2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);
#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// GPIO 9 as input
let input = io.pins.gpio9.into_pull_down_input();
// Async requires the GPIO interrupt to wake futures
esp32h2_hal::interrupt::enable(
esp32h2_hal::peripherals::Interrupt::GPIO,
esp32h2_hal::interrupt::Priority::Priority1,
)
.unwrap();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(ping(input)).ok();
});
}