ch32-rs/wlink

"Invalid payload length" on WCH-Link CH549 at `ProbeInfo` response payload

Opened this issue · 4 comments

Running wlink -vv regs gives the following output:

13:15:09 [TRACE] (1) wlink::usb_device::libusb: [src/usb_device.rs:118] Device: Bus 002 Device 003: ID 1a86:8010
13:15:09 [DEBUG] (1) wlink::usb_device::libusb: Serial number: "0001A0000001"
13:15:09 [TRACE] (1) wlink::probe: [src/probe.rs:196] send command: GetProbeInfo
13:15:09 [TRACE] (1) wlink::probe: [src/probe.rs:177] send 810d01 01
13:15:09 [TRACE] (1) wlink::probe: [src/probe.rs:187] recv 820d02 0201
Error: Invalid payload length

The error arises from the ProbeInfo response here, where there must be at least three bytes in the payload but my payload only contains two bytes [2, 1]. It then seems as though only the first two bytes are being used.

What should the payload of three bytes look like if only the first two are going to being used?

Thanks,
Rowan

Almost the same here

$ ./wlink -vv status
15:24:37 [TRACE] (1) wlink::usb_device::libusb: [src/usb_device.rs:118] Device: Bus 007 Device 031: ID 1a86:8010
15:24:37 [DEBUG] (1) wlink::usb_device::libusb: Serial number: "0001A0000001"
15:24:37 [TRACE] (1) wlink::probe: [src/probe.rs:196] send command: GetProbeInfo
15:24:37 [TRACE] (1) wlink::probe: [src/probe.rs:177] send 810d01 01
15:24:37 [TRACE] (1) wlink::probe: [src/probe.rs:187] recv 820d02 0203
Error: Invalid payload length

this is wch link with older combined firmware where you can still switch between ARM CMSIS-DAP and RISCV mode by grounding TX pin when plugging device in.

When updating to newer RISCV only firmware it works fine

$ ./wlink -vv status
15:31:09 [TRACE] (1) wlink::usb_device::libusb: [src/usb_device.rs:118] Device: Bus 007 Device 033: ID 1a86:8010
15:31:09 [DEBUG] (1) wlink::usb_device::libusb: Serial number: "434AC30F0659"
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:196] send command: GetProbeInfo
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:177] send 810d01 01
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:187] recv 820d04 020c0100
15:31:09 [INFO] Connected to WCH-Link v2.12(v32) (WCH-Link-CH549)
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:196] send command: SetSpeed { riscvchip: 1, speed: High }
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:177] send 810c02 0101
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:187] recv 820c01 01
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:196] send command: AttachChip
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:177] send 810d01 02
15:31:09 [TRACE] (1) wlink::probe: [src/probe.rs:187] recv 815501 01
15:31:09 [DEBUG] (1) wlink::operations: retrying...
...

the 0203 response is probably firmware version 2.03, newer one is 2.12 and the response is 4 bytes instead of 2

I encountered this same error using a CH549 based WCH-Link. Flashing a new firmware using the official WCH-LinkUtility and then switching to WinUSB drivers fixed it for me.

but anyway, the line

if bytes.len() < 3 {

should probably have < 2 not < 3 so it is worth fixing if older firmware would otherwise work. As mentioned older firmware has both ARM and RISCV versions combined, so if one uses devices with both cpu families and it otherwise works, one may prefer to keep the older version.

EDIT: Here is build with this change https://github.com/fanoush/wlink/actions/runs/11108154349 with downloadable artifacts, did not try with the device yet.

I have tried it and detection now works but it does not detect attached CH32V305 device

$ ./wlink status
16:09:18 [INFO] Connected to WCH-Link v2.3(v23) (WCH-Link-CH549)
Error: Probe is not attached to an MCU, or debug is not enabled. (hint: use wchisp to enable debug)

When testing another CH549 based wch-link with updated firmware it finds it

$ ./wlink status
16:16:40 [INFO] Connected to WCH-Link v2.12(v32) (WCH-Link-CH549)
16:16:40 [INFO] Attached chip: CH32V30X [CH32V305FBP6] (ChipID: 0x30520528)
16:16:40 [INFO] Chip ESIG: FlashSize(128KB) UID(...........)
16:16:40 [INFO] Flash protected: false
16:16:40 [INFO] RISC-V ISA(misa): Some("RV32ACFIMUX")
16:16:40 [INFO] RISC-V arch(marchid): Some("WCH-V4A")
....

so unfortunately this is not the only change needed. I don't have WCH RISCV devices older than 305/307 so can't test if older chips would work with older firmware.