Skip to content

hexeguitar/CH559_flash_verify_test

Repository files navigation

CH559_flash_verify_test

Quick project to test the verify function of the wchflasher python script.
There is still a problem with veryfing the flash content above ~12k range in the CH559 MCU.
I have created this project to automate the test procedures using a few useful bash and python scripts.

The main part, the python flash script is based on work of Aaron Christopel (https://ATCnetz.de).
I have rewritten it to add more options, especially usb logging features which hopefully will help with solving the issue.

The main idea for the test is to generate a compiled bin file with sort of controlled length. To achieve that there is python script that generates long const uint8_t arrays that are included in the main file. The values can be random, increasing or just a one constant integer.

Problems to solve:

At the moment there is a problem with verifying the firmware image after writing it to the flash. The bootloader starts to send 0xF5 replies starting from the 0x2bf8 flash address. Work is still in progress to solve that issue. To help and provide more data to analyze i have added the 'test' directory with a few files:

  • CH559_verify_test.bin - test firmware with a huge array of 0x00 bytes included. Helps to see the operations done by the bootloader.
  • chflasher_usb.log - log file generated by the chflasher python script showing USB IO operations and data packets.
  • wch_app_usb_dump.json - captured usb packets while programming the same bin file using the original WCH app on windows. Data exported as json file for futher parsing with the included python parser.
  • usb_dump_parsed.txt - file above parsed through the python parser to make the data leasier ot compare with the log file created by the wchflasher script. Detects commands and shows read/write packets.

Assuming that sdcc is installed and in PATH.


Scripts:

scripts/gen_data.py

Generates a const uint8_t array with required length.

usage: gen_data.py [-h] (--rnd | --lst | --val VAL) -l LENGTH -o OUT

  -h, --help            show this help message and exit
  --rnd                 Generate random values
  --lst                 Generate 0x00-0xFF increasing vealues
  --val VAL             Populate array with one value.
  -l LENGTH, --length LENGTH
                        Length of the array in bytes.
  -o OUT, --out OUT     Output file name.

Examples (execute in the projects root directory):
generate an array in size of 8192 bytes, populated with random values and save it as array.h file
python3 scripts/gen_data.py --rnd -l 8192 -o array.h
generate an array in size of 8192 bytes, populated with values increasing from 0 to 255 and save it as array.h file
python3 scripts/gen_data.py --lst -l 8192 -o array.h
generate an array in size of 8192 bytes, populated with zeros and save it as array.h file
python3 scripts/gen_data.py --val 0x00 -l 8192 -o array.h


scripts/print_bin_size.py

Prints out the size of the input bin file in bytes. Use at the end of compilation process. Usage (from the projects root folder):
python3 scripts/print_bin_size.py -i <input file>


linux_ch55x_install_udev_rules.sh

Installs required for the CH55x chips udev rules on the Linux systems.


scripts/chflasher.py

The main flash programming and diagnostic script.
Usage (from the projects root folder):
python3 scripts/gen_data.py <mode> -l <length> -o <output file>

python3 chflasher.py [-h] [--version] [-f FILE] [-w | -v | -d | -e] [-s] [--log LOG]
Options:
	-h		show help
	--version	show version
	--log LOG_FILE	write diagnostic log file
Operations:
	-d		identify chip
	-e		erase flash
	-f FILE	input bin file
	-w		write bin file to flash
	-v		verify the flash against the bin file
	-s		exit bootloader/start application

Logging option is implemented for the bootloader ver 2.1 only (for now).

Examples:

write the blink.bin file, verify it and log the usb operations in usb.log file:
python3 chflasher.py --log usb.log -w -f blink.bin

Write/verify the blink file, exit the bootloader and start the application:
python3 chflasher.py -w -f blink.bin

Erase the flash:
python3 chflasher.py -e

Identify the chip and the bootloader version:
python3 chflasher.py -d

Normally when the --log option is used MCU will stay in the bootloader mode, to exit the bootloader and start the application use:
python3 chflasher.py -s


tests/usb_parser.py

Parses the exported json USB data packets and formats in similar way the chflasher script is generating the log files.
Usage:
usb_parser [-h] -i INPUT -o OUTPUT

Example:
usb_parser [-h] -i wch_app_usb_dump.json -o usb_data_parsed-txt


test.sh

Generates test firmware, compiles it, tries to flash and writes an usb.log file:
Usage:
./test.sh 8BIT_VALUE

8BIT_VALUE is any value in range 0x00 to 0xFF, it will be used to populate the test_array included in the firmware. Filling the flash with a bunch of zeroes really helps to see what is the bootloader doing while writing/veryfing the flash.
The length of the test_array can be set using the size variable inside the script. Set to 24000 to cross the 0x2bf8 boundary where the flar verify error starts to occur.


(c) 01.2020 by Piotr Zapart
www.hexefx.com

About

testing the python flasher - verify

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published