From df835bbcf24b31a5074dd232499e53134cba9122 Mon Sep 17 00:00:00 2001 From: Andelf Date: Tue, 28 Feb 2023 00:56:36 +0800 Subject: [PATCH] fix: add reset after flash --- README.md | 10 ++++++++-- src/commands/control.rs | 10 +++++----- src/main.rs | 16 +++++++++++++++- src/operations.rs | 10 +++++----- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 341fcb7..9074d14 100644 --- a/README.md +++ b/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 ```` diff --git a/src/commands/control.rs b/src/commands/control.rs index 9f758c3..1beb98f 100644 --- a/src/commands/control.rs +++ b/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) } } diff --git a/src/main.rs b/src/main.rs index f584b91..7d1739c 100644 --- a/src/main.rs +++ b/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!(), } } diff --git a/src/operations.rs b/src/operations.rs index 0b5f198..a0256c0 100644 --- a/src/operations.rs +++ b/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::()?; 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::()?; 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(())