About
Blink a LED with the 30 cents USB chip WinChipHead CH552G, built around a programmable 8051 core.
Order
I ordered 10 of those chips at LCSC, at 0.30USD each.
Format
The chip is in SOIC16 format, I soldered it to a SOP16 breakout board.
Ingredients
You need:
0. (optional) salvage a USB cable for breadboard usage
1. a micro-usb cable
2. a breadboard
3. a micro-usb breakout
4. a LED
5. two 100nF capacitors
6. some male-male breaboard wires
7. a SOP16 breakout board
8. a CH552G chip
This follows the excellent tutorial from Rabid Inventor: "on a breadboard all it requires is 2 x 100nF ceramic capacitors between 3v3 and GND and between VCC and GND as down below:"
Lsusb
Once your circuit is properly done, you should plug it into your Linux laptop and see it with lsusb:
$ lsusb
ADD LSUSB HERE
Tools
You need the required tools for Linux:
- sdcc (check yours has with mcs51 support)
$ sdcc -v
SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/TININative/ds400/hc08/s08/stm8 3.5.0 #9253 (Apr 3 2018) (Linux)
published under GNU General Public License (GPL)
- ch55xtool
- objcopy (binutils)
Code
1. Clone the repo with examples https://github.com/Blinkinlabs/ch554_sdcc
2. Go to "examples/blink" directory, and do a make:
zoobab@muno /home/zoobab/soft/ch554_sdcc/examples/blink [12]$ make
sdcc -c -V -mmcs51 --model-small --xram-size 0x0400 --xram-loc 0x0000 --code-size 0x3800 -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 main.c
+ /usr/bin/sdcpp -nostdinc -Wall -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 -obj-ext=.rel -D__SDCC_MODEL_SMALL -D__SDCC_FLOAT_REENT -D__SDCC=3_5_0 -DSDCC=350 -D__SDCC_REVISION=9253 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__ -D__STDC_NO_THREADS__ -D__STDC_NO_ATOMICS__ -D__STDC_NO_VLA__ -isystem /usr/bin/../share/sdcc/include/mcs51 -isystem /usr/share/sdcc/include/mcs51 -isystem /usr/bin/../share/sdcc/include -isystem /usr/share/sdcc/include main.c
+ /usr/bin/sdas8051 -plosgffw main.rel main.asm
sdcc -c -V -mmcs51 --model-small --xram-size 0x0400 --xram-loc 0x0000 --code-size 0x3800 -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 ../../include/debug.c
+ /usr/bin/sdcpp -nostdinc -Wall -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 -obj-ext=.rel -D__SDCC_MODEL_SMALL -D__SDCC_FLOAT_REENT -D__SDCC=3_5_0 -DSDCC=350 -D__SDCC_REVISION=9253 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__ -D__STDC_NO_THREADS__ -D__STDC_NO_ATOMICS__ -D__STDC_NO_VLA__ -isystem /usr/bin/../share/sdcc/include/mcs51 -isystem /usr/share/sdcc/include/mcs51 -isystem /usr/bin/../share/sdcc/include -isystem /usr/share/sdcc/include ../../include/debug.c
../../include/debug.c:177: warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG
../../include/debug.c:272: warning 158: overflow in implicit constant conversion
+ /usr/bin/sdas8051 -plosgffw debug.rel debug.asm
sdcc main.rel debug.rel -V -mmcs51 --model-small --xram-size 0x0400 --xram-loc 0x0000 --code-size 0x3800 -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 -o blink.ihx
+ /usr/bin/sdld -nf blink.lk
objcopy -I ihex -O binary blink.ihx blink.bin
packihx blink.ihx > blink.hex
packihx: read 19 lines, wrote 28: OK.
Note that the code in main.c is blinking a "LED connected to pin 1.7":
// Blink an LED connected to pin 1.7 #include <ch554.h> #include <debug.h> #define LED_PIN 7 SBIT(LED, 0x90, LED_PIN); void main() { CfgFsys(); // Configure pin 1.7 as GPIO output P1_DIR_PU &= 0x0C; P1_MOD_OC = P1_MOD_OC & ~(1<<LED_PIN); P1_DIR_PU = P1_DIR_PU | (1<<LED_PIN); while (1) { mDelaymS(100); LED = !LED; } }
3. Change the value of WCHISP to "ch55xtool" in "ch554_sdcc/examples/Makefile.include":
# toolchain
CC = sdcc
OBJCOPY = objcopy
PACK_HEX = packihx
WCHISP = ch55xtool
3. Do a make flash:
zoobab@muno /home/zoobab/soft/ch554_sdcc/examples/blink [16]$ make flash
sdcc -c -V -mmcs51 --model-small --xram-size 0x0400 --xram-loc 0x0000 --code-size 0x3800 -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 ../../include/debug.c
+ /usr/bin/sdcpp -nostdinc -Wall -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 -obj-ext=.rel -D__SDCC_MODEL_SMALL -D__SDCC_FLOAT_REENT -D__SDCC=3_5_0 -DSDCC=350 -D__SDCC_REVISION=9253 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__ -D__STDC_NO_THREADS__ -D__STDC_NO_ATOMICS__ -D__STDC_NO_VLA__ -isystem /usr/bin/../share/sdcc/include/mcs51 -isystem /usr/share/sdcc/include/mcs51 -isystem /usr/bin/../share/sdcc/include -isystem /usr/share/sdcc/include ../../include/debug.c
../../include/debug.c:177: warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG
../../include/debug.c:272: warning 158: overflow in implicit constant conversion
+ /usr/bin/sdas8051 -plosgffw debug.rel debug.asm
sdcc main.rel debug.rel -V -mmcs51 --model-small --xram-size 0x0400 --xram-loc 0x0000 --code-size 0x3800 -I/home/zoobab/soft/ch554_sdcc/examples/../include -DFREQ_SYS=16000000 -o blink.ihx
+ /usr/bin/sdld -nf blink.lk
objcopy -I ihex -O binary blink.ihx blink.bin
ch55xtool -f blink.bin
NO_DEV_FOUND
../Makefile.include:57: recipe for target 'flash' failed
make: *** [flash] Error 1
You have to put the device in Bootloader mode, by installing a 10K resistor at the right location, thanks to alt_bier:
"You need to use a 10K pull up resistor on pin 3.6 which is D+ for USB. I wire this into a tactile switch that can be held down while connecting the USB. Here is my CH552G boilerplate design."
Todo
- Package SDCC for Alpine Linux
- Dockerfile with all the tools
- How to switch to bootloader mode after blink has been flashed? see https://github.com/MarsTechHAN/ch552tool/issues/8
- Add the chip to Fritzing SOIC16
Links
- https://rabid-inventor.blogspot.com/2020/05/getting-started-with-ch55x.html
- https://lcsc.com/product-detail/WCH_WCH-Jiangsu-Qin-Heng-CH552G_C111292.html
- https://fr.aliexpress.com/item/32862658659.html
- https://github.com/diodep/ch55x_jtag
- https://84badge.com/code_0/
- http://kyoro205.blog.fc2.com/blog-entry-662.html
- https://www.youtube.com/watch?v=EKhntUyfqhQ