Skip to content

Commit

Permalink
Use wfi in otherwise empty infinite loops in examples
Browse files Browse the repository at this point in the history
- Clippy warns about empty loops, rust-lang/rust-clippy#6161
- wfi allows to CPU to save some power

WFI was avoided in examples for fear of ill interactions with debuggers.
However the rp2040 debug port does continue to work, as long as the
relevant clocks are not disabled in SLEEP_EN0/SLEEP_EN1. (By default,
all clocks stay enabled in sleep mode.)

This patch replaces several different workarounds with just calling wfi.
  • Loading branch information
jannic authored and ExplodingWaffle committed Aug 14, 2022
1 parent 7a2a714 commit 4e8e10e
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion boards/rp-pico/examples/pico_i2c_pio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn main() -> ! {
print_temperature(&mut uart, temp);

loop {
cortex_m::asm::nop();
cortex_m::asm::wfi();
}
}

Expand Down
3 changes: 1 addition & 2 deletions rp2040-hal/examples/dht11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ fn main() -> ! {
// In this case, we just ignore the result. A real application
// would do something with the measurement.

#[allow(clippy::empty_loop)]
loop {
// Empty loop
cortex_m::asm::wfi();
}
}

Expand Down
6 changes: 1 addition & 5 deletions rp2040-hal/examples/gpio_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ fn main() -> ! {

loop {
// interrupts handle everything else in this example.
// if we wanted low power we could go to sleep. to
// keep this example simple we'll just execute a `nop`.
// the `nop` (No Operation) instruction does nothing,
// but if we have no code here clippy would complain.
cortex_m::asm::nop();
cortex_m::asm::wfi();
}
}

Expand Down
3 changes: 1 addition & 2 deletions rp2040-hal/examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ fn main() -> ! {

// Demo finish - just loop until reset

#[allow(clippy::empty_loop)]
loop {
// Empty loop
cortex_m::asm::wfi();
}
}

Expand Down
3 changes: 1 addition & 2 deletions rp2040-hal/examples/lcd_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ fn main() -> ! {
lcd.write_str("HD44780!", &mut delay).unwrap();

// Do nothing - we're finished
#[allow(clippy::empty_loop)]
loop {
// Empty loop
cortex_m::asm::wfi();
}
}

Expand Down
5 changes: 3 additions & 2 deletions rp2040-hal/examples/pio_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn main() -> ! {
sm.start();

// PIO runs in background, independently from CPU
#[allow(clippy::empty_loop)]
loop {}
loop {
cortex_m::asm::wfi();
}
}
5 changes: 3 additions & 2 deletions rp2040-hal/examples/pio_proc_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn main() -> ! {
sm.start();

// PIO runs in background, independently from CPU
#[allow(clippy::empty_loop)]
loop {}
loop {
cortex_m::asm::wfi();
}
}
5 changes: 3 additions & 2 deletions rp2040-hal/examples/pio_side_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() -> ! {
sm.start();

// PIO runs in background, independently from CPU
#[allow(clippy::empty_loop)]
loop {}
loop {
cortex_m::asm::wfi();
}
}
5 changes: 3 additions & 2 deletions rp2040-hal/examples/pio_synchronized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn main() -> ! {
cortex_m::asm::delay(10_000_000);
let _sm2 = sm2.stop();

#[allow(clippy::empty_loop)]
loop {}
loop {
cortex_m::asm::wfi();
}
}
2 changes: 1 addition & 1 deletion rp2040-hal/examples/rom_funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn main() -> ! {

// In case the reboot fails
loop {
cortex_m::asm::nop();
cortex_m::asm::wfi();
}
}

Expand Down
3 changes: 1 addition & 2 deletions rp2040-hal/examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ fn main() -> ! {
Err(_) => {} // handle errors
};

#[allow(clippy::empty_loop)]
loop {
// Empty loop
cortex_m::asm::wfi();
}
}

Expand Down

0 comments on commit 4e8e10e

Please sign in to comment.