diff --git a/CHANGELOG.md b/CHANGELOG.md index b0df4be..a1d1b00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Rename ChipUID response to ESignature, #58 +- Add functions to control 3.3V and 5V outputs of probe ## [0.0.8] - 2024-03-30 diff --git a/README.md b/README.md index 4b23aba..4c2fd12 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ - [x] Read chip memory(flash) - [x] Read/write chip register - very handy for debugging - [x] Code-Protect & Code-Unprotect for supported chips +- [x] Enable or Disable 3.3V, 5V output - [x] [SDI print](https://www.cnblogs.com/liaigu/p/17628184.html) support, requires 2.10+ firmware - [x] [Serial port watching](https://github.com/ch32-rs/wlink/pull/36) for a smooth development experience - [x] Windows native driver support, no need to install libusb manually (requires x86 build) diff --git a/src/commands/control.rs b/src/commands/control.rs index 8f90d23..3f71b8a 100644 --- a/src/commands/control.rs +++ b/src/commands/control.rs @@ -160,22 +160,26 @@ impl Command for OptEnd { } /// Set Power, from pow3v3, pow5v fn -#[derive(Debug)] +#[derive(clap::Subcommand, PartialEq, Clone, Copy, Debug)] pub enum SetPower { - Enable3V3, - Disable3V3, - Enable5V, - Disable5V, + /// Enable 3.3V output + Enable3v3, + /// Disable 3.3V output + Disable3v3, + /// Enable 5V output + Enable5v, + /// Disable 5V output + Disable5v, } impl Command for SetPower { type Response = (); const COMMAND_ID: u8 = 0x0d; fn payload(&self) -> Vec { match self { - SetPower::Enable3V3 => vec![0x09], - SetPower::Disable3V3 => vec![0x0A], - SetPower::Enable5V => vec![0x0B], - SetPower::Disable5V => vec![0x0C], + SetPower::Enable3v3 => vec![0x09], + SetPower::Disable3v3 => vec![0x0A], + SetPower::Enable5v => vec![0x0B], + SetPower::Disable5v => vec![0x0C], } } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7e300b0..d894a2b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -425,4 +425,4 @@ impl Command for DisableDebug { // 81 0D 01 0F ClearCodeFlashB // 81 0D 02 08 xx ClearCodeFlash // 81 11 01 0D unknown in query info, before GetChipRomRamSplit -// 81 0d 02 ee 00 stop flash ? +// 81 0D 02 EE 00 stop flash ? diff --git a/src/main.rs b/src/main.rs index a5419c3..b5cdb0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,6 +148,11 @@ enum Commands { }, /// List probes List {}, + /// Enable or disable power output + SetPower { + #[command(subcommand)] + cmd: commands::control::SetPower, + }, /// SDI virtual serial port, #[command(subcommand)] SdiPrint(SdiPrint), @@ -204,6 +209,9 @@ fn main() -> Result<()> { Some(Commands::List {}) => { WchLink::list_probes()?; } + Some(Commands::SetPower { cmd }) => { + WchLink::set_power_output_enabled(device_index, cmd)?; + } Some(Commands::Erase { method }) if method != EraseMode::Default => { // Special handling for non-default erase: bypass attach chip diff --git a/src/probe.rs b/src/probe.rs index 9a37767..06f2043 100644 --- a/src/probe.rs +++ b/src/probe.rs @@ -173,6 +173,27 @@ impl WchLink { Ok(()) } + pub fn set_power_output_enabled(nth: usize, cmd: commands::control::SetPower) -> Result<()> { + let mut probe = Self::open_nth(nth)?; + + if !probe.info.variant.support_power_funcs() { + return Err(Error::Custom( + "Probe doesn't support power control".to_string(), + )); + } + + probe.send_command(cmd)?; + + match cmd { + commands::control::SetPower::Enable3v3 => log::info!("Enable 3.3V Output"), + commands::control::SetPower::Disable3v3 => log::info!("Disable 3.3V Output"), + commands::control::SetPower::Enable5v => log::info!("Enable 5V Output"), + commands::control::SetPower::Disable5v => log::info!("Disable 5V Output"), + } + + Ok(()) + } + fn write_raw_cmd(&mut self, buf: &[u8]) -> Result<()> { log::trace!("send {} {}", hex::encode(&buf[..3]), hex::encode(&buf[3..])); self.device.write_endpoint(ENDPOINT_OUT, buf)?;