Skip to content

Commit

Permalink
Fix examples clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jan 21, 2025
1 parent 5022441 commit a338016
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 91 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ fn main() -> ! {
}

heprintln!("looping").unwrap();
loop {}
loop {
heprintln!("looping").unwrap();
}
}
6 changes: 3 additions & 3 deletions examples/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
47 changes: 28 additions & 19 deletions examples/late.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! examples/late.rs
#![deny(unsafe_code)]
// something something about:
//
// error: use of deprecated item
Expand All @@ -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<u32, 4> = 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 {
Expand All @@ -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();
}
};
}
8 changes: 5 additions & 3 deletions examples/prince.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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) };
Expand All @@ -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) };
Expand Down
31 changes: 11 additions & 20 deletions examples/puf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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])
Expand All @@ -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);

Expand Down Expand Up @@ -203,5 +192,7 @@ fn main() -> ! {
}

dbg!("Looping");
loop {}
loop {
dbg!("Loop");
}
}
5 changes: 4 additions & 1 deletion examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down
43 changes: 25 additions & 18 deletions examples/rtic_led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pins::Pio1_6, pin::state::Gpio<pin::gpio::direction::Output>>;
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<pins::Pio1_6, pin::state::Gpio<pin::gpio::direction::Output>>;
#[shared]
struct SharedResources {}

#[local]
struct LocalResources {
led: RedLed,
// delay: hal::clock::Ticks<'static, hal::syscon::Fro1MhzUtickClock<states::init_state::Enabled>>,
// 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;
Expand Down Expand Up @@ -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();
Expand All @@ -70,4 +77,4 @@ const APP: () = {
// c.resources.sleep.sleep(c.resources.delay);
}
}
};
}
1 change: 1 addition & 0 deletions examples/semihosting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) };
}
Expand Down
2 changes: 1 addition & 1 deletion examples/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
37 changes: 14 additions & 23 deletions examples/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit a338016

Please sign in to comment.