diff --git a/CHANGELOG.md b/CHANGELOG.md index fcd3c07..355729b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add CH32X033 support +- Add CH585 support +- New `enable-debug` subcommand, also added to chip metadata + ## [0.2.2] - 2023-10-03 ### Added diff --git a/src/device.rs b/src/device.rs index 5f18f0a..f33c369 100644 --- a/src/device.rs +++ b/src/device.rs @@ -93,8 +93,9 @@ pub struct ConfigRegister { pub offset: usize, pub name: String, #[serde(default)] - description: String, + pub description: String, pub reset: Option, + pub enable_debug: Option, #[serde(default)] pub explaination: BTreeMap, #[serde(default)] diff --git a/src/flashing.rs b/src/flashing.rs index 9cd8448..e572014 100644 --- a/src/flashing.rs +++ b/src/flashing.rs @@ -309,6 +309,40 @@ impl<'a> Flashing<'a> { Ok(()) } + pub fn enable_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(enable_debug) = reg_desc.enable_debug { + raw.pwrite_with(enable_debug, reg_desc.offset, scroll::LE)?; + } + } + + log::info!( + "Reset config registers to debug enabled: {}", + 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> { const CHUNK: usize = 0x3a; diff --git a/src/main.rs b/src/main.rs index eb4074f..22717f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,8 @@ enum ConfigCommands { Info {}, /// Reset config register to default Reset {}, + /// Enable SWD mode(simulation mode) + EnableDebug {}, /// Set config register to new value Set { /// New value of the config register @@ -318,6 +320,10 @@ fn main() -> Result<()> { "Config register restored to default value(non-protected, debug-enabled)" ); } + Some(ConfigCommands::EnableDebug {}) => { + flashing.enable_debug()?; + log::info!("Debug mode enabled"); + } Some(ConfigCommands::Set { value }) => { // flashing.write_config(value)?; log::info!("setting cfg value {}", value);