Skip to content

Commit

Permalink
Update all examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Nov 14, 2024
1 parent 7eb50ed commit 9fe6a53
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 95 deletions.
14 changes: 2 additions & 12 deletions esp-hal-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,10 @@ trait IntoAnyTimer: Into<AnyTimer> {}

impl IntoAnyTimer for AnyTimer {}

impl<T, DM> IntoAnyTimer for TimgTimer<T, DM>
where
DM: esp_hal::Mode,
Self: Into<AnyTimer>,
{
}
impl<T: esp_hal::timer::timg::Instance> IntoAnyTimer for TimgTimer<T> where Self: Into<AnyTimer> {}

#[cfg(not(feature = "esp32"))]
impl<T, DM, COMP, UNIT> IntoAnyTimer for Alarm<'_, T, DM, COMP, UNIT>
where
DM: esp_hal::Mode,
Self: Into<AnyTimer>,
{
}
impl IntoAnyTimer for Alarm<'_> where Self: Into<AnyTimer> {}

impl<T> TimerCollection for T
where
Expand Down
54 changes: 47 additions & 7 deletions esp-hal/src/timer/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
use core::{
fmt::{Debug, Formatter},
marker::PhantomData,
ptr::addr_of_mut,
};

use fugit::{Instant, MicrosDurationU64};
Expand Down Expand Up @@ -170,6 +171,45 @@ impl<'d> SystemTimer<'d> {
}
}

/// Alarms created from the System Timer peripheral.
pub struct SysTimerAlarms {
/// Alarm 0
pub alarm0: Alarm<'static>,
/// Alarm 1
pub alarm1: Alarm<'static>,
/// Alarm 2
pub alarm2: Alarm<'static>,

/// Unit 1
///
/// Leftover unit which wasn't used to create the three alarms.
#[cfg(not(esp32s2))]
pub unit1: SpecificUnit<'static, 1>,
}

impl SystemTimer<'static> {
/// Split the System Timer into three alarms.
///
/// This is a convenience method to create `'static` alarms of the same
/// type. You are encouraged to use [Alarm::new] over this very specific
/// helper.
pub fn split(self) -> SysTimerAlarms {
static mut UNIT0: Option<AnyUnit<'static>> = None;
let unit0 = unsafe { &mut *addr_of_mut!(UNIT0) };

let unit0 = unit0.insert(self.unit0.into());
let unit = FrozenUnit::new(unit0);

SysTimerAlarms {
alarm0: Alarm::new(self.comparator0.into(), &unit),
alarm1: Alarm::new(self.comparator1.into(), &unit),
alarm2: Alarm::new(self.comparator2.into(), &unit),
#[cfg(not(esp32s2))]
unit1: self.unit1,
}
}
}

