From 4e8e10e2ac409272c66432deb1de854871ba9abb Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Mon, 1 Aug 2022 14:50:48 +0000 Subject: [PATCH] Use wfi in otherwise empty infinite loops in examples - Clippy warns about empty loops, https://github.com/rust-lang/rust-clippy/issues/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. --- boards/rp-pico/examples/pico_i2c_pio.rs | 2 +- rp2040-hal/examples/dht11.rs | 3 +-- rp2040-hal/examples/gpio_irq_example.rs | 6 +----- rp2040-hal/examples/i2c.rs | 3 +-- rp2040-hal/examples/lcd_display.rs | 3 +-- rp2040-hal/examples/pio_blink.rs | 5 +++-- rp2040-hal/examples/pio_proc_blink.rs | 5 +++-- rp2040-hal/examples/pio_side_set.rs | 5 +++-- rp2040-hal/examples/pio_synchronized.rs | 5 +++-- rp2040-hal/examples/rom_funcs.rs | 2 +- rp2040-hal/examples/spi.rs | 3 +-- 11 files changed, 19 insertions(+), 23 deletions(-) diff --git a/boards/rp-pico/examples/pico_i2c_pio.rs b/boards/rp-pico/examples/pico_i2c_pio.rs index 7bff71fd1..5bb2f18af 100644 --- a/boards/rp-pico/examples/pico_i2c_pio.rs +++ b/boards/rp-pico/examples/pico_i2c_pio.rs @@ -149,7 +149,7 @@ fn main() -> ! { print_temperature(&mut uart, temp); loop { - cortex_m::asm::nop(); + cortex_m::asm::wfi(); } } diff --git a/rp2040-hal/examples/dht11.rs b/rp2040-hal/examples/dht11.rs index 31f9dd4cc..6dbf8abf2 100644 --- a/rp2040-hal/examples/dht11.rs +++ b/rp2040-hal/examples/dht11.rs @@ -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(); } } diff --git a/rp2040-hal/examples/gpio_irq_example.rs b/rp2040-hal/examples/gpio_irq_example.rs index 336585722..3fc3d6f4c 100644 --- a/rp2040-hal/examples/gpio_irq_example.rs +++ b/rp2040-hal/examples/gpio_irq_example.rs @@ -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(); } } diff --git a/rp2040-hal/examples/i2c.rs b/rp2040-hal/examples/i2c.rs index 43423f153..2acf21bce 100644 --- a/rp2040-hal/examples/i2c.rs +++ b/rp2040-hal/examples/i2c.rs @@ -97,9 +97,8 @@ fn main() -> ! { // Demo finish - just loop until reset - #[allow(clippy::empty_loop)] loop { - // Empty loop + cortex_m::asm::wfi(); } } diff --git a/rp2040-hal/examples/lcd_display.rs b/rp2040-hal/examples/lcd_display.rs index 7788bc789..0781d0286 100644 --- a/rp2040-hal/examples/lcd_display.rs +++ b/rp2040-hal/examples/lcd_display.rs @@ -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(); } } diff --git a/rp2040-hal/examples/pio_blink.rs b/rp2040-hal/examples/pio_blink.rs index d0ea164eb..09136071d 100644 --- a/rp2040-hal/examples/pio_blink.rs +++ b/rp2040-hal/examples/pio_blink.rs @@ -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(); + } } diff --git a/rp2040-hal/examples/pio_proc_blink.rs b/rp2040-hal/examples/pio_proc_blink.rs index 1528cb374..fab202f2a 100644 --- a/rp2040-hal/examples/pio_proc_blink.rs +++ b/rp2040-hal/examples/pio_proc_blink.rs @@ -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(); + } } diff --git a/rp2040-hal/examples/pio_side_set.rs b/rp2040-hal/examples/pio_side_set.rs index 46f45b807..1fcc39db8 100644 --- a/rp2040-hal/examples/pio_side_set.rs +++ b/rp2040-hal/examples/pio_side_set.rs @@ -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(); + } } diff --git a/rp2040-hal/examples/pio_synchronized.rs b/rp2040-hal/examples/pio_synchronized.rs index 39cf23db9..096b88534 100644 --- a/rp2040-hal/examples/pio_synchronized.rs +++ b/rp2040-hal/examples/pio_synchronized.rs @@ -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(); + } } diff --git a/rp2040-hal/examples/rom_funcs.rs b/rp2040-hal/examples/rom_funcs.rs index 8fc976972..548400fff 100644 --- a/rp2040-hal/examples/rom_funcs.rs +++ b/rp2040-hal/examples/rom_funcs.rs @@ -166,7 +166,7 @@ fn main() -> ! { // In case the reboot fails loop { - cortex_m::asm::nop(); + cortex_m::asm::wfi(); } } diff --git a/rp2040-hal/examples/spi.rs b/rp2040-hal/examples/spi.rs index e13e22204..0f67fe79f 100644 --- a/rp2040-hal/examples/spi.rs +++ b/rp2040-hal/examples/spi.rs @@ -121,9 +121,8 @@ fn main() -> ! { Err(_) => {} // handle errors }; - #[allow(clippy::empty_loop)] loop { - // Empty loop + cortex_m::asm::wfi(); } }