1. Development environment
Development board: stm32f407
Compilation environment: 18.04.6 LTS
Toolchain: gcc-arm-none-eabi, gcc-arm-none-objcopy
2. Toolchain download and installation
Download address: (download 64-bit linux version)
https://launchpad.net/gcc-arm-embedded/+download
https://developer.arm.com/downloads/-/gnu-rm
The installation process is as follows:
Unzip the downloaded toolchain, put the toolchain in the /usr/bin/ directory of ubuntu to decompress, and get the gcc-arm-none-eabi/ directory after decompression, and the command
tar -jvxf gcc-arm-none-eabi-xxxx
Configure environment variables, as follows:
Modify environment variables (①Only valid for the current user (in the home directory (cd ~), modify .bashrc and add export PATH=path:$PATH or export PATH=$PATH:path at the end of the file after modifying .bashrc); ②Valid for all users ( sudo vi /etc/profile) Add export PATH=path:$PATH or export PATH=$PATH:path) at the end of the file with a new line -> restart (sudo reboot) -> verify.
Only valid for the current user, as follows
内容:export PATH=$PATH:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin:/usr/local/arm/gcc-arm-none-eabi-7-2017-q4-major/bin
Reboot, verify. Enter arm and press the table button to check whether it is successful. The tool chain list that pops up includes the arm-none-eabi-gcc and armnone-eabi-objcop we need.
The toolchain configuration was successful.
Three, makefile file
basic knowledge
make tool : It can help us find out the files that have been modified and changed in the project, and find out other related files affected by the modification according to the dependencies, and then compile these files separately according to the rules, so that we can avoid Recompile all files of the project.
Makefile : The rules and dependencies mentioned above are mainly defined in this Makefile. After we reasonably define the dependencies of the files in it, the make tool can accurately compile.
Fourth, the compilation process
The compilation process of gcc can be divided into four steps :
1. Preprocessing (-E) to generate precompiled files (.i files)
2. Compile (-S) to generate assembly code (.S file)
3. Compile (-c) to generate object files (.o files)
4. Link (-o) to generate an executable file
preprocessing-E: only preprocess the file, do not compile and link. (Generate precompiled files.i)
Use gcc -E hello.c -o hello.i to get the preprocessed file
Compile -S, compile the file into assembly code (generate assembly file.s)
The -S parameter compiles the hello.i file into a hello.s file
gcc -S hello.i -o hello.s
Assembly_C, compile the assembly file into machine code (generate object file.o)
The -c parameter can compile the hello.s file into a hello.o file
gcc -c hello.s -o hello.o
Link -o, directly compile the object file into an executable file
gcc hello.o -o hello
Five, makefile file writing
file structure
Makefile
Author = mcu
######################################
# target
######################################
TARGET = target
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og
#######################################
# paths
#######################################
# source path
# firmware library path
PERIFLIB_PATH =
# Build path
BUILD_DIR = build
######################################
# source
######################################
# C sources
C_SOURCES = $(shell find ./ -name '*.c')
# ASM sources
ASM_SOURCES = \
startup_stm32f407xx.s
######################################
# firmware library
######################################
PERIFLIB_SOURCES =
#######################################
# binaries
#######################################
#BINPATH = /usr/local/arm/gcc-arm-none-eabi-7-2017-q4-major/bin
#PREFIX = arm-none-eabi-
#CC = $(BINPATH)/$(PREFIX)gcc
#AS = $(BINPATH)/$(PREFIX)gcc -x assembler-with-cpp
#CP = $(BINPATH)/$(PREFIX)objcopy
#AR = $(BINPATH)/$(PREFIX)ar
#SZ = $(BINPATH)/$(PREFIX)size
#HEX = $(CP) -O ihex
#BIN = $(CP) -O binary -S
CROSS_COMPILE = arm-none-eabi-
CC = $(CROSS_COMPILE)gcc
AS = $(CROSS_COMPILE)gcc -x assembler-with-cpp
CP = $(CROSS_COMPILE)objcopy
AR = $(CROSS_COMPILE)ar
SZ = $(CROSS_COMPILE)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4
# fpu
FPU = -mfpu=fpv4-sp-d16
# float-abi
FLOAT-ABI = -mfloat-abi=hard
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# macros for gcc
# AS defines
AS_DEFS =
# C defines
C_DEFS = \
-DSTM32F40_41xxx \
-DUSE_STDPERIPH_DRIVER\
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES = -I CORE \
-I FWLIB/inc \
-I HARDWARE \
-I SYSTEM \
-I USER \
# 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 += -g -gdwarf-2
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32F407ZETx_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
# 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 .dep $(BUILD_DIR)
flash:
st-flash write $(BUILD_DIR)/$(TARGET).bin 0x8000000
erase:
st-flash erase
#######################################
# dependencies
#######################################
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
# *** EOF ***
View the effect:
Welcome to pay attention to the personal public number: embedded learning and practice
reference:
https://blog.csdn.net/Ertuil/article/details/80612035
https://www.gd32mcu.com/data/documents/userManual/AN033_CN_Rev1.0.pdf
https://blog.csdn.net/ybhuangfugui/article/details/98136988