Skip to content
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

Add 3.3V and 5V output control #72

Merged
merged 12 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 13 additions & 9 deletions src/commands/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
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],
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down