Browse Source

chore: refine command debug output

pull/32/head
Andelf 1 year ago
parent
commit
c4c9b20c73
  1. 48
      src/commands.rs

48
src/commands.rs

@ -77,7 +77,6 @@ impl Response for u8 {
} }
/// Generic raw command /// Generic raw command
#[derive(Debug)]
pub struct RawCommand<const N: u8>(pub Vec<u8>); pub struct RawCommand<const N: u8>(pub Vec<u8>);
impl<const N: u8> Command for RawCommand<N> { impl<const N: u8> Command for RawCommand<N> {
type Response = Vec<u8>; type Response = Vec<u8>;
@ -86,6 +85,11 @@ impl<const N: u8> Command for RawCommand<N> {
self.0.clone() self.0.clone()
} }
} }
impl<const N: u8> fmt::Debug for RawCommand<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RawCommand<0x{:02x}>({})", N, hex::encode(&self.0))
}
}
/// Set address and offset of the firmware, 0x01. /// Set address and offset of the firmware, 0x01.
#[derive(Debug)] #[derive(Debug)]
@ -170,7 +174,7 @@ pub enum ConfigChip {
ProtectEx(u8), // with 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, ProtectEx(u8), // with 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
/// Config flags /// Config flags
/// 81 06 08 02 3f 00 00 ff ff ff ff /// 81 06 08 02 3f 00 00 ff ff ff ff
/// __ __ __ __ __ [DATA] [WRP ] /// __ __ __ ?? ?? [DATA] [WRP ]
Config { Config {
/// User data /// User data
data: u16, data: u16,
@ -197,22 +201,6 @@ impl Command for ConfigChip {
} }
} }
#[derive(Debug)]
pub struct SetFlashProtected {
pub protected: bool,
}
impl Command for SetFlashProtected {
type Response = u8;
const COMMAND_ID: u8 = 0x06;
fn payload(&self) -> Vec<u8> {
if self.protected {
vec![0x03]
} else {
vec![0x02]
}
}
}
/// Get Chip UID, the UID is also avaliable in the `wchisp` command. /// Get Chip UID, the UID is also avaliable in the `wchisp` command.
// ??? 0x11, 0x01, _ (riscvchip) // ??? 0x11, 0x01, _ (riscvchip)
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
@ -299,11 +287,11 @@ impl Command for Reset {
/// Speed settings /// Speed settings
#[derive(Debug, Copy, Clone, clap::ValueEnum, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Debug, Copy, Clone, clap::ValueEnum, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Speed { pub enum Speed {
/// 400 /// 400kHz
Low = 0x03, Low = 0x03,
/// 4000 /// 4000kHz
Medium = 0x02, Medium = 0x02,
/// 6000 /// 6000kHz
High = 0x01, High = 0x01,
} }
impl Default for Speed { impl Default for Speed {
@ -337,19 +325,19 @@ impl Response for bool {
/// DMI operations /// DMI operations
#[derive(Debug)] #[derive(Debug)]
pub enum DmiOp { pub enum DmiOp {
Nop, DmiNop,
Read { addr: u8 }, DmiRead { addr: u8 },
Write { addr: u8, data: u32 }, DmiWrite { addr: u8, data: u32 },
} }
impl DmiOp { impl DmiOp {
pub fn nop() -> Self { pub fn nop() -> Self {
Self::Nop Self::DmiNop
} }
pub fn read(addr: u8) -> Self { pub fn read(addr: u8) -> Self {
Self::Read { addr } Self::DmiRead { addr }
} }
pub fn write(addr: u8, data: u32) -> Self { pub fn write(addr: u8, data: u32) -> Self {
Self::Write { addr, data } Self::DmiWrite { addr, data }
} }
} }
impl Command for DmiOp { impl Command for DmiOp {
@ -361,14 +349,14 @@ impl Command for DmiOp {
const DMI_OP_WRITE: u8 = 2; const DMI_OP_WRITE: u8 = 2;
let mut bytes = vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let mut bytes = vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
match self { match self {
DmiOp::Nop => { DmiOp::DmiNop => {
bytes[5] = DMI_OP_NOP; // :) bytes[5] = DMI_OP_NOP; // :)
} }
DmiOp::Read { addr } => { DmiOp::DmiRead { addr } => {
bytes[0] = *addr; bytes[0] = *addr;
bytes[5] = DMI_OP_READ; bytes[5] = DMI_OP_READ;
} }
DmiOp::Write { addr, data } => { DmiOp::DmiWrite { addr, data } => {
bytes[0] = *addr; bytes[0] = *addr;
bytes[5] = DMI_OP_WRITE; bytes[5] = DMI_OP_WRITE;
bytes[1..5].copy_from_slice(&data.to_be_bytes()); bytes[1..5].copy_from_slice(&data.to_be_bytes());

Loading…
Cancel
Save