/// A
#[cfg_attr(esp32s2, doc = "64-bit")]
#[cfg_attr(not(esp32s2), doc = "52-bit")]
Expand Down Expand Up @@ -942,26 +982,26 @@ pub mod etm {
use super::*;

/// An ETM controlled SYSTIMER event
pub struct Event<'a, 'd, M, DM: crate::Mode> {
alarm: &'a mut Alarm<'d, M>,
pub struct Event<'a, 'd> {
alarm: &'a mut Alarm<'d>,
}

impl<'a, 'd, M, DM: crate::Mode> Event<'a, 'd, M> {
impl<'a, 'd> Event<'a, 'd> {
/// Creates an ETM event from the given [Alarm]
pub fn new(alarm: &'a mut Alarm<'d, M>) -> Self {
pub fn new(alarm: &'a mut Alarm<'d>) -> Self {
Self { alarm }
}

/// Execute closure f with mutable access to the wrapped [Alarm].
pub fn with<R>(&self, f: impl FnOnce(&&'a mut Alarm<'d, M>) -> R) -> R {
pub fn with<R>(&self, f: impl FnOnce(&&'a mut Alarm<'d>) -> R) -> R {
let alarm = &self.alarm;
f(alarm)
}
}

impl<M, DM: crate::Mode> crate::private::Sealed for Event<'_, '_, M> {}
impl crate::private::Sealed for Event<'_, '_> {}

impl<M, DM: crate::Mode> crate::etm::EtmEvent for Event<'_, '_, M> {
impl crate::etm::EtmEvent for Event<'_, '_> {
fn id(&self) -> u8 {
50 + self.alarm.comparator.channel()
}
Expand Down
35 changes: 34 additions & 1 deletion esp-hal/src/timer/timg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ where
}

async fn wait(&self) {
asynch::TimerFuture::new(&self).await
asynch::TimerFuture::new(self).await
}

fn async_interrupt_handler(&self) -> InterruptHandler {
Expand Down Expand Up @@ -996,6 +996,39 @@ where
}
}

impl<TG> embedded_hal_02::watchdog::WatchdogDisable for Wdt<TG>
where
TG: TimerGroupInstance,
{
fn disable(&mut self) {
self.disable();
}
}

impl<TG> embedded_hal_02::watchdog::WatchdogEnable for Wdt<TG>
where
TG: TimerGroupInstance,
{
type Time = MicrosDurationU64;

fn start<T>(&mut self, period: T)
where
T: Into<Self::Time>,
{
self.enable();
self.set_timeout(MwdtStage::Stage0, period.into());
}
}

impl<TG> embedded_hal_02::watchdog::Watchdog for Wdt<TG>
where
TG: TimerGroupInstance,
{
fn feed(&mut self) {
self.feed();
}
}

// Async functionality of the timer groups.
mod asynch {
use core::{
Expand Down
25 changes: 5 additions & 20 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,10 @@ pub trait EspWifiTimerSource: private::Sealed {
/// conflicting implementations.
trait IntoAnyTimer: Into<AnyTimer> {}

impl<T, DM> IntoAnyTimer for TimgTimer<T, DM>
where
DM: esp_hal::Mode,
Self: Into<AnyTimer>,
{
}
impl<T: esp_hal::timer::timg::Instance> IntoAnyTimer for TimgTimer<T> where Self: Into<AnyTimer> {}

#[cfg(not(feature = "esp32"))]
impl<T, DM, COMP, UNIT> IntoAnyTimer for Alarm<'_, T, DM, COMP, UNIT>
where
DM: esp_hal::Mode,
Self: Into<AnyTimer>,
{
}
impl IntoAnyTimer for Alarm<'_> where Self: Into<AnyTimer> {}

impl IntoAnyTimer for AnyTimer {}

Expand All @@ -318,19 +308,14 @@ impl EspWifiTimerSource for TimeBase {
}

impl private::Sealed for TimeBase {}
impl<T, DM> private::Sealed for TimgTimer<T, DM>
impl<T> private::Sealed for TimgTimer<T>
where
DM: esp_hal::Mode,
T: esp_hal::timer::timg::Instance,
Self: Into<AnyTimer>,
{
}
#[cfg(not(feature = "esp32"))]
impl<T, DM, COMP, UNIT> private::Sealed for Alarm<'_, T, DM, COMP, UNIT>
where
DM: esp_hal::Mode,
Self: Into<AnyTimer>,
{
}
impl private::Sealed for Alarm<'_> where Self: Into<AnyTimer> {}

/// A marker trait for suitable Rng sources for esp-wifi
pub trait EspWifiRngSource: rand_core::RngCore + private::Sealed {}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_multiprio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ async fn main(low_prio_spawner: Spawner) {

cfg_if::cfg_if! {
if #[cfg(feature = "esp32c2")] {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
let timer1: AnyTimer = systimer.alarm0.into();
} else {
let timg1 = TimerGroup::new(peripherals.TIMG1);
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_parl_io_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main(_spawner: Spawner) {
esp_println::println!("Init!");
let peripherals = esp_hal::init(esp_hal::Config::default());

let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);

let (rx_buffer, rx_descriptors, _, _) = dma_buffers!(32000, 0);
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_parl_io_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main(_spawner: Spawner) {
esp_println::println!("Init!");
let peripherals = esp_hal::init(esp_hal::Config::default());

let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);

let (_, _, tx_buffer, tx_descriptors) = dma_buffers!(0, 32000);
Expand Down
35 changes: 0 additions & 35 deletions examples/src/bin/watchdog.rs

This file was deleted.

4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ async fn main(spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_access_point_with_sta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ async fn main(spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ async fn main(spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ async fn main(_spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ async fn main(spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_esp_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ async fn main(_spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/wifi_embassy_esp_now_duplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ async fn main(spawner: Spawner) -> ! {
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
} else {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
use esp_hal::timer::systimer::SystemTimer;
let systimer = SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/src/bin/wifi_embassy_trouble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ async fn main(_s: Spawner) {

#[cfg(not(feature = "esp32"))]
{
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER)
.split::<esp_hal::timer::systimer::Target>();
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER).split();
esp_hal_embassy::init(systimer.alarm0);
}

Expand Down

0 comments on commit 9fe6a53

Please sign in to comment.