pxa270&Linux2.6蜂鸣器驱动编写代码

编写按键及蜂鸣器驱动程序,蜂鸣器驱动按照字符设备驱动实现write、read、open、ioctl、release方法。编写测试程序进行测试并实现每按一次KEY1键,蜂鸣器响两声的功能。

beep.c//源文件

/*需要手动创建设备文件/dev/mgc270-led
* mknod /dev/mgc270-led c 253 0
*主设备号有可能不是253 */

#include<linux/module.h>  /*所有模块都需要的头文件*/
#include<linux/init.h>     /*init和exit相关宏*/
#include<linux/kernel.h>   /*所有模块都需要的头文件*/
#include<linux/fs.h>
#include<linux/errno.h>
#include<linux/types.h>
#include<linux/fcntl.h>
#include<linux/device.h>

#include<asm/arch/pxa-regs.h>
#include<asm/arch/hardware.h>

#define DEVICE_NAME "/dev/mgc270-led"
static int ledMajor=0;

#define BEEP  (1<<11)
#define KEY (1<<0)

static int beep = 11;

static int mgc270_led_open(struct inode *inode,struct file *file)
{
	unsigned int i;

		pxa_gpio_mode(beep|GPIO_OUT|GPIO_DFLT_LOW);
		pxa_gpio_mode(KEY|GPIO_OUT|GPIO_DFLT_LOW);
		//GPDR0 =GPDR0|BEEP;
		//GPDR0=GPDR0&(~KEY);
		GPSR0=BEEP;
		printk("mgc270-led open!\n");
		return 0;

}

void Delay(unsigned int dly){
	unsigned int i;
	for(;dly>0;dly--)
		for(i=0;i<50000;i++);
}

void RunBeep(void){
	GPCR0 = BEEP;
	Delay(800);
	GPSR0=BEEP;
	Delay(20);
}

static int mgc270_led_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
	unsigned int i;
	GPDR0 =GPDR0|BEEP;
	GPDR0=GPDR0&(~KEY);
	GAFR0_L =GAFR0_L & (~(0x03<<22));
	GAFR0_L = GAFR0_L&(~(0x03<<0));
	while(1)	{
	
	if(KEY&GPLR0){}
	else{
		for(i=0;i<cmd;i++)
		{
			RunBeep();
			Delay(2000);
		}
	return 0;
	}
}
	//GPSR0 = BEEP;
	Delay(10);
	return 0;
}


static struct file_operations mgc270_led_fops=
{
	.owner =THIS_MODULE,
	.open  =mgc270_led_open,
	.ioctl =mgc270_led_ioctl,

};

static int __init mgc270_led_init(void)
{
	int ret;
	
	
	ret=register_chrdev(0,DEVICE_NAME,&mgc270_led_fops);
	if(ret<0)
	{
		printk(DEVICE_NAME"can't get major number\n");
	}
	else
	{
		printk(DEVICE_NAME"\t %d\n",ret);
		ledMajor=ret;
	}
	return 0;
}

static void __exit mgc270_led_exit(void)
{
	unregister_chrdev(ledMajor,DEVICE_NAME);
	GPSR0 = BEEP;
}

module_exit(mgc270_led_exit);
module_init(mgc270_led_init);

MODULE_LICENSE("Dual BSD/GPL");
beep2test.c//测试文件
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

int main(void)
   {
	int fd,i;
	fd=open("/dev/mgc270-led",O_RDWR);
	if(fd)
	{	
		ioctl(fd,2,NULL);	
		sleep(2);
		//ioctl(fd,2,NULL);
		
	}
	close(fd);
	return 0;
   }



Makefile文件
# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and target modules are listed here.
#gpiodrv-objs := beep2.c
obj-m := beep2.o
else
PWD  := $(shell pwd)
KVER = 2.6.18
KDIR := /pxa270/linux-2.6.18/
all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
	rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值