Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance all the things! :) #22

Merged
merged 11 commits into from
May 25, 2022
Merged

Conversation

karlp
Copy link
Contributor

@karlp karlp commented Mar 19, 2022

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

  • simplifies adding new parts
  • Adds newer bootloader support
  • Adds support for (verified) ch32v307

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 and others added 11 commits March 18, 2022 23:57
It tries to check the payload size before it has a payload.  Move it
after.

Fixes: 2bd501c
Signed-off-by: Karl Palsson <karlp@tweak.net.au>
Not entirely useful perhaps, but a useful cleanup if you are going to do
more in this project.

Signed-off-by: Karl Palsson <karlp@tweak.net.au>
Use namedtuples to make it a bit easier than raw hand maintained dicts.

Signed-off-by: Karl Palsson <karlp@tweak.net.au>
This is sufficient to detect and report bootloader version.  Flashing as
yet untested.

Signed-off-by: Karl Palsson <karlp@tweak.net.au>
Sufficient to report part number and bootloader version.
Have not tested flashing, as I don't (yet) want to risk my only
wch-link.

On the ch32v307-r1-1v0 board, (the black eval board) you can short the
J1 pads on the wch-link section to enter bootloader mode.

Signed-off-by: Karl Palsson <karlp@tweak.net.au>
appears to have introduced a new send key command,
and the checksum calculation is based off 8 bytes of the read config
response (instead of the previous 4 bytes).
…d file,

provided the `CH55X_IC_REF` entry for the device has a key: `erase_required_pages`
with the value of True

Tested-by: Karl Palsson <karlp@tweak.net.au>
Reviewed-by: Karl Palsson <karlp@tweak.net.au>

I've (karlp) actually pretty heavily modified this, to be basically equivalent
to what was already introduced in 2bd501c via MarsTechHAN#17
My CH32V103 with bootloader 2.60 needs an empty WRITE_CMD_V2
command after the payload has been uploaded:  Without this
the verify will fail immediately.

My CH579 device with bootloader 2.7 will fail with this
empty write.

I'm not sure if this write is needed for bootloader 2.60 only,
or if its needed for all CH32V103 devices.  Err'ing on the latter
for now.
Bootloader version 2.60, appears to need the same dummy write.  Still
not enough to say anything conclusive, perhaps it's actually a "riscv"
vs "arm" vs "8051"?  Existing code implies that ARM CH579 _didn't_ want
this, but RISCV CH32v103 and CH32v307 both do.  8051 cores never needed
this either.

See also comments on: cddc995

Signed-off-by: Karl Palsson <karlp@tweak.net.au>
@karlp
Copy link
Contributor Author

karlp commented Mar 19, 2022

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
Copy link

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

  • CH32V103C8 BTVER 02.60
  • CH32V103R8 BTVER 02.60
  • CH571F BTVER 02.80
  • CH573F BTVER 02.70
  • CH579F BTVER 02.70
  • CH579M BTVER 02.61
  • CH582M BTVER 02.40

@karlp
Copy link
Contributor Author

karlp commented Mar 21, 2022

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
Copy link

@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
Copy link

@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
Copy link

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
Copy link
Contributor

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

@karlp
Copy link
Contributor Author

karlp commented Mar 21, 2022

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
Copy link
Contributor

Pe3ucTop commented Mar 21, 2022

@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 :)

@Pe3ucTop Pe3ucTop mentioned this pull request Mar 22, 2022
@arturo182
Copy link

arturo182 commented Apr 12, 2022

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
Copy link
Contributor

Pe3ucTop commented Apr 12, 2022

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

  • Could you try pull request Reworked most  #21 and report which "chip subid" it will show.
    Unfortunately it will probably detect your board as CH571 too, it is because some chips do not have unique ChipId (I found 4 such pairs) and even nor equal to chipid in original file (your case). I'm trying to make more complicated chip identification algorithm based on known data and reverse engineered WCHISP .
    Your reported ChipSubId could help me to figure out such cases.
    Pairs I found with same chipid :
    CH32F203 / CH32V303 chipid: 48 (0x30)
    CH32F103 / CH32V103 chipid: 50 (0x32)
    CH32F205 / CH32V305 chipid: 80 (0x50)
    CH32F207 / CH32V307 chipid: 112 (0x70) !! But your report CH32V307 got chipid 113 (0x71) and it is one more new problem.

@arturo182
Copy link

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
Copy link

For posterity:
image

@karlp
Copy link
Contributor Author

karlp commented Apr 12, 2022

@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
Copy link

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

@karlp
Copy link
Contributor Author

karlp commented Apr 12, 2022

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.

@vowstar vowstar merged commit d00794e into MarsTechHAN:master May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants