BSP engineering management experiment
We must manage the project files and put the source files of different functions in different directories. In addition, we also need to extract all the code that completes the same function in the source file and put it in a separate file, that is, the program is managed by function.
Create a new folder named "5_ledc_bsp", and create four folders: BSP, imx6ul, obj and project.
imx6ul is used to store files related to the chip, such as the official SDK library files of NXP;
obj is used to store the compiled. o file;
project stores start.S and main.c files, that is, application files;
cc.h, fsl_common.h,fsl_ Copy the four files iomuxc. H and MCIMX6Y2.h to the folder imx6ul; Copy the start.S and main.c files to the project folder.
In our previous experiments, all driver related functions are written to the main.c file, such as the function clk_enable,led_init and delay, these three functions can be divided into three categories: clock driver, LED driver and delay driver. Therefore, we can create three subfolders under the bsp folder: clk, delay and led, which are used to store clock driver files, delay driver files and LED driver files respectively. In this way, the main.c function will be much cleaner and the program function module will be clear.
1, Code implementation
-
Create imx6ul.h file
Create a new file imx6ul.h and save it to the folder imx6ul. Enter the following in imx6ul.h:
#ifndef __IMX6UL_H #define __IMX6UL_H #include "cc.h" #include "MCIMX6Y2.h" #include "fsl_common.h" #include "fsl_iomuxc.h" #endif
The file imx6ul.h is very simple. It refers to some header files. Later, we can reference them in other files
imx6ul.h will do.
-
Write led driver code
New bsp_led.h and bsp_led.c two files, which are stored in bsp/led,
In BSP_ Input the following contents in LED. H:
#ifndef __BSP_LED_H #define __BSP_LED_H #include "imx6ul.h" #define LED0 0 /* Function declaration */ void led_init(void); void led_switch(int led, int status); #endif
In BSP_ Enter the following in LED. C:
#include "bsp_led.h" /* * @description : Initialize GPIO corresponding to LED * @param : nothing * @return : nothing */ void led_init(void) { /* 1,Initialize IO multiplexing */ IOMUXC_SetPinMux(IOMUXC_GPIO1_IO03_GPIO1_IO03,0); /* Multiplexed to GPIO1_IO03 */ /* 2,,Configure gpio1_ IO properties of io03 *bit 16:0 HYS close *bit [15:14]: 00 Default drop-down *bit [13]: 0 kepper function *bit [12]: 1 pull/keeper Enable *bit [11]: 0 Turn off the open circuit output *bit [7:6]: 10 Speed 100Mhz *bit [5:3]: 110 R0/6 Driving capability *bit [0]: 0 Low conversion rate */ IOMUXC_SetPinConfig(IOMUXC_GPIO1_IO03_GPIO1_IO03,0X10B0); /* 3,Initialize GPIO,GPIO1_IO03 set to output*/ GPIO1->GDIR |= (1 << 3); /* 4,Set GPIO1_IO03 outputs low level and turns on LED0*/ GPIO1->DR &= ~(1 << 3); } /* * @description : LED Control function to control whether the LED is on or off * @param - led : LED number to be controlled * @param - status : 0,Close LED0, 1 open LED0 * @return : nothing */ void led_switch(int led, int status) { switch(led) { case LED0: if(status == ON) GPIO1->DR &= ~(1<<3); /* Open LED0 */ else if(status == OFF) GPIO1->DR |= (1<<3); /* Close LED0 */ break; } }
-
Write clock driver code
New bsp_clk.h and bsp_clk.c files. Store these two files in bsp/clk,
In BSP_ Enter the following in CLK. H:
#ifndef __BSP_CLK_H #define __BSP_CLK_H #include "imx6ul.h" /* Function declaration */ void clk_enable(void); #endif
In BSP_ Enter the following in CLK. C:
#include "bsp_clk.h" /* * @description : Enable all peripheral clocks of I.MX6U * @param : nothing * @return : nothing */ void clk_enable(void) { CCM->CCGR0 = 0XFFFFFFFF; CCM->CCGR1 = 0XFFFFFFFF; CCM->CCGR2 = 0XFFFFFFFF; CCM->CCGR3 = 0XFFFFFFFF; CCM->CCGR4 = 0XFFFFFFFF; CCM->CCGR5 = 0XFFFFFFFF; CCM->CCGR6 = 0XFFFFFFFF; }
-
Write delay driver code
New bsp_delay.h and bsp_delay.c two files, which are stored in bsp/delay,
In BSP_ Enter the following in delay. H:
#ifndef __BSP_DELAY_H #define __BSP_DELAY_H #include "imx6ul.h" /* Function declaration */ void delay(volatile unsigned int n); #endif
In BSP_ Enter the following in delay. C:
#include "bsp_delay.h" /* * @description : Short time delay function * @param - n : Number of cycles to delay (number of empty operation cycles, mode delay) * @return : nothing */ void delay_short(volatile unsigned int n) { while(n--){} } /* * @description : Delay function, at the main frequency of 396Mhz * The delay time is about 1ms * @param - n : Number of ms to delay * @return : nothing */ void delay(volatile unsigned int n) { while(n--) { delay_short(0x7ff); } }
-
Write main.c code
#include "bsp_clk.h" #include "bsp_delay.h" #include "bsp_led.h" /* * @description : mian function * @param : nothing * @return : nothing */ int main(void) { clk_enable(); /* Enable all clocks `` */ led_init(); /* Initialize led */ while(1) { /* Open LED0 */ led_switch(LED0,ON); delay(500); /* Close LED0 */ led_switch(LED0,OFF); delay(500); } return 0; }
2, Compile and download
-
Writing Makefile and link scripts
Create Makefile and imx6ul.lds files in the project root directory
Makefile code is as follows:
CROSS_COMPILE ?= arm-linux-gnueabihf-TARGET ?= bspCC := $(CROSS_COMPILE)gccLD := $(CROSS_COMPILE)ldOBJCOPY := $(CROSS_COMPILE)objcopyOBJDUMP := $(CROSS_COMPILE)objdumpINCDIRS := imx6ul \ bsp/clk \ bsp/led \ bsp/delay SRCDIRS := project \ bsp/clk \ bsp/led \ bsp/delay INCLUDE := $(patsubst %, -I %, $(INCDIRS))SFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))CFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))SFILENDIR := $(notdir $(SFILES))CFILENDIR := $(notdir $(CFILES))SOBJS := $(patsubst %, obj/%, $(SFILENDIR:.S=.o))COBJS := $(patsubst %, obj/%, $(CFILENDIR:.c=.o))OBJS := $(SOBJS) $(COBJS)VPATH := $(SRCDIRS).PHONY: clean $(TARGET).bin : $(OBJS) $(LD) -Timx6ul.lds -o $(TARGET).elf $^ $(OBJCOPY) -O binary -S $(TARGET).elf $@ $(OBJDUMP) -D -m arm $(TARGET).elf > $(TARGET).dis$(SOBJS) : obj/%.o : %.S $(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<$(COBJS) : obj/%.o : %.c $(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $< clean: rm -rf $(TARGET).elf $(TARGET).dis $(TARGET).bin $(COBJS) $(SOBJS)
imx6ul.lds writes the following:
SECTIONS{ . = 0X87800000; .text : { obj/start.o *(.text) } .rodata ALIGN(4) : {*(.rodata*)} .data ALIGN(4) : { *(.data) } __bss_start = .; .bss ALIGN(4) : { *(.bss) *(COMMON) } __bss_end = .;}
-
dsfg
Copy the imxdownload software to the project folder and give permission,
chmod 777 imxdownload
/imxdownload ledc.bin /dev/sdb / / it is downloaded to the SD card and cannot be written to / dev/sda or sda1 devices!
Insert the SD card into the development board, and the DS0 on the development board will flash once every 500ms.
-