Skip to content

Commit 52683a8

Browse files
committed
Sleep now working: Use embassy-time instead of rtic-monotonics
Previously, core core SYST peripheral was used as the monotonics provider. This is probably problematic when doing IDLE mode sleepping.
1 parent 5641127 commit 52683a8

File tree

10 files changed

+178
-219
lines changed

10 files changed

+178
-219
lines changed

nrf52-code/boards/dk-solution/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ embassy-time = "0.5"
1515
embedded-hal = "1.0"
1616
nb = "1"
1717
grounded = { version = "0.2.0", features = ["cas"] }
18-
hal = { package = "embassy-nrf", version = "0.9", features = ["nrf52840", "unstable-pac"] }
18+
hal = { package = "embassy-nrf", version = "0.9", features = ["nrf52840", "time-driver-rtc1", "unstable-pac"] }
1919

2020
[features]
2121
advanced = []

nrf52-code/boards/dk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ embassy-time = "0.5"
1515
embedded-hal = "1.0"
1616
nb = "1"
1717
grounded = { version = "0.2.0", features = ["cas"] }
18-
hal = { package = "embassy-nrf", version = "0.9", features = ["nrf52840", "unstable-pac"] }
18+
hal = { package = "embassy-nrf", version = "0.9", features = ["nrf52840", "time-driver-rtc1", "unstable-pac"] }
1919

2020
[features]
2121
advanced = []

nrf52-code/boards/dongle/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version = "0.0.0"
99
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
1010
cortex-m-rt = "0.7.5"
1111
cortex-m-semihosting = "0.5.0"
12-
hal = { package = "embassy-nrf", version = "0.9", features = ["nrf52840", "unstable-pac"] }
12+
hal = { package = "embassy-nrf", version = "0.9", features = ["nrf52840", "time-driver-rtc1", "unstable-pac"] }
1313
embedded-io = "0.7"
1414
critical-section = "1.2.0"
1515
defmt = "1"

nrf52-code/loopback-fw/Cargo.lock

Lines changed: 51 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nrf52-code/loopback-fw/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ bsp = { package = "dongle", path = "../boards/dongle" }
1515
# bsp = { package = "dk", path = "../boards/dk", features = ["usbd", "radio"] }
1616
heapless = { version = "0.9", features = ["defmt"] }
1717
rtic = { version = "2.1.2", features = ["thumbv7-backend"] }
18-
rtic-monotonics = { version = "2.0.3", features = ["cortex-m-systick"] }
1918
usb-device = { version = "0.3.2", features = ["defmt"] }
2019
usbd-hid = { version = "0.9", features = ["defmt"] }
2120
usbd-serial = "0.2.2"
2221
static_cell = "2"
2322
embedded-io-async = "0.6"
23+
embedded-hal-async = "1"
2424
embassy-usb = { version = "0.5", features = ["defmt"] }
2525
embassy-sync = "0.7"
26+
embassy-futures = "0.1"
27+
# Configure embassy-time to be executor agnostic.
28+
embassy-time = { version = "0.5", features = ["defmt-timestamp-uptime", "generic-queue-16"] }
2629

2730
[features]
2831
dk = []

nrf52-code/loopback-fw/src/main.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ use bsp::RgbLed;
1111
#[rtic::app(device = bsp, peripherals = false, dispatchers = [QSPI, CRYPTOCELL])]
1212
mod app {
1313
use bsp::hal::{self, usb::vbus_detect::HardwareVbusDetect};
14+
use embedded_hal_async::delay::DelayNs as _;
1415
use core::fmt::Write as _;
1516
use defmt_rtt as _;
1617
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
18+
use embassy_time::Delay;
19+
use embassy_time::Duration;
1720
use embassy_usb::class::cdc_acm;
1821
use embassy_usb::class::hid;
1922
use embedded_io_async::Write as _;
20-
use rtic_monotonics::fugit::ExtU32;
2123

22-
use rtic_monotonics::systick::prelude::*;
2324
use static_cell::StaticCell;
2425

2526
const SERIAL_BUFFER_SIZE: usize = 512;
@@ -34,8 +35,6 @@ mod app {
3435
RADIO => hal::radio::InterruptHandler<hal::peripherals::RADIO>;
3536
});
3637

