Browse Source

fix: add reset after flash

pull/4/head
Andelf 2 years ago
parent
commit
df835bbcf2
  1. 10
      README.md
  2. 10
      src/commands/control.rs
  3. 16
      src/main.rs
  4. 10
      src/operations.rs

10
README.md

@ -18,7 +18,13 @@
```console
> # Flash firmware.bin to Code FLASH
> wlink -v flash ./firmware.bin
> cargo run -- flash ./firmware.bin`
16:55:56 [INFO] WCH-Link v2.7 (WCH-Link-CH549)
16:55:56 [INFO] attached chip: CH32V20x(0x20360510)
16:55:56 [INFO] flash 860 bytes
16:55:57 [INFO] flash done
16:55:58 [INFO] now reset...
16:55:59 [INFO] resume executing...
> # Dump Code FLASH, for verification
> cargo run -- -v dump 0x08000000 100`
@ -56,7 +62,7 @@ x5 t0: 0xb4a9b38a
....
> # Set dpc(pc) to System Flash and Run
> # Set dpc(pc) to System Flash and Run - Not working :(
> cargo run -- write-reg 0x7b1 0x1fff8000
````

10
src/commands/control.rs

@ -57,6 +57,8 @@ impl Command for AttachChip {
vec![0x02]
}
}
#[derive(Debug)]
pub struct AttachChipResponse {
pub chip_family: RiscvChip,
pub riscvchip: u8,
@ -75,12 +77,10 @@ impl Response for AttachChipResponse {
})
}
}
impl fmt::Debug for AttachChipResponse {
// For logging
impl fmt::Display for AttachChipResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ChipInfo")
.field("chip_family", &self.chip_family)
.field("chip_type", &format!("{:#010x}", self.chip_type))
.finish()
write!(f, "{:?}(0x{:08x})", self.chip_family, self.chip_type)
}
}

16
src/main.rs

@ -1,7 +1,7 @@
use std::{thread::sleep, time::Duration};
use anyhow::Result;
use wlink::{commands, device::WchLink};
use wlink::{commands, device::WchLink, regs};
use clap::{Parser, Subcommand};
@ -54,6 +54,8 @@ enum Commands {
Resume {},
/// Reset the MCU
Reset {},
/// Debug, check status
Status {},
}
fn main() -> Result<()> {
@ -122,6 +124,14 @@ fn main() -> Result<()> {
log::info!("flash {} bytes", firmware.len());
probe.write_flash(&firmware)?;
log::info!("flash done");
sleep(Duration::from_secs(1));
log::info!("now reset...");
probe.send_command(commands::Reset::Quit)?;
sleep(Duration::from_secs(1));
log::info!("resume executing...");
probe.ensure_mcu_resume()?;
}
Reset {} => {
probe.send_command(commands::Reset::Quit)?;
@ -134,6 +144,10 @@ fn main() -> Result<()> {
log::info!("set reg 0x{:04x} to 0x{:08x}", regno, value);
probe.write_reg(regno, value)?;
}
Status {} => {
let dmstatus: regs::Dmstatus = probe.dmi_read()?;
println!("=> {:?}", dmstatus);
}
_ => todo!(),
}
}

10
src/operations.rs

@ -22,7 +22,7 @@ impl WchLink {
log::warn!("chip already attached");
}
let chip_info = self.send_command(commands::control::AttachChip)?;
log::info!("attached chip: {:?}", chip_info);
log::info!("attached chip: {}", chip_info);
let uid = self.send_command(commands::GetChipId)?;
log::debug!("Chip UID: {uid}");
@ -275,10 +275,10 @@ impl WchLink {
let abstractcs = self.dmi_read::<Abstractcs>()?;
log::debug!("{:?}", abstractcs);
if abstractcs.busy() {
log::info!("absctract command busy");
log::error!("absctract command busy");
}
if abstractcs.cmderr() == 0b000 {
log::info!("abstract command OK");
log::trace!("abstract command OK");
}
let resp = self.send_command(DmiOp::read(0x04))?;
@ -296,10 +296,10 @@ impl WchLink {
let abstractcs = self.dmi_read::<Abstractcs>()?;
log::debug!("{:?}", abstractcs);
if abstractcs.busy() {
log::info!("absctract command busy");
log::error!("absctract command busy");
}
if abstractcs.cmderr() == 0b000 {
log::info!("abstract command OK");
log::trace!("abstract command OK");
}
Ok(())

Loading…
Cancel
Save