## ## This file is part of the libopencm3 project. ## ## Copyright (C) 2009 Uwe Hermann ## Copyright (C) 2010 Piotr Esden-Tempski ## Copyright (C) 2013 Frantisek Burian ## ## This library is free software: you can redistribute it and/or modify ## it under the terms of the GNU Lesser General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. ## ## This library is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU Lesser General Public License for more details. ## ## You should have received a copy of the GNU Lesser General Public License ## along with this library. If not, see . ## # Be silent per default, but 'make V=1' will show all compiler calls. ifneq ($(V),1) Q := @ NULL := 2>/dev/null endif PREFIX ?= arm-none-eabi CC = $(PREFIX)-gcc CPP = $(PREFIX)-g++ LD = $(PREFIX)-gcc OBJCOPY = $(PREFIX)-objcopy OBJDUMP = $(PREFIX)-objdump GDB = $(PREFIX)-gdb STFLASH = $(shell which st-flash) LDSCRIPT ?= $(BINARY).ld OBJS += $(BINARY).o TOOLCHAIN_DIR ?= ../../../../../libopencm3 ifeq ($(wildcard $(TOOLCHAIN_DIR)/lib/lib$(LIBNAME).a),) ifneq ($(strip $(shell which $(CC))),) TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) endif else ifeq ($(V),1) $(info Building the example in the source directory. Using local library!) endif endif INCLUDE_DIR = $(TOOLCHAIN_DIR)/include LIB_DIR = $(TOOLCHAIN_DIR)/lib SCRIPT_DIR = $(TOOLCHAIN_DIR)/share/libopencm3/scripts ifneq ($(OPENCM3_DIR),) INCLUDE_DIR = $(OPENCM3_DIR)/include LIB_DIR = $(OPENCM3_DIR)/lib SCRIPT_DIR = $(OPENCM3_DIR)/scripts endif # C flags CFLAGS += -Os -g $(ARCH_FLAGS) -MD $(DEFS) CFLAGS += -Wall -Wextra -Wimplicit-function-declaration CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes CFLAGS += -Wundef -Wshadow -fno-common CFLAGS += -I$(INCLUDE_DIR) # C++ flags CPPFLAGS += -Os -g $(ARCH_FLAGS) -MD $(DEFS) CPPFLAGS += -Wall -Wextra -Wimplicit-function-declaration CPPFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes CPPFLAGS += -Wundef -Wshadow -fno-common CPPFLAGS += -I$(INCLUDE_DIR) # Linker flags LDFLAGS += --static -nostartfiles $(ARCH_FLAGS) -l$(LIBNAME) LDFLAGS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group LDFLAGS += -Wl,--gc-sections LDFLAGS += -L$(LIB_DIR) LDFLAGS += -T$(LDSCRIPT) ifeq ($(V),1) LDFLAGS += -Wl,--print-gc-sections endif .SUFFIXES: .elf .bin .hex .srec .list .images .SECONDEXPANSION: .SECONDARY: all: images images: $(BINARY).images flash: $(BINARY).flash %.images: %.bin %.hex %.srec %.list @#printf "*** $* images generated ***\n" %.bin: %.elf @#printf " OBJCOPY $(*).bin\n" $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin %.hex: %.elf @#printf " OBJCOPY $(*).hex\n" $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex %.srec: %.elf @#printf " OBJCOPY $(*).srec\n" $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec %.list: %.elf @#printf " OBJDUMP $(*).list\n" $(Q)$(OBJDUMP) -S $(*).elf > $(*).list %.elf: $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a @#printf " LD $(*).elf\n" $(Q)$(LD) -o $(*).elf $(OBJS) $(LDFLAGS) %.o: %.c @#printf " CC $(*).c\n" $(Q)$(CC) $(CFLAGS) -o $(*).o -c $(*).c %.o: %.cpp @#printf " CPP $(*).cpp\n" $(Q)$(CPP) $(CPPFLAGS) -o $(*).o -c $(*).cpp clean: @#printf " CLEAN\n" $(Q)rm -f *.o $(Q)rm -f *.d $(Q)rm -f *.elf $(Q)rm -f *.bin $(Q)rm -f *.hex $(Q)rm -f *.srec $(Q)rm -f *.list %.stlink-flash: %.bin @printf " FLASH $<\n" $(Q)$(STFLASH) write $(*).bin 0x8000000 ifeq ($(STLINK_PORT),) ifeq ($(BMP_PORT),) ifeq ($(OOCD_SERIAL),) %.flash: %.hex @printf " FLASH $<\n" @# IMPORTANT: Don't use "resume", only "reset" will work correctly! $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ -f board/$(OOCD_BOARD).cfg \ -c "init" -c "reset init" \ -c "stm32f1x mass_erase 0" \ -c "flash write_image $(*).hex" \ -c "reset" \ -c "shutdown" $(NULL) else %.flash: %.hex @printf " FLASH $<\n" @# IMPORTANT: Don't use "resume", only "reset" will work correctly! $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ -f board/$(OOCD_BOARD).cfg \ -c "ft2232_serial $(OOCD_SERIAL)" \ -c "init" -c "reset init" \ -c "stm32f1x mass_erase 0" \ -c "flash write_image $(*).hex" \ -c "reset" \ -c "shutdown" $(NULL) endif else %.flash: %.elf @printf " GDB $(*).elf (flash)\n" $(Q)$(GDB) --batch \ -ex 'target extended-remote $(BMP_PORT)' \ -x $(SCRIPT_DIR)/black_magic_probe_flash.scr \ $(*).elf endif else %.flash: %.elf @printf " GDB $(*).elf (flash)\n" $(Q)$(GDB) --batch \ -ex 'target extended-remote $(STLINK_PORT)' \ -x $(SCRIPT_DIR)/stlink_flash.scr \ $(*).elf endif .PHONY: images clean -include $(OBJS:.o=.d)