Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common bootloader implementation #54

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/CI_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
run: |
make arm_sdk_install
make -j8
make bootloaders -j8

- name: Archive build
uses: actions/upload-artifact@v3
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/CI_build_macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI Build MacOS

on: [push, pull_request]

jobs:
build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Build CI
run: |
make arm_sdk_install
make -j8
make bootloaders -j8

- name: Archive build
uses: actions/upload-artifact@v3
with:
name: AM32-binaries
path: |
obj/*.hex
retention-days: 7
1 change: 1 addition & 0 deletions .github/workflows/CI_build_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
run: |
make arm_sdk_install
tools/windows/make/bin/make
tools/windows/make/bin/make bootloaders

- name: Archive build
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion Inc/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
#define TARGET_VOLTAGE_DIVIDER 94
#define MILLIVOLT_PER_AMP 100
#define USE_SERIAL_TELEMETRY
#define EEPROM_START_ADD (uint32_t)0x0801F800
#define EEPROM_START_ADD (uint32_t)0x0800F800
#endif

#ifdef REF_L431
Expand Down
3 changes: 3 additions & 0 deletions Inc/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*/
#define VERSION_MAJOR 2
#define VERSION_MINOR 16

// update this when bootloader build changes
#define BOOTLOADER_VERSION 12
164 changes: 94 additions & 70 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
# include the rules for OS independence
include $(ROOT)/make/tools.mk

# Include processor specific makefiles
include f051makefile.mk
include g071makefile.mk
include f031makefile.mk
include f421makefile.mk
include e230makefile.mk
include f415makefile.mk
include gd32makefile.mk

# Default MCU type to F051
MCU_TYPE ?= F051
# supported MCU types
MCU_TYPES := E230 F031 F051 F415 F421 G071 L431
MCU_TYPE := NONE

# MCU types that we build a bootloader for - this should be $(MCU_TYPES) in the future
# when all bootloader porting is completed
BL_MCU_TYPES := E230 F031 F051 F415 F421 G071 L431

# Function to include makefile for each MCU type
define INCLUDE_MCU_MAKEFILES
$(foreach MCU_TYPE,$(MCU_TYPES),$(eval include $(call lc,$(MCU_TYPE))makefile.mk))
endef
$(call INCLUDE_MCU_MAKEFILES)

# additional libs
LIBS := -lnosys
Expand All @@ -44,100 +46,122 @@ VERSION_MINOR := $(shell $(FGREP) "define VERSION_MINOR" $(MAIN_INC_DIR)/version
FIRMWARE_VERSION := $(VERSION_MAJOR).$(VERSION_MINOR)

# Compiler options
CFLAGS_COMMON := -DUSE_MAKE -fsingle-precision-constant -fomit-frame-pointer -ffast-math
CFLAGS_COMMON += -I$(MAIN_INC_DIR) -g -O3 -Wall -ffunction-sections
CFLAGS_COMMON += -D$(TARGET)

CFLAGS_BASE := -DUSE_MAKE -fsingle-precision-constant -fomit-frame-pointer -ffast-math
CFLAGS_BASE += -I$(MAIN_INC_DIR) -g3 -O2 -Wall -ffunction-sections

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]))

TARGET_FNAME = $(IDENTIFIER)_$(TARGET)_$(FIRMWARE_VERSION)
TARGET_BASENAME = $(BIN_DIR)/$(TARGET_FNAME)

# configure some directories that are relative to wherever ROOT_DIR is located
OBJ := obj
BIN_DIR := $(ROOT)/$(OBJ)

TOOLS_DIR ?= $(ROOT)/tools
DL_DIR := $(ROOT)/downloads

.PHONY : clean all binary f051 g071 f031 e230 f421 f415
ALL_TARGETS := $(TARGETS_F051) $(TARGETS_G071) $(TARGETS_F031) $(TARGETS_E230) $(TARGETS_F421) $(TARGETS_F415)
.PHONY : clean all binary $(foreach MCU,$(MCU_TYPES),$(call lc,$(MCU)))
ALL_TARGETS := $(foreach MCU,$(MCU_TYPES),$(TARGETS_$(MCU)))
all : $(ALL_TARGETS)
f051 : $(TARGETS_F051)
g071 : $(TARGETS_G071)
f031 : $(TARGETS_F031)
e230 : $(TARGETS_E230)
f421 : $(TARGETS_F421)
f415 : $(TARGETS_F415)

# create targets for compiling one mcu type, eg "make f421"
define CREATE_TARGET
$(call lc,$(1)) : $$(TARGETS_$(1))
endef
$(foreach MCU,$(MCU_TYPES),$(eval $(call CREATE_TARGET,$(MCU))))

clean :
@echo Removing $(OBJ) directory
@$(RM) -rf $(OBJ)

# lowercase version of MCU_TYPE
MCU_LOWER = $(call lc,$(MCU_TYPE))
#####################
# main firmware build
define CREATE_BUILD_TARGET
$(2)_BASENAME = $(BIN_DIR)/$(IDENTIFIER)_$(2)_$(FIRMWARE_VERSION)

binary : $(TARGET_BASENAME).bin
$(2) : $$($(2)_BASENAME).bin

# Generate bin and hex files from elf
$$($(2)_BASENAME).bin: $$($(2)_BASENAME).elf
echo building BIN $$@
@$(ECHO) Generating $$(notdir $$@)
$(QUIET)$(OBJCOPY) -O binary $$(<) $$@
$(QUIET)$(OBJCOPY) $$(<) -O ihex $$(@:.bin=.hex)

CFLAGS_$(2) = $(MCU_$(1)) -D$(2) $(CFLAGS_$(1)) $(CFLAGS_COMMON)
LDFLAGS_$(2) = $(LDFLAGS_COMMON) $(LDFLAGS_$(1)) -T$(LDSCRIPT_$(1))

-include $$($(2)_BASENAME).d

$$($(2)_BASENAME).elf: $(SRC_COMMON) $$(SRC_$(1))
@$(ECHO) Compiling $$(notdir $$@)
$(QUIET)$(MKDIR) -p $(OBJ)
$(QUIET)$(CC) $$(CFLAGS_$(2)) $$(LDFLAGS_$(2)) -MMD -MP -MF $$(@:.elf=.d) -o $$(@) $(SRC_COMMON) $$(SRC_$(1))
# we copy debug.elf to give us a constant debug target for vscode
# this means the debug button will always debug the last target built
@$(CP) -f $(OBJ)$(DSEP)$(TARGET_FNAME).elf $(OBJ)$(DSEP)debug.elf > $(NUL)
@$(CP) -f $$(@) $(OBJ)$(DSEP)debug.elf > $(NUL)
# also copy the openocd.cfg from the MCU directory to obj/openocd.cfg for auto config of Cortex-Debug
# in vscode
@$(CP) -f Mcu$(DSEP)$(MCU_LOWER)$(DSEP)openocd.cfg $(OBJ)$(DSEP)openocd.cfg > $(NUL)
@$(ECHO) done $(TARGET)
@$(CP) -f Mcu$(DSEP)$(call lc,$(1))$(DSEP)openocd.cfg $(OBJ)$(DSEP)openocd.cfg > $(NUL)
endef
$(foreach MCU,$(MCU_TYPES),$(foreach TARGET,$(TARGETS_$(MCU)), $(eval $(call CREATE_BUILD_TARGET,$(MCU),$(TARGET)))))

##################
# bootloader build
BOOTLOADER_VERSION := $(shell $(FGREP) "define BOOTLOADER_VERSION" $(MAIN_INC_DIR)/version.h | $(CUT) -d" " -f3 )

$(TARGETS_F051) :
@$(MAKE) -s MCU_TYPE=F051 TARGET=$@ binary
# we support bootloader comms on a list of possible pins
BOOTLOADER_PINS = PB4 PA2 PA15

$(TARGETS_G071) :
@$(MAKE) -s MCU_TYPE=G071 TARGET=$@ binary
SRC_BL := $(foreach dir,bootloader,$(wildcard $(dir)/*.[cs]))
LDSCRIPT_BL := bootloader/ldscript_bl.ld

$(TARGETS_F031) :
@$(MAKE) -s MCU_TYPE=F031 TARGET=$@ binary
# bootloader target names for example "make AM32_F421_BOOTLOADER"
define BOOTLOADER_BASENAME
$(IDENTIFIER)_$(1)_BOOTLOADER_$(2)
endef

$(TARGETS_E230) :
@$(MAKE) -s MCU_TYPE=E230 TARGET=$@ binary
# bootloader target names with version for filename
define BOOTLOADER_BASENAME_VER
$(call BOOTLOADER_BASENAME,$(1),$(2)_V$(BOOTLOADER_VERSION))
endef

$(TARGETS_F421) :
@$(MAKE) -s MCU_TYPE=F421 TARGET=$@ binary
define CREATE_BOOTLOADER_TARGET

$(TARGETS_F415) :
@$(MAKE) -s MCU_TYPE=F415 TARGET=$@ binary
-include $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).d

# Compile target
$(TARGET_BASENAME).elf: CFLAGS := $(MCU_$(MCU_TYPE)) $(CFLAGS_$(MCU_TYPE)) $(CFLAGS_COMMON)
$(TARGET_BASENAME).elf: LDFLAGS := $(LDFLAGS_COMMON) $(LDFLAGS_$(MCU_TYPE)) -T$(LDSCRIPT_$(MCU_TYPE))
$(TARGET_BASENAME).elf: $(SRC_COMMON) $(SRC_$(MCU_TYPE))
@$(ECHO) Compiling $(notdir $@)
$(QUIRT)$(MKDIR) -p $(OBJ)
$(QUIET)$(CC) $(CFLAGS) $(LDFLAGS) -MMD -MP -MF $(@:.elf=.d) -o $(@) $(SRC_COMMON) $(SRC_$(MCU_TYPE))
$(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).elf: CFLAGS_BL := $$(MCU_$(1)) $$(CFLAGS_$(1)) $$(CFLAGS_BASE) -DBOOTLOADER -DUSE_$(2)
$(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).elf: LDFLAGS_BL := $$(LDFLAGS_COMMON) $$(LDFLAGS_$(1)) -T$$(LDSCRIPT_BL)
$(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).elf: $$(SRC_$(1)_BL) $$(SRC_BL)
$$(QUIET)echo building bootloader for $(1) with pin $(2)
$$(QUIET)$$(MKDIR) -p $(OBJ)
$$(QUIET)echo Compiling $(notdir $$@)
$$(QUIET)$$(CC) $$(CFLAGS_BL) $$(LDFLAGS_BL) -MMD -MP -MF $$(@:.elf=.d) -o $$(@) $$(SRC_$(1)_BL) $$(SRC_BL) -Os
$$(QUIET)$$(CP) -f $$@ $$(OBJ)$$(DSEP)debug.elf
$$(QUIET)$$(CP) -f Mcu$(DSEP)$(call lc,$(1))$(DSEP)openocd.cfg $$(OBJ)$$(DSEP)openocd.cfg > $$(NUL)

# Generate bin and hex files
$(TARGET_BASENAME).bin: $(TARGET_BASENAME).elf
@$(ECHO) Generating $(notdir $@)
$(QUIET)$(OBJCOPY) -O binary $(<) $@
$(QUIET)$(OBJCOPY) $(<) -O ihex $(@:.bin=.hex)
$(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).hex: $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).elf
$$(QUIET)echo Generating $(notdir $$@)
$$(QUIET)$$(OBJCOPY) -O binary $$(<) $$(@:.hex=.bin)
$$(QUIET)$$(OBJCOPY) $$(<) -O ihex $$(@:.bin=.hex)

$(call BOOTLOADER_BASENAME,$(1),$(2)): $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).hex
endef
$(foreach MCU,$(MCU_TYPES),$(foreach PIN,$(BOOTLOADER_PINS),$(eval $(call CREATE_BOOTLOADER_TARGET,$(MCU),$(PIN)))))

# mkdirs
$(DL_DIR):
$(QUIET)$(MKDIR) -p $@
ALL_BOOTLOADERS := $(foreach MCU,$(BL_MCU_TYPES),$(foreach PIN,$(BOOTLOADER_PINS),$(call BOOTLOADER_BASENAME,$(MCU),$(PIN))))

$(TOOLS_DIR):
$(QUIET)$(MKDIR) -p $@
bootloaders: $(ALL_BOOTLOADERS)

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

# useful target to list all of the board targets so you can see what
# make target to use for your board
targets:
$(QUIET)echo Targets for each MCU. To build a target use 'make TARGETNAME'
$(QUIET)echo F051 Targets: $(TARGETS_F051)
$(QUIET)echo G071 Targets: $(TARGETS_G071)
$(QUIET)echo F031 Targets: $(TARGETS_F031)
$(QUIET)echo E230 Targets: $(TARGETS_E230)
$(QUIET)echo F421 Targets: $(TARGETS_F421)
$(QUIET)echo F415 Targets: $(TARGETS_F415)
$(QUIET)echo GD32 Targets: $(TARGETS_GD32)
$(QUIET)echo List of targets. To build a target use 'make TARGETNAME'
$(QUIET)echo $(ALL_TARGETS)
$(QUIET)echo Bootloaders: $(ALL_BOOTLOADERS)
4 changes: 3 additions & 1 deletion Mcu/e230/GD32E230K8_FLASH.ld
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08001000, LENGTH = 27K
FLASH_VERSION (rx) : ORIGIN = 0x08007C00 - 48, LENGTH = 14
FLASH_VERSION (rx) : ORIGIN = 0x08007C00 - 48, LENGTH = 16
FILE_NAME (rx) : ORIGIN = 0x08007C00 - 32, LENGTH = 32
EEPROM (rx) : ORIGIN = 0x08007C00, LENGTH = 1K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K
Expand Down Expand Up @@ -88,13 +88,15 @@ SECTIONS
{
. = ALIGN(4);
KEEP (*(.firmware_info))
. = ALIGN(4);
} >FLASH_VERSION

/* The file name */
.file_name :
{
. = ALIGN(4);
KEEP (*(.file_name))
. = ALIGN(4);
} >FILE_NAME


Expand Down
Loading
Loading