Skip to content

Commit

Permalink
Implement timer conversion for some arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Aug 27, 2024
1 parent 5dcde78 commit 726ec5d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 59 deletions.
2 changes: 1 addition & 1 deletion esp-hal-embassy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Updated to latest release (`0.6.0`) for `embassy-executor` (#1942)
- Changed `init` to accept timers of multiple types (#1957)
- Changed `init` to accept timers of multiple types (#1957, #2012)

### Fixed

Expand Down
18 changes: 18 additions & 0 deletions esp-hal-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ impl<const N: usize> TimerCollection for &'static mut [Timer; N] {
}
}

macro_rules! impl_array {
($n:literal) => {
impl<T> TimerCollection for [T; $n]
where
T: IntoErasedTimer,
{
fn timers(self) -> &'static mut [Timer] {
mk_static!([Timer; $n], self.map(|t| Timer::new(t.into())))
}
}
};
}

impl_array!(2);
impl_array!(3);
impl_array!(4);

/// Initialize embassy.
///
/// Call this as soon as possible, before the first timer-related operation.
Expand All @@ -133,6 +150,7 @@ impl<const N: usize> TimerCollection for &'static mut [Timer; N] {
/// - A `OneShotTimer` instance
/// - A mutable static slice of `OneShotTimer` instances
/// - A mutable static array of `OneShotTimer` instances
/// - A 2, 3, 4 element array of `ErasedTimer` instances
///
/// # Examples
///
Expand Down
14 changes: 1 addition & 13 deletions examples/src/bin/embassy_multicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ use static_cell::StaticCell;

static mut APP_CORE_STACK: Stack<8192> = Stack::new();

// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

/// Waits for a message that contains a duration, then flashes a led for that
/// duration of time.
#[embassy_executor::task]
Expand Down Expand Up @@ -73,9 +63,7 @@ async fn main(_spawner: Spawner) {
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let timer0: ErasedTimer = timg0.timer0.into();
let timer1: ErasedTimer = timg0.timer1.into();
let timers = [OneShotTimer::new(timer0), OneShotTimer::new(timer1)];
let timers = mk_static!([OneShotTimer<ErasedTimer>; 2], timers);
esp_hal_embassy::init(&clocks, timers);
esp_hal_embassy::init(&clocks, [timer0, timer1]);

let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);

Expand Down
16 changes: 2 additions & 14 deletions examples/src/bin/embassy_multicore_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,14 @@ use esp_hal::{
peripherals::Peripherals,
prelude::*,
system::SystemControl,
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
timer::{timg::TimerGroup, ErasedTimer},
};
use esp_hal_embassy::InterruptExecutor;
use esp_println::println;
use static_cell::StaticCell;

static mut APP_CORE_STACK: Stack<8192> = Stack::new();

// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

/// Waits for a message that contains a duration, then flashes a led for that
/// duration of time.
#[embassy_executor::task]
Expand Down Expand Up @@ -93,9 +83,7 @@ fn main() -> ! {
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let timer0: ErasedTimer = timg0.timer0.into();
let timer1: ErasedTimer = timg0.timer1.into();
let timers = [OneShotTimer::new(timer0), OneShotTimer::new(timer1)];
let timers = mk_static!([OneShotTimer<ErasedTimer>; 2], timers);
esp_hal_embassy::init(&clocks, timers);
esp_hal_embassy::init(&clocks, [timer0, timer1]);

let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);

Expand Down
20 changes: 3 additions & 17 deletions examples/src/bin/embassy_multiprio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,12 @@ use esp_hal::{
interrupt::Priority,
peripherals::Peripherals,
system::SystemControl,
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
timer::{timg::TimerGroup, ErasedTimer},
};
use esp_hal_embassy::InterruptExecutor;
use esp_println::println;
use static_cell::StaticCell;

// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

/// Periodically print something.
#[embassy_executor::task]
async fn high_prio() {
Expand Down Expand Up @@ -89,23 +79,19 @@ async fn main(low_prio_spawner: Spawner) {

let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let timer0: ErasedTimer = timg0.timer0.into();
let timer0 = OneShotTimer::new(timer0);

cfg_if::cfg_if! {
if #[cfg(feature = "esp32c2")] {
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
let alarm0: ErasedTimer = systimer.alarm0.into();
let timer1 = OneShotTimer::new(alarm0);
let timer1: ErasedTimer = systimer.alarm0.into();
} else {
let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks);
let timer1: ErasedTimer = timg1.timer0.into();
let timer1 = OneShotTimer::new(timer1);
}
}

let timers = mk_static!([OneShotTimer<ErasedTimer>; 2], [timer0, timer1]);
esp_hal_embassy::init(&clocks, timers);
esp_hal_embassy::init(&clocks, [timer0, timer1]);

static EXECUTOR: StaticCell<InterruptExecutor<2>> = StaticCell::new();
let executor = InterruptExecutor::new(system.software_interrupt_control.software_interrupt2);
Expand Down
15 changes: 1 addition & 14 deletions hil-test/tests/usb_serial_jtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@ mod tests {
};
use hil_test as _;

// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

#[test]
fn creating_peripheral_does_not_break_debug_connection() {
let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let timer0: ErasedTimer = timg0.timer0.into();
let timers = [OneShotTimer::new(timer0)];
let timers = mk_static!([OneShotTimer<ErasedTimer>; 1], timers);
esp_hal_embassy::init(&clocks, timers);
esp_hal_embassy::init(&clocks, timg0.timer0);

_ = UsbSerialJtag::new_async(peripherals.USB_DEVICE).split();
}
Expand Down

0 comments on commit 726ec5d

Please sign in to comment.