Skip to content

Commit

Permalink
feat: erase by RST pin
Browse files Browse the repository at this point in the history
Close #26
  • Loading branch information
andelf committed Oct 3, 2023
1 parent 6d31014 commit 0eb7b4c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions src/commands/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
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],
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 18 additions & 7 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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
Expand Down

0 comments on commit 0eb7b4c

Please sign in to comment.