Linux之ARM(MX6U)裸机篇----7.蜂鸣器实验
一,蜂鸣器模块
封装步骤:
①初始化SNVS_TAMPER这IO复用为GPIO
②设置SNVS_TAMPPER这个IO的电气属性
③初始化GPIO
④控制GPIO输出高低电平
bsp_beep.c:
#include "bsp_beep.h"
#include "cc.h"
/* BEEP初始化 */
void beep_init(void)
{
IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0);/* 复用为GPIO01-03 */
IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0x10b0);
/* GPIO初始化 */
GPIO5->GDIR |= (1<<1);
GPIO5->DR &= (1<<1);
}
/* 蜂鸣器控制函数 */
void beep_switch(int status)
{
if(status == ON)
GPIO5->DR |= (1<<1);
else if(status ==OFF)
GPIO5->DR &= (1<<1);
}
bsp_beep.h:
#ifndef __BSP_BEEP_H
#define __BSP_BEEP_H
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "MCIMX6Y2.h"
void beep_init(void);
void beep_switch(int status);
#endif
二,main文件
main.c:
#include "main.h"
#include "bsp_delay.h"
#include "bsp_led.h"
#include "bsp_clk.h"
#include "bsp_beep.h"
/*
* @description : mian函数
* @param : 无
* @return : 无
*/
int main(void)
{
clk_enable(); /* 使能所有的时钟 */
led_init(); /* 初始化led */
while(1) /* 死循环 */
{
led_off(); /* 关闭LED */
beep_switch(ON);
delay(500); /* 延时500ms */
led_on(); /* 打开LED */
beep_switch(OFF);
delay(500); /* 延时500ms */
}
return 0;
}
main.h:
#ifndef __MAIN_H
#define __MAIN_H
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "MCIMX6Y2.h"
#include "cc.h"
#endif
三,makefile文件
CROSS_COMPILE ?= arm-linux-gnueabihf-
TARGET ?= beep
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCOPY := $(CROSS_COMPILE)objcopy
OBJDUMP := $(CROSS_COMPILE)objdump
#\表示此行没写完,下一行也是
INCDIRS := imx6u \
bsp/clk \
bsp/led \
bsp/delay \
bsp/beep #添加了这一步
SRCDIRS := project \
bsp/clk \
bsp/led \
bsp/delay \
bsp/beep #添加了这一步
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) -Timx6u.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)
print:
@echo INCLUDE = $(INCLUDE)
@echo SFILES = $(SFILES)
@echo CFILES = $(CFILES)
@echo SFILENDIR = $(SFILENDIR)
@echo CFILENDIR = $(CFILENDIR)
@echo SOBJS = $(SOBJS)
@echo COBJS = $(COBJS)
@echo OBJS = $(OBJS)