MarsTechHAN / ch552tool

An open sourced python tool for flashing WCH CH55x series USB microcontroller with bootloader v2.30, v2.31 & v2.40.
GNU General Public License v3.0
154 stars 24 forks source link

Enhance all the things! :) #22

Closed karlp closed 2 years ago

karlp commented 2 years ago

A series of commits, pulled from both @pablomarx as well as my own code, that does...

I've tried to split these commits as well as possible, but I do realize this is a wide ranging commit. The commits from @pablomarx I've taken at face value, but without them, my ch32v307 with bootloader v2.60 didn't work.

Unfortunately I have not explicitly tested this with any of the more "traditional" parts (yet) I simply don't have enough test devices for that.

karlp commented 2 years ago

I like some of the refactoring that was done in #21, but it does allll of it in a single big commit, so it's hard to pull in pieces of it. I've tried to make less structural changes up front while expanding functionality, though some structural overhauls do feel in order to grow this moving forward. Thanks so much for having this base line functionality "just working" though!

pablomarx commented 2 years ago

For what its worth, for my changes that were incorporated into this, I had successfully tested them on the following devices:

karlp commented 2 years ago

hrm, I failed to get ch582m btver 2.40 toprogram in my fork today, either with or without the dummy extra write. I'll have to try your original code, see if that works for me and if I've got somethign wrong.

pablomarx commented 2 years ago

@karlp Is it failing on the first write, or does everything appear successful but the firmware doesn't execute?

If its failing after the first write, it may be because the "Enable sumilat interface" was enabled via WCH ISP Tool.

pablomarx commented 2 years ago

@karlp I think 2.4 needs the 8 byte checksum instead of 4 byte. Changing that has made it able to flash my CH549F on BTVER 02.40.

pablomarx commented 2 years ago

USB traffic from a patched WCH ISP Tool programming a CH549F with BTVER 02.40:

# DETECT_CHIP_CMD_V2
>>> a1120049124d4355204953502026205743482e434e
<<< a10e02004912

# READ_CFG_CMD_V2
>>> a702001f00
<<< a70e1a001f00000000002000000000d2954e00020400bb4eb84400007393

# WRITE_CFG_CMD_V2
>>> a80e000700000000000000000000d20000
<<< a80e02000000

# READ_CFG_CMD_V2
>>> a702001f00
<<< a70e1a001f00000000002000000000d2954e00020400bb4eb84400007393

# SEND_KEY_CMD_V26
>>> a31e00000000000000000000000000000000000000000000000000000000000000
<<< a30e0200a100

# ERASE_CHIP_CMD_V2_32BIT
>>> a404003b000000
<<< a40e02000000

# WRITE_CMD_V2
# file: 00000000  02 06 e1 00 00 00 00 00  00 00 00 00 00 00 00 00  |..?.............|
>>> a53d000000000000090dea0b0b0b0b540b0b0b0b0b0b0b540b0b0b0b0b0b0b540b0b0b0b…
<<< a50e02000000

>>> a53d0038000000000b0b0b0b0b0b0b540b0b0b0907480b540b0b0b0b0b0b0b540b0b0b0b…
<<< a50e02000000
  1. Its using the SEND_KEY_CMD_V26 instead of SEND_KEY_CMD_V24
  2. The checksum is clearly 0x0b (easy to see with all the 0x00s being written). Based on the read config, if we used four bytes for the checksum it would be (0xbb+0x4e+0xb8+0x44)%256 == 0x05. Using all eight bytes gives (0xbb+0x4e+0xb8+0x44+0x00+0x00+0x73+0x93)%256 == 0x0b.
Pe3ucTop commented 2 years ago

@karlp Please check my comment about check sum calculation length dependence in #20

karlp commented 2 years ago

Will do. I'm also going to recheck with the ch32v307 board, but I ran out of time last night. It's failing immediately, not in the verify stage, but I really ran out of time. I managed to flash it using the wch-link and the binary openocd in the MounRiver toolkit.

Pe3ucTop commented 2 years ago

@pablomarx and @karlp please give a try for #21 I will add all mentioned boot loader versions to my code as well and possibly dummy write. File size check (compare with device size) is a good idea too which i plan to apply. My code should be fully working now, and was not finished(reworked) only 1 function (2 old) it is "end" with or without resetting of chip. Because of limit of time that evening, as well :)

arturo182 commented 2 years ago

Hi, thanks for all the work!

I was bringing up my CH32V307 board and was very happy to find this PR here.

I managed to get the PR to work with my board, but had to make two changes: I'm using the 64-pin version of the V307 - the CH32V307RCT6. I'm not sure if the 100-pin version is different, but for me, the chip id returned is 0x71, so the script was wrongly identifying my board as CH571, after changing the chip id in lines 54 and 229, I managed to flash and verify the chip!

In case it's relevant, the bootloader version on my chip is also V2.60

Pe3ucTop commented 2 years ago

@arturo182 it's nice to hear and could you do small favor:

arturo182 commented 2 years ago

Thanks, I will check it out later today and let you know.

BTW I'm a part of an unofficial WCH Discord server that also has a WCH employee there, in case you want to ask questions. Here's the server: https://discord.gg/2prGQmA3ez

And here's a note from him regarding this issue: Screenshot_20220412-100401~2

(UUID is the chk_sum)

arturo182 commented 2 years ago

For posterity: image

karlp commented 2 years ago

@arturo182 I got stuff working here, but @Pe3ucTop 's branch has more and goes further, it's more likely to be the "future" code at least

arturo182 commented 2 years ago

I see, I thought this one was more relevant because it was newer. But I'll check out that one then :)

karlp commented 2 years ago

It's only newer by a couple of days iirc, and the other person seems to be more motivated in adding all the rest of it, I was just poking this enough to try and get things running :) I needed some other parts that their branch didn't address, but it's now been updated as the superset of all.