Skip to content

Commit

Permalink
added bootloader update firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Sep 16, 2024
1 parent cca27f2 commit b524159
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 8 deletions.
59 changes: 51 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ IDENTIFIER := AM32

# Folders
HAL_FOLDER := Mcu
MAIN_SRC_DIR := Src
MAIN_INC_DIR := Inc

SRC_DIRS_COMMON := $(MAIN_SRC_DIR)

# Working directories
ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))

Expand Down Expand Up @@ -58,9 +55,6 @@ CFLAGS_COMMON := $(CFLAGS_BASE)
# Linker options
LDFLAGS_COMMON := -specs=nano.specs $(LIBS) -Wl,--gc-sections -Wl,--print-memory-usage

# Search source files
SRC_COMMON := $(foreach dir,$(SRC_DIRS_COMMON),$(wildcard $(dir)/*.[cs]))

# configure some directories that are relative to wherever ROOT_DIR is located
OBJ := obj
BIN_DIR := $(ROOT)/$(OBJ)
Expand Down Expand Up @@ -89,7 +83,10 @@ clean :
BOOTLOADER_VERSION := $(shell $(FGREP) "define BOOTLOADER_VERSION" $(MAIN_INC_DIR)/version.h | $(CUT) -d" " -f3 )

SRC_BL := $(foreach dir,bootloader,$(wildcard $(dir)/*.[cs]))
SRC_BLU := $(foreach dir,bl_update,$(wildcard $(dir)/*.[cs]))

LDSCRIPT_BL := bootloader/ldscript_bl.ld
LDSCRIPT_BLU := bl_update/ldscript_bl.ld

# get a define in form -DBOARD_FLASH_SIZE=N if the target has a _NK suffix
define flash_size_flag
Expand All @@ -106,13 +103,23 @@ define BOOTLOADER_BASENAME
$(IDENTIFIER)_$(call base_mcu,$(1))_BOOTLOADER_$(2)$(call get_k_tag,$(1))
endef

define BOOTLOADER_UPDATE_BASENAME
$(IDENTIFIER)_$(call base_mcu,$(1))_BL_UPDATER_$(2)$(call get_k_tag,$(1))
endef

# bootloader target names with version for filename
define BOOTLOADER_BASENAME_VER
$(call BOOTLOADER_BASENAME,$(1),$(2))_V$(BOOTLOADER_VERSION)
endef

# bootloader target names with version for filename
define BOOTLOADER_UPDATE_BASENAME_VER
$(call BOOTLOADER_UPDATE_BASENAME,$(1),$(2))_V$(BOOTLOADER_VERSION)
endef

# list of targets formed using CREATE_BOOTLOADER_TARGET
ALL_BUILDS :=
BLU_BUILDS :=

# create a bootloader build target given a build in the form MCU or MCU_nK and a PIN
define CREATE_BOOTLOADER_TARGET
Expand All @@ -123,14 +130,22 @@ $(eval EXTRA_CFLAGS := $(call flash_size_flag,$(1)))
$(eval ELF_FILE := $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(BUILD),$(PIN)).elf)
$(eval HEX_FILE := $(ELF_FILE:.elf=.hex))
$(eval DEP_FILE := $(ELF_FILE:.elf=.d))
$(eval BIN_FILE := $(ELF_FILE:.elf=.bin))
$(eval H_FILE := $(ELF_FILE:.elf=.h))
$(eval BLU_ELF_FILE := $(BIN_DIR)/$(call BOOTLOADER_UPDATE_BASENAME_VER,$(BUILD),$(PIN)).elf)
$(eval BLU_HEX_FILE := $(BLU_ELF_FILE:.elf=.hex))
$(eval BLU_DEP_FILE := $(BLU_ELF_FILE:.elf=.d))
$(eval TARGET := $(call BOOTLOADER_BASENAME,$(BUILD),$(PIN)))
$(eval BLU_TARGET := $(call BOOTLOADER_UPDATE_BASENAME,$(BUILD),$(PIN)))

# get MCU specific compiler, objcopy and link script or use the ARM SDK one
$(eval xCC := $(if $($(MCU)_CC), $($(MCU)_CC), $(CC)))
$(eval xOBJCOPY := $(if $($(MCU)_OBJCOPY), $($(MCU)_OBJCOPY), $(OBJCOPY)))
$(eval xLDSCRIPT := $(if $($(MCU)_LDSCRIPT), $($(MCU)_LDSCRIPT), $(LDSCRIPT_BL)))
$(eval xBLU_LDSCRIPT := $(if $($(MCU)_LDSCRIPT_BLU), $($(MCU)_LDSCRIPT_BLU), $(LDSCRIPT_BLU)))

-include $(DEP_FILE)
-include $(BLU_DEP_FILE)

$(ELF_FILE): CFLAGS_BL := $$(MCU_$(MCU)) $$(CFLAGS_$(MCU)) $$(CFLAGS_BASE) -DBOOTLOADER -DUSE_$(PIN) $(EXTRA_CFLAGS)
$(ELF_FILE): LDFLAGS_BL := $$(LDFLAGS_COMMON) $$(LDFLAGS_$(MCU)) -T$(xLDSCRIPT)
Expand All @@ -143,22 +158,46 @@ $(ELF_FILE): $$(SRC_$(MCU)_BL) $$(SRC_BL)
$$(QUIET)$$(CP) -f $$(SVD_$(MCU)) $$(OBJ)/debug.svd
$$(QUIET)$$(CP) -f Mcu$(DSEP)$(call lc,$(MCU))$(DSEP)openocd.cfg $$(OBJ)$$(DSEP)openocd.cfg > $$(NUL)

$(H_FILE): $(BIN_FILE)
python3 bl_update/make_binheader.py $(BIN_FILE) $(H_FILE)

$(BLU_ELF_FILE): CFLAGS_BLU := $$(MCU_$(MCU)) $$(CFLAGS_$(MCU)) $$(CFLAGS_BASE) -DBOOTLOADER -DUSE_$(PIN) $(EXTRA_CFLAGS) -Wno-unused-variable -Wno-unused-function
$(BLU_ELF_FILE): LDFLAGS_BLU := $$(LDFLAGS_COMMON) $$(LDFLAGS_$(MCU)) -T$(xBLU_LDSCRIPT)
$(BLU_ELF_FILE): $$(SRC_$(MCU)_BL) $$(SRC_BLU) $(H_FILE)
$$(QUIET)echo building bootloader updater for $(BUILD) with pin $(PIN)
$$(QUIET)$$(MKDIR) -p $(OBJ)
$$(QUIET)echo Compiling $(notdir $$@)
$$(QUIET)$(xCC) $$(CFLAGS_BLU) $$(LDFLAGS_BLU) -MMD -MP -MF $(DEP_FILE) -o $$(@) $$(SRC_$(MCU)_BL) $$(SRC_BLU) -I. -DBL_HEADER_FILE=$(H_FILE)

# Generate bin and hex files
$(HEX_FILE): $(ELF_FILE)
$(HEX_FILE): $$(ELF_FILE)
$$(QUIET)echo Generating $(notdir $$@)
$$(QUIET)$(xOBJCOPY) -O binary $$(<) $$(@:.hex=.bin)
$$(QUIET)$(xOBJCOPY) $$(<) -O ihex $$(@:.bin=.hex)

$(BIN_FILE): $$(HEX_FILE)

# Generate bin and hex files for bl updater
$(BLU_HEX_FILE): $$(BLU_ELF_FILE)
$$(QUIET)echo Generating $(notdir $$@)
$$(QUIET)$(xOBJCOPY) -O binary $$(<) $$(@:.hex=.bin)
$$(QUIET)$(xOBJCOPY) $$(<) -O ihex $$(@:.bin=.hex)

$(TARGET): $(HEX_FILE)
$(TARGET): $$(HEX_FILE)

$(BLU_TARGET): $$(BLU_HEX_FILE)

# add to list
ALL_BUILDS := $(ALL_BUILDS) $(TARGET)
BLU_BUILDS := $(BLU_BUILDS) $(BLU_TARGET)
endef

$(foreach BUILD,$(MCU_BUILDS),$(foreach PIN,$(BOOTLOADER_PINS),$(eval $(call CREATE_BOOTLOADER_TARGET,$(BUILD),$(PIN)))))

bootloaders: $(ALL_BUILDS)

updaters: $(BLU_BUILDS)

# include the targets for installing tools
include $(ROOT)/make/tools_install.mk

Expand All @@ -167,3 +206,7 @@ include $(ROOT)/make/tools_install.mk
targets:
$(QUIET)echo List of targets. To build a target use 'make TARGETNAME'
$(QUIET)echo $(ALL_BUILDS)

updater_targets:
$(QUIET)echo List of updater targets. To build a target use 'make TARGETNAME'
$(QUIET)echo $(BLU_BUILDS)
2 changes: 2 additions & 0 deletions Mcu/l431/Inc/blutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void Error_Handler()
while (1) {}
}

#ifdef FIRMWARE_RELATIVE_START
/*
jump from the bootloader to the application code
*/
Expand All @@ -170,6 +171,7 @@ static inline void jump_to_application(void)
"bx %1 \n"
: : "r"(stack_top), "r"(JumpAddress) :);
}
#endif // FIRMWARE_RELATIVE_START

