热门关键字:  ubuntu  分区  函数  Fedora  linux系统进程

当前位置 :| 主页>Linux教程>内核研究>

linux2.6内核下的一个按键中断驱动程序示例

来源: 作者: 时间:2007-12-07 Tag: 点击:



static int __init s3c2410_button_init(void)
{
    int ret;
    set_irq_type(BUTTON_IRQ1,IRQT_FALLING);
    set_irq_type(BUTTON_IRQ2,IRQT_FALLING);

    buttonEvent=buttonEvent_dummy;
   
    ret=register_chrdev(0,DEVICE_NAME,&button_fops);
    if(ret<0) {
    printk("button: can't get major number\n");
    return ret;
     }
    buttonMajor=ret;
#ifdef  CONFIG_DEVFS_FS
 devfs_mk_cdev(MKDEV(buttonMajor,BUTTONMINOR),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,DEVICE_NAME);
#endif
    //buttondev.head=buttondev.tail=0;
    buttondev.buttonStatus=BUTTONSTATUS_1;
    init_waitqueue_head(&(buttondev.wq));
    printk(DEVICE_NAME"initialized\n");
    return 0;
}

static unsigned char buttonRead(void)
{
    unsigned char button_ret;
    button_ret=BUF_TAIL;
    buttondev.tail=INCBUF(buttondev.tail,MAX_BUTTON_BUF);
    return button_ret;
}
   
static void __exit s3c2410_button_eixt(void)
{
 
 #ifdef CONFIG_DEVFS_FS
    devfs_remove(DEVICE_NAME);
   
 #endif
    unregister_chrdev(buttonMajor,DEVICE_NAME);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kision");
MODULE_DESCRIPTION ("the first char device driver");

module_init(s3c2410_button_init);
module_exit(s3c2410_button_eixt);
/*************************end***************************/   
 
2.   当然在编写2.6内核驱动程序之前应该已经自己建立好一个2.6的内核源码树(我这里是基于s3c2410移植的源码树,本处该源码是放在宿主机的 /home/src/linux-2.6.16目录下的),如果没有的话,那么需要自己去建立好这个源码树.自己编写的模块化驱动程序可以不放在内核源码之内,但是此外还需要一个自己编写一个Makefile文件(该文件和上面的button.c文件应放在同一个目录下),其内容如下示:
 
ifneq ($(KERNELRELEASE),)
    obj-m :=button.o
else
    KERNELDIR ?= /home/src/linux-2.6.16
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
endif
 
3.在宿主机的终端下,进入驱动程序目录内,敲入命令:
#make
就会在该目录下生成button.ko文件,这就是2.6内核下生成的驱动加载模块,注意是.ko文件,不同于2.4内核下的.o文件.
把该button.ko文件拷贝到目标板上,在minicom终端下进入该文件目录,敲入:
#insmod button.ko
如果终端显示有buttoninitialized则表示加载成功.
这时可以用命令lsmod查看动态加载模块:
#lsmod
当然,可以用如下命令查看devfs文件系统信息:
#cat /proc/devices
如果用卸载该模块,敲入命令:
#rmmod button
 
4.加载驱动程序后,可以自己再编写一个简单的测试程序,如下:
/**************************start***********************/

#i nclude <sys/stat.h>

#i nclude <fcntl.h>

#i nclude <stdio.h>

#i nclude <sys/time.h>

#i nclude <sys/types.h>

#i nclude <unistd.h>
 

 

main()

{

  

   int retval;

   int fileno;

   int ts, maxfd;

   int  ret= 0,i,j;

   int number;

 

   fileno = open("/dev/button",O_RDWR);

       if (fileno == -1) {

               printf("open device led errr!\n");

               return 0;

              }

   while(1) {

   read(fileno,&number,1);
   printf("key=0x%x\n",number);
    }
  

   close(fileno);

   return 0;

 

}
/**************************end***********************/
 
命名为test.c,并交叉编译该文件:
#arm-linux-gcc test.c -o test
将二进制文件同样拷贝到目标板上,运行:
#./test &
即可看到实验效果.

最新评论共有 4 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表
热点关注
相关文章