update makefile to support iar build starting with stm32f070

This commit is contained in:
hathach 2023-01-17 16:20:04 +07:00
parent fa9d19027b
commit 3fee8b402e
5 changed files with 135 additions and 51 deletions

View File

@ -49,14 +49,31 @@ endif
#-------------- Cross Compiler ------------ #-------------- Cross Compiler ------------
# Can be set by board, default to ARM GCC # Can be set by board, default to ARM GCC
CROSS_COMPILE ?= arm-none-eabi- CROSS_COMPILE ?= arm-none-eabi-
# Allow for -Os to be changed by board makefiles in case -Os is not allowed # Allow for -Os to be changed by board makefiles in case -Os is not allowed
CFLAGS_OPTIMIZED ?= -Os CFLAGS_OPTIMIZED ?= -Os
CC = $(CROSS_COMPILE)gcc ifeq ($(CC),iccarm)
CXX = $(CROSS_COMPILE)g++ USE_IAR = 1
GDB = $(CROSS_COMPILE)gdb endif
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size ifdef USE_IAR
AS = iasmarm
LD = ilinkarm
OBJCOPY = ielftool
SIZE = echo "size not available for IAR"
else
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
AS = $(CC) -x assembler-with-cpp
LD = $(CC)
GDB = $(CROSS_COMPILE)gdb
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size
endif
MKDIR = mkdir MKDIR = mkdir
ifeq ($(CMDEXE),1) ifeq ($(CMDEXE),1)
@ -78,8 +95,8 @@ SRC_C += $(subst $(TOP)/,,$(wildcard $(TOP)/$(BOARD_PATH)/*.c))
INC += $(TOP)/$(FAMILY_PATH) INC += $(TOP)/$(FAMILY_PATH)
# Compiler Flags # GCC Compiler Flags
CFLAGS += \ GCC_CFLAGS += \
-ggdb \ -ggdb \
-fdata-sections \ -fdata-sections \
-ffunction-sections \ -ffunction-sections \
@ -110,13 +127,13 @@ CFLAGS += \
# conversion is too strict for most mcu driver, may be disable sign/int/arith-conversion # conversion is too strict for most mcu driver, may be disable sign/int/arith-conversion
# -Wconversion # -Wconversion
# Debugging/Optimization # Debugging/Optimization
ifeq ($(DEBUG), 1) ifeq ($(DEBUG), 1)
CFLAGS += -O0 GCC_CFLAGS += -O0
NO_LTO = 1 NO_LTO = 1
else else
CFLAGS += $(CFLAGS_OPTIMIZED) GCC_CFLAGS += $(CFLAGS_OPTIMIZED)
endif endif
# Log level is mapped to TUSB DEBUG option # Log level is mapped to TUSB DEBUG option

View File

@ -46,31 +46,34 @@ INC += $(TOP)/src
CFLAGS += $(addprefix -I,$(INC)) CFLAGS += $(addprefix -I,$(INC))
ifdef USE_IAR
IAR_CFLAGS += $(CFLAGS) -e --debug
IAR_LDFLAGS += --config $(TOP)/$(IAR_LD_FILE)
else
CFLAGS += $(GCC_CFLAGS)
# LTO makes it difficult to analyze map file for optimizing size purpose # LTO makes it difficult to analyze map file for optimizing size purpose
# We will run this option in ci # We will run this option in ci
ifeq ($(NO_LTO),1) ifeq ($(NO_LTO),1)
CFLAGS := $(filter-out -flto,$(CFLAGS)) CFLAGS := $(filter-out -flto,$(CFLAGS))
endif endif
ifneq ($(LD_FILE),) LDFLAGS += $(CFLAGS) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections
LDFLAGS_LD_FILE ?= -Wl,-T,$(TOP)/$(LD_FILE)
ifdef LD_FILE
LDFLAGS += -Wl,-T,$(TOP)/$(LD_FILE)
endif endif
LDFLAGS += $(CFLAGS) $(LDFLAGS_LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections
ifneq ($(SKIP_NANOLIB), 1) ifneq ($(SKIP_NANOLIB), 1)
LDFLAGS += -specs=nosys.specs -specs=nano.specs LDFLAGS += -specs=nosys.specs -specs=nano.specs
endif endif
ASFLAGS += $(CFLAGS) ASFLAGS += $(CFLAGS)
# Assembly files can be name with upper case .S, convert it to .s endif
SRC_S := $(SRC_S:.S=.s)
# Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
# assembly file should be placed first in linking order
# '_asm' suffix is added to object of assembly file
OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=_asm.o))
OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
# Verbose mode # Verbose mode
ifeq ("$(V)","1") ifeq ("$(V)","1")
@ -79,6 +82,21 @@ $(info LDFLAGS $(LDFLAGS)) $(info )
$(info ASFLAGS $(ASFLAGS)) $(info ) $(info ASFLAGS $(ASFLAGS)) $(info )
endif endif
# Assembly files can be name with upper case .S, convert it to .s
SRC_S := $(SRC_S:.S=.s)
IAR_SRC_S := $(IAR_SRC_S:.S=.s)
# Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
# assembly file should be placed first in linking order
# '_asm' suffix is added to object of assembly file
ifdef USE_IAR
OBJ += $(addprefix $(BUILD)/obj/, $(IAR_SRC_S:.s=_asm.o))
else
OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=_asm.o))
endif
OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
# --------------------------------------- # ---------------------------------------
# Rules # Rules
# --------------------------------------- # ---------------------------------------
@ -96,9 +114,15 @@ else
@$(MKDIR) -p $@ @$(MKDIR) -p $@
endif endif
$(BUILD)/$(PROJECT).elf: $(OBJ) # We set vpath to point to the top of the tree so that the source files
@echo LINK $@ # can be located. By following this scheme, it allows a single build rule
@$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group # to be used to compile all .c files.
vpath %.c . $(TOP)
vpath %.s . $(TOP)
vpath %.S . $(TOP)
ifndef USE_IAR
# GCC based compiler
$(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf $(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf
@echo CREATE $@ @echo CREATE $@
@ -108,6 +132,56 @@ $(BUILD)/$(PROJECT).hex: $(BUILD)/$(PROJECT).elf
@echo CREATE $@ @echo CREATE $@
@$(OBJCOPY) -O ihex $^ $@ @$(OBJCOPY) -O ihex $^ $@
$(BUILD)/$(PROJECT).elf: $(OBJ)
@echo LINK $@
@$(LD) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group
$(BUILD)/obj/%.o: %.c
@echo CC $(notdir $@)
@$(CC) $(CFLAGS) -c -MD -o $@ $<
# ASM sources lower case .s
$(BUILD)/obj/%_asm.o: %.s
@echo AS $(notdir $@)
@$(AS) $(ASFLAGS) -c -o $@ $<
# ASM sources upper case .S
$(BUILD)/obj/%_asm.o: %.S
@echo AS $(notdir $@)
@$(AS) $(ASFLAGS) -c -o $@ $<
else
# IAR Compiler
$(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf
@echo CREATE $@
@$(OBJCOPY) --bin $^ $@
$(BUILD)/$(PROJECT).hex: $(BUILD)/$(PROJECT).elf
@echo CREATE $@
@$(OBJCOPY) --ihex $^ $@
$(BUILD)/$(PROJECT).elf: $(OBJ)
@echo LINK $@
@$(LD) $(IAR_LDFLAGS) $^ -o $@
$(BUILD)/obj/%.o: %.c
@echo CC $(notdir $@)
@$(CC) $(IAR_CFLAGS) -c -o $@ $<
# ASM sources lower case .s
$(BUILD)/obj/%_asm.o: %.s
@echo AS $(notdir $@)
@$(AS) $(IAR_ASFLAGS) -c -o $@ $<
# ASM sources upper case .S
$(BUILD)/obj/%_asm.o: %.S
@echo AS $(notdir $@)
@$(AS) $(IAR_ASFLAGS) -c -o $@ $<
endif
# UF2 generation, iMXRT need to strip to text only before conversion # UF2 generation, iMXRT need to strip to text only before conversion
ifeq ($(FAMILY),imxrt) ifeq ($(FAMILY),imxrt)
$(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).elf $(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).elf
@ -122,27 +196,8 @@ endif
copy-artifact: $(BUILD)/$(PROJECT).bin $(BUILD)/$(PROJECT).hex $(BUILD)/$(PROJECT).uf2 copy-artifact: $(BUILD)/$(PROJECT).bin $(BUILD)/$(PROJECT).hex $(BUILD)/$(PROJECT).uf2
# We set vpath to point to the top of the tree so that the source files
# can be located. By following this scheme, it allows a single build rule
# to be used to compile all .c files.
vpath %.c . $(TOP)
$(BUILD)/obj/%.o: %.c
@echo CC $(notdir $@)
@$(CC) $(CFLAGS) -c -MD -o $@ $<
# ASM sources lower case .s
vpath %.s . $(TOP)
$(BUILD)/obj/%_asm.o: %.s
@echo AS $(notdir $@)
@$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
# ASM sources upper case .S
vpath %.S . $(TOP)
$(BUILD)/obj/%_asm.o: %.S
@echo AS $(notdir $@)
@$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
endif endif
# ---------------- GNU Make End -----------------------
.PHONY: clean .PHONY: clean
clean: clean:
@ -151,7 +206,6 @@ ifeq ($(CMDEXE),1)
else else
$(RM) -rf $(BUILD) $(RM) -rf $(BUILD)
endif endif
# ---------------- GNU Make End -----------------------
# get depenecies # get depenecies
.PHONY: get-deps .PHONY: get-deps

View File

@ -1,11 +1,13 @@
CFLAGS += -DSTM32F070xB -DCFG_EXAMPLE_VIDEO_READONLY CFLAGS += -DSTM32F070xB -DCFG_EXAMPLE_VIDEO_READONLY
SRC_S += $(ST_CMSIS)/Source/Templates/gcc/startup_stm32f070xb.s
LD_FILE = $(BOARD_PATH)/stm32F070rbtx_flash.ld LD_FILE = $(BOARD_PATH)/stm32F070rbtx_flash.ld
SRC_S += $(ST_CMSIS)/Source/Templates/gcc/startup_stm32f070xb.s IAR_SRC_S += $(ST_CMSIS)/Source/Templates/iar/startup_stm32f070xb.s
IAR_LD_FILE = $(ST_CMSIS)/Source/Templates/iar/linker/stm32f070xb_flash.icf
# For flash-jlink target # For flash-jlink target
JLINK_DEVICE = stm32f070rb JLINK_DEVICE = stm32f070rb
# flash target using on-board stlink # flash target using on-board stlink
flash: flash-stlink flash: flash-stlink

View File

@ -153,7 +153,7 @@ uint32_t board_millis(void)
void HardFault_Handler (void) void HardFault_Handler (void)
{ {
asm("bkpt"); __asm("BKPT #0\n");
} }
#ifdef USE_FULL_ASSERT #ifdef USE_FULL_ASSERT

View File

@ -8,17 +8,28 @@ ST_HAL_DRIVER = hw/mcu/st/stm32$(ST_FAMILY)xx_hal_driver
include $(TOP)/$(BOARD_PATH)/board.mk include $(TOP)/$(BOARD_PATH)/board.mk
CFLAGS += \ CFLAGS += \
-DCFG_EXAMPLE_MSC_READONLY \
-DCFG_TUSB_MCU=OPT_MCU_STM32F0
# --------------
# GCC Flags
# --------------
GCC_CFLAGS += \
-flto \ -flto \
-mthumb \ -mthumb \
-mabi=aapcs \ -mabi=aapcs \
-mcpu=cortex-m0 \ -mcpu=cortex-m0 \
-mfloat-abi=soft \ -mfloat-abi=soft \
-nostdlib -nostartfiles \ -nostdlib -nostartfiles \
-DCFG_EXAMPLE_MSC_READONLY \
-DCFG_TUSB_MCU=OPT_MCU_STM32F0
# suppress warning caused by vendor mcu driver # suppress warning caused by vendor mcu driver
CFLAGS += -Wno-error=unused-parameter -Wno-error=cast-align -Wno-error=cast-qual GCC_CFLAGS += -Wno-error=unused-parameter -Wno-error=cast-align -Wno-error=cast-qual
# --------------
# IAR Flags
# --------------
IAR_CFLAGS += --cpu cortex-m0
IAR_ASFLAGS += --cpu cortex-m0
SRC_C += \ SRC_C += \
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \ src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \