Browse Source

fix: flash command

feat: add --speed option

See-also #24
pull/28/head
Andelf 1 year ago
parent
commit
2e4f455b99
  1. 8
      CHANGELOG.md
  2. 4
      src/commands.rs
  3. 7
      src/device.rs
  4. 10
      src/main.rs
  5. 13
      src/operations.rs
  6. 3
      src/transport.rs

8
CHANGELOG.md

@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Add `--speed` option to specify flash speed
### Fixed
- Regression in `flash` command
## [0.0.5] - 2023-07-31
### Added

4
src/commands.rs

@ -265,12 +265,12 @@ impl Command for Reset {
}
/// Speed settings
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, clap::ValueEnum, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Speed {
Low = 0x03,
Medium = 0x02,
High = 0x01,
VeryHigh = 0x00,
Max = 0x00,
}
impl Default for Speed {

7
src/device.rs

@ -44,6 +44,7 @@ pub struct WchLink {
pub(crate) device_handle: DeviceHandle<rusb::Context>,
pub chip: Option<ChipInfo>,
pub probe: Option<ProbeInfo>,
pub(crate) speed: crate::commands::Speed,
}
impl WchLink {
@ -113,6 +114,7 @@ impl WchLink {
device_handle,
chip: None,
probe: None,
speed: Default::default(),
})
}
@ -123,6 +125,10 @@ impl WchLink {
C::Response::from_raw(&resp)
}
pub fn set_speed(&mut self, speed: crate::commands::Speed) {
self.speed = speed;
}
}
impl Drop for WchLink {
@ -141,6 +147,7 @@ pub fn try_switch_from_rv_to_dap(nth: usize) -> Result<()> {
device_handle: dev,
chip: None,
probe: None,
speed: Default::default(),
};
let info = dev.probe_info()?;
info!("probe info: {:?}", info);

10
src/main.rs

@ -24,6 +24,10 @@ struct Cli {
#[arg(long, global = true)]
chip: Option<RiscvChip>,
/// Connection Speed
#[arg(long, global = true, default_value = "high")]
speed: crate::commands::Speed,
#[command(subcommand)]
command: Option<Commands>,
}
@ -140,6 +144,7 @@ fn main() -> Result<()> {
}
Some(command) => {
let mut probe = WchLink::open_nth(device_index)?;
probe.set_speed(cli.speed);
probe.probe_info()?;
probe.attach_chip(cli.chip)?;
match command {
@ -171,6 +176,9 @@ fn main() -> Result<()> {
probe.erase_flash()?;
}
Flash { address, path } => {
// NOTE: this is required for Flash command
probe.dump_info()?;
let firmware = read_firmware_from_file(path)?;
let start_address =
@ -189,6 +197,8 @@ fn main() -> Result<()> {
log::info!("Now reset...");
probe.send_command(commands::Reset::Quit)?;
sleep(Duration::from_millis(500));
// reattach
probe.attach_chip(cli.chip)?;
log::info!("Resume executing...");
probe.ensure_mcu_resume()?;
}

13
src/operations.rs

@ -34,7 +34,7 @@ impl WchLink {
self.send_command(commands::SetTwoLineMode {
riscvchip: expected_chip.unwrap_or(RiscvChip::CH32V30X) as u8,
speed: Default::default(), // 1 high, 2, medium, 3 low
speed: self.speed,
})?;
if let Ok(resp) = self.send_command(commands::control::AttachChip) {
@ -186,10 +186,8 @@ impl WchLink {
/// Detach chip and let it resume
pub fn detach_chip(&mut self) -> Result<()> {
log::debug!("Detach chip");
if self.chip.is_some() {
self.send_command(commands::control::DetachChip)?;
self.chip = None;
}
self.send_command(commands::control::DetachChip)?;
self.chip = None;
Ok(())
}
@ -241,6 +239,7 @@ impl WchLink {
Ok(())
}
// wlink_write
pub fn write_flash(&mut self, data: &[u8], address: u32) -> Result<()> {
let write_pack_size = self.chip.as_ref().unwrap().chip_family.write_pack_size();
@ -248,6 +247,10 @@ impl WchLink {
if data.len() % 256 != 0 {
data.resize((data.len() / 256 + 1) * 256, 0);
}
log::trace!("Using write pack size {}", write_pack_size);
if data.len() < write_pack_size as usize {
data.resize(write_pack_size as usize, 0xff);
}
self.send_command(Program::Prepare)?;
self.send_command(SetRamAddress {

3
src/transport.rs

@ -66,6 +66,9 @@ impl Transport for DeviceHandle<rusb::Context> {
return Err(crate::Error::InvalidPayloadLength);
}
log::trace!("read data ep {} bytes", bytes_read);
if bytes_read == 4 {
log::trace!("recv data {}", hex::encode(&buf));
}
Ok(buf)
}

Loading…
Cancel
Save