void SystemInit(void)
{
Expand Down
138 changes: 138 additions & 0 deletions Mcu/v203/Link_BLU.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
linker script for CH32V203 bootloader updater
*/
ENTRY( _start )

__stack_size = 2048;

PROVIDE( _stack_size = __stack_size );

MEMORY
{
/*
note that flash for eeprom is at 0x08000000, but we flash at 0x00000000 for link
*/
FLASH (rx) : ORIGIN = 0x00001000, LENGTH = 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
}


SECTIONS
{

.init :
{
_sinit = .;
. = ALIGN(4);
KEEP(*(SORT_NONE(.init)))
. = ALIGN(4);
_einit = .;
} >FLASH AT>FLASH

.vector :
{
*(.vector);
. = ALIGN(64);
} >FLASH AT>FLASH

.text :
{
. = ALIGN(4);
*(.text)
*(.text.*)
*(.rodata)
*(.rodata*)
*(.gnu.linkonce.t.*)
. = ALIGN(4);
} >FLASH AT>FLASH

.fini :
{
KEEP(*(SORT_NONE(.fini)))
. = ALIGN(4);
} >FLASH AT>FLASH

PROVIDE( _etext = . );
PROVIDE( _eitcm = . );

.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH AT>FLASH

.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH AT>FLASH

.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH AT>FLASH

.dalign :
{
. = ALIGN(4);
PROVIDE(_data_vma = .);
} >RAM AT>FLASH

.dlalign :
{
. = ALIGN(4);
PROVIDE(_data_lma = .);
} >FLASH AT>FLASH

.data :
{
*(.gnu.linkonce.r.*)
*(.data .data.*)
*(.gnu.linkonce.d.*)
. = ALIGN(8);
PROVIDE( __global_pointer$ = . + 0x800 );
*(.sdata .sdata.*)
*(.sdata2.*)
*(.gnu.linkonce.s.*)
. = ALIGN(8);
*(.srodata.cst16)
*(.srodata.cst8)
*(.srodata.cst4)
*(.srodata.cst2)
*(.srodata .srodata.*)
. = ALIGN(4);
PROVIDE( _edata = .);
} >RAM AT>FLASH

.bss :
{
. = ALIGN(4);
PROVIDE( _sbss = .);
*(.sbss*)
*(.gnu.linkonce.sb.*)
*(.bss*)
*(.gnu.linkonce.b.*)
*(COMMON*)
. = ALIGN(4);
PROVIDE( _ebss = .);
} >RAM AT>FLASH

PROVIDE( _end = _ebss);
PROVIDE( end = . );

.stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
{
PROVIDE( _heap_end = . );
. = ALIGN(4);
PROVIDE(_susrstack = . );
. = . + __stack_size;
PROVIDE( _eusrstack = .);
} >RAM

}
Loading

0 comments on commit b524159

Please sign in to comment.