MCU-based Makefile project management and tool chain configuration under Linux environment

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

4424151fbb12397a8e4e2c62f5319121.png

34286914dfdf1e9388126f4455b8c56d.png

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

cab34bcbefe0f430287d7bf5ac8ef361.png

内容: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.

385dd9d90582159264cac5f7ccc3fc7c.png

The toolchain configuration was successful.

Three, makefile file

basic knowledge

ce16d22c81d6fe7537e3a1ae563ec460.png

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.

367fb9f559c70abc8be1c33b97a22581.png

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

f566e0982e366ef0fcc35116c4127621.png

Five, makefile file writing

9c3dd421bae50d38310a49405a7ada47.png

file structure

47c30da6c3f72f354aa7c7bdfe0da2da.png

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:

2f50908db190983db8ff6e4640e214f8.png

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

Guess you like

Origin blog.csdn.net/weixin_46158019/article/details/131255801