37-
systick_monotonic!(Mono);
38-
3938
/// An adapter that simplifies asynchronously writing to the USB ACM by buffering writes.
4039
///
4140
/// All writes are buffered until `flush` is called, which performs the async write.
@@ -125,9 +124,8 @@ mod app {
125124
struct MySharedResources {}
126125

127126
#[init]
128-
fn init(ctx: init::Context) -> (MySharedResources, MyLocalResources) {
127+
fn init(mut ctx: init::Context) -> (MySharedResources, MyLocalResources) {
129128
let board = bsp::init().unwrap();
130-
Mono::start(ctx.core.SYST, 64_000_000);
131129
defmt::println!("-- Radio Loopback firmware --");
132130

133131
#[cfg(feature = "dk")]
@@ -280,9 +278,7 @@ mod app {
280278

281279
// Set the ARM SLEEPONEXIT bit to go to sleep after handling interrupts
282280
// See https://developer.arm.com/docs/100737/0100/power-management/sleep-mode/sleep-on-exit-bit
283-
// TODO: Unfortunately, this does not work yet. Radio packets are not
284-
// arriving properly.
285-
//ctx.core.SCB.set_sleepdeep();
281+
ctx.core.SCB.set_sleepdeep();
286282

287283
(shared, local)
288284
}
@@ -293,11 +289,7 @@ mod app {
293289
// Now Wait For Interrupt is used instead of a busy-wait loop
294290
// to allow MCU to sleep between interrupts
295291
// https://developer.arm.com/documentation/ddi0406/c/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/WFI
296-
//
297-
// TODO: Unfortunately, this does not work yet. Radio packets are not
298-
// arriving properly.
299-
// rtic::export::wfi()
300-
cortex_m::asm::nop();
292+
rtic::export::wfi()
301293
}
302294
}
303295

@@ -326,7 +318,12 @@ mod app {
326318
async fn usb_acm(mut ctx: usb_acm::Context) {
327319
loop {
328320
// Wait for up to 200 ms for a connection, discard ACM data otherwise.
329-
match Mono::timeout_after(200.millis(), ctx.local.usb_acm.wait_connection()).await {
321+
match embassy_time::with_timeout(
322+
Duration::from_millis(200),
323+
ctx.local.usb_acm.wait_connection(),
324+
)
325+
.await
326+
{
330327
Ok(_) => {
331328
defmt::info!("ACM Connected");
332329
connected_usb_acm(&mut ctx).await;
@@ -349,8 +346,11 @@ mod app {
349346
let mut buffer = [0u8; 64];
350347
loop {
351348
// Poll for a frame for up to 50 milliseconds.
352-
if let Ok(result) =
353-
Mono::timeout_after(50.millis(), ctx.local.usb_acm.read_packet(&mut buffer)).await
349+
if let Ok(result) = embassy_time::with_timeout(
350+
Duration::from_millis(50),
351+
ctx.local.usb_acm.read_packet(&mut buffer),
352+
)
353+
.await
354354
{
355355
match result {
356356
Ok(n) => {
@@ -377,8 +377,8 @@ mod app {
377377
.usb_acm_reader
378378
.try_read(&mut buffer[0..MAX_ACM_PACKET_SIZE - 1])
379379
{
380-
match Mono::timeout_after(
381-
20.millis(),
380+
match embassy_time::with_timeout(
381+
Duration::from_millis(20),
382382
ctx.local.usb_acm.write_packet(&buffer[0..bytes_read]),
383383
)
384384
.await
@@ -463,8 +463,11 @@ mod app {
463463
defmt::debug!("Waiting for packet..");
464464

465465
// Poll for a frame for up to 200 milliseconds.
466-
if let Ok(result) =
467-
Mono::timeout_after(200.millis(), ctx.local.radio.receive(ctx.local.packet)).await
466+
if let Ok(result) = embassy_time::with_timeout(
467+
Duration::from_millis(200),
468+
ctx.local.radio.receive(ctx.local.packet),
469+
)
470+
.await
468471
{
469472
match result {
470473
Ok(_) => {
@@ -481,7 +484,7 @@ mod app {
481484
// send packet after 5ms (we know the client waits for 10ms and
482485
// we want to ensure they are definitely in receive mode by the
483486
// time we send this reply)
484-
Mono::delay(5.millis()).await;
487+
Delay.delay_ms(5).await;
485488
if let Err(e) = ctx.local.radio.try_send(ctx.local.packet).await {
486489
let _ = writeln!(
487490
&mut ctx.local.usb_acm_write_adapter,
@@ -528,5 +531,3 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
528531
core::hint::spin_loop();
529532
}
530533
}
531-
532-
defmt::timestamp!("{=u64:tus}", bsp::uptime_us());

0 commit comments

Comments
 (0)