diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e47950..6c78623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `--speed` option to specify protocol speed - Add `erase --method power-off` option to support erase with power off +- Add `erase --method pin-rst` option to support erase with RST pin, close #26 - Add a simple chip ID db, now wlink can identify chip type automatically ### Fixed diff --git a/src/commands/control.rs b/src/commands/control.rs index 947afdf..3437a5c 100644 --- a/src/commands/control.rs +++ b/src/commands/control.rs @@ -105,20 +105,21 @@ impl fmt::Display for AttachChipResponse { /// Erase code flash, only supported by WCH-LinkE. #[derive(Debug)] pub enum EraseCodeFlash { - ByPinRST, - ByPowerOff, + ByPinRST(RiscvChip), + ByPowerOff(RiscvChip), } impl Command for EraseCodeFlash { type Response = (); const COMMAND_ID: u8 = 0x0d; + fn payload(&self) -> Vec { match self { - // TODO: This is more complex, require RST pin to be connected. - EraseCodeFlash::ByPinRST => { - // vec![0x08, 0x06] - todo!("ByPinRST, This is more complex, require RST pin to be connected") - } - EraseCodeFlash::ByPowerOff => vec![0x0f, 0x06], + // This is more complex, require RST pin to be connected. + EraseCodeFlash::ByPinRST(c) => vec![0x08, *c as u8], + // NOTE: From the protocol, this command's bytes is wrongly seted + // 81 0d 01 0f 09, note here, the "length" bytes is wrong. + // I guess it is not verified. So here we use `02`. + EraseCodeFlash::ByPowerOff(c) => vec![0x0f, *c as u8], } } } diff --git a/src/lib.rs b/src/lib.rs index 793b9cc..1489da5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -152,11 +152,7 @@ impl RiscvChip { pub fn support_special_erase(&self) -> bool { !matches!( self, - RiscvChip::CH57X - | RiscvChip::CH56X - | RiscvChip::CH58X - | RiscvChip::CH59X - | RiscvChip::CH32V003 + RiscvChip::CH57X | RiscvChip::CH56X | RiscvChip::CH58X | RiscvChip::CH59X ) } diff --git a/src/main.rs b/src/main.rs index 7aa60c5..d023e59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -215,12 +215,14 @@ fn main() -> Result<()> { probe.erase_flash()?; } EraseMode::PinRst => { - unimplemented!("Erase by RST pin"); + log::warn!("Code flash erase by RST pin requires a RST pin connection"); + probe.erase_flash_by_rst_pin()?; } EraseMode::PowerOff => { probe.erase_flash_by_power_off()?; } } + log::info!("Erase done"); } Flash { address, diff --git a/src/operations.rs b/src/operations.rs index 856171b..9ec8fba 100644 --- a/src/operations.rs +++ b/src/operations.rs @@ -242,15 +242,11 @@ impl WchLink { /// Clear All Code Flash - By Power off pub fn erase_flash_by_power_off(&mut self) -> Result<()> { + let chip_family = self.chip.as_ref().unwrap().chip_family; if self.probe.as_ref().unwrap().variant.support_power_funcs() - && self - .chip - .as_ref() - .unwrap() - .chip_family - .support_special_erase() + && chip_family.support_special_erase() { - self.send_command(control::EraseCodeFlash::ByPowerOff)?; + self.send_command(control::EraseCodeFlash::ByPowerOff(chip_family))?; Ok(()) } else { Err(Error::Custom(format!( @@ -259,6 +255,21 @@ impl WchLink { } } + /// Clear All Code Flash - By RST pin + pub fn erase_flash_by_rst_pin(&mut self) -> Result<()> { + let chip_family = self.chip.as_ref().unwrap().chip_family; + if self.probe.as_ref().unwrap().variant.support_power_funcs() + && chip_family.support_special_erase() + { + self.send_command(control::EraseCodeFlash::ByPinRST(chip_family))?; + Ok(()) + } else { + Err(Error::Custom(format!( + "Probe or Chip doesn't support RST pin erase", + ))) + } + } + /// Erases flash and re-attach pub fn erase_flash(&mut self) -> Result<()> { if self