ch32-rs / wlink

An open source WCH-Link library/command line tool written in Rust.
Apache License 2.0
155 stars 25 forks source link

Start address always be 0x00000000 when flashing a hex file? #43

Closed kaidegit closed 6 months ago

kaidegit commented 8 months ago

logs are shown as follows

PS C:\Users\xxx\GitRepo\xxx-sw> wlink erase                                 
07:31:10 [INFO] WCH-Link v2.10 (WCH-LinkE-CH32V305)
07:31:10 [INFO] Attached chip: CH58X [CH582] (ChipID: 0x82000000)
07:31:10 [WARN] The debug interface has been opened, there is a risk of code leakage.
07:31:10 [WARN] Please ensure that the debug interface has been closed before leaving factory!
07:31:10 [INFO] Erase Flash...
07:31:11 [INFO] Erase done
PS C:\Users\xxx\GitRepo\xxx-sw> wlink flash .\cmake-build-debug\xxx-SW.hex  
07:31:19 [INFO] WCH-Link v2.10 (WCH-LinkE-CH32V305)
07:31:19 [INFO] Attached chip: CH58X [CH582] (ChipID: 0x82000000)
07:31:19 [WARN] The debug interface has been opened, there is a risk of code leakage.
07:31:19 [WARN] Please ensure that the debug interface has been closed before leaving factory!
07:31:19 [INFO] Read .\cmake-build-debug\xxx-SW.hex as IntelHex format
07:31:19 [INFO] Firmware size: 31364, start_address: 0x00008000, end_address: 0x0000fa84
07:31:19 [INFO] Flashing 31364 bytes to 0x00000000
████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ 31364/31364
07:31:23 [INFO] Flash done
07:31:23 [INFO] Now reset...
PS C:\Users\xxx\GitRepo\xxx-sw> wlink -v dump 0x00000000 100
07:32:00 [DEBUG] (1) wlink::device: Serial number: "A1B28F06268E"
07:32:00 [INFO] WCH-Link v2.10 (WCH-LinkE-CH32V305)
07:32:00 [INFO] Attached chip: CH58X [CH582] (ChipID: 0x82000000)
07:32:00 [WARN] The debug interface has been opened, there is a risk of code leakage.
07:32:00 [WARN] Please ensure that the debug interface has been closed before leaving factory!
07:32:00 [INFO] Read memory from 0x00000000 to 0x00000064
Length: 100 (0x64) bytes | printable whitespace ascii_other non_ascii
00000000:   6f 00 90 25  00 00 00 00  00 00 00 00  90 00 00 20   o0×%00000000×00 
00000010:   90 00 00 20  a9 bd f9 f5  90 00 00 20  00 00 00 00   ×00 ×××××00 0000
00000020:   00 00 00 00  90 00 00 20  90 00 00 20  00 00 00 00   0000×00 ×00 0000
00000030:   00 00 00 00  90 00 00 20  00 00 00 00  90 00 00 20   0000×00 0000×00 
00000040:   00 00 00 00  90 00 00 20  90 00 00 20  04 06 00 20   0000×00 ×00 ••0 
00000050:   90 00 00 20  90 00 00 20  90 00 00 20  90 00 00 20   ×00 ×00 ×00 ×00 
00000060:   90 00 00 20                                          ×00 
07:32:00 [DEBUG] (1) wlink::operations: Detach chip
PS C:\Users\xxx\GitRepo\xxx-sw> wlink -v dump 0x00008000 100
07:32:09 [DEBUG] (1) wlink::device: Serial number: "A1B28F06268E"
07:32:09 [INFO] WCH-Link v2.10 (WCH-LinkE-CH32V305)
07:32:09 [INFO] Attached chip: CH58X [CH582] (ChipID: 0x82000000)
07:32:09 [WARN] The debug interface has been opened, there is a risk of code leakage.
07:32:09 [WARN] Please ensure that the debug interface has been closed before leaving factory!
07:32:09 [INFO] Read memory from 0x00008000 to 0x00008064
Length: 100 (0x64) bytes | printable whitespace ascii_other non_ascii
00008000:   a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3   ××××××××××××××××
00008010:   a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3   ××××××××××××××××
00008020:   a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3   ××××××××××××××××
00008030:   a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3   ××××××××××××××××
00008040:   a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3   ××××××××××××××××
00008050:   a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3  a9 bd f9 f3   ××××××××××××××××
00008060:   a9 bd f9 f3                                          ××××
07:32:09 [DEBUG] (1) wlink::operations: Detach chip

My hex file is started at 0x00008000, however, the program flashed it to 0x00000000.

i think the issue is caused by this, which always return a 0x00000000 or 0x8000000.

https://github.com/ch32-rs/wlink/blob/59459e298906ec41283595cbe3ac86dd08b6cac9/src/main.rs#L335-L337

andelf commented 8 months ago

This is not easy to fix. Some chip maps 0x0800_0000 to 0x0000_0000. When running, it uses 0x0800_0000, when flashing, it uses 0x0000_0000. I need to add full memory region info to handle this.

Schildkroet commented 7 months ago

HEX files provide the write address. So i think the you can ignore default address and write to address from HEX file. Default address is only needed for BIN files, because they provide no location.

andelf commented 7 months ago

HEX files provide the write address. So i think the you can ignore default address and write to address from HEX file. Default address is only needed for BIN files, because they provide no location.

Agreed. .hex(intel hex) and .elf has the required region info.