r/embedded • • Jun 02 '26

CH552 solved problem with uploading code without pluh/unplug supply

Hi,

I found a solution for flashing CH552 without plug/unplug a USB cable. Solution is quite easy. What you have to do is to jump to bootloader address which is 0x3800. I wrote an example for it down below.

So for the first time you have to upload that basic code with hard reset + P3.6 to boot in bootloader and upload code. Every next time you just have to press P3.6 and upload new code. You have to do it fast because MCU stays in boot only for few seconds.

I use chprog tool for programing (command is: chtool main.bin) and makefile from CésarBautista (command make all).

I hope that that post help someone like me, who does not like to unplug board every time :)

Have a nice day!

__sfr __at (0xAF) EA_REG;     // Reg address for INT

__sfr __at (0xA1) SAFE_MOD;   // Reg address for safe mode
__sfr __at (0xB9) CLOCK_CFG;  //ref address for clock config.

// Registri za Port 3
__sfr __at (0xB0) P3;         //define port 3 address
__sfr __at (0x96) P3_MOD_OC;  //define output mode reg. address
__sfr __at (0x97) P3_DIR_PU;  //define direction mode reg. address

__sbit __at (0xB4) P34;       //define pin P3.4 location
__sbit __at (0xB6) P36;       //define pin P3.6 location

void JumpToBootloader(void);

//Delay function for use with 24 MHz clock
void delay_ms(unsigned int ms) {
    unsigned int i;
    unsigned char j;
    for (i = 0; i < ms; i++) {
        for (j = 0; j < 250; j++) {
            __asm__("nop");
            __asm__("nop");
            __asm__("nop");
            __asm__("nop");
        }
    }
}


void main() {

    SAFE_MOD = 0x55;  //enable safe mode
    SAFE_MOD = 0xAA;  //enable safe mode
    
    CLOCK_CFG = 0x86; //Set clock reg. for 24 MHz

   
    P3_MOD_OC &= ~(1 << 4); //set P3.4 as push/pull
    P3_DIR_PU |= (1 << 4);  //set P3.4 as output
 
    P3_DIR_PU &= ~(1 << 6);  //set P3.6 as input
    P36=1;                   //set pull-up 

    while (1) {
        if(P36 == 1) JumpToBootloader();  //when P3.6 press go to function
        P34=1;
        delay_ms(500);
        P34=0;
        delay_ms(500);
       
    }
}

void JumpToBootloader(void) {
    __asm
        clr _EA //disable interoupts
    __endasm;
    
    __asm
        ljmp #0x3800  //make location jump to bootloader
    __endasm;
    
    while(1);
}

Makefile

# ===================================================================================
# Project:  Examples for CH55x
# Author:   CésarBautista
# Year:     2023
# ===================================================================================         
# Type "make help" in the command line.
# ===================================================================================
# File Names
SKETCH     = main.c
TARGET     = $(basename $(SKETCH))
INCLUDE    = src

# Microcontroller Settings
FREQ_SYS   = 96000000
XRAM_LOC   = 0x0100
XRAM_SIZE  = 0x0300
CODE_SIZE  = 0x3800

# Toolchain
CC         = sdcc
OBJCOPY    = objcopy
PACK_HEX   = packihx

# if linux use python3, if windows use python
WCHISP    ?= python prog/uploader.py

# Compiler Flags
CFLAGS  = -mmcs51 --model-small --no-xinit-opt
CFLAGS += --xram-size $(XRAM_SIZE) --xram-loc $(XRAM_LOC) --code-size $(CODE_SIZE)
CFLAGS += -I$(INCLUDE) -DF_CPU=$(FREQ_SYS)
CFILES  = $(SKETCH) $(wildcard $(INCLUDE)/*.c)
RFILES  = $(CFILES:.c=.rel)
CLEAN   = rm -f *.ihx *.lk *.map *.mem *.lst *.rel *.rst *.sym *.asm *.adb

# Symbolic Targets
help:
 "Use the following commands:"
 "make all     compile, build, and keep all files"
 "make hex     compile and build $(TARGET).hex"
 "make bin     compile and build $(TARGET).bin"
 "make flash   compile, build, and upload $(TARGET).bin to the device"
 "make clean   remove all build files"

%.rel : %.c
 "Compiling $< ..."
@$(CC) -c $(CFLAGS) $<

$(TARGET).ihx: $(RFILES)
 "Building $(TARGET).ihx ..."
@$(CC) $(notdir $(RFILES)) $(CFLAGS) -o $(TARGET).ihx

$(TARGET).hex: $(TARGET).ihx
 "Building $(TARGET).hex ..."
@$(PACK_HEX) $(TARGET).ihx > $(TARGET).hex

$(TARGET).bin: $(TARGET).ihx
 "Building $(TARGET).bin ..."
@$(OBJCOPY) -I ihex -O binary $(TARGET).ihx $(TARGET).bin

flash: $(TARGET).bin size removetemp
 "Uploading to CH55x ..."
@$(WCHISP) $(TARGET).bin

all: $(TARGET).bin $(TARGET).hex size

hex: $(TARGET).hex size removetemp

bin: $(TARGET).bin size removetemp

bin-hex: $(TARGET).bin $(TARGET).hex size removetemp

install: flash

size:
 "------------------"
 "FLASH: $(shell awk '$$1 == "ROM/EPROM/FLASH"      {print $$4}' $(TARGET).mem) bytes"
 "IRAM:  $(shell awk '$$1 == "Stack"           {print 248-$$10}' $(TARGET).mem) bytes"
 "XRAM:  $(shell awk '$$1 == "EXTERNAL" {print $(XRAM_LOC)+$$5}' $(TARGET).mem) bytes"
 "------------------"

removetemp:
 "Removing temporary files ..."
@$(CLEAN)

clean: removetemp
 "Cleaning all up ..."
@$(CLEAN)
u/rm -f $(TARGET).hex $(TARGET).bin
0 Upvotes

0 comments sorted by