How to write a project folder under the generic Makefile

 

New project folder, in which the new bsp, imx6ul, obj project, and three folders, after the completion as shown:

The new project root folder

Which drive used to store files bsp; imx6ul used to store documents related with the chip, such as NXP official SDK libraries; obj used to store the compiled .o files; project and main.c file storage start.S, that is, application files; copying twelve chapters experiments cc.h, fsl_common.h, fsl_iomuxc.h and MCIMX6Y2.h these four files in the folder imx6ul; start.S and copy these two files to main.c folder in the project. Our previous experiments, all functions related to driving are written to the main.c file, such as functions clk_enable, led_init and delay, these three functions can be divided into three categories: the clock drivers, LED drivers and driver delay. Therefore, we can create a folder in the bsp three subfolders: clk, delay and led, respectively, used to store the clock driver files, file and drive delay LED driver files, so will a lot of fresh main.c function, program function module clear. Project folders are created, the next step is to write code, in fact, is to clock drivers, LED drivers and driver delays related functions extracted from the main.c made a separate driver file.

Use VScode new construction, the project name is "ledc_bsp". New File imx6ul.h, and then saved to a folder imx6ul, the new bsp_led.h and bsp_led.c two files, these two files are stored to the bsp / led, the two functions led_init bsp_led.c the inside and led_switch, led_init function is used to initialize the IO LED used, led_switch function is to control the LED lamp on and off, these two functions are very simple.

New bsp_clk.h and bsp_clk.c two files, these two files are stored to the bsp / clk in, bsp_delay.c the inside two functions, delay_short and delay. In main.c we just left a main function, so far, this routine procedures with relevant content on all written better.

New Makefile and imx6ul.lds these two files in the root directory of the project, created after the completion of the project as shown:

The final project directory

 

Typing the Makefile file as follows:

1 CROSS_COMPILE ?= arm-linux-gnueabihf-

2 TARGET ?= bsp 

3

