Skip to content

Commit c7a7760

Browse files
authored
Implement timer conversion for some arrays (#2012)
1 parent fbee4e5 commit c7a7760

File tree

7 files changed

+29
-62
lines changed

7 files changed

+29
-62
lines changed

esp-hal-embassy/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Changed
1515

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

1919
### Fixed
2020

esp-hal-embassy/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ impl<const N: usize> TimerCollection for &'static mut [Timer; N] {
121121
}
122122
}
123123

124+
macro_rules! impl_array {
125+
($n:literal) => {
126+
impl<T> TimerCollection for [T; $n]
127+
where
128+
T: IntoErasedTimer,
129+
{
130+
fn timers(self) -> &'static mut [Timer] {
131+
mk_static!([Timer; $n], self.map(|t| Timer::new(t.into())))
132+
}
133+
}
134+
};
135+
}
136+
137+
impl_array!(2);
138+
impl_array!(3);
139+
impl_array!(4);
140+
124141
/// Initialize embassy.
125142
///
126143
/// Call this as soon as possible, before the first timer-related operation.
@@ -133,6 +150,7 @@ impl<const N: usize> TimerCollection for &'static mut [Timer; N] {
133150
/// - A `OneShotTimer` instance
134151
/// - A mutable static slice of `OneShotTimer` instances
135152
/// - A mutable static array of `OneShotTimer` instances
153+
/// - A 2, 3, 4 element array of `ErasedTimer` instances
136154
///
137155
/// # Examples
138156
///

examples/src/bin/embassy_multicore.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,14 @@ use esp_hal::{
2525
gpio::{AnyOutput, Io, Level},
2626
peripherals::Peripherals,
2727
system::SystemControl,
28-
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
28+
timer::{timg::TimerGroup, ErasedTimer},
2929
};
3030
use esp_hal_embassy::Executor;
3131
use esp_println::println;
3232
use static_cell::StaticCell;
3333

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

