Skip to content
Open
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
21 changes: 16 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ object = { version = "0.36.0", default-features = false, features = [
] }
indicatif = "0.17"
serialport = { version = "4.5", default-features = false }
libloading = "0.8.1"
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ Click the newest runs at [Github Actions Page](https://github.com/ch32-rs/wchisp

### Note for Windows

If you are using Windows, you need to install the WinUSB driver for your device.
See [Zadig](https://zadig.akeo.ie/).
If you are using Windows, you may need to install the CH375DLL64.dll if you do not have it in your system. If you encounter a "CH375DLL64.dll not found" error, please download it from the WCH official website and put the dll next to this executable. You may download it from https://www.wch-ic.com/downloads/CH372DRV_ZIP.html, or search for 'CH375' on WCH websites if the link is broken. When you use the official WCH driver you installed with IDE or WCHISPTOOL, CH375DLL64.dll allows you to program the target chips without changing the driver.

NOTE: This is not compatible with the Official WCH driver you installed with IDE.
It is also OK to use the WinUSB driver with [Zadig](https://zadig.akeo.ie/).

### Note for Linux

Expand Down
5 changes: 3 additions & 2 deletions devices/0x13-CH57x.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ config_registers_ch571_ch573: &config_registers_ch571_ch573
name: USER_CFG
description: User config register
# reset: 0x4FFF0F4D
# CFG_DEBUG_EN=1、CFG_RESET_EN=0、CFG_ROM_READ=0
# CFG_DEBUG_EN=1、CFG_RESET_EN=0、CFG_ROM_READ=1
# enable 2-wire debug
reset: 0x4FFF0F55
reset: 0x4FFF0FD5
disable_debug: 0x4FFF0F45
type: u32
fields:
- bit_range: [2, 0]
Expand Down
1 change: 1 addition & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct ConfigRegister {
pub description: String,
pub reset: Option<u32>,
pub enable_debug: Option<u32>,
pub disable_debug: Option<u32>,
#[serde(default)]
pub explaination: BTreeMap<String, String>,
#[serde(default)]
Expand Down
34 changes: 34 additions & 0 deletions src/flashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,40 @@ impl<'a> Flashing<'a> {
Ok(())
}

pub fn disable_debug(&mut self) -> Result<()> {
let read_conf = Command::read_config(CFG_MASK_RDPR_USER_DATA_WPR);
let resp = self.transport.transfer(read_conf)?;
anyhow::ensure!(resp.is_ok(), "read_config failed");

let mut raw = resp.payload()[2..].to_vec();

log::info!("Current config registers: {}", hex::encode(&raw));

for reg_desc in &self.chip.config_registers {
if let Some(reset) = reg_desc.reset {
raw.pwrite_with(reset, reg_desc.offset, scroll::LE)?;
}
if let Some(disable_debug) = reg_desc.disable_debug {
raw.pwrite_with(disable_debug, reg_desc.offset, scroll::LE)?;
}
}

log::info!(
"Reset config registers to debug disabled: {}",
hex::encode(&raw)
);
let write_conf = Command::write_config(CFG_MASK_RDPR_USER_DATA_WPR, raw);
let resp = self.transport.transfer(write_conf)?;
anyhow::ensure!(resp.is_ok(), "write_config failed");

// read back
let read_conf = Command::read_config(CFG_MASK_RDPR_USER_DATA_WPR);
let resp = self.transport.transfer(read_conf)?;
anyhow::ensure!(resp.is_ok(), "read_config failed");

Ok(())
}

/// Dump EEPROM, i.e. data flash.
pub fn dump_eeprom(&mut self) -> Result<Vec<u8>> {
const CHUNK: usize = 0x3a;
Expand Down
29 changes: 29 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ struct Cli {
#[arg(long, short, ignore_case = true, value_enum, requires = "serial")]
baudrate: Option<Baudrate>,

/// Retry scan for certain seconds, helpful on slow USB devices
#[arg(long, short, default_value = "0")]
retry: u32,

#[command(subcommand)]
command: Option<Commands>,
}
Expand Down Expand Up @@ -93,6 +97,8 @@ enum ConfigCommands {
Reset {},
/// Enable SWD mode(simulation mode)
EnableDebug {},
/// Disable SWD mode(simulation mode)
DisableDebug {},
/// Set config register to new value
Set {
/// New value of the config register
Expand Down Expand Up @@ -141,6 +147,25 @@ fn main() -> Result<()> {
);
}

if cli.retry > 0 {
log::info!("Retrying scan for {} seconds", cli.retry);
let start_time = std::time::Instant::now();
while start_time.elapsed().as_secs() < cli.retry as u64 {
if cli.usb {
let ndevices = UsbTransport::scan_devices()?;
if ndevices > 0 {
break;
}
} else if cli.serial {
let ports = SerialTransport::scan_ports()?;
if !ports.is_empty() {
break;
}
}
sleep(Duration::from_millis(100));
}
}

match &cli.command {
None | Some(Commands::Probe {}) => {
if cli.usb {
Expand Down Expand Up @@ -324,6 +349,10 @@ fn main() -> Result<()> {
flashing.enable_debug()?;
log::info!("Debug mode enabled");
}
Some(ConfigCommands::DisableDebug {}) => {
flashing.disable_debug()?;
log::info!("Debug mode disabled");
}
Some(ConfigCommands::Set { value }) => {
// flashing.write_config(value)?;
log::info!("setting cfg value {}", value);
Expand Down
Loading