Created
March 15, 2017 20:16
-
-
Save macegr/11887b067116cc17fefb071c955e6ee4 to your computer and use it in GitHub Desktop.
Current makefile for STM32L476 (HAL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile for ST ARM Cortex microcontrollers | |
# Important settings should be updated for each project. | |
# The rest of the makefile can usually be left alone. | |
#### /!\ IMPORTANT SETTINGS START HERE /!\ ##### | |
# Device options | |
CPU = cortex-m4 | |
DEVICE = STM32L476xx | |
ENVDIR = /Users/macegr/Development/ARM | |
ARMTOOLSDIR = $(ENVDIR)/gcc-arm-none-eabi-6_2/bin | |
MFGSUPPORTDIR = $(ENVDIR)/STM32/STMDevSupport | |
DEVICEDIR = $(MFGSUPPORTDIR)/STM32L4xx | |
DRIVERDIR = $(DEVICEDIR)/STM32Cube_FW_L4_V1.6.0/Drivers | |
STARTUPDIR = $(DRIVERDIR)/CMSIS/Device/ST/STM32L4xx/Source/Templates/gcc | |
DEVINC = $(DRIVERDIR)/CMSIS/Device/ST/STM32L4xx/Include | |
HALINC = $(DRIVERDIR)/STM32L4xx_HAL_Driver/Inc | |
HALSRC = $(DRIVERDIR)/STM32L4xx_HAL_Driver/Src | |
CMSISINC = $(DRIVERDIR)/CMSIS/Include | |
BSPINC = $(DRIVERDIR)/BSP/STM32L476G-Discovery | |
INCLUDES = -I$(HALINC) -I$(DEVINC) -I$(CMSISINC) -I$(BSPINC) -I. | |
GENLIBS = libstm32lxxhal.a | |
LINKSCRIPT = STM32L476link.ld | |
STARTUPFILE = startup_stm32l476xx.s | |
STARTUPSRC = $(STARTUPDIR)/$(STARTUPFILE) | |
# Programmer options | |
OPENOCD = openocd | |
OPENOCDDIR = $(MFGSUPPORTDIR)/OpenOCD | |
OPENOCDCFG = $(OPENOCDDIR)/stm32l4discovery.cfg | |
OPENOCDCMD = program $(BUILDTARGET).hex verify reset exit | |
###### /!\ IMPORTANT SETTINGS END HERE /!\ ###### | |
# Source search paths | |
vpath %.c $(HALSRC) | |
# Build structure | |
TARGET = main | |
BUILDDIR = builds | |
BUILDTARGET = $(BUILDDIR)/$(TARGET) | |
OBJDIR = $(BUILDDIR)/obj | |
DEPDIR = $(BUILDDIR)/dep | |
CSOURCES = $(wildcard *.c) $(wildcard */*.c) | |
CPPSOURCES = $(wildcard *.cpp) $(wildcard */*.cpp) | |
ASOURCES = $(wildcard *.s) $(wildcard */*.s) | |
ifeq ("$(wildcard $(STARTUPFILE))","") | |
ASOURCES += $(STARTUPFILE) | |
endif | |
OBJECTS = $(addprefix $(OBJDIR)/, $(CPPSOURCES:.cpp=.o)) | |
OBJECTS += $(addprefix $(OBJDIR)/, $(CSOURCES:.c=.o)) | |
OBJECTS += $(addprefix $(OBJDIR)/, $(ASOURCES:.s=.o)) | |
DEPS = $(addprefix $(DEPDIR)/, $(CPPSOURCES:.cpp=.d)) | |
DEPS += $(addprefix $(DEPDIR)/, $(CSOURCES:.c=.d)) | |
DEPS += $(addprefix $(DEPDIR)/, $(ASOURCES:.s=.d)) | |
# ARM tools location, specify absolute path if necessary | |
FORMAT = ihex | |
CC = $(ARMTOOLSDIR)/arm-none-eabi-gcc | |
SIZE = $(ARMTOOLSDIR)/arm-none-eabi-size | |
OBJCOPY = $(ARMTOOLSDIR)/arm-none-eabi-objcopy | |
OBJDUMP = $(ARMTOOLSDIR)/arm-none-eabi-objdump | |
AR = $(ARMTOOLSDIR)/arm-none-eabi-ar | |
GDB = $(ARMTOOLSDIR)/arm-none-eabi-gdb | |
LD = | |
# Compiler options | |
OPTIMIZATION = s | |
ERRORLEVEL = all | |
CSTANDARD = | |
CFLAGS = -W$(ERRORLEVEL) -O$(OPTIMIZATION) -mcpu=$(CPU) -D$(DEVICE) | |
CFLAGS += -g -fno-builtin | |
CFLAGS += -mlittle-endian -mthumb -g | |
CFLAGS += -ffunction-sections -fdata-sections | |
CFLAGS += -ffreestanding -fno-common -Wno-unused-function | |
LFLAGS = -Wl,--gc-sections | |
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d | |
REMOVE = rm -f | |
build: .hex .size | |
HAL_LIB_SRC = $(wildcard $(HALSRC)/*.c) | |
HAL_LIB_SRC := $(filter-out %_template.c, $(HAL_LIB_SRC)) | |
HAL_LIB_OBJ = $(HAL_LIB_SRC:.c=.o) | |
HAL_LOCAL_LIB_OBJ = $(addprefix $(OBJDIR)/, $(notdir $(HAL_LIB_OBJ))) | |
libstm32lxxhal.a: $(HAL_LOCAL_LIB_OBJ) | |
$(AR) -cr $@ $(HAL_LOCAL_LIB_OBJ) | |
touch $@ | |
# Copy in the startup file from templates if needed | |
$(STARTUPFILE): | |
cp $(STARTUPSRC) . | |
# Virtual target for dependency files | |
$(DEPDIR)/%.d: ; | |
@mkdir -p $(dir $@) | |
# Don't delete dependency files | |
.PRECIOUS: $(DEPDIR)/%.d | |
# Don't rebuild deps if cleaning | |
ifneq ($(MAKECMDGOALS),clean) | |
-include $(DEPS) | |
endif | |
# Build rule for C++ files | |
# Depends on Makefile to force recompile if any Makefile options changed | |
$(OBJDIR)/%.o: %.cpp | |
$(OBJDIR)/%.o: %.cpp Makefile | |
@mkdir -p $(dir $@) | |
$(CC) $(DEPFLAGS) $(INCLUDES) $(CFLAGS) -c -o $@ $< | |
# Build rule for C files | |
# Depends on Makefile to force recompile if any Makefile options changed | |
$(OBJDIR)/%.o: %.c | |
$(OBJDIR)/%.o: %.c Makefile | |
@mkdir -p $(dir $@) | |
$(CC) $(DEPFLAGS) $(INCLUDES) $(CFLAGS) -c -o $@ $< | |
# Build rule for assembler files | |
# Depends on Makefile to force recompile if any Makefile options changed | |
$(OBJDIR)/%.o: %.s | |
$(OBJDIR)/%.o: %.s Makefile | |
@mkdir -p $(dir $@) | |
$(CC) $(DEPFLAGS) $(INCLUDES) $(CFLAGS) -c -o $@ $< | |
# Don't delete object files | |
.PRECIOUS: $(OBJDIR)/%.o | |
# Rule for creating ELF files | |
%.elf: $(OBJECTS) $(GENLIBS) | |
@echo "Linking...." | |
$(CC) $(INCLUDES) $(CFLAGS) $(LFLAGS) -T$(LINKSCRIPT) -o $@ $(OBJECTS) $(GENLIBS) | |
# Rule for creating hex files | |
%.hex: %.elf | |
$(OBJCOPY) -O $(FORMAT) $< $@ | |
# Build hex file | |
.hex: $(BUILDTARGET).hex | |
# Display the flash and RAM size | |
.size: $(BUILDTARGET).elf | |
$(SIZE) $< | |
# Write the hex file to the device | |
program: $(BUILDTARGET).hex .size | |
@echo "Uploading firmware..." | |
$(OPENOCD) -f $(OPENOCDCFG) -c "$(OPENOCDCMD)" | |
@echo "Done uploading." | |
# Delete any compiled code and dependency files | |
# Avoiding recursive delete due to potential for mistakes | |
clean: | |
@echo "Cleaning..." | |
-$(REMOVE) $(OBJECTS) | |
-$(REMOVE) $(DEPDIR)/*.d | |
-$(REMOVE) $(HAL_LOCAL_LIB_OBJ) | |
-$(REMOVE) $(BUILDTARGET).hex $(BUILDTARGET).elf | |
-$(REMOVE) $(HALSRC)/*.o | |
-$(REMOVE) $(GENLIBS) | |
-find $(BUILDDIR) -type d -empty -delete | |
@echo "Done cleaning." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment