From 23f1ed08ca142a108222c50528fcb9797b8c168a Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Thu, 8 Dec 2022 14:50:50 +0100 Subject: [PATCH] Embassy init updates: - Rename timg feature to timg0 to better refect which TG is being used - Use the time_driver::TimerType in the signature of init to fix #268 - Update examples - Fix CI features - Add timg0 cfg to build.rs --- .github/workflows/ci-async.yml | 8 ++- esp-hal-common/Cargo.toml | 2 +- esp-hal-common/build.rs | 7 +- esp-hal-common/src/embassy.rs | 12 ++-- .../src/embassy/time_driver_systimer.rs | 12 ++-- .../src/embassy/time_driver_timg.rs | 72 ++++++------------- esp32-hal/Cargo.toml | 2 +- esp32-hal/examples/embassy_hello_world.rs | 4 +- esp32c2-hal/Cargo.toml | 1 + esp32c2-hal/examples/embassy_hello_world.rs | 7 +- esp32c3-hal/Cargo.toml | 1 + esp32c3-hal/examples/embassy_hello_world.rs | 6 +- esp32s2-hal/Cargo.toml | 2 +- esp32s2-hal/examples/embassy_hello_world.rs | 3 +- esp32s3-hal/Cargo.toml | 2 +- esp32s3-hal/examples/embassy_hello_world.rs | 6 +- 16 files changed, 71 insertions(+), 76 deletions(-) diff --git a/.github/workflows/ci-async.yml b/.github/workflows/ci-async.yml index 68619f9b7ea..cfd0227ff12 100644 --- a/.github/workflows/ci-async.yml +++ b/.github/workflows/ci-async.yml @@ -23,7 +23,9 @@ jobs: chip_features: [ { chip: esp32c2, features: "embassy,embassy-time-systick" }, + { chip: esp32c2, features: "embassy,embassy-time-timg0" }, { chip: esp32c3, features: "embassy,embassy-time-systick" }, + { chip: esp32c3, features: "embassy,embassy-time-timg0" }, ] toolchain: [nightly] example: @@ -52,11 +54,11 @@ jobs: matrix: chip_features: [ - { chip: esp32, features: "embassy,embassy-time-timg" }, + { chip: esp32, features: "embassy,embassy-time-timg0" }, # { chip: esp32s2, features: "embassy,embassy-time-systick" }, # Removed for now, see esp32s2-hal/Cargo.toml - { chip: esp32s2, features: "embassy,embassy-time-timg" }, + { chip: esp32s2, features: "embassy,embassy-time-timg0" }, { chip: esp32s3, features: "embassy,embassy-time-systick" }, - { chip: esp32s3, features: "embassy,embassy-time-timg" }, + { chip: esp32s3, features: "embassy,embassy-time-timg0" }, ] example: [ diff --git a/esp-hal-common/Cargo.toml b/esp-hal-common/Cargo.toml index 543f5d959af..688b90f15c6 100644 --- a/esp-hal-common/Cargo.toml +++ b/esp-hal-common/Cargo.toml @@ -80,7 +80,7 @@ async = ["embedded-hal-async", "eh1", "embassy-sync"] embassy = ["embassy-time"] embassy-time-systick = [] -embassy-time-timg = [] +embassy-time-timg0 = [] # Architecture-specific features (intended for internal use) riscv = ["dep:riscv", "critical-section/restore-state-u8", "procmacros/riscv", "riscv-atomic-emulation-trap"] diff --git a/esp-hal-common/build.rs b/esp-hal-common/build.rs index 051ea2cc8af..7037f198f50 100644 --- a/esp-hal-common/build.rs +++ b/esp-hal-common/build.rs @@ -30,6 +30,7 @@ fn main() { // - 'rmt' // - 'spi3' // - 'systimer' + // - 'timg0' // - 'timg1' // - 'uart2' // - 'usb_otg' @@ -49,11 +50,12 @@ fn main() { "pdma", "rmt", "spi3", + "timg0", "timg1", "uart2", ] } else if esp32c2 { - vec!["esp32c2", "riscv", "single_core", "gdma", "systimer"] + vec!["esp32c2", "riscv", "single_core", "gdma", "systimer", "timg0"] } else if esp32c3 { vec![ "esp32c3", @@ -64,6 +66,7 @@ fn main() { "rmt", "spi3", "systimer", + "timg0", "timg1", "usb_serial_jtag", ] @@ -79,6 +82,7 @@ fn main() { "rmt", "spi3", "systimer", + "timg0", "timg1", "usb_otg", ] @@ -94,6 +98,7 @@ fn main() { "rmt", "spi3", "systimer", + "timg0", "timg1", "uart2", "usb_otg", diff --git a/esp-hal-common/src/embassy.rs b/esp-hal-common/src/embassy.rs index 684f18f1178..5ad86d6d63f 100644 --- a/esp-hal-common/src/embassy.rs +++ b/esp-hal-common/src/embassy.rs @@ -2,26 +2,22 @@ use core::{cell::Cell, ptr}; use embassy_time::driver::{AlarmHandle, Driver}; -use crate::clock::Clocks; - #[cfg_attr( all(systimer, feature = "embassy-time-systick",), path = "embassy/time_driver_systimer.rs" )] #[cfg_attr( - all(feature = "embassy-time-timg", any(esp32, esp32s2, esp32s3)), + all(timg0, feature = "embassy-time-timg0"), path = "embassy/time_driver_timg.rs" )] mod time_driver; use time_driver::EmbassyTimer; -pub fn init(clocks: &Clocks) { - // TODO: - // In the future allow taking of &mut Peripheral when we move to the - // PeripheralRef way of driver initialization, see: https://github.com/esp-rs/esp-idf-hal/blob/5d1aea58cdda195e20d1489fcba8a8ecb6562d9a/src/peripheral.rs#L94 +use crate::clock::Clocks; - EmbassyTimer::init(clocks); +pub fn init(clocks: &Clocks, td: time_driver::TimerType) { + EmbassyTimer::init(clocks, td) } pub struct AlarmState { diff --git a/esp-hal-common/src/embassy/time_driver_systimer.rs b/esp-hal-common/src/embassy/time_driver_systimer.rs index 8fd8a30addb..fbc01d0b0ec 100644 --- a/esp-hal-common/src/embassy/time_driver_systimer.rs +++ b/esp-hal-common/src/embassy/time_driver_systimer.rs @@ -9,11 +9,13 @@ use crate::{ pub const ALARM_COUNT: usize = 3; +pub type TimerType = SystemTimer; + pub struct EmbassyTimer { - pub alarms: Mutex<[AlarmState; ALARM_COUNT]>, - pub alarm0: Alarm, - pub alarm1: Alarm, - pub alarm2: Alarm, + pub(crate) alarms: Mutex<[AlarmState; ALARM_COUNT]>, + pub(crate) alarm0: Alarm, + pub(crate) alarm1: Alarm, + pub(crate) alarm2: Alarm, } const ALARM_STATE_NONE: AlarmState = AlarmState::new(); @@ -52,7 +54,7 @@ impl EmbassyTimer { }) } - pub(crate) fn init(_clocks: &Clocks) { + pub fn init(_clocks: &Clocks, _systimer: TimerType) { use crate::{interrupt, interrupt::Priority, macros::interrupt}; interrupt::enable(pac::Interrupt::SYSTIMER_TARGET0, Priority::max()).unwrap(); diff --git a/esp-hal-common/src/embassy/time_driver_timg.rs b/esp-hal-common/src/embassy/time_driver_timg.rs index e2572d5b7c6..4695b455da5 100644 --- a/esp-hal-common/src/embassy/time_driver_timg.rs +++ b/esp-hal-common/src/embassy/time_driver_timg.rs @@ -8,26 +8,28 @@ use crate::{ clock::Clocks, pac, prelude::*, - timer::{Instance, TimerGroup}, + timer::{Timer, Timer0}, }; -pub const ALARM_COUNT: usize = 2; +pub const ALARM_COUNT: usize = 1; + +pub type TimerType = Timer>; pub struct EmbassyTimer { - pub alarms: Mutex<[AlarmState; ALARM_COUNT]>, + pub(crate) alarms: Mutex<[AlarmState; ALARM_COUNT]>, + pub(crate) timer: Mutex>>, } const ALARM_STATE_NONE: AlarmState = AlarmState::new(); -static TG: Mutex>>> = Mutex::new(RefCell::new(None)); - embassy_time::time_driver_impl!(static DRIVER: EmbassyTimer = EmbassyTimer { alarms: Mutex::new([ALARM_STATE_NONE; ALARM_COUNT]), + timer: Mutex::new(RefCell::new(None)), }); impl EmbassyTimer { pub(crate) fn now() -> u64 { - critical_section::with(|cs| TG.borrow_ref(cs).as_ref().unwrap().timer0.now()) + critical_section::with(|cs| DRIVER.timer.borrow_ref(cs).as_ref().unwrap().now()) } pub(crate) fn trigger_alarm(&self, n: usize, cs: CriticalSection) { @@ -42,80 +44,50 @@ impl EmbassyTimer { fn on_interrupt(&self, id: u8) { critical_section::with(|cs| { - let mut tg = TG.borrow_ref_mut(cs); + let mut tg = self.timer.borrow_ref_mut(cs); let tg = tg.as_mut().unwrap(); - match id { - 0 => tg.timer0.clear_interrupt(), - 1 => tg.timer1.clear_interrupt(), - _ => unreachable!(), - }; + tg.clear_interrupt(); self.trigger_alarm(id as usize, cs); }); } - pub(crate) fn init(clocks: &Clocks) { + pub fn init(clocks: &Clocks, mut timer: TimerType) { use crate::{interrupt, interrupt::Priority}; - // TODO can we avoid this steal in the future... - let mut tg = TimerGroup::new(unsafe { pac::Peripherals::steal().TIMG0 }, clocks); // set divider to get a 1mhz clock. abp (80mhz) / 80 = 1mhz... // TODO assert // abp clock is the source and its at the correct speed for the divider - tg.timer0.set_divider(clocks.apb_clock.to_MHz() as u16); - tg.timer1.set_divider(clocks.apb_clock.to_MHz() as u16); + timer.set_divider(clocks.apb_clock.to_MHz() as u16); - critical_section::with(|cs| TG.borrow_ref_mut(cs).replace(tg)); + critical_section::with(|cs| DRIVER.timer.borrow_ref_mut(cs).replace(timer)); interrupt::enable(pac::Interrupt::TG0_T0_LEVEL, Priority::max()).unwrap(); - interrupt::enable(pac::Interrupt::TG0_T1_LEVEL, Priority::max()).unwrap(); #[interrupt] fn TG0_T0_LEVEL() { DRIVER.on_interrupt(0); } - - #[interrupt] - fn TG0_T1_LEVEL() { - DRIVER.on_interrupt(1); - } } pub(crate) fn set_alarm(&self, alarm: embassy_time::driver::AlarmHandle, timestamp: u64) -> bool { critical_section::with(|cs| { let now = Self::now(); let alarm_state = unsafe { self.alarms.borrow(cs).get_unchecked(alarm.id() as usize) }; - let mut tg = TG.borrow_ref_mut(cs); + let mut tg = self.timer.borrow_ref_mut(cs); let tg = tg.as_mut().unwrap(); if timestamp < now { - match alarm.id() { - 0 => tg.timer0.unlisten(), - 1 => tg.timer1.unlisten(), - _ => unreachable!() - } + tg.unlisten(); alarm_state.timestamp.set(u64::MAX); return false; } alarm_state.timestamp.set(timestamp); - match alarm.id() { - 0 => { - tg.timer0.load_alarm_value(timestamp); - tg.timer0.listen(); - tg.timer0.set_counter_decrementing(false); - tg.timer0.set_auto_reload(false); - tg.timer0.set_counter_active(true); - tg.timer0.set_alarm_active(true); - } - 1 => { - tg.timer1.load_alarm_value(timestamp); - tg.timer1.listen(); - tg.timer1.set_counter_decrementing(false); - tg.timer1.set_auto_reload(false); - tg.timer1.set_counter_active(true); - tg.timer1.set_alarm_active(true); - } - _ => unreachable!(), - } - + tg.load_alarm_value(timestamp); + tg.listen(); + tg.set_counter_decrementing(false); + tg.set_auto_reload(false); + tg.set_counter_active(true); + tg.set_alarm_active(true); + true }) } diff --git a/esp32-hal/Cargo.toml b/esp32-hal/Cargo.toml index b0636f86021..c38e8f05af1 100644 --- a/esp32-hal/Cargo.toml +++ b/esp32-hal/Cargo.toml @@ -55,7 +55,7 @@ ufmt = ["esp-hal-common/ufmt"] vectored = ["esp-hal-common/vectored"] async = ["esp-hal-common/async", "embedded-hal-async"] embassy = ["esp-hal-common/embassy"] -embassy-time-timg = ["esp-hal-common/embassy-time-timg", "embassy-time/tick-hz-1_000_000"] +embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"] [[example]] name = "hello_rgb" diff --git a/esp32-hal/examples/embassy_hello_world.rs b/esp32-hal/examples/embassy_hello_world.rs index 0db5e216c02..2fedcc9e4ae 100644 --- a/esp32-hal/examples/embassy_hello_world.rs +++ b/esp32-hal/examples/embassy_hello_world.rs @@ -38,7 +38,6 @@ fn main() -> ! { let peripherals = Peripherals::take().unwrap(); let system = peripherals.DPORT.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - embassy::init(&clocks); let mut rtc = Rtc::new(peripherals.RTC_CNTL); let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -51,6 +50,9 @@ fn main() -> ! { wdt0.disable(); wdt1.disable(); + #[cfg(feature = "embassy-time-timg0")] + embassy::init(&clocks, timer_group0.timer0); + let executor = EXECUTOR.init(Executor::new()); executor.run(|spawner| { diff --git a/esp32c2-hal/Cargo.toml b/esp32c2-hal/Cargo.toml index c61d8342047..7446251e4f0 100644 --- a/esp32c2-hal/Cargo.toml +++ b/esp32c2-hal/Cargo.toml @@ -55,6 +55,7 @@ vectored = ["esp-hal-common/vectored"] async = ["esp-hal-common/async", "embedded-hal-async"] embassy = ["esp-hal-common/embassy"] embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"] +embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"] [[example]] name = "spi_eh1_loopback" diff --git a/esp32c2-hal/examples/embassy_hello_world.rs b/esp32c2-hal/examples/embassy_hello_world.rs index 39529b02b3f..2801037885b 100644 --- a/esp32c2-hal/examples/embassy_hello_world.rs +++ b/esp32c2-hal/examples/embassy_hello_world.rs @@ -39,7 +39,6 @@ fn main() -> ! { let peripherals = Peripherals::take().unwrap(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - embassy::init(&clocks); let mut rtc = Rtc::new(peripherals.RTC_CNTL); let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -50,6 +49,12 @@ fn main() -> ! { rtc.rwdt.disable(); wdt0.disable(); + #[cfg(feature = "embassy-time-systick")] + embassy::init(&clocks, esp32c2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER)); + + #[cfg(feature = "embassy-time-timg0")] + embassy::init(&clocks, timer_group0.timer0); + let executor = EXECUTOR.init(Executor::new()); executor.run(|spawner| { spawner.spawn(run1()).ok(); diff --git a/esp32c3-hal/Cargo.toml b/esp32c3-hal/Cargo.toml index ed90ba4c9ba..c44c1a2d04d 100644 --- a/esp32c3-hal/Cargo.toml +++ b/esp32c3-hal/Cargo.toml @@ -60,6 +60,7 @@ allow-opt-level-z = [] async = ["esp-hal-common/async", "embedded-hal-async"] embassy = ["esp-hal-common/embassy"] embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"] +embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"] [[example]] name = "hello_rgb" diff --git a/esp32c3-hal/examples/embassy_hello_world.rs b/esp32c3-hal/examples/embassy_hello_world.rs index c5d38369b65..f974e23bb40 100644 --- a/esp32c3-hal/examples/embassy_hello_world.rs +++ b/esp32c3-hal/examples/embassy_hello_world.rs @@ -38,7 +38,6 @@ fn main() -> ! { let peripherals = Peripherals::take().unwrap(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - embassy::init(&clocks); let mut rtc = Rtc::new(peripherals.RTC_CNTL); let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -52,6 +51,11 @@ fn main() -> ! { wdt0.disable(); wdt1.disable(); + #[cfg(feature = "embassy-time-systick")] + embassy::init(&clocks, esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER)); + + #[cfg(feature = "embassy-time-timg0")] + embassy::init(&clocks, timer_group0.timer0); let executor = EXECUTOR.init(Executor::new()); executor.run(|spawner| { diff --git a/esp32s2-hal/Cargo.toml b/esp32s2-hal/Cargo.toml index 89090e9ae28..a7dbfbbf140 100644 --- a/esp32s2-hal/Cargo.toml +++ b/esp32s2-hal/Cargo.toml @@ -61,7 +61,7 @@ embassy = ["esp-hal-common/embassy"] # - add 80_000_000 support to embassy time # - Fix https://github.com/esp-rs/esp-hal/issues/253 # embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-1_000_000"] -embassy-time-timg = ["esp-hal-common/embassy-time-timg", "embassy-time/tick-hz-1_000_000"] +embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"] [[example]] name = "hello_rgb" diff --git a/esp32s2-hal/examples/embassy_hello_world.rs b/esp32s2-hal/examples/embassy_hello_world.rs index 70578196ea6..b013a98f0fd 100644 --- a/esp32s2-hal/examples/embassy_hello_world.rs +++ b/esp32s2-hal/examples/embassy_hello_world.rs @@ -39,7 +39,6 @@ fn main() -> ! { let peripherals = Peripherals::take().unwrap(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - embassy::init(&clocks); let mut rtc = Rtc::new(peripherals.RTC_CNTL); let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -52,6 +51,8 @@ fn main() -> ! { wdt0.disable(); wdt1.disable(); + #[cfg(feature = "embassy-time-timg0")] + embassy::init(&clocks, timer_group0.timer0); let executor = EXECUTOR.init(Executor::new()); executor.run(|spawner| { diff --git a/esp32s3-hal/Cargo.toml b/esp32s3-hal/Cargo.toml index e8bb8c3b023..68affc9ed7c 100644 --- a/esp32s3-hal/Cargo.toml +++ b/esp32s3-hal/Cargo.toml @@ -60,7 +60,7 @@ vectored = ["esp-hal-common/vectored"] async = ["esp-hal-common/async", "embedded-hal-async"] embassy = ["esp-hal-common/embassy"] embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"] -embassy-time-timg = ["esp-hal-common/embassy-time-timg", "embassy-time/tick-hz-1_000_000"] +embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"] [[example]] name = "hello_rgb" diff --git a/esp32s3-hal/examples/embassy_hello_world.rs b/esp32s3-hal/examples/embassy_hello_world.rs index 3e0aa90e28c..f707a86a5cd 100644 --- a/esp32s3-hal/examples/embassy_hello_world.rs +++ b/esp32s3-hal/examples/embassy_hello_world.rs @@ -38,7 +38,6 @@ fn main() -> ! { let peripherals = Peripherals::take().unwrap(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - embassy::init(&clocks); let mut rtc = Rtc::new(peripherals.RTC_CNTL); let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -52,6 +51,11 @@ fn main() -> ! { 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 executor = EXECUTOR.init(Executor::new()); executor.run(|spawner| {