diff --git a/example/boot2/CMakeLists.txt b/example/boot2/CMakeLists.txt new file mode 100644 index 0000000..d154bdc --- /dev/null +++ b/example/boot2/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.10) + +include(proj.conf) + +project(${EXAMPLE_NAME} VERSION 0.1) +add_executable(${EXAMPLE_NAME}.elf main.c) + +enable_language(ASM) +target_sources(${EXAMPLE_NAME}.elf PUBLIC boot2_pre.S) + +add_subdirectory(${SDK_BASE_DIR} sdk) +target_link_libraries(${EXAMPLE_NAME}.elf sdk) + + diff --git a/example/boot2/Makefile b/example/boot2/Makefile new file mode 100644 index 0000000..d383a42 --- /dev/null +++ b/example/boot2/Makefile @@ -0,0 +1,20 @@ +EXAMPLE_BASE_DIR ?= $(shell realpath .) +EXAMPLE_NAME := $(notdir $(patsubst %/,%,$(CURDIR))) +SDK_BASE_DIR ?= $(shell realpath ./../..) + +export SDK_BASE_DIR +export EXAMPLE_NAME +export EXAMPLE_BASE_DIR + +GCC_PATH := $(shell which arm-none-eabi-gcc) +CROSS_COMPILE := $(patsubst %gcc,%,$(GCC_PATH)) +ifeq ($(GCC_PATH),) +$(error arm-none-eabi-gcc not found in PATH. Please install the ARM toolchain.) +endif + +# add custom cmake definition +#cmake_definition+=-Dxxx=sss + +LDFLAGS += -T$(EXAMPLE_BASE_DIR)/flash.ld + +include $(SDK_BASE_DIR)/project.build diff --git a/example/boot2/boot2_pre.S b/example/boot2/boot2_pre.S new file mode 100644 index 0000000..1910ca1 --- /dev/null +++ b/example/boot2/boot2_pre.S @@ -0,0 +1,21 @@ +.syntax unified +.cpu cortex-m0plus +.thumb + +.global boot2_pre + +.section .text.boot2_pre +.type boot2_pre, %function +boot2_pre: + b boot2_copy_self + .word 0x11223344 + .word 0xabcdef58 + + + +boot2_copy_self: + + + bl main + +.size .boot2_pre, .-boot2_pre diff --git a/example/boot2/flash.ld b/example/boot2/flash.ld new file mode 100644 index 0000000..dc5ade1 --- /dev/null +++ b/example/boot2/flash.ld @@ -0,0 +1,62 @@ +_start = ORIGIN(FLASH); +ENTRY(_start) + +MEMORY +{ + FLASH (xrx) :ORIGIN = 0x10000000, LENGTH = 256K + RAM (xrw) :ORIGIN = 0x20000000, LENGTH = 256K + STACK (rw) :ORIGIN = 0x20040000, LENGTH = 8K - 256 + AUTO(xrx) :ORIGIN = 0x20041F00, LENGTH = 256 +} + +_stack_top = ORIGIN(STACK) + LENGTH(STACK); + +PHDRS +{ + boot2_pre PT_LOAD FLAGS(5); /* R + X */ + text PT_LOAD FLAGS(5); /* R + X */ + rodata PT_LOAD FLAGS(5); /* R + W */ + data PT_LOAD FLAGS(6); /* R + W */ + bss PT_LOAD FLAGS(6); /* R + W */ +} + +SECTIONS +{ + .boot2_pre : + { + KEEP(*(.text.boot2_pre)) + _boot2_pre_size = . - ADDR(.boot2_pre); + ASSERT(_boot2_pre_size <= 252, "Error: .boot2_pre size exceeds 252 bytes!"); + . += 256 - _boot2_pre_size; + } >AUTO AT > FLASH :boot2_pre + + .text : + { + . = ALIGN(4); + *(.text*) + . = ALIGN(4); + } >RAM AT > FLASH :text + + .rodata : + { + . = ALIGN(4); + *(.rodata*) + . = ALIGN(4); + } >RAM AT > FLASH :rodata + + .data : + { + . = ALIGN(4); + *(.data*) + . = ALIGN(4); + } >RAM AT > FLASH :data + + .bss (NOLOAD) : + { + . = ALIGN(4); + _bss_start = .; + *(.bss*) + . = ALIGN(4); + _bss_end = .; + } >RAM :bss +} diff --git a/example/boot2/main.c b/example/boot2/main.c new file mode 100644 index 0000000..43df275 --- /dev/null +++ b/example/boot2/main.c @@ -0,0 +1,4 @@ +int main(void) +{ + return 0; +} diff --git a/example/boot2/proj.conf b/example/boot2/proj.conf new file mode 100644 index 0000000..7d2ef31 --- /dev/null +++ b/example/boot2/proj.conf @@ -0,0 +1 @@ +# set(CONFIG_COMPONENT1 1) diff --git a/project.build b/project.build index b5581a1..b3fba9b 100644 --- a/project.build +++ b/project.build @@ -11,6 +11,9 @@ CFLAGS += -ffunction-sections CFLAGS += -fdata-sections CFLAGS += -Wpointer-arith +ifneq ($(findstring -T, $(LDFLAGS)), -T) +LDFLAGS += -T$(SDK_BASE_DIR)/ram.ld +endif LDFLAGS += -mcpu=cortex-m0plus LDFLAGS += -mthumb LDFLAGS += -g3 @@ -18,11 +21,11 @@ LDFLAGS += -O2 LDFLAGS += -Wall LDFLAGS += -Wl,--gc-sections LDFLAGS += -nostartfiles -LDFLAGS += -T$(SDK_BASE_DIR)/ram.ld LDFLAGS += -Wl,-Map=$(EXAMPLE_NAME).map LDFLAGS += -Wl,--print-memory-usage CMAKE := cmake +ELF2UF2 := $(SDK_BASE_DIR)/tools/elf2uf2 # The command to remove a file. RM = $(CMAKE) -E remove_directory @@ -55,8 +58,10 @@ build:Makefile $(MAKE) -C build -j $(CROSS_COMPILE)objcopy -O binary $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).bin $(CROSS_COMPILE)objdump -d -S $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).asm + $(CROSS_COMPILE)nm $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).nm + $(ELF2UF2) $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).uf2 size $(FINAL_NAME_PRE).elf - sha256sum $(FINAL_NAME_PRE).elf + sha256sum $(FINAL_NAME_PRE).bin clean:: $(RM) build diff --git a/tools/elf2uf2 b/tools/elf2uf2 new file mode 100755 index 0000000..104e879 Binary files /dev/null and b/tools/elf2uf2 differ