Skip to content

Commit

Permalink
feat: progress bar for flash and verify
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Nov 13, 2022
1 parent 21acd73 commit 2bc8b4c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- EEPROM dump support, fix #12
- Refactor all subcommands, using clap v4
- Probe support, multiple chips can be selected by an index
- Progressbar for flash and verify commands

### Changed
- Disable debug log by default

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ object = { version = "0.29", default-features = false, features = [
"read_core",
"std",
] }
indicatif = "0.17"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ This tool should work on most WCH MCU chips. But I haven't tested it on any othe
- [x] CH32F103
- [x] CH582
- CH58xM-EVT
- [x] CH559
- CH559TL_MINIEVT_V20 by wch.cn
- [x] CH32V203
- [CH32V203G6 FlappyBoard](https://github.com/metro94/FlappyBoard)
- ... (feel free to open an issue whether it works on your chip or not)
Expand Down
21 changes: 15 additions & 6 deletions src/flashing.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Chip flashing routine
use std::thread::sleep;
use std::time::Duration;

use anyhow::Result;
use indicatif::ProgressBar;
use scroll::{Pread, Pwrite, LE};

use crate::{
Expand Down Expand Up @@ -202,21 +200,23 @@ impl<T: Transport> Flashing<T> {

const CHUNK: usize = 56;
let mut address = 0x0;

let bar = ProgressBar::new(raw.len() as _);
for ch in raw.chunks(CHUNK) {
self.flash_chunk(address, ch, key)?;
address += ch.len() as u32;
bar.inc(ch.len() as _);
}
// NOTE: require a write action of empty data for success flashing
self.flash_chunk(address, &[], key)?;
bar.finish();

log::info!("Code flash {} bytes written", address);

Ok(())
}

pub fn verify(&mut self, raw: &[u8]) -> Result<()> {
sleep(Duration::from_secs(1));

let key = self.xor_key();
let key_checksum = key.iter().fold(0_u8, |acc, &x| acc.overflowing_add(x).0);
// NOTE: use all-zero key seed for now.
Expand All @@ -227,10 +227,13 @@ impl<T: Transport> Flashing<T> {

const CHUNK: usize = 56;
let mut address = 0x0;
let bar = ProgressBar::new(raw.len() as _);
for ch in raw.chunks(CHUNK) {
self.verify_chunk(address, ch, key)?;
address += ch.len() as u32;
bar.inc(ch.len() as _);
}
bar.finish();

Ok(())
}
Expand Down Expand Up @@ -270,6 +273,8 @@ impl<T: Transport> Flashing<T> {
if self.chip.eeprom_size == 0 {
anyhow::bail!("Chip does not support EEPROM");
}
let bar = ProgressBar::new(self.chip.eeprom_size as _);

let mut ret: Vec<u8> = Vec::with_capacity(self.chip.eeprom_size as _);
let mut address = 0x0;
while address < self.chip.eeprom_size as u32 {
Expand All @@ -278,7 +283,7 @@ impl<T: Transport> Flashing<T> {
let cmd = Command::data_read(address, chunk_size);
let resp = self.transport.transfer(cmd)?;
anyhow::ensure!(resp.is_ok(), "data_read failed");
address += CHUNK as u32;

anyhow::ensure!(
resp.payload()[2..].len() == chunk_size as usize,
"data_read length mismatch"
Expand All @@ -287,7 +292,11 @@ impl<T: Transport> Flashing<T> {
anyhow::bail!("EEPROM read failed, required chunk size cannot be satisfied");
}
ret.extend_from_slice(&resp.payload()[2..]);
address += chunk_size as u32;

bar.inc(chunk_size as _);
if chunk_size < CHUNK as u16 {
bar.finish();
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ fn main() -> Result<()> {
log::info!("Erase done");
}

log::info!("Writing to code flash...");
flashing.flash(&binary)?;
sleep(Duration::from_millis(500));

Expand Down Expand Up @@ -210,6 +211,7 @@ fn main() -> Result<()> {
flashing.reidenfity()?;

sleep(Duration::from_millis(500));
log::info!("Reading EEPROM...");

let eeprom = flashing.dump_eeprom()?;
log::info!("EEPROM data size: {}", eeprom.len());
Expand Down

0 comments on commit 2bc8b4c

Please sign in to comment.