SOURCES := main.c mspdebug_wrapper.c spi_flash.c SOURCES += mspdebug/drivers/jtaglib.c BUILDDIR ?= build BINARY := safetyreset LDSCRIPT := stm32f407.ld LIBNAME := opencm3_stm32f4 OPENCM3_DIR ?= libopencm3 PREFIX ?= arm-none-eabi- CC := $(PREFIX)gcc CXX := $(PREFIX)g++ LD := $(PREFIX)gcc AR := $(PREFIX)ar AS := $(PREFIX)as OBJCOPY := $(PREFIX)objcopy OBJDUMP := $(PREFIX)objdump GDB := $(PREFIX)gdb CFLAGS += -I$(OPENCM3_DIR)/include -Imspdebug/util -Imspdebug/drivers CFLAGS += -Os -std=gnu11 -g -DSTM32F4 # Note: libopencm3 requires some standard libc definitions from stdint.h and stdbool.h, so we don't pass -nostdinc here. CFLAGS += -nostdlib -ffreestanding CFLAGS += -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration -Wundef CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes CFLAGS += -fno-common -ffunction-sections -fdata-sections LDFLAGS += --static -nostartfiles LDFLAGS += -T$(LDSCRIPT) LDFLAGS += -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g LDFLAGS += -Wl,--cre #LDFLAGS += -Wl,--gc-sections LDFLAGS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group LDFLAGS += -L$(OPENCM3_DIR)/lib LDFLAGS += -l$(LIBNAME) $(shell $(CC) -print-libgcc-file-name) all: elf elf: $(BUILDDIR)/$(BINARY).elf bin: $(BUILDDIR)/$(BINARY).bin hex: $(BUILDDIR)/$(BINARY).hex srec: $(BUILDDIR)/$(BINARY).srec list: $(BUILDDIR)/$(BINARY).list images: $(BUILDDIR)/$(BINARY).images $(OPENCM3_DIR)/lib/lib$(LIBNAME).a: echo "Warning, $@ not found, attempting to rebuild in $(OPENCM3_DIR)" $(MAKE) -C $(OPENCM3_DIR) OBJS := $(addprefix $(BUILDDIR)/,$(SOURCES:%.c=%.o)) # Define a helper macro for debugging make errors online # you can type "make print-OPENCM3_DIR" and it will show you # how that ended up being resolved by all of the included # makefiles. print-%: @echo $*=$($*) $(BUILDDIR)/%.images: $(BUILDDIR)/%.bin $(BUILDDIR)/%.hex $(BUILDDIR)/%.srec $(BUILDDIR)/%.list $(BUILDDIR)/%.map $(BUILDDIR)/%.bin: $(BUILDDIR)/%.elf $(OBJCOPY) -Obinary $< $@ $(BUILDDIR)/%.hex: $(BUILDDIR)/%.elf $(OBJCOPY) -Oihex $< $@ $(BUILDDIR)/%.srec: $(BUILDDIR)/%.elf $(OBJCOPY) -Osrec $< $@ $(BUILDDIR)/%.list: $(BUILDDIR)/%.elf $(OBJDUMP) -S $< > $@ $(BUILDDIR)/%.elf: $(OBJS) $(LDSCRIPT) $(OPENCM3_DIR)/lib/lib$(LIBNAME).a $(LD) $(OBJS) $(LDFLAGS) -o $@ -Wl,-Map=$*.map $(BUILDDIR)/%.o: %.c mkdir -p $(dir $@) $(CC) $(CFLAGS) -o $@ -c $< clean: -rm -r $(BUILDDIR) .PHONY: images clean elf bin hex srec list -include $(OBJS:.o=.d)