jmaselbas / wch-isp

mirror of https://sr.ht/~jmaselbas/wch-isp/
https://sr.ht/~jmaselbas/wch-isp/
GNU General Public License v2.0
21 stars 8 forks source link

Flash over UART #5

Open COKPOWEHEU opened 9 months ago

COKPOWEHEU commented 9 months ago

It would be nice to implement programming via UART. I can implement this myself, but right now your code is very cohesive. You will have to tear off all references to the USB and put it in a module that can be replaced with a uart. At the same time, the isp commands themselves are almost identical. Bytes (0x57, 0xAB) are added at the beginning and a checksum (sum of all bytes except 0x57, 0xAB) at the end. For reception at the beginning, not 0x57, 0xAB, but 0x55, 0xAA. This will require refactoring the entire source file. Should I do this or just redo everything from scratch in my fork?

jmaselbas commented 9 months ago

yeah that might be an interesting thing to add, can you provide a very crude/simple (maybe standalone) implementation as an example? It doesn't have to be clean, it's mainly to have a starting point. Depending on how this goes I might refactor the entire source file.

COKPOWEHEU commented 9 months ago

cmd_identify_example.zip My test code with command 0xA1 (identify). Connects to /dev/ttyUSB0 and writes (tested on v307) 57 AB A1 12 00 00 00 4D 43 55 20 49 53 50 20 26 20 57 43 48 2E 43 4E AB recv 2: 55 AA A1 00 02 00 71 17 2B

request structure: (57 AB)(const preamble) A1(command) (12 00)(length) (00 00 4D 43 55 20 49 53 50 20 26 20 57 43 48 2E 43 4E)(data) AB(checksum) checksum is sum of (command) + (length) + (data). response structure: (55 AA)(const preamble) A1(command) 00(I dont know what is it) (02 00)(length) (71 17)(data) 2B(checksum)

COKPOWEHEU commented 9 months ago

I tried to disassemble bootloader of v307: https://github.com/COKPOWEHEU/ch32v307_bootloader/blob/main/Bootloader_original/result/src/main.S#L1150 UART buffer starts from 0x2000'02c0. And there is variable on 0x2000'0300 (USB configuration). So size of UART buffer is 64 bytes. Analysis of exchange between official WCH tool and controller shows that 'size' field is 61 bytes or less. (Bytes 0x57, 0xAB does not store, checksum too), 1 byte of 'cmd' + 2 bytes of 'len' + 61 bytes of data = 64 bytes.

COKPOWEHEU commented 9 months ago

Work in progress! Exchange with 307 made for both usb and uart. https://github.com/COKPOWEHEU/wch-isp/tree/refactor Another commands does not implemented yet

jmaselbas commented 9 months ago

I took a look at your code. I also tried to communicate with a device (CH32V003) but it didn't worked, I didn't managed to get any answers on the UART

COKPOWEHEU commented 9 months ago

Yes, I read the v003 have some problems with bootloader. At least it haven't boot0 / boot1 pins... But my chips doesn't arrived so I can't test them myself. I tested v307 (works with USB and UART) and v203 (USB only: UART does not work even with the WCH proprietary utility).

I took a look at your code.

What version? Recently I added reading database from rust version of wch-isp and make a lot of changes

jmaselbas commented 9 months ago

the very first version with only the identification CMD. It fails to receive anything, I think I haven't setup my device properly. What are you doing for the device to enter the UART bootloader ? And which uart are you using ? I also tried with the USART1 on CH32V203 (PB6 & PB7)

COKPOWEHEU commented 9 months ago

USB-UART bridge on ft4232 and homemade bridge on stm32f103. Controller v307 works, but v203 does not. v203g8 works with USB only. I tried UART1 (PA9, PA10) and UART1-remap (PB6, PB7) and swapped Rx/Tx (I dont know... if there is a mistake in my pcb). The only answer is 'timeout'. May be I will try on ch32v103 but it will take a long time BTW, by default UART1 mapped on PA9, PA10. This is the case in stm32, in gd32 and in ch32v307.

jmaselbas commented 9 months ago

all I have is CH32V003, CH32V103, CH32V203 and CH32V208

delfer commented 7 months ago

Hello friends! Any updates on UART support?

jmaselbas commented 7 months ago

I don't think its going to happen for two reasons:

COKPOWEHEU commented 7 months ago

All updates have already been applied in my version. As I said I rewrite the entire code. So I can not send small discrete patches. But it is opensource - you may copy any part of it. Including UART module and its wodows version.https://github.com/COKPOWEHEU/wch-isp

COKPOWEHEU commented 7 months ago

I tested on v307rct6 (both USBs and UART), v303cbt6, v103, v208rbt6 and v203g8r6. The last controller, v203g8r6, burns via USB and UART2 (on PA2, PA3 - instead of UART1 on PA9, PA10). Although, as I heard, v203c8 uses normal UART1.