Explanation of the Makefile content for the FreeRTOS demo project on Ubuntu:
1. Tool Definition Section
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
CXX = $(PREFIX)g++
AS = $(PREFIX)gcc -x assembler-with-cpp
LD = $(PREFIX)g++
OBJCOPY = $(PREFIX)objcopy
OBJDUMP = $(PREFIX)objdump
SIZE = $(PREFIX)size
GDB = $(PREFIX)gdb
-
Defines the prefix for the cross-compilation toolchain and various tools
-
Uses the ARM embedded toolchain (arm-none-eabi-)
-
Includes C compiler (gcc), C++ compiler (g++), assembler, linker, etc.
2. Project Definition Section
PROJECT = STM32F407_FREERTOS_DEMO_Project
BUILD_DIR = Build/Application
TARGET = $(BUILD_DIR)/$(PROJECT)
-
Defines the project name, build directory, and target file path
3. Source File Definition Section
C_SOURCES = \
Core/FreeRTOS/Source/croutine.c \
...(other source files omitted)...
User/Src/drivers/logger.c
-
Lists all C source files, including:
-
FreeRTOS kernel source files
-
STM32 HAL drivers
-
CMSIS system files
-
User-defined code
4. Assembly Source File Section
STARTUP_SRC = Startup/startup_stm32f407xx.s
ASM_SOURCES = $(STARTUP_SRC)
-
Defines the startup assembly file, which is the first code executed when the STM32 starts
5. Include Path Section
C_INCLUDES = \
-IUser/Inc \
...(other paths omitted)...
-ICore/FreeRTOS/Source/portable/GCC/ARM_CM4F
-
Specifies all header file search paths
-
Includes paths for user code, FreeRTOS, CMSIS, and HAL driver header files
6. Compiler Flags Section
CPU = -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard
FPU = -mfpu=fpv4-sp-d16 -mfloat-abi=hard
MCU = $(CPU) $(FPU)
DEBUG = -g OPT = -O0
CFLAGS = $(MCU) $(DEBUG) $(OPT) \
-std=gnu11 \
...(other flags omitted)...
-DSTM32F407xx
-
Defines CPU architecture-related flags (Cortex-M4, Thumb instruction set, FPU, etc.)
-
Sets debug information and optimization level
-
Configures various options for the C compiler
7. Linker Flags Section
LDSCRIPT = Config/STM32F407VGTx_FLASH.ld
LDFLAGS = $(MCU) $(DEBUG) $(OPT) \
-T$(LDSCRIPT) \
...(other flags omitted)...
-Wl,--gc-sections
-
Specifies the linker script (defines memory layout)
-
Sets linker options, including garbage collection of unused sections
8. Build Rules Section
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) $(C_INCLUDES) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(TARGET).elf: $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(SIZE) $@
-
Defines the rules for generating .o files from .c/.s files
-
Defines the rules for linking all object files to generate the ELF executable file
-
Displays memory usage after generation
9. Auxiliary Targets Section
clean:
-rm -fR $(BUILD_DIR)
flash: $(TARGET).bin
st-flash write $< 0x08000000
debug: $(TARGET).elf
$(GDB) -ex "target extended-remote :3333" -ex "monitor reset halt" -ex "load" -ex "monitor reset" $<
-
clean: Cleans the build directory
-
flash: Uses the st-link tool to program the binary file to the MCU
-
debug: Uses GDB to debug the program via OpenOCD
The Makefile is a tool for automating the build and compilation of projects, primarily used to define and manage dependencies and build rules within the project. We must master its content and write it correctly, and subsequently, based on this base framework, add or modify content according to the project.
