beep buzzer experiment to tell you the benefits of modular and engineering programming!
Write before:
This is followed by the previous blog post on BSP. In that blog post, we built a project framework of "although the sparrow is small, it has all kinds of internal organs". Now the framework has been set up, so that it is convenient for you to add new functions. Stop gossiping and get started!
text
1. The first step is to look at the picture
Be sure to look at the circuit diagram first and find the IO port you want to call. (there is no picture here. The makedown editor is too troublesome to make pictures.)
2. Step 2: create a new function module
Directly create a beep folder under the bsp folder. We all want to put the buzzer driver files in it.
3. Step 3 write driver file
In the beep folder created in the previous step, create a new bsp_beep.h,bsp_beep.c.
Remember that. c and. h are a bunch of CP S. Both appear in pairs. Related functions are constructed in. c and. h files provide interface API s for main function calls.
bsp_beep.h code is as follows:
1 #ifndef __BSP_BEEP_H 2 #define __BSP_BEEP_H 3 4 #include "imx6ul.h" 5 6 /* Function declaration */ 7 void beep_init(void); 8 void beep_switch(int status); 9 #endif
It's simple. There's nothing to say.
bsp_beep.c code is as follows:
#include "bsp_beep.h" /*Initialize buzzer IO*/ void beep_init(void) { /*IO Multiplexed to GPIO5_IO01*/ IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0); /*Configure IO properties*/ IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0x10B0); /*Initialization set to output*/ GPIO5->GDIR |= (1 << 1); /*Output high with you flat, turn off the buzzer*/ GPIO5->DR |= (1 << 1); } /*Buzzer control function*/ void beep_switch(int status) { if(status == ON) GPIO5->DR &= ~(1 << 1);//open else if (status == OFF) GPIO5->DR |= (1 << 1);//close }
Here are the difficulties or difficulties to understand:
- GPIO5->GDIR |= (1 << 1);
- GPIO5->DR |= (1 << 1);
- GPIO5->DR &= ~(1 << 1);// open
- GPIO5->DR |= (1 << 1);// close
If you don't understand what this means, don't worry. It's not difficult to think back to your knowledge of structures and operators step by step.
4. The fourth part is to write main.c
The main.c file only needs to be changed slightly:
#include "bsp_clk.h" #include "bsp_delay.h" #include "bsp_led.h" #include "bsp_beep.h" int main(void) { clk_enable(); /* Enable all clocks */ led_init(); /* Initialize led */ beep_init();//Initialize beep while(1) /* Dead cycle */ { led_switch(LED0,'ON'); /* Turn on the LED */ beep_switch(ON); delay(500); /* Delay 500ms */ led_switch(LED0,'OFF'); /* Turn off the LED */ beep_switch(OFF); delay(500); /* Delay 500ms */ } return 0; }
Add header file bsp_beep.h, and beep's initialization function beep_init(), and directly call the function beep that controls beep_switch().
5. Step 5 write a makefile file
Simply modify it:
CROSS_COMPILE ?= arm-linux-gnueabihf-#This line can be changed for different compilers TARGET ?= beep#The name of the target should also be changed for different processes CC := $(CROSS_COMPILE)gcc LD := $(CROSS_COMPILE)ld OBJCOPY := $(CROSS_COMPILE)objcopy OBJDUMP := $(CROSS_COMPILE)objdump #The variable INCDIRS contains the. h header file directory of the whole project. All header file directories in the file should be added to the variable INCDIRS INCDIRS := imx6ul \ bsp/clk \ bsp/led \ bsp/delay\ bsp/beep #SRCDIRS contains all. c and. S file directories of the whole project SRCDIRS := project \ bsp/clk \ bsp/led \ bsp/delay\ bsp/beep #The variable INCLUDE uses the function patsubst. Add a "- I" to the variable incdir through the function patsubst, because the Makefile syntax requires that "- I" be added when indicating the header file directory INCLUDE := $(patsubst %, -I %, $(INCDIRS)) #The variable SFILES saves all. S assembly files (including absolute paths) in the project. The variable SRCDIRS has stored all. c and. S files in the project, so we only need to pick out all. S assembly files from it SFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S)) #The variable CFILES is the same as the variable SFILES, except that CFILES saves all. c files (including absolute paths) in the project CFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c)) #Use the function notdir to remove the paths in SFILES and CFILES SFILENDIR := $(notdir $(SFILES)) CFILENDIR := $(notdir $(CFILES)) #By default, all compiled. o files and source files are in the same directory SOBJS := $(patsubst %, obj/%, $(SFILENDIR:.S=.o)) COBJS := $(patsubst %, obj/%, $(CFILENDIR:.c=.o)) #The variable OBJS is a collection of variables SOBJS and COBJS OBJS := $(SOBJS) $(COBJS) #VPATH specifies the search directory. The search element directory specified here is the directory saved by the variable SRCDIRS, so that the required. S and. c files will be found in the directory specified in SRCDIRS when compiling 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)
- The name of the modification target is "beep"
- Add the buzzer driver header file path in the variable INCDIRS, that is, the path of the file beep.h
- Add the buzzer driver file Lu Jin in the variable SRCDIRS, that is, the path of the file beep.c
5. The fifth step is to compile
Complete project files can be downloaded from my code cloud warehouse:
https://gitee.com/iron2222/linux-driver-development/tree/master/6_beep
Thank you for your support!
Good morning, good afternoon and good night!