-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defmt log / Unknown cmd #7
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
use crate::transport::Transport; | ||
use crate::CLASS_MASS_STORAGE; | ||
use core::fmt::{Debug, Formatter}; | ||
use num_enum::TryFromPrimitive; | ||
use usb_device::bus::InterfaceNumber; | ||
use usb_device::bus::UsbBus; | ||
|
@@ -40,14 +41,34 @@ const WRITE_10: u8 = 0x2A; | |
/* MMC */ | ||
const READ_FORMAT_CAPACITIES: u8 = 0x23; | ||
|
||
/// A u8, when printed shown as hex | ||
#[repr(transparent)] | ||
#[derive(Copy, Clone)] | ||
pub struct U8Hex(pub u8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of printing in hex, however I would expand it to all I'm not sure that As a last resort, I would still try to implement newtypes for other primitives and see how much of a code change it requires. (I don't mind changing a public API. that's what semver is for :) ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think propagation does help, as entries with display hint (= show hex) can't be overwritten. Also only a struct (or so) can implement Format, but since the command is a u8 it can't be formatted without newtype. Yes, I should add =u8, and probably use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried #[derive(defmt::Format)]
enum FakeCommand {
Unknown {
u16: u16,
u32: u32,
u64: u64,
u128: u128,
}
}
#[cortex_m_rt::entry]
fn main() -> ! {
let foo = FakeCommand::Unknown{
u16: 0xCAFE,
u32: 0xCAFE,
u64: 0xCAFE,
u128: 0xCAFE,
};
defmt::info!("{:#X}", foo);
} and it printed So if Note, that this does not currently work with |
||
|
||
impl Debug for U8Hex { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
write!(f, "0x{:02x}", self.0) | ||
} | ||
} | ||
|
||
#[cfg(feature = "defmt")] | ||
impl defmt::Format for U8Hex { | ||
fn format(&self, fmt: defmt::Formatter) { | ||
defmt::write!(fmt, "0x{:02x}", self.0) | ||
} | ||
} | ||
|
||
/// SCSI command | ||
/// | ||
/// Refer to specifications (SPC,SAM,SBC,MMC,etc.) | ||
#[derive(Copy, Clone, Debug)] | ||
#[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
#[non_exhaustive] | ||
pub enum ScsiCommand { | ||
Unknown, | ||
Unknown { | ||
cmd: U8Hex, | ||
}, | ||
|
||
/* SPC */ | ||
Inquiry { | ||
|
@@ -151,7 +172,7 @@ fn parse_cb(cb: &[u8]) -> ScsiCommand { | |
READ_FORMAT_CAPACITIES => ScsiCommand::ReadFormatCapacities { | ||
alloc_len: u16::from_be_bytes([cb[7], cb[8]]), | ||
}, | ||
_ => ScsiCommand::Unknown, | ||
cmd => ScsiCommand::Unknown { cmd: U8Hex(cmd) }, | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was to rely on
defmt
's filtering mechanism: https://defmt.ferrous-systems.com/filtering#disabling-logsSo one would enable a
defmt
feature on a crate and then compile a binary withDEFMT_LOG=trace,usbd-storage=off cargo run --bin app
Do you have a different use-case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very new to defmt, if that is the usual way, I'll use it no problem