Skip to content

Commit

Permalink
feat: impl enable-debug subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Nov 20, 2024
1 parent c7f3fc6 commit d42f04d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ pub struct ConfigRegister {
pub offset: usize,
pub name: String,
#[serde(default)]
description: String,
pub description: String,
pub reset: Option<u32>,
pub enable_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 @@ -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<Vec<u8>> {
const CHUNK: usize = 0x3a;
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d42f04d

Please sign in to comment.