diff --git a/examples/make.mk b/examples/make.mk index 73a39a8fe..dd7a5cf4e 100644 --- a/examples/make.mk +++ b/examples/make.mk @@ -49,14 +49,31 @@ endif #-------------- Cross Compiler ------------ # Can be set by board, default to ARM GCC CROSS_COMPILE ?= arm-none-eabi- + # Allow for -Os to be changed by board makefiles in case -Os is not allowed CFLAGS_OPTIMIZED ?= -Os -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ -GDB = $(CROSS_COMPILE)gdb -OBJCOPY = $(CROSS_COMPILE)objcopy -SIZE = $(CROSS_COMPILE)size +ifeq ($(CC),iccarm) +USE_IAR = 1 +endif + +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 ifeq ($(CMDEXE),1) @@ -78,8 +95,8 @@ SRC_C += $(subst $(TOP)/,,$(wildcard $(TOP)/$(BOARD_PATH)/*.c)) INC += $(TOP)/$(FAMILY_PATH) -# Compiler Flags -CFLAGS += \ +# GCC Compiler Flags +GCC_CFLAGS += \ -ggdb \ -fdata-sections \ -ffunction-sections \ @@ -110,13 +127,13 @@ CFLAGS += \ # conversion is too strict for most mcu driver, may be disable sign/int/arith-conversion # -Wconversion - + # Debugging/Optimization ifeq ($(DEBUG), 1) - CFLAGS += -O0 + GCC_CFLAGS += -O0 NO_LTO = 1 else - CFLAGS += $(CFLAGS_OPTIMIZED) + GCC_CFLAGS += $(CFLAGS_OPTIMIZED) endif # Log level is mapped to TUSB DEBUG option diff --git a/examples/rules.mk b/examples/rules.mk index c3134056a..2c4df85ba 100644 --- a/examples/rules.mk +++ b/examples/rules.mk @@ -46,31 +46,34 @@ INC += $(TOP)/src 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 # We will run this option in ci ifeq ($(NO_LTO),1) CFLAGS := $(filter-out -flto,$(CFLAGS)) endif -ifneq ($(LD_FILE),) -LDFLAGS_LD_FILE ?= -Wl,-T,$(TOP)/$(LD_FILE) +LDFLAGS += $(CFLAGS) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections + +ifdef LD_FILE +LDFLAGS += -Wl,-T,$(TOP)/$(LD_FILE) endif -LDFLAGS += $(CFLAGS) $(LDFLAGS_LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections ifneq ($(SKIP_NANOLIB), 1) LDFLAGS += -specs=nosys.specs -specs=nano.specs endif ASFLAGS += $(CFLAGS) -# Assembly files can be name with upper case .S, convert it to .s -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)) +endif # Verbose mode ifeq ("$(V)","1") @@ -79,6 +82,21 @@ $(info LDFLAGS $(LDFLAGS)) $(info ) $(info ASFLAGS $(ASFLAGS)) $(info ) 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 # --------------------------------------- @@ -96,9 +114,15 @@ else @$(MKDIR) -p $@ endif -$(BUILD)/$(PROJECT).elf: $(OBJ) - @echo LINK $@ - @$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(LIBS) -Wl,--end-group +# 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) +vpath %.s . $(TOP) +vpath %.S . $(TOP) + +ifndef USE_IAR +# GCC based compiler $(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf @echo CREATE $@ @@ -108,6 +132,56 @@ $(BUILD)/$(PROJECT).hex: $(BUILD)/$(PROJECT).elf @echo CREATE $@ @$(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 ifeq ($(FAMILY),imxrt) $(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).elf @@ -122,27 +196,8 @@ endif 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 +# ---------------- GNU Make End ----------------------- .PHONY: clean clean: @@ -151,7 +206,6 @@ ifeq ($(CMDEXE),1) else $(RM) -rf $(BUILD) endif -# ---------------- GNU Make End ----------------------- # get depenecies .PHONY: get-deps diff --git a/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.mk b/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.mk index 7c0ee40b6..242b5a034 100644 --- a/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.mk +++ b/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.mk @@ -1,11 +1,13 @@ CFLAGS += -DSTM32F070xB -DCFG_EXAMPLE_VIDEO_READONLY +SRC_S += $(ST_CMSIS)/Source/Templates/gcc/startup_stm32f070xb.s 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 JLINK_DEVICE = stm32f070rb # flash target using on-board stlink -flash: flash-stlink +flash: flash-stlink \ No newline at end of file diff --git a/hw/bsp/stm32f0/family.c b/hw/bsp/stm32f0/family.c index 8de3147e2..806c4af51 100644 --- a/hw/bsp/stm32f0/family.c +++ b/hw/bsp/stm32f0/family.c @@ -153,7 +153,7 @@ uint32_t board_millis(void) void HardFault_Handler (void) { - asm("bkpt"); + __asm("BKPT #0\n"); } #ifdef USE_FULL_ASSERT diff --git a/hw/bsp/stm32f0/family.mk b/hw/bsp/stm32f0/family.mk index 39831e154..a81ab426f 100644 --- a/hw/bsp/stm32f0/family.mk +++ b/hw/bsp/stm32f0/family.mk @@ -8,17 +8,28 @@ ST_HAL_DRIVER = hw/mcu/st/stm32$(ST_FAMILY)xx_hal_driver include $(TOP)/$(BOARD_PATH)/board.mk CFLAGS += \ + -DCFG_EXAMPLE_MSC_READONLY \ + -DCFG_TUSB_MCU=OPT_MCU_STM32F0 + +# -------------- +# GCC Flags +# -------------- +GCC_CFLAGS += \ -flto \ -mthumb \ -mabi=aapcs \ -mcpu=cortex-m0 \ -mfloat-abi=soft \ -nostdlib -nostartfiles \ - -DCFG_EXAMPLE_MSC_READONLY \ - -DCFG_TUSB_MCU=OPT_MCU_STM32F0 # 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/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \