From a33801642b2f01e5bb63faafdfee848fb65ba3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 21 Jan 2025 11:13:16 +0100 Subject: [PATCH] Fix examples clippy warnings --- Cargo.toml | 3 +-- examples/adc.rs | 4 +++- examples/aes.rs | 6 +++--- examples/late.rs | 47 ++++++++++++++++++++++++----------------- examples/prince.rs | 8 ++++--- examples/puf.rs | 31 ++++++++++----------------- examples/pwm.rs | 5 ++++- examples/rtic_led.rs | 43 +++++++++++++++++++++---------------- examples/semihosting.rs | 1 + examples/touch.rs | 2 +- examples/usb.rs | 37 ++++++++++++-------------------- 11 files changed, 96 insertions(+), 91 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 24042d2..02cb814 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,8 +38,7 @@ littlefs2 = { version = "0.5", optional = true } [dev-dependencies] aes = "0.7" cortex-m-rt = "0.6" -# cortex-m-rtic = "0.5" -lpc55-rtic = "0.5" +rtic = { package = "cortex-m-rtic", version = "1" } cortex-m-semihosting = "0.3" heapless = "0.7" panic-halt = "0.2" diff --git a/examples/adc.rs b/examples/adc.rs index 4411843..611e19d 100644 --- a/examples/adc.rs +++ b/examples/adc.rs @@ -143,5 +143,7 @@ fn main() -> ! { } heprintln!("looping").unwrap(); - loop {} + loop { + heprintln!("looping").unwrap(); + } } diff --git a/examples/aes.rs b/examples/aes.rs index 9c4227c..027825e 100644 --- a/examples/aes.rs +++ b/examples/aes.rs @@ -36,8 +36,8 @@ fn main() -> ! { // // via software // - let mut sw_block = block.clone(); - let cipher = aes::Aes256::new(&key); + let mut sw_block = block; + let cipher = aes::Aes256::new(key); let (sw_cyc_enc, _) = hal::count_cycles(|| { cipher.encrypt_block(&mut sw_block); @@ -57,7 +57,7 @@ fn main() -> ! { // // via hardware // - let mut hw_block = block.clone(); + let mut hw_block = block; let cipher = hashcrypt.aes256(&raw_key); cipher.prime_for_encryption(); diff --git a/examples/late.rs b/examples/late.rs index b05fd67..7f5fc69 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -1,6 +1,5 @@ //! examples/late.rs -#![deny(unsafe_code)] // something something about: // // error: use of deprecated item @@ -11,34 +10,44 @@ #![no_main] #![no_std] -use cortex_m_semihosting::hprintln; -use hal::raw::Interrupt; -use heapless::spsc::{Consumer, Producer, Queue}; -use lpc55_hal as hal; use panic_semihosting as _; -#[rtic::app(device = hal::raw)] -const APP: () = { - // Late resources - struct Resources { +#[rtic::app(device = lpc55_hal::raw)] +mod app { + use core::ptr::addr_of_mut; + + use cortex_m_semihosting::hprintln; + use hal::raw::Interrupt; + use heapless::spsc::{Consumer, Producer, Queue}; + use lpc55_hal as hal; + + #[shared] + struct SharedResources {} + + #[local] + struct LocalResources { p: Producer<'static, u32, 4>, c: Consumer<'static, u32, 4>, } #[init] - fn init(_: init::Context) -> init::LateResources { + fn init(_ctx: init::Context) -> (SharedResources, LocalResources, init::Monotonics) { static mut Q: Queue = Queue::new(); - let (p, c) = Q.split(); + let (p, c) = unsafe { (*addr_of_mut!(Q)).split() }; // Initialization of late resources - init::LateResources { p, c } + ( + SharedResources {}, + LocalResources { p, c }, + init::Monotonics(), + ) } - #[idle(resources = [c])] - fn idle(c: idle::Context) -> ! { + #[idle(local = [c])] + fn idle(ctx: idle::Context) -> ! { loop { - if let Some(byte) = c.resources.c.dequeue() { + if let Some(byte) = ctx.local.c.dequeue() { hprintln!("received message: {}", byte).unwrap(); // cortex_m::asm::wfi(); } else { @@ -47,8 +56,8 @@ const APP: () = { } } - #[task(binds = ADC0, resources = [p])] - fn adc0(c: adc0::Context) { - c.resources.p.enqueue(42).unwrap(); + #[task(binds = ADC0, local = [p])] + fn adc0(ctx: adc0::Context) { + ctx.local.p.enqueue(42).unwrap(); } -}; +} diff --git a/examples/prince.rs b/examples/prince.rs index b79a50c..5e15452 100644 --- a/examples/prince.rs +++ b/examples/prince.rs @@ -39,15 +39,15 @@ fn main() -> ! { let flash = hal.flash.enabled(&mut syscon); let mut flash = hal::FlashGordon::new(flash); - let mut rng = hal.rng.enabled(&mut syscon); + let rng = hal.rng.enabled(&mut syscon); - let mut prince = hal.prince.enabled(&mut rng); + let mut prince = hal.prince.enabled(&rng); prince.enable_all_region_2(); hprintln!("writing AA's to flash data.").ok(); - flash.erase_page((DATA_ADDR / 512)).unwrap(); + flash.erase_page(DATA_ADDR / 512).unwrap(); flash.erase_page((DATA_ADDR / 512) + 1).unwrap(); prince.write_encrypted(|_prince| { @@ -58,6 +58,7 @@ fn main() -> ! { hprintln!("Read bytes PRINCE ON:").ok(); let mut buf = [0u8; 1024]; + #[allow(clippy::needless_range_loop)] for i in 0..buf.len() { let ptr = DATA_ADDR as *const u8; buf[i] = unsafe { *ptr.add(i) }; @@ -68,6 +69,7 @@ fn main() -> ! { // Turn off PRINCE. prince.disable_all_region_2(); + #[allow(clippy::needless_range_loop)] for i in 0..buf.len() { let ptr = DATA_ADDR as *const u8; buf[i] = unsafe { *ptr.add(i) }; diff --git a/examples/puf.rs b/examples/puf.rs index 610a708..c965724 100644 --- a/examples/puf.rs +++ b/examples/puf.rs @@ -94,7 +94,7 @@ fn main() -> ! { // write first 512-byte chunk write_buf[0..4].copy_from_slice(&(State::Enrolled as u32).to_ne_bytes()); write_buf[16..].copy_from_slice(&ac[..496]); - flash.write(PUF_STATE_FLASH + 0, &write_buf).unwrap(); + flash.write(PUF_STATE_FLASH, &write_buf).unwrap(); // // write 2nd chunk write_buf.copy_from_slice(&ac[496..1008]); @@ -117,10 +117,10 @@ fn main() -> ! { } for i in 0..kc1.len() { - assert!(kc1[i] == check_buf[1192 + 52 * 0 + i]) + assert!(kc1[i] == check_buf[1192 + i]) } for i in 0..kc2.len() { - assert!(kc2[i] == check_buf[1192 + 52 * 1 + i]) + assert!(kc2[i] == check_buf[1192 + 52 + i]) } for i in 0..kc3.len() { assert!(kc3[i] == check_buf[1192 + 52 * 2 + i]) @@ -133,23 +133,12 @@ fn main() -> ! { } else { dbg!("The device is already enrolled."); flash.read(PUF_STATE_FLASH + 16, &mut check_buf); - for i in 0..1192 { - ac[i] = check_buf[i]; - } - - for i in 0..kc1.len() { - kc1[i] = check_buf[1192 + 52 * 0 + i]; - } - for i in 0..kc2.len() { - kc2[i] = check_buf[1192 + 52 * 1 + i]; - } - for i in 0..kc3.len() { - kc3[i] = check_buf[1192 + 52 * 2 + i]; - } - for i in 0..kc4.len() { - kc4[i] = check_buf[1192 + 52 * 3 + i]; - } + ac.copy_from_slice(&check_buf[..1192]); + kc1.copy_from_slice(&check_buf[1192..][..52]); + kc2.copy_from_slice(&check_buf[1192 + 52..][..52]); + kc3.copy_from_slice(&check_buf[1192 + 52 * 2..][..52]); + kc4.copy_from_slice(&check_buf[1192 + 52 * 3..][..52]); dump_hex!(ac[..16], 16); dump_hex!(ac[1192 - 16..], 16); @@ -203,5 +192,7 @@ fn main() -> ! { } dbg!("Looping"); - loop {} + loop { + dbg!("Loop"); + } } diff --git a/examples/pwm.rs b/examples/pwm.rs index 533e05c..9465bed 100644 --- a/examples/pwm.rs +++ b/examples/pwm.rs @@ -7,6 +7,8 @@ extern crate panic_semihosting; // 4004 bytes #[macro_use(block)] extern crate nb; +use core::f32; + use cortex_m_rt::entry; use hal::drivers::{Pins, Pwm, Timer}; @@ -95,8 +97,9 @@ fn main() -> ! { } } + #[allow(clippy::needless_range_loop)] for i in 0..3 { - let duty = (sin(duties[i] * 3.141_592_7_f32 / 180f32) * 255f32) as u16; + let duty = (sin(duties[i] * f32::consts::PI / 180f32) * 255f32) as u16; match i { 0 => { // need to tune down red some diff --git a/examples/rtic_led.rs b/examples/rtic_led.rs index 5fac54b..10e2c13 100644 --- a/examples/rtic_led.rs +++ b/examples/rtic_led.rs @@ -3,23 +3,27 @@ extern crate panic_semihosting; // extern crate panic_halt; -use cortex_m_semihosting::dbg; -use hal::{drivers::pins, drivers::pins::Level, prelude::*, typestates::pin}; -use lpc55_hal as hal; +#[rtic::app(device = lpc55_hal::raw, peripherals = true)] +mod app { + use cortex_m_semihosting::dbg; -type RedLed = hal::Pin>; + use hal::{drivers::pins, drivers::pins::Level, prelude::*, typestates::pin}; + use lpc55_hal as hal; -#[rtic::app(device = crate::hal::raw, peripherals = true)] -const APP: () = { - struct Resources { + type RedLed = hal::Pin>; + #[shared] + struct SharedResources {} + + #[local] + struct LocalResources { led: RedLed, // delay: hal::clock::Ticks<'static, hal::syscon::Fro1MhzUtickClock>, // sleep: hal::sleep::Busy<'static, 'static>, } #[init] - fn init(c: init::Context) -> init::LateResources { + fn init(c: init::Context) -> (SharedResources, LocalResources, init::Monotonics) { // dbg!("init"); let _cp = c.core; let dp = c.device; @@ -48,18 +52,21 @@ const APP: () = { // let mut utick = hal::utick::wrap(dp.UTICK).enabled(&mut syscon, &clock); // // let mut sleep = hal::sleep::Busy::prepare(&mut utick); // let mut sleep = hal::sleep::Busy::wrap(&mut utick); - - init::LateResources { - led: red_led, - // delay, - // sleep, - } + ( + SharedResources {}, + LocalResources { + led: red_led, + // delay, + // sleep, + }, + init::Monotonics(), + ) } // #[idle(resources = [led, delay, sleep])] - #[idle(resources = [led])] - fn idle(c: idle::Context) -> ! { - let led = c.resources.led; + #[idle(local = [led])] + fn idle(ctx: idle::Context) -> ! { + let led = ctx.local.led; loop { dbg!("low"); led.set_low().unwrap(); @@ -70,4 +77,4 @@ const APP: () = { // c.resources.sleep.sleep(c.resources.delay); } } -}; +} diff --git a/examples/semihosting.rs b/examples/semihosting.rs index 3f0d227..ecba457 100644 --- a/examples/semihosting.rs +++ b/examples/semihosting.rs @@ -27,6 +27,7 @@ fn main() -> ! { const UUID: *mut u32 = 0x0009_FC70 as *mut u32; // dbg!(UUID); let mut uuid: [u32; 4] = [0; 4]; + #[allow(clippy::needless_range_loop)] for i in 0..4 { uuid[i] = unsafe { dbg!(UUID.add(i).read_volatile()) }; } diff --git a/examples/touch.rs b/examples/touch.rs index 906a7b7..9ec4f7b 100644 --- a/examples/touch.rs +++ b/examples/touch.rs @@ -84,7 +84,7 @@ fn main() -> ! { let mut touch_sensor = touch_sensor.enabled(&mut dma, touch_token); // Used to get tunning information for capacitive touch - if 1 == 1 { + { let mut counts = [0u32; 3]; let mut times = [0u32; 128]; let mut results = [0u32; 128]; diff --git a/examples/usb.rs b/examples/usb.rs index 191d8ea..422b5d0 100644 --- a/examples/usb.rs +++ b/examples/usb.rs @@ -96,39 +96,30 @@ fn main() -> ! { } if !(buf_in_use || need_zlp) { - match cdc_acm.read_packet(&mut buf) { - Ok(count) => { - size = count; - buf_in_use = true; - // dbg!(&buf[..count]); - // if count > 1 { - // dbg!(count); - // } - } - _ => {} + if let Ok(count) = cdc_acm.read_packet(&mut buf) { + size = count; + buf_in_use = true; + // dbg!(&buf[..count]); + // if count > 1 { + // dbg!(count); + // } } } if buf_in_use { red_led.set_low().ok(); // Turn on - match cdc_acm.write_packet(&buf[..size]) { - Ok(count) => { - assert!(count == size); - buf_in_use = false; - need_zlp = size == 8; - } - _ => {} + if let Ok(count) = cdc_acm.write_packet(&buf[..size]) { + assert!(count == size); + buf_in_use = false; + need_zlp = size == 8; } red_led.set_high().ok(); // Turn off } if need_zlp { - match cdc_acm.write_packet(&[]) { - Ok(count) => { - assert!(count == 0); - need_zlp = false; - } - _ => {} + if let Ok(count) = cdc_acm.write_packet(&[]) { + assert!(count == 0); + need_zlp = false; } }