From 889d9f6e052398548b612e71889f376b2856afa0 Mon Sep 17 00:00:00 2001 From: Andelf Date: Fri, 24 Feb 2023 15:58:49 +0800 Subject: [PATCH] chore: refine doc comments chore: bump version --- Cargo.toml | 8 ++++---- src/commands.rs | 10 ++++++++-- src/commands/control.rs | 13 ++++++++----- src/lib.rs | 19 +++++++++++-------- src/main.rs | 11 +++++------ src/transport.rs | 9 +++++---- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 032b69a..29c5b03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,14 @@ [package] name = "wlink" -version = "0.0.0" +version = "0.0.1" edition = "2021" authors = ["Andelf "] repository = "https://github.com/ch32-rs/wlink" documentation = "https://docs.rs/wlink" homepage = "https://github.com/ch32-rs/wlink" -categories = [] -description = "WCH-Link command link util by Rust (WIP)" -keywords = ["WCH", "CH32V"] +categories = ["embedded", "hardware-support", "development-tools::debugging"] +description = "WCH-Link tool for CH32V faimily MCUs(WIP)" +keywords = ["embedded", "WCH", "CH32V", "WCH-Link"] readme = "README.md" license = "MIT/Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/commands.rs b/src/commands.rs index 29d23d5..908c982 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,10 +1,12 @@ -// pub mod dmi; +//! WCH-Link commands and response types use std::fmt; use crate::error::{Error, Result}; pub mod control; + +/// Command to call the WCH-Link pub trait Command { type Response: Response; const COMMAND_ID: u8; @@ -17,6 +19,7 @@ pub trait Command { } } +/// Response type of a command call pub trait Response { /// parse the PAYLOAD part only fn from_payload(bytes: &[u8]) -> Result @@ -172,11 +175,14 @@ impl Command for DmiOp { type Response = DmiOpResponse; const COMMAND_ID: u8 = 0x08; fn payload(&self) -> Vec { + const DMI_OP_NOP: u8 = 0; const DMI_OP_READ: u8 = 1; const DMI_OP_WRITE: u8 = 2; let mut bytes = vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; match self { - DmiOp::Nop => (), + DmiOp::Nop => { + bytes[5] = DMI_OP_NOP; // :) + } DmiOp::Read { addr } => { bytes[0] = *addr; bytes[5] = DMI_OP_READ; diff --git a/src/commands/control.rs b/src/commands/control.rs index 7421e8d..80fb030 100644 --- a/src/commands/control.rs +++ b/src/commands/control.rs @@ -1,3 +1,5 @@ +//! Probe control commands. + use std::fmt; use crate::{RiscvChip, WchLinkVariant}; @@ -14,9 +16,9 @@ impl Command for GetProbeInfo { } #[derive(Debug)] pub struct ProbeInfo { - major_version: u8, - minor_version: u8, - link_variant: WchLinkVariant, + pub major_version: u8, + pub minor_version: u8, + pub variant: WchLinkVariant, } impl Response for ProbeInfo { fn from_payload(bytes: &[u8]) -> Result { @@ -26,8 +28,9 @@ impl Response for ProbeInfo { Ok(Self { major_version: bytes[0], minor_version: bytes[1], - link_variant: if bytes.len() == 4 { - WchLinkVariant::from(bytes[2]) + // Only avaliable in newer version of firmware + variant: if bytes.len() == 4 { + WchLinkVariant::try_from_u8(bytes[2])? } else { WchLinkVariant::Ch549 }, diff --git a/src/lib.rs b/src/lib.rs index 7b68177..1544042 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,13 +21,13 @@ pub enum WchLinkVariant { } impl WchLinkVariant { - pub fn from(value: u8) -> Self { + pub fn try_from_u8(value: u8) -> Result { match value { - 1 => Self::Ch549, - 2 => Self::ECh32v305, - 3 => Self::SCh32v203, - 4 => Self::B, - _ => panic!("invalid WCH-Link variant {}", value), + 1 => Ok(Self::Ch549), + 2 => Ok(Self::ECh32v305), + 3 => Ok(Self::SCh32v203), + 4 => Ok(Self::B), + _ => Err(Error::Custom(format!("Unknown WCH-Link variant {}", value))), } } } @@ -43,7 +43,7 @@ impl fmt::Display for WchLinkVariant { } } -/// Currently supported RISC-V chip series +/// Currently supported RISC-V chip series/family #[derive(Clone, Copy, Debug)] #[repr(u8)] pub enum RiscvChip { @@ -73,7 +73,10 @@ impl RiscvChip { 0x06 => Ok(RiscvChip::CH32V30x), 0x07 => Ok(RiscvChip::CH58x), 0x09 => Ok(RiscvChip::CH32V003), - _ => Err(Error::Custom(format!("invalid riscvchip {}", value))), + _ => Err(Error::Custom(format!( + "Unknown riscvchip type 0x{:02x}", + value + ))), } } } diff --git a/src/main.rs b/src/main.rs index 302a776..c1b45a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,17 +9,16 @@ fn main() -> Result<()> { let mut link = WchLink::open_nth(0)?; let resp = link.send_command(commands::control::GetProbeInfo)?; - - println!("=> {:?}", resp); + println!("probe info: {:?}", resp); let r = link.send_command(commands::control::AttachChip)?; - println!("=> {:?}", r); + println!("chip info: {:?}", r); let protected = link.send_command(commands::GetChipProtected)?; println!("protected => {:?}", protected); - let uid = link.send_command(commands::GetChipId); - println!("=> {}", uid); + let uid = link.send_command(commands::GetChipId)?; + println!("UID => {}", uid); // read csr link.send_command(DmiOp::write(0x10, 0x80000001))?; @@ -32,7 +31,7 @@ fn main() -> Result<()> { println!("marchid => {:08x}", marchid.data); let marchid = marchid.data; println!( - "{}{}{}-{}{}{}", + "Parsed marchid: {}{}{}-{}{}{}", (((marchid >> 26) & 0x1F) + 64) as u8 as char, (((marchid >> 21) & 0x1F) + 64) as u8 as char, (((marchid >> 16) & 0x1F) + 64) as u8 as char, diff --git a/src/transport.rs b/src/transport.rs index 6a2d18e..c007d56 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,3 +1,5 @@ +//! USB transport of WCH-Link + use std::time::Duration; use rusb::{DeviceHandle, UsbContext}; @@ -7,7 +9,8 @@ use crate::{ error::{Error, Result}, }; -const USB_TIMEOUT_MS: u64 = 5000; +const VENDOR_ID: u16 = 0x1a86; +const PRODUCT_ID: u16 = 0x8010; const ENDPOINT_OUT: u8 = 0x01; const ENDPOINT_IN: u8 = 0x81; @@ -16,9 +19,7 @@ const RAW_ENDPOINT_OUT: u8 = 0x02; const RAW_ENDPOINT_IN: u8 = 0x82; // 1a86:8010 1a86 WCH-Link Serial: 0001A0000000 - -const VENDOR_ID: u16 = 0x1a86; -const PRODUCT_ID: u16 = 0x8010; +const USB_TIMEOUT_MS: u64 = 5000; #[derive(Debug)] pub struct WchLink {