## ## 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 CXX = $(PREFIX)-g++ LD = $(PREFIX)-gcc AR = $(PREFIX)-ar AS = $(PREFIX)-as OBJCOPY = $(PREFIX)-objcopy OBJDUMP = $(PREFIX)-objdump GDB = $(PREFIX)-gdb STFLASH = $(shell which st-flash) LDSCRIPT ?= $(BINARY).ld OBJS += $(BINARY).o ifeq ($(strip $(OPENCM3_DIR)),) # user has not specified the library path, so we try to detect it # where we search for the library LIBPATHS := ./libopencm3 ../../../../../libopencm3 OPENCM3_DIR := $(wildcard $(LIBPATHS:=/locm3.sublime-project)) OPENCM3_DIR := $(firstword $(shell dirname $(OPENCM3_DIR))) ifeq ($(strip $(OPENCM3_DIR)),) $(error Cannot find libopencm3 library in the standard search paths.) endif endif ifeq ($(V),1) $(info Using $(OPENCM3_DIR) path to library) endif INCLUDE_DIR = $(OPENCM3_DIR)/include LIB_DIR = $(OPENCM3_DIR)/lib SCRIPT_DIR = $(OPENCM3_DIR)/scripts # C flags CFLAGS += -Os -g -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 CXXFLAGS += -Os -g -MD $(DEFS) CXXFLAGS += -Wall -Wextra -Wimplicit-function-declaration CXXFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes CXXFLAGS += -Wundef -Wshadow -fno-common CXXFLAGS += -I$(INCLUDE_DIR) # Linker flags LDFLAGS += --static -nostartfiles -l$(LIBNAME) LDFLAGS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group LDFLAGS += -Wl,--gc-sections LDFLAGS += -L$(LIB_DIR) LDFLAGS += -T$(LDSCRIPT) LDFLAGS += -Wl,-Map=$(*).map ifeq ($(V),1) LDFLAGS += -Wl,--print-gc-sections endif .SUFFIXES: .elf .bin .hex .srec .list .map .images .SECONDEXPANSION: .SECONDARY: all: images images: $(BINARY).images flash: $(BINARY).flash %.images: %.bin %.hex %.srec %.list %.map @#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 %.map: $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a @#printf " LD $(*).elf\n" $(Q)$(LD) -o $(*).elf $(OBJS) $(LDFLAGS) $(ARCH_FLAGS) %.o: %.c @#printf " CC $(*).c\n" $(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).c %.o: %.cxx @#printf " CXX $(*).cxx\n" $(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).cxx %.o: %.cpp @#printf " CXX $(*).cpp\n" $(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -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 $(Q)rm -f *.map %.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 "flash write_image erase $(*).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 "flash write_image erase $(*).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)