# Copyright 2019, Max Regan # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # Tools # TOOL_PREFIX ?= arm-none-eabi- CC = $(TOOL_PREFIX)gcc CXX = $(TOOL_PREFIX)g++ CPP = $(TOOL_PREFIX)cpp AS = $(TOOL_PREFIX)as LD = $(TOOL_PREFIX)g++ OBJCOPY = $(TOOL_PREFIX)objcopy STM32_PROG = STM32_Programmer.sh # # Device Variables # DEVICE_TYPE ?= stm32l031k6 DEVICE_FAMILY = stm32l031xx DEVICE_LINE = stm32l0xx # # Filenames and paths # # Ignores dotfiles and other garbage some tools leave behind define find_important $(shell find $(1) -type f -and -name $(2) -and -not -iname "*~" -and -not -iname "*#*" -and -not \( -path "*.cquery_cached_index*" \) ) endef C_SOURCES := $(call find_important, $(SOURCEDIR), '*.c') CXX_SOURCES := $(call find_important, $(SOURCEDIR), '*.cpp') S_SOURCES := $(call find_important, $(SOURCEDIR), '*.s') SPP_SOURCES := $(DEVICE_TYPE).S SOURCES = $(C_SOURCES) $(S_SOURCES) $(SPP_SOURCES) $(CPP_SOURCES) C_OBJS := $(patsubst %.c,%.o,$(C_SOURCES)) CXX_OBJS := $(patsubst %.cpp,%.o,$(CXX_SOURCES)) S_OBJS := $(patsubst %.s,%.o,$(S_SOURCES)) SPP_OBJS := $(patsubst %.S,%.o,$(SPP_SOURCES)) OBJS = $(C_OBJS) $(S_OBJS) $(SPP_OBJS) $(CXX_OBJS) LINKER_SCRIPT ?= $(DEVICE_TYPE).ld OUTPUT_NAME ?= watch OUTPUT_BIN ?= $(OUTPUT_NAME).bin OUTPUT_ELF ?= $(OUTPUT_NAME).elf # # Flags # DEVICE_DEFINE = $(subst XX,xx,$(shell echo $(DEVICE_FAMILY) | tr '[:lower:]' '[:upper:]')) CPU_FLAGS = -mthumb -mcpu=cortex-m0plus -mfloat-abi=soft # C pedantism CFLAGS = -Wall -Wextra -Wpedantic -Werror CXX_FLAGS = -Wsuggest-override -Wsuggest-final-methods -Wsuggest-final-types # Debug/optimization CFLAGS += -Os -ggdb -g3 CFLAGS += -fdata-sections -ffunction-sections # Architecture CFLAGS += $(CPU_FLAGS) CFLAGS += -ffreestanding # Defines CFLAGS += -D$(DEVICE_DEFINE) # Includes CFLAGS += -I./lib/stm32/$(DEVICE_LINE)/Include CFLAGS += -I./lib/CMSIS/Core/Include CFLAGS += -I./lib/fonts/ CXX_FLAGS += -std=c++14 -fno-exceptions -fno-rtti # Startup Definitions ASFLAGS += $(CPU_FLAGS) ASFLAGS += -D__STARTUP_CLEAR_BSS ASFLAGS += -D__HEAP_SIZE=0 # No heap- let the linker decide it all LDFLAGS += $(CPU_FLAGS) LDFLAGS += -Wl,--gc-sections -Wl,--build-id=none -static LDFLAGS += -Wl,--wrap=malloc -Wl,--wrap=free # Fail to link if dynamic allocation is sneaking through LDFLAGS += -Wl,-print-memory-usage # # Default Target # .PHONY: build build: $(OUTPUT_BIN) # # Build Logic # %.o: %.c @echo "CC $@" @$(CC) $(CFLAGS) -c $< -o $@ %.o: %.S @echo "AS $@" @$(CC) $(ASFLAGS) -c $< -o $@ %.o: %.cpp @echo "CXX $@" @$(CXX) $(CXX_FLAGS) $(CFLAGS) -c $< -o $@ $(OUTPUT_BIN): $(OUTPUT_ELF) @echo "OBJCOPY $@" @$(OBJCOPY) -O binary $(OUTPUT_ELF) $(OUTPUT_BIN) $(OUTPUT_ELF): $(LINKER_SCRIPT) $(OBJS) @echo "LD $@" @$(LD) -T $(LINKER_SCRIPT) $(LDFLAGS) -o $(OUTPUT_ELF) $(OBJS) # # Utilities # STM32FLASH_DEVICE = /dev/ttyUSB0 .PHONY: flash flash: $(OUTPUT_BIN) @echo "FLASH $(OUTPUT_BIN)" $(STM32_PROG) --connect port=SWD reset=Hwrst -w $(OUTPUT_BIN) 0x8000000 -v --go .PHONY: clean clean: @echo "RM $(OBJS)" @rm -f $(OBJS) $(OUTPUT_BIN) $(OUTPUT_ELF)