Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Add ch32v307 into 'devices.h' and some other changes #4

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e8db4d3
Add ch32v307 into 'devices.h', change page_size (1k -> 4k) and flash_…
COKPOWEHEU Nov 11, 2023
1c1b8a1
Remove device list. All devices except selected will close right afte…
COKPOWEHEU Nov 16, 2023
029ba88
start rewriting code. Connection over USB works
COKPOWEHEU Nov 18, 2023
64298a0
Add UART version
COKPOWEHEU Nov 18, 2023
c33bc88
Add cmd_isp_key, cmd_isp_end, cmd_verify
COKPOWEHEU Nov 19, 2023
db4fc54
Add reading .bin and .hex
COKPOWEHEU Nov 19, 2023
72ac6c6
Add control of RTD, DTR; add command line parser
COKPOWEHEU Nov 19, 2023
bb47d62
Add 'program' and 'verify'. Looks like this works!
COKPOWEHEU Nov 21, 2023
fd5b9d2
Change bit flags type to unsigned
COKPOWEHEU Nov 22, 2023
dab90ee
Add documentation
COKPOWEHEU Nov 22, 2023
196217d
Fix cross-compiling with mingw32
COKPOWEHEU Nov 23, 2023
44b54db
Add database (yaml from rust version of wch-isp) ; Add Erase option ;…
COKPOWEHEU Nov 26, 2023
82c84af
Add --device flag ; add testing the flash size
COKPOWEHEU Nov 27, 2023
ede864f
Remove outdated files
COKPOWEHEU Nov 27, 2023
d6607f5
Append README and man
COKPOWEHEU Nov 27, 2023
a88dc02
Fix write/verify from address != 0 ; Fix UART timeout in Linux versio…
COKPOWEHEU Nov 28, 2023
bcb9ba9
Fix opening unavaible COM-port in win-version ; fix debug recv len
COKPOWEHEU Dec 3, 2023
5346398
Add test of 'RDPR' bits while writing / verifyng ; change 'unlock' me…
COKPOWEHEU Dec 3, 2023
14bdc74
Add 'help' on no arguments
COKPOWEHEU Dec 3, 2023
ed52ea1
Add 'small' version: without libusb and libyaml (=> without database)
COKPOWEHEU Jan 8, 2024
2322524
Add 'flash_total'. This field displays total flash size ((zero-wait a…
COKPOWEHEU Jan 8, 2024
3b1309f
Fix bug if UART received length more than buffer size
COKPOWEHEU Jan 24, 2024
64b2c9b
Flush UART at init and after change of RTS, DTR
COKPOWEHEU Jan 25, 2024
ccbee40
Add CMD_SET_BAUD command
COKPOWEHEU Mar 17, 2024
f4384f9
Search database in system directory
Jun 11, 2024
1c99214
Fix manual and Makefile
Jun 11, 2024
722bddd
Merge pull request #1 from FrBrGeorge/master
COKPOWEHEU Jun 11, 2024
6d4fb7d
Add --device-path option
COKPOWEHEU Jun 11, 2024
94defee
Add some devices to database
COKPOWEHEU Jun 11, 2024
564b8c7
disable making-package-mode by default
COKPOWEHEU Jun 11, 2024
f10d2f5
Merge pull request #1 from COKPOWEHEU/master
FrBrGeorge Jun 12, 2024
3d7048f
Ramove wch-isp.1 which is generated from wch-isp.man
Jun 12, 2024
95c000c
More man fixes
Jun 12, 2024
e9ce8f1
Merge pull request #2 from FrBrGeorge/master
COKPOWEHEU Jun 12, 2024
787ba1a
fix memory distributions in database
COKPOWEHEU Jun 13, 2024
38619f8
Add build date into '-v' option; Add full/reduced version info
COKPOWEHEU Jun 15, 2024
45f80d4
Add unlock methon via CFG_ROM_READ (ch58x, ...)
COKPOWEHEU Jun 15, 2024
c3487cc
Add test for Karakatitsa hardware (https://karakatitsariscv.github.io…
COKPOWEHEU Jul 28, 2024
1f1f1b7
Fix __DATE_D__: Build: '-155.12.2024' -> 'Build: 05.12.2024'
COKPOWEHEU Dec 5, 2024
d467a5d
Add --database-path into README
COKPOWEHEU Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gitignore.txt
.gitignore
*.o
*.1
*~
wch-isp
test.sh
mingw32
2 changes: 1 addition & 1 deletion 50-wchisp.rules
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# WCH ISP bootloader
SUBSYSTEMS=="usb", ATTRS{idVendor}=="4348", ATTRS{idProduct}=="55e0", GROUP="uucp", MODE="0666"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="4348", ATTRS{idProduct}=="55e0", GROUP="uucp", MODE="0660"
79 changes: 55 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
# SPDX-License-Identifier: GPL-2.0-only
VERSION = 0.3.0
VERSION = 0.1.1
NAME := wch-isp

# Install paths
PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man
UDEVPREFIX = /etc/udev
UDEVPREFIX = $(PREFIX)/lib/udev

#make CROSS_COMPILE=i686-w64-mingw32- INCS="-Imingw32/include -Imingw32/include/libusb-1.0" LIBS="-Lmingw32/lib mingw32/bin/libusb-1.0.dll -lyaml"
ifneq ($(CROSS_COMPILE),)
CC = $(CROSS_COMPILE)cc
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
endif

#CFLAGS += -DPREFIX=\"$(PREFIX)\" -DNAME=\"$(NAME)\" -g
PKG_CONFIG = pkg-config

ifneq ($(OPTIONS),small)
# include and libs
INCS = `$(PKG_CONFIG) --cflags libusb-1.0`
LIBS = `$(PKG_CONFIG) --libs libusb-1.0`
INCS += `$(PKG_CONFIG) --cflags libusb-1.0` `$(PKG_CONFIG) --cflags yaml-0.1`
LIBS += `$(PKG_CONFIG) --libs libusb-1.0` `$(PKG_CONFIG) --libs yaml-0.1`
else
# include and libs
INCS += -DBUILD_SMALL
LIBS +=
endif

# Flags
WCHISP_CPPFLAGS = -DVERSION=\"$(VERSION)\" $(CPPFLAGS)
WCHISP_CFLAGS = -Wall -O2 $(INCS) $(CFLAGS)
WCHISP_LDFLAGS = $(LIBS) $(LDFLAGS)

SRC = wch-isp.c
SRC = main.c wch_if_usb.c wch_if_uart.c wch_yaml_parse.c
#SRC = $(NAME).c
HDR = arg.h devices.h
OBJ = $(SRC:.c=.o)
BIN = wch-isp
MAN = wch-isp.1
DISTFILES = $(SRC) $(HDR) $(MAN) 50-wchisp.rules Makefile
BIN = $(NAME)
MAN = $(NAME).1
DISTFILES = $(SRC) $(HDR) *.man 50-wchisp.rules Makefile


all: $(BIN)
all: $(BIN) $(MAN)

$(OBJ): arg.h devices.h
%.1: %.man
sed "s/VERSION/$(VERSION)/g" < $< > $@

small:
make OPTIONS="small" all

$(BIN): $(OBJ)
$(CC) -o $@ $^ $(WCHISP_LDFLAGS)
.c.o:
$(CC) $(WCHISP_CFLAGS) $(WCHISP_CPPFLAGS) -c $<

install:
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
chmod 755 $(DESTDIR)$(PREFIX)/bin/$(BIN)
mkdir -p $(DESTDIR)$(MANPREFIX)/man1
sed "s/VERSION/$(VERSION)/g" < $(MAN) > $(DESTDIR)$(MANPREFIX)/man1/$(MAN)
chmod 644 $(DESTDIR)$(MANPREFIX)/man1/$(MAN)

install-rules:
mkdir -p $(DESTDIR)$(UDEVPREFIX)/rules.d
cp -f 50-wchisp.rules $(DESTDIR)$(UDEVPREFIX)/rules.d
install: all
install -D $(BIN) $(DESTDIR)$(PREFIX)/bin/${BIN}
install -m644 -D $(MAN) $(DESTDIR)$(MANPREFIX)/man1/$(MAN)
mkdir -p $(DESTDIR)$(PREFIX)/share/$(NAME)
cp -a devices $(DESTDIR)$(PREFIX)/share/$(NAME)/
install -D 50-wchisp.rules $(DESTDIR)$(UDEVPREFIX)/rules.d/50-wchisp.rules

uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)
rm -f $(DESTDIR)$(MANPREFIX)/man1/$(MAN)
rm -rf $(DESTDIR)$(PREFIX)/share/$(NAME)
rm -f $(DESTDIR)$(UDEVPREFIX)/rules.d/50-wchisp.rules

dist:
mkdir -p $(BIN)-$(VERSION)
Expand All @@ -62,6 +74,25 @@ dist:
rm -rf $(BIN)-$(VERSION)

clean:
rm -f $(OBJ) $(BIN)
rm -f $(OBJ) $(BIN) $(MAN)

test:
@if [ $(PORT)"" = "" ] ; then \
echo "examples: " ;\
echo "make PORT=/dev/ttyUSB0 test" ;\
echo "./"$(BIN)" --port=USB info" ;\
echo "./"$(BIN)" --port='//./COM3' --reset=RTS --boot0=DTR info" ;\
else \
stty -F $(PORT) 300 ;\
stty -F $(PORT) 50 ;\
bash -c "echo 'RBU' > $(PORT)" ;\
bash -c "echo 'rBU' > $(PORT)" ;\
sleep 1 ;\
./$(BIN) --port=$(PORT) info ;\
stty -F $(PORT) 50 ;\
bash -c "echo 'RbU' > $(PORT)" ;\
sleep 1 ;\
bash -c "echo 'rbuz' > $(PORT)" ;\
fi

.PHONY: all install install-rules uninstall dist clean
.PHONY: all install uninstall dist clean test
109 changes: 79 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,92 @@
wch-isp
=======

wch-isp is an utility to write firmware into the flash of WCH microcontrollers, over USB.
wch-isp is an utility to write firmware into the flash of WCH microcontrollers, over USB or COM-port.
This utility started as a rewrite in C of the rust tool [wchisp](https://github.com/ch32-rs/wchisp).

```
usage: wch-isp [-VDnpr] [-d <uid>] COMMAND [ARG ...]
wch-isp [-VDnpr] [-d <uid>] [flash|write|verify|reset] FILE
wch-isp [-VDnpr] [-d <uid>] [erase|config|remove-wp]
wch-isp [-VDnpr] list

options:
-d <uid> Select the usb device that matches the uid
-n No verify after writing to flash, done by default
-p Print a progress-bar during command operation
-r Reset after command completed
-D Print raw isp command (for debug)
-V Print version and exit
usage: ./wch-isp [OPTIONS] COMMAND [ARG...]
OPTIONS:
-v Print version and exit
-h, --help Show this help and exit
-n No verify after writing
-p Show progress bar
-d Debug mode, show raw commands
-r Reset after command completed
-b Do not read database\n");
-f Ignore if firmware size more than cached flash size (program memory)
-F Ignore if firmware size more than total flash size (program memory + const data memory
--port=USB Specify port as USB (default)
--port=/dev/ttyUSB0 9600 Specify port as COM-port '/dev/ttyUSB0' with speed 9600 baud
--port='//./COM3' Specify port as COM-port '//./COM3'
--device=DEV Test if connected device is DEV and exit if they differ
--uid=AA-BB-CC-DD-EE-FF-GG-HH Specify device UID
--reset=PIN Use PIN as RESET
--boot0=PIN Use PIN as Boot0
'PIN' may be 'RTS', 'DTR', 'nRTS' or 'nDTR'
--address=0x08000000 Write or verify data from specified address
--database-path=/home/user/wch-isp/devices Search device info in specified path

COMMAND:
write FILE write file (.hex or .bin)
verify FILE verify file (.hex ot .bin)
erase erase all memory
list show connected devices
unlock remove write protection
info show device info: bootloader version, option bytes, flash size etc
optionbytes 'CMD' change optionbytes
example: ./wch-isp optionbytes 'RDPR=0xA5, DATA0 = 0x42'
optionshow 'CMD' show changes after apply CMD to optionbytes; Do not write
```

This utility has been tested on:
- CH32V103
- CH569W
- CH32V307RCT6
- CH32V303CBT6
- CH32V203G8R6


## Examples

List detected device in bootloader mode:

```sh
$ wch-isp list
0: BTVER v2.7 UID 8d-ff-ba-e4-c2-84-09-69 [0x1069] CH569
1: BTVER v2.5 UID f2-3e-88-26-3b-38-b5-9d [0x1980] CH32V208WBU6
./wch-isp list
found 0x19 0x3B ( CH32V203G8R6 ), bt ver.0206 uid = [ CD-AB-1D-36-51-BC-3B-9E ]
found 0x17 0x71 ( CH32V307RCT6 ), bt ver.0209 uid = [ 87-80-CB-26-3B-38-8D-DF ]
```

Flash the `firmware.bin` file, `-p` enable the progress bar.
```
$ wch-isp -p flash firmware.bin
BTVER v2.5 UID f2-3e-88-26-3b-38-b5-9d [0x1980] CH32V208WBU6
[####################################################] write 35392/35392
[####################################################] verify 35392/35392
flash done
Flash the `firmware.bin` file via USB, `-p` enable the progress.

```sh
$ ./wch-isp --port=USB -p write firmware.bin
Erase 1 sectors (1024 bytes)
Write: 100.0 % Write 792 bytes: DONE
Verify: 100.0 % Verify 792 bytes: DONE
```

Erase the device's flash, select the device by it's uid (option `-d`).
Verify the `firmware.hex` file via COM-port (reset connected to RTS, Boot0 connected to DTR):

```sh
$ ./wch-isp --port=/dev/ttyUSB0 --reset=RTS --boot0=DTR verify firmware.hex
Verify 792 bytes: DONE

```
$ wch-isp -d f2-3e-88-26-3b-38-b5-9d erase
BTVER v2.5 UID f2-3e-88-26-3b-38-b5-9d [0x1980] CH32V208WBU6
erase done

Unlock read-protection and write 0x42 to DATA0 field in optionbytes:

```sh
$ ./wch-isp --device=CH32V203G8R6 optionbytes 'RDPR=0xA5 DATA0 = 0x42'
Option bytes write:
0xC03F5AA5
0xBA45BD42
0xFFFFFFFF
Done

```

## Dependency

wch-isp depends on libusb 1.0 or above.
wch-isp depends on **libusb-1.0** and **yaml-0.1** or above.

## How to build

Expand All @@ -72,6 +107,16 @@ Default udev rules are provided and can be installed with this command:
make install-rules
```

### Cross-compilling using mingw32

Download and unarchive **mingw-w64-i686-libusb** and **mingw-w64-i686-libyaml** into special directory, for example, ```lib/mingw32```. Then execute makefile:

```
make CROSS_COMPILE=i686-w64-mingw32- INCS="-Imingw32/include -Imingw32/include/libusb-1.0" LIBS="-Lmingw32/lib mingw32/bin/libusb-1.0.dll -lyaml"
```

Comilled binary will reqire **libusb-1.0.dll**, **libyaml-0-2.dll** (from ```lib/mingw32/bin```) and **libgcc_s_dw2-1.dll** (in my system: ```/usr/lib/gcc/i686-w64-mingw32/10-win32/libgcc_s_dw2-1.dll```)

### Windows using MSYS2

On Windows the build is done using MSYS2 and mingw64, you can install this from https://www.msys2.org
Expand All @@ -89,3 +134,7 @@ Then the `wch-isp.exe` binary can be run like so:
```
PATH="$PATH:/mingw64/bin" ./wch-isp.exe
```

## TODO:

- Test compilling on Windows (Jules Maselbas probably tested it, but I (COKPOWEHEU) haven't yet)
37 changes: 0 additions & 37 deletions arg.h

This file was deleted.

Loading