4 CC := $(CROSS_COMPILE)gcc 5 LD := $(CROSS_COMPILE)ld 6 OBJCOPY := $(CROSS_COMPILE)objcopy 7 OBJDUMP := $(CROSS_COMPILE)objdump 8 9 INCDIRS := imx6ul \ 10 bsp/clk \ 11 bsp/led \ 12 bsp/delay 13 14 SRCDIRS := project \ 15 bsp/clk \ 16 bsp/led \ 17 bsp/delay 18 19 INCLUDE := $(patsubst %, -I %, $(INCDIRS)) 20 21 SFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S)) 22 CFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c)) 23 24 SFILENDIR := $(notdir $(SFILES)) 25 CFILENDIR := $(notdir $(CFILES)) 26 27 SOBJS := $(patsubst %, obj/%, $(SFILENDIR:.S=.o)) 28 COBJS := $(patsubst %, obj/%, $(CFILENDIR:.c=.o)) 29 OBJS := $(SOBJS) $(COBJS) 30 31 VPATH := $(SRCDIRS) 32 33 .PHONY: clean 34 35 $(TARGET).bin : $(OBJS) 36 $(LD) -Timx6ul.lds -o $(TARGET).elf $^ 37 $(OBJCOPY) -O binary -S $(TARGET).elf $@ 38 $(OBJDUMP) -D -m arm $(TARGET).elf > $(TARGET).dis 39 40 $(SOBJS) : obj/%.o : %.S 41 $(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ {#content}lt; 42 43 $(COBJS) : obj/%.o : %.c 44 $(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ {#content}lt; 45 46 clean: 47 rm -rf $(TARGET).elf $(TARGET).dis $(TARGET).bin $(COBJS) $(SOBJS)

As can be seen in this test than the previous Makefile for many complex experiments, because the code in the code is a generic Makefile Makefile, we are after all the bare metal routines use this Makefile. Use whenever required to add the directory where the compiled source files to the Makefile can, we then analyze in detail the Makefile Source:

Line 1-7 defines a number of variables, in addition to the other row 2 are associated with the compiler, if you use other compilers, then only need to modify the first line can be. Variable on line 2 TARGET target name, the name of different different routines certainly the same.

Variable line 9 of INCDIRS .h header files directory contains the whole project, the header file directory of all files to be added to the variable INCDIRS. Such directory header .h file contains present routine imx6ul, bsp / clk, bsp / delay and bsp / led, so we need to add these directories INCDIRS variables, namely:

INCDIRS := imx6ul bsp/clk bsp/led bsp/delay

Look closely, you will find behind the line 9 to 11, there will be a symbol "\", this is equivalent to "line breaks", said the Bank belong to the same line and the next line, the general and his party when he wrote no less than by the symbol "\" to newline. We will add the header file directory in the variable INCDIRS in the back of the bare routine according to the actual situation.

Line 14 is variable SRCDIRS, and variable INCDIRS the same, but SRCDIRS contains all .c and .S file directory of the whole project. This directory contains such routines are .c and .S bsp / clk, bsp / delay, bsp / led and project, namely:

SRCDIRS := project bsp/clk bsp/led bsp/delay

Similarly, the back of the bare routine we have to add the corresponding file directory in the variable SRCDIRS in accordance with the actual situation.

Variable INCLUDE line 19 is used as a function patsubst, INCDIRS add a "-I" by patsubst function to a variable, namely:

INCLUDE := -I imx6ul -I bsp/clk -I bsp/led -I bsp/delay

The purpose plus "-I" is because the Makefile syntax requirements specified in the header file directory when the need to add "-I".

Line 21 variables SFILES save the project in all .s assembly file (containing the absolute path), variable SRCDIRS has been stored all .c and .S file project, so we just need to pick out from the inside all the .S compilation of documents can, by means of a function here foreach function and wildcard, eventually SFILES as follows:

SFILES := project/start.S

Line 22 CFILES variables and variables SFILES the same, but CFILES save the project in all .c files (containing the absolute path), final CFILES as follows:

CFILES = project/main.c bsp/clk/bsp_clk.c bsp/led/bsp_led.c bsp/delay/bsp_delay.c

24 and variable SFILENDIR and CFILENDIR 25 rows contain all the .S compilation of documents and .c files, compared to variable SFILES and CFILES, SFILENDIR and CFILNDIR just the file name, does not contain an absolute path to the file. Using the function notdir SFILES path and can be removed in CFILES, SFILENDIR and CFILENDIR follows:

SFILENDIR = start.SCFILENDIR = main.c bsp_clk.c bsp_led.c bsp_delay.c

The first 27 rows and 28 variables corresponding SOBJS and COBJS after .S and .c file compiled .o file directory, the default for all files compiled .o files and source files in the same directory, where we will all .o files into obj folder, SOBJS and COBJS reads as follows:

SOBJS = obj/start.o

COBJS = obj/main.o obj/bsp_clk.o obj/bsp_led.o obj/bsp_delay.o

Line 29 is a collection of variables variables OBJS SOBJS and COBJS follows:

OBJS = obj/start.o obj/main.o obj/bsp_clk.o obj/bsp_led.o obj/bsp_delay.o

After all .o files compiled on all stored next to the obj directory, as shown:

obj file folder BUILD

VPATH Line 31 is to specify a search directory, catalog search elements specified here is variable SRCDIRS saved directory, .S and .c files required to compile so that when the time will be specified in the SRCDIRS in directory lookup.

Line 34 specifies a pseudo target clean, pseudotarget previous article explained Makefile time has passed.

Makefile file contents focus is to find which files to compile? Compiled .o files are stored where? Use the same command to compile and use the previous experiments, in fact, the focus is to solve Makefile "Where come from" problem, that is, to find the source file to be compiled, compiled the results in the Where? The real compiler command is very simple.

Content linker script imx6ul.lds and last article, " to define and use the register address by way of structure ," like, you can use the links on a script file directly.

This switched small flat electronic technology community: https://www.xiaopingtou.cn/article-104184.html

Guess you like

Origin www.cnblogs.com/cniot/p/12040665.html