36-
// 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
37-
macro_rules! mk_static {
38-
($t:ty,$val:expr) => {{
39-
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
40-
#[deny(unused_attributes)]
41-
let x = STATIC_CELL.uninit().write(($val));
42-
x
43-
}};
44-
}
45-
4636
/// Waits for a message that contains a duration, then flashes a led for that
4737
/// duration of time.
4838
#[embassy_executor::task]
@@ -73,9 +63,7 @@ async fn main(_spawner: Spawner) {
7363
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
7464
let timer0: ErasedTimer = timg0.timer0.into();
7565
let timer1: ErasedTimer = timg0.timer1.into();
76-
let timers = [OneShotTimer::new(timer0), OneShotTimer::new(timer1)];
77-
let timers = mk_static!([OneShotTimer<ErasedTimer>; 2], timers);
78-
esp_hal_embassy::init(&clocks, timers);
66+
esp_hal_embassy::init(&clocks, [timer0, timer1]);
7967

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

examples/src/bin/embassy_multicore_interrupt.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,14 @@ use esp_hal::{
2626
peripherals::Peripherals,
2727
prelude::*,
2828
system::SystemControl,
29-
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
29+
timer::{timg::TimerGroup, ErasedTimer},
3030
};
3131
use esp_hal_embassy::InterruptExecutor;
3232
use esp_println::println;
3333
use static_cell::StaticCell;
3434

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

37-
// 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
38-
macro_rules! mk_static {
39-
($t:ty,$val:expr) => {{
40-
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
41-
#[deny(unused_attributes)]
42-
let x = STATIC_CELL.uninit().write(($val));
43-
x
44-
}};
45-
}
46-
4737
/// Waits for a message that contains a duration, then flashes a led for that
4838
/// duration of time.
4939
#[embassy_executor::task]
@@ -93,9 +83,7 @@ fn main() -> ! {
9383
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
9484
let timer0: ErasedTimer = timg0.timer0.into();
9585
let timer1: ErasedTimer = timg0.timer1.into();
96-
let timers = [OneShotTimer::new(timer0), OneShotTimer::new(timer1)];
97-
let timers = mk_static!([OneShotTimer<ErasedTimer>; 2], timers);
98-
esp_hal_embassy::init(&clocks, timers);
86+
esp_hal_embassy::init(&clocks, [timer0, timer1]);
9987

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

examples/src/bin/embassy_multiprio.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,12 @@ use esp_hal::{
2828
interrupt::Priority,
2929
peripherals::Peripherals,
3030
system::SystemControl,
31-
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
31+
timer::{timg::TimerGroup, ErasedTimer},
3232
};
3333
use esp_hal_embassy::InterruptExecutor;
3434
use esp_println::println;
3535
use static_cell::StaticCell;
3636

37-
// 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
38-
macro_rules! mk_static {
39-
($t:ty,$val:expr) => {{
40-
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
41-
#[deny(unused_attributes)]
42-
let x = STATIC_CELL.uninit().write(($val));
43-
x
44-
}};
45-
}
46-
4737
/// Periodically print something.
4838
#[embassy_executor::task]
4939
async fn high_prio() {
@@ -89,23 +79,19 @@ async fn main(low_prio_spawner: Spawner) {
8979

9080
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
9181
let timer0: ErasedTimer = timg0.timer0.into();
92-
let timer0 = OneShotTimer::new(timer0);
9382

9483
cfg_if::cfg_if! {
9584
if #[cfg(feature = "esp32c2")] {
9685
use esp_hal::timer::systimer::{SystemTimer, Target};
9786
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
98-
let alarm0: ErasedTimer = systimer.alarm0.into();
99-
let timer1 = OneShotTimer::new(alarm0);
87+
let timer1: ErasedTimer = systimer.alarm0.into();
10088
} else {
10189
let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks);
10290
let timer1: ErasedTimer = timg1.timer0.into();
103-
let timer1 = OneShotTimer::new(timer1);
10491
}
10592
}
10693

107-
let timers = mk_static!([OneShotTimer<ErasedTimer>; 2], [timer0, timer1]);
108-
esp_hal_embassy::init(&clocks, timers);
94+
esp_hal_embassy::init(&clocks, [timer0, timer1]);
10995

11096
static EXECUTOR: StaticCell<InterruptExecutor<2>> = StaticCell::new();
11197
let executor = InterruptExecutor::new(system.software_interrupt_control.software_interrupt2);

hil-test/tests/gpio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use esp_hal::{
2020
macros::handler,
2121
peripherals::Peripherals,
2222
system::SystemControl,
23-
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
23+
timer::timg::TimerGroup,
2424
InterruptConfigurable,
2525
};
2626
use hil_test as _;

hil-test/tests/usb_serial_jtag.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,19 @@ mod tests {
1212
clock::ClockControl,
1313
peripherals::Peripherals,
1414
system::SystemControl,
15-
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
15+
timer::timg::TimerGroup,
1616
usb_serial_jtag::UsbSerialJtag,
1717
};
1818
use hil_test as _;
1919

20-
// 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
21-
macro_rules! mk_static {
22-
($t:ty,$val:expr) => {{
23-
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
24-
#[deny(unused_attributes)]
25-
let x = STATIC_CELL.uninit().write(($val));
26-
x
27-
}};
28-
}
29-
3020
#[test]
3121
fn creating_peripheral_does_not_break_debug_connection() {
3222
let peripherals = Peripherals::take();
3323
let system = SystemControl::new(peripherals.SYSTEM);
3424
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
3525

3626
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
37-
let timer0: ErasedTimer = timg0.timer0.into();
38-
let timers = [OneShotTimer::new(timer0)];
39-
let timers = mk_static!([OneShotTimer<ErasedTimer>; 1], timers);
40-
esp_hal_embassy::init(&clocks, timers);
27+
esp_hal_embassy::init(&clocks, timg0.timer0);
4128

4229
_ = UsbSerialJtag::new_async(peripherals.USB_DEVICE).split();
4330
}

0 commit comments

Comments
 (0)