Skip to content

Add 3.3V and 5V output control #72

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

Merged
merged 12 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
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 ?
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ enum Commands {
},
/// List probes
List {},
/// Enable 3.3V output
Enable3v3 {},
/// Disable 3.3V output
Disable3v3 {},
/// Enable 5V output
Enable5v {},
/// Disable 5V output
Disable5v {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better make them grouped into a SetPower(commands::SetPower) sub-command.

Copy link
Contributor Author

@21km43 21km43 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for review.
I grouped them into sub-command.
Detail: 35b3713

/// SDI virtual serial port,
#[command(subcommand)]
SdiPrint(SdiPrint),
Expand Down Expand Up @@ -204,6 +212,18 @@ fn main() -> Result<()> {
Some(Commands::List {}) => {
WchLink::list_probes()?;
}
Some(Commands::Enable3v3 {}) => {
WchLink::set_3v3_output_enabled(device_index, true)?;
}
Some(Commands::Disable3v3 {}) => {
WchLink::set_3v3_output_enabled(device_index, false)?;
}
Some(Commands::Enable5v {}) => {
WchLink::set_5v_output_enabled(device_index, true)?;
}
Some(Commands::Disable5v {}) => {
WchLink::set_5v_output_enabled(device_index, false)?;
}

Some(Commands::Erase { method }) if method != EraseMode::Default => {
// Special handling for non-default erase: bypass attach chip
Expand Down
40 changes: 40 additions & 0 deletions src/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,46 @@ impl WchLink {
Ok(())
}

pub fn set_3v3_output_enabled(nth: usize, enable: bool) -> 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(),
));
}

if enable {
log::info!("Enable 3.3V Output");
probe.send_command(commands::control::SetPower::Enable3V3)?;
} else {
log::info!("Disable 3.3V Output");
probe.send_command(commands::control::SetPower::Disable3V3)?;
}

Ok(())
}

pub fn set_5v_output_enabled(nth: usize, enable: bool) -> 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(),
));
}

if enable {
log::info!("Enable 5V Output");
probe.send_command(commands::control::SetPower::Enable5V)?;
} else {
log::info!("Disable 5V Output");
probe.send_command(commands::control::SetPower::Disable5V)?;
}

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