Skip to content

Commit

Permalink
chore: ch32v203 verfied
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Oct 3, 2023
1 parent 3cb6f81 commit d444c46
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 226 deletions.
8 changes: 4 additions & 4 deletions docs/CH32V003.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ GND <-> GND@MCU
## Chips

```
* CH32V003F4P6-0x003005x0
* CH32V003F4U6-0x003105x0
* CH32V003A4M6-0x003205x0
* CH32V003J4M6-0x003305x0
* CH32V003F4P6-0x003005x0
* CH32V003F4U6-0x003105x0
* CH32V003A4M6-0x003205x0
* CH32V003J4M6-0x003305x0
```
4 changes: 2 additions & 2 deletions docs/CH32V103.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ BOOT0 to VCC
## Chips

```
* CH32F103C8T6-0x20004102
* CH32F103R8T6-0x2000410F
* CH32F103C8T6-0x20004102
* CH32F103R8T6-0x2000410F
```
30 changes: 15 additions & 15 deletions docs/CH32V20x.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
## Chips

```
* CH32V203C8U6-0x203005x0
* CH32V203C8T6-0x203105x0
* CH32V203K8T6-0x203205x0
* CH32V203C6T6-0x203305x0
* CH32V203K6T6-0x203505x0
* CH32V203G6U6-0x203605x0
* CH32V203G8R6-0x203B05x0
* CH32V203F8U6-0x203E05x0
* CH32V203F6P6-0x203705x0-0x203905x0
* CH32V203F8P6-0x203A05x0
* CH32V203RBT6-0x203405xC
* CH32V208WBU6-0x208005xC
* CH32V208RBT6-0x208105xC
* CH32V208CBU6-0x208205xC
* CH32V208GBU6-0x208305xC
* CH32V203C8U6-0x203005x0
* CH32V203C8T6-0x203105x0
* CH32V203K8T6-0x203205x0
* CH32V203C6T6-0x203305x0
* CH32V203K6T6-0x203505x0
* CH32V203G6U6-0x203605x0
* CH32V203G8R6-0x203B05x0
* CH32V203F8U6-0x203E05x0
* CH32V203F6P6-0x203705x0-0x203905x0
* CH32V203F8P6-0x203A05x0
* CH32V203RBT6-0x203405xC
* CH32V208WBU6-0x208005xC
* CH32V208RBT6-0x208105xC
* CH32V208CBU6-0x208205xC
* CH32V208GBU6-0x208305xC
```
22 changes: 11 additions & 11 deletions docs/CH32V307.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ wlink -v dump 0x08000000 0x10`
## Chips

```
* CH32V303CBT6-0x303305x4
* CH32V303RBT6-0x303205x4
* CH32V303RCT6-0x303105x4
* CH32V303VCT6-0x303005x4
* CH32V305FBP6-0x305205x8
* CH32V305RBT6-0x305005x8
* CH32V305GBU6-0x305B05x8
* CH32V307WCU6-0x307305x8
* CH32V307FBP6-0x307205x8
* CH32V307RCT6-0x307105x8
* CH32V307VCT6-0x307005x8
* CH32V303CBT6-0x303305x4
* CH32V303RBT6-0x303205x4
* CH32V303RCT6-0x303105x4
* CH32V303VCT6-0x303005x4
* CH32V305FBP6-0x305205x8
* CH32V305RBT6-0x305005x8
* CH32V305GBU6-0x305B05x8
* CH32V307WCU6-0x307305x8
* CH32V307FBP6-0x307205x8
* CH32V307RCT6-0x307105x8
* CH32V307VCT6-0x307005x8
```
2 changes: 1 addition & 1 deletion docs/CH32X035.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LED1, LED2 blue LED, active low, open, connected to open jumper
## Chips

```
* CH32X035R8T6-0x035006x1
* CH32X035R8T6-0x035006x1
* CH32X035C8T6-0x035106x1
* CH32X035F8U6-0x035E06x1
* CH32X035G8U6-0x035606x1
Expand Down
2 changes: 1 addition & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl WchLink {
}

pub fn send_command<C: crate::commands::Command>(&mut self, cmd: C) -> Result<C::Response> {
log::debug!("send command: {:?}", cmd);
log::trace!("send command: {:?}", cmd);
let raw = cmd.to_raw();
self.device_handle.write_command_endpoint(&raw)?;
let resp = self.device_handle.read_command_endpoint()?;
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Error {
ProbeNotFound,
#[error("WCH-Link is connected, but is not in RV mode")]
ProbeModeNotSupported,
#[error("WCH-Link doesn't support current chip: {0:?}")]
UnsupportedChip(RiscvChip),
#[error("Unknown WCH-Link variant: {0}")]
UnknownLinkVariant(u8),
#[error("Unknown RISC-V Chip: 0x{0:02x}")]
Expand Down
312 changes: 132 additions & 180 deletions src/flash_op.rs

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl WchLinkVariant {
}
}

/// CH549 variant does not support mode switch. re-program is needed.
pub fn support_switch_mode(&self) -> bool {
!matches!(self, WchLinkVariant::Ch549)
}
Expand All @@ -50,10 +51,13 @@ impl WchLinkVariant {
matches!(self, WchLinkVariant::WCh32v208 | WchLinkVariant::ECh32v305)
}

/// Better use E variant
/// Better use E variant, the Old CH549-based variant does not support all chips
pub fn support_chip(&self, chip: RiscvChip) -> bool {
match self {
WchLinkVariant::Ch549 => !matches!(chip, RiscvChip::CH32V003),
WchLinkVariant::Ch549 => !matches!(
chip,
RiscvChip::CH32V003 | RiscvChip::CH32X035 | RiscvChip::CH643
),
WchLinkVariant::WCh32v208 => !matches!(
chip,
RiscvChip::CH56X | RiscvChip::CH57X | RiscvChip::CH58X | RiscvChip::CH59X
Expand Down Expand Up @@ -191,8 +195,7 @@ impl RiscvChip {
RiscvChip::CH57X => &flash_op::CH573,
RiscvChip::CH58X | RiscvChip::CH59X => &flash_op::CH583,
RiscvChip::CH8571 => &flash_op::OP8571,
RiscvChip::CH643 => &flash_op::CH643,
RiscvChip::CH32X035 => &flash_op::CH643,
RiscvChip::CH32X035 | RiscvChip::CH643 => &flash_op::CH643,
RiscvChip::CH32L103 => &flash_op::CH32L103,
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::{thread::sleep, time::Duration};

use anyhow::Result;
use wlink::{
commands::{self, RawCommand}, device::WchLink, dmi::DebugModuleInterface, format::read_firmware_from_file, regs,
RiscvChip,
commands::{self, RawCommand},
device::WchLink,
dmi::DebugModuleInterface,
format::read_firmware_from_file,
regs, RiscvChip,
};

use clap::{Parser, Subcommand};
Expand Down
22 changes: 16 additions & 6 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ impl WchLink {
log::warn!("Chip already attached");
}

// if expected_chip.is_none() {
// log::warn!("No expected chip type specified, assume CH32V30X (use --chip to specify chip type)");
// }

let probe_info = self.send_command(commands::control::GetProbeInfo)?;

if let Some(chip) = expected_chip {
if !probe_info.variant.support_chip(chip) {
log::error!("WCH-Link doesn't support the choosen MCU, please use WCH-LinkE!");
return Err(Error::UnsupportedChip(chip));
}
}

let mut chip_info = None;
for _ in 0..3 {
self.send_command(commands::SetSpeed {
Expand Down Expand Up @@ -304,8 +307,13 @@ impl WchLink {

log::debug!("Flash op written");

if let Ok(_n) = self.send_command(Program::Unknown07AfterFlashOPWritten) {
//todo: check 0x07
if let Ok(n) = self.send_command(Program::Unknown07AfterFlashOPWritten) {
if n != 0x07 {
return Err(Error::Custom(format!(
"Unknown07AfterFlashOPWritten: {}",
n
)));
}
}

// wlink_fastprogram
Expand All @@ -318,6 +326,8 @@ impl WchLink {
println!("=> {rxbuf:02x?}");
if rxbuf[3] != 0x02 && rxbuf[3] != 0x04 {
return Err(Error::Custom(format!(
// 0x05
// 0x18
"Error while fastprogram: {:02x}",
rxbuf[3]
)));
Expand Down

0 comments on commit d444c46

Please sign in to comment.