ch32-rs / wlink

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

write-mem seems not to be working? #44

Open thirstyone opened 1 year ago

thirstyone commented 1 year ago

something strange is going on. the first time I did wlink dump 0x08003fc0 64 i got:

23:56:02 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305)
23:56:02 [INFO] Attached chip: CH32V003 [CH32V003J4M6] (ChipID: 0x00330500)
23:56:02 [INFO] Read memory from 0x08003fc0 to 0x08004000
Length: 64 (0x40) bytes | printable whitespace ascii_other non_ascii
08003fc0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fd0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fe0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003ff0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××

Then immediately I did it the second time: wlink dump 0x08003fc0 64 I got:

23:56:05 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305)
23:56:05 [INFO] Attached chip: CH32V003 (ChipID: 0x73655207)
23:56:05 [INFO] Read memory from 0x08003fc0 to 0x08004000
Length: 64 (0x40) bytes | printable whitespace ascii_other non_ascii
08003fc0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fd0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fe0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003ff0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××

See the changes in chip id? then I did wlink write-mem 0x08003fc0 0: 23:57:28 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305) 23:57:28 [INFO] Attached chip: CH32V003 (ChipID: 0xffffffff) 23:57:28 [INFO] Write memory 0x00000000 to 0x08003fc0 Then I rechecked it with wlink dump 0x08003fc0 64 and got:

23:57:34 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305)
23:57:34 [INFO] Attached chip: CH32V003 (ChipID: 0x74617405)
23:57:34 [INFO] Read memory from 0x08003fc0 to 0x08004000
Length: 64 (0x40) bytes | printable whitespace ascii_other non_ascii
08003fc0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fd0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fe0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003ff0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××

So, I tried once again: wlink write-mem 0x08003fc0 10 and got

23:58:29 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305)
23:58:30 [INFO] Attached chip: CH32V003 (ChipID: 0xffffffff)
23:58:30 [INFO] Write memory 0x0000000a to 0x08003fc0

After checking with wlink dump 0x08003fc0 64 I got:

23:58:37 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305)
23:58:37 [INFO] Attached chip: CH32V003 (ChipID: 0x0000000a)
23:58:37 [INFO] Read memory from 0x08003fc0 to 0x08004000
Length: 64 (0x40) bytes | printable whitespace ascii_other non_ascii
08003fc0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fd0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003fe0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××
08003ff0:   ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ××××××××××××××××

What is going on with chip-id? am I doing something wrong?

andelf commented 1 year ago

write-mem is not for "write-flash". You can check out the chip manual for memory split.

thirstyone commented 1 year ago

I am looking at Figure 1-2. Memory address map. So, what are the valid addresses for write-mem (and btw, what are the valid addresses for write-reg?) So, there's no way to alter a few bytes in the flash with wlink?

andelf commented 1 year ago

https://github.com/ch32-rs/wlink/blob/59459e298906ec41283595cbe3ac86dd08b6cac9/src/dmi.rs#L495-L508

It's more complex. Flash requires erasing by pages and programming by sectors.

You cannot expect a command line tool to flash with complex erase and program sequence. But you can always use it as a library to do it.