Skip to content

Commit

Permalink
Clippy fixes and clean ups.
Browse files Browse the repository at this point in the history
The usb_device is only used in the ISR, so it can be a local. Saves us a lock.
  • Loading branch information
jonathanpallant committed Dec 12, 2024
1 parent f27c694 commit 7e0a25e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
37 changes: 18 additions & 19 deletions nrf52-code/loopback-fw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ mod app {
msg_queue_out: heapless::spsc::Consumer<'static, Message, QUEUE_LEN>,
/// A place to write to the message queue
msg_queue_in: heapless::spsc::Producer<'static, Message, QUEUE_LEN>,
/// A status LED
led: dongle::Led,
/// The status LEDs
leds: dongle::Leds,
/// Handles the lower-level USB Device interface
usb_device: usb_device::device::UsbDevice<'static, dongle::UsbBus>,
}

#[derive(Debug, defmt::Format, Copy, Clone, PartialEq, Eq)]
Expand All @@ -67,8 +69,6 @@ mod app {
usb_serial: usbd_serial::SerialPort<'static, dongle::UsbBus>,
/// Handles the USB HID interface
usb_hid: usbd_hid::hid_class::HIDClass<'static, dongle::UsbBus>,
/// Handles the lower-level USB Device interface
usb_device: usb_device::device::UsbDevice<'static, dongle::UsbBus>,
}

#[init(local = [
Expand Down Expand Up @@ -154,7 +154,6 @@ mod app {
let shared = MySharedResources {
usb_serial,
usb_hid,
usb_device,
};
let local = MyLocalResources {
radio: board.radio,
Expand All @@ -165,15 +164,16 @@ mod app {
err_count: 0,
msg_queue_out,
msg_queue_in,
led: board.leds.ld1,
leds: board.leds,
usb_device,
};

defmt::debug!("Init Complete!");

(shared, local)
}

#[idle(local = [radio, current_channel, packet, timer, rx_count, err_count, msg_queue_out, led], shared = [usb_serial])]
#[idle(local = [radio, current_channel, packet, timer, rx_count, err_count, msg_queue_out, leds], shared = [usb_serial])]
fn idle(mut ctx: idle::Context) -> ! {
use core::fmt::Write as _;
let mut writer = Writer(|b: &[u8]| {
Expand All @@ -189,6 +189,9 @@ mod app {
ctx.local.current_channel
);

ctx.local.leds.ld1.on();
ctx.local.leds.ld2_blue.on();

loop {
while let Some(msg) = ctx.local.msg_queue_out.dequeue() {
match msg {
Expand Down Expand Up @@ -299,12 +302,12 @@ mod app {

defmt::debug!("Waiting for packet..");
match ctx.local.radio.recv_timeout(
&mut ctx.local.packet,
&mut ctx.local.timer,
ctx.local.packet,
ctx.local.timer,
1_000_000,
) {
Ok(crc) => {
ctx.local.led.toggle();
ctx.local.leds.ld1.toggle();
defmt::info!(
"Received {=u8} bytes (CRC=0x{=u16:04x}, LQI={})",
ctx.local.packet.len(),
Expand Down Expand Up @@ -344,15 +347,11 @@ mod app {
///
/// USB Device is set to fire this whenever there's a Start of Frame from
/// the USB Host.
#[task(binds = USBD, local = [msg_queue_in], shared = [usb_serial, usb_hid, usb_device])]
fn usb_isr(mut ctx: usb_isr::Context) {
let mut all = (
&mut ctx.shared.usb_serial,
&mut ctx.shared.usb_hid,
&mut ctx.shared.usb_device,
);
all.lock(|usb_serial, usb_hid, usb_device| {
if usb_device.poll(&mut [usb_serial, usb_hid]) {
#[task(binds = USBD, local = [msg_queue_in, usb_device], shared = [usb_serial, usb_hid])]
fn usb_isr(ctx: usb_isr::Context) {
let mut all = (ctx.shared.usb_serial, ctx.shared.usb_hid);
all.lock(|usb_serial, usb_hid| {
if ctx.local.usb_device.poll(&mut [usb_serial, usb_hid]) {
let mut buffer = [0u8; 64];
if let Ok(n) = usb_serial.read(&mut buffer) {
if n > 0 {
Expand Down
22 changes: 9 additions & 13 deletions nrf52-code/puzzle-fw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ mod app {
msg_queue_in: heapless::spsc::Producer<'static, Message, QUEUE_LEN>,
/// The status LEDs
leds: dongle::Leds,
/// Handles the lower-level USB Device interface
usb_device: usb_device::device::UsbDevice<'static, dongle::UsbBus>,
}

#[derive(Debug, defmt::Format, Copy, Clone, PartialEq, Eq)]
Expand All @@ -82,8 +84,6 @@ mod app {
usb_serial: usbd_serial::SerialPort<'static, dongle::UsbBus>,
/// Handles the USB HID interface
usb_hid: usbd_hid::hid_class::HIDClass<'static, dongle::UsbBus>,
/// Handles the lower-level USB Device interface
usb_device: usb_device::device::UsbDevice<'static, dongle::UsbBus>,
}

#[init(local = [
Expand Down Expand Up @@ -169,7 +169,6 @@ mod app {
let shared = MySharedResources {
usb_serial,
usb_hid,
usb_device,
};
let local = MyLocalResources {
radio: board.radio,
Expand All @@ -181,6 +180,7 @@ mod app {
msg_queue_out,
msg_queue_in,
leds: board.leds,
usb_device,
};

defmt::debug!("Init Complete!");
Expand Down Expand Up @@ -427,15 +427,11 @@ mod app {
///
/// USB Device is set to fire this whenever there's a Start of Frame from
/// the USB Host.
#[task(binds = USBD, local = [msg_queue_in], shared = [usb_serial, usb_hid, usb_device])]
fn usb_isr(mut ctx: usb_isr::Context) {
let mut all = (
&mut ctx.shared.usb_serial,
&mut ctx.shared.usb_hid,
&mut ctx.shared.usb_device,
);
all.lock(|usb_serial, usb_hid, usb_device| {
if usb_device.poll(&mut [usb_serial, usb_hid]) {
#[task(binds = USBD, local = [msg_queue_in, usb_device], shared = [usb_serial, usb_hid])]
fn usb_isr(ctx: usb_isr::Context) {
let mut all = (ctx.shared.usb_serial, ctx.shared.usb_hid);
all.lock(|usb_serial, usb_hid| {
if ctx.local.usb_device.poll(&mut [usb_serial, usb_hid]) {
let mut buffer = [0u8; 64];
if let Ok(n) = usb_serial.read(&mut buffer) {
if n > 0 {
Expand Down Expand Up @@ -472,7 +468,7 @@ mod app {
dict: &heapless::LinearMap<u8, u8, 128>,
) -> Option<Command> {
let payload = packet.get_mut(ADDR_BYTES..)?;
if payload.len() == 0 {
if payload.is_empty() {
Some(Command::SendSecret)
} else if payload.len() == 1 {
// They give us plaintext, we give them ciphertext
Expand Down

0 comments on commit 7e0a25e

Please sign in to comment.