Skip to content

Instantly share code, notes, and snippets.

@glegrain
Last active January 18, 2025 13:01
Show Gist options
  • Select an option

  • Save glegrain/21b22691e94c11e6010652f2e13ffa3d to your computer and use it in GitHub Desktop.

Select an option

Save glegrain/21b22691e94c11e6010652f2e13ffa3d to your computer and use it in GitHub Desktop.
STM32 Makefile boilerplate
######################################
# target
######################################
TARGET = Project
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -O0
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
######################################
# source
######################################
# C sources
C_SOURCES := ./Src/main.c
C_SOURCES += ./Src/stm32l4xx_hal_msp.c
C_SOURCES += ./Src/stm32l4xx_it.c
C_SOURCES += ./Src/system_stm32l4xx.c
C_SOURCES += ./Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c
C_SOURCES += ./Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c
C_SOURCES += ./Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c
C_SOURCES += ./Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c
C_SOURCES += ./Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c
C_SOURCES += ./Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c
# ASM sources
ASM_SOURCES = ./startup_stm32l475xx.s
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# For STM32F1, STM32L1, STM32F2:
# CPU = -mcpu=cortex-m3
# For STM32WL:
# CPU = -mcpu=cortex-m4+nofp
# For STM32F3, STM32F4, STM32L4, STM32L4+, STM32G4, STM32WB:
CPU = -mcpu=cortex-m4
FPU = -mfpu=fpv4-sp-d16
FLOAT-ABI = -mfloat-abi=hard
# For STM32F7:
# CPU = -mcpu=cortex-m7
# FPU = -mfpu=fpv5-sp-d16
# FLOAT-ABI = -mfloat-abi=hard
# For STM32H7:
# CPU = -mcpu=cortex-m7
# FPU = -mfpu=fpv5-d16
# FLOAT-ABI = -mfloat-abi=hard
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# macros for gcc
# AS defines
AS_DEFS =
# C defines
C_DEFS := -DUSE_HAL_DRIVER
C_DEFS += -DSTM32L475xx
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES := -I./Inc
C_INCLUDES += -I./Drivers/STM32L4xx_HAL_Driver/Inc
C_INCLUDES += -I./Drivers/STM32L4xx_HAL_Driver/Inc/Legacy
C_INCLUDES += -I./Drivers/CMSIS/Device/ST/STM32L4xx/Include
C_INCLUDES += -I./Drivers/CMSIS/Core/Include
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
ifeq ($(DEBUG), 1)
CFLAGS += -g3 -gdwarf-2
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = ./STM32L475VGTx_FLASH.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
# Uncomment to enable %f formatted output
# LDFLAGS += -u _printf_float
LDFLAGS += -Wl,--print-memory-usage
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
$(BUILD_DIR):
mkdir $@
#######################################
# clean up
#######################################
clean:
-rm -fR $(BUILD_DIR)
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***
@chintal
Copy link
Copy Markdown

chintal commented Jan 18, 2025

Thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment