tiny6410按键驱动(一)---驱动框架

         看了韦东山老师的视频后,自己动手写了几个驱动。这些驱动都是照着韦东山老师程序的框架写的,但绝非复制粘贴。之前我看了视频,再看看代码,感觉蛮简单的,以为自己会了。但是当我自己写的时候才发现,经常出现各种错误。我用的开发板是tiny6410,韦老师用的是2440。我板子上用的内核是linux-2.6.38,韦老师用的是linux-2.6.22.6,所以有些内核函数有些区别。还有一个奇葩的问题是我在开发板上执行rmmod命令时,无法卸载驱动,就那个问题纠结我我一天,开始一直怀疑是我的程序的问题,后来把自己的程序简化到仅仅只剩个驱动框架,加载到开发板上,可是在卸载是执行rmmod命令时,还是出错。最后我没办法,把开发板自己带的驱动编译后加载,然后执行rmmod命令卸载,还是出错,这时我确定不是程序的问题了。后来百度很久,才知道是开发板自带的rmmod工具的问题。我自己也把那篇博客转载了。

     在学习驱动的过程中,除了看韦老师的视频,还有一本书值得一看,名叫《LINUX设备驱动程序》,也就是传说中的LDDR3,是个外国人写的 ,之前那本书我一直看不懂,也觉得很枯燥,看了视频后,在我写驱动的过程中,用到哪方面的知识就看书上的对应部分,发现那书写的确实很好。学校图书馆关于linux驱动相关的书能借的我都借了,感觉写的都没LDDR3好。在网上LDDR3的评价也很高,真不愧称之为经典。

     这几天我写了7个版本的按键驱动,一个比一个完善。先贴个驱动框架吧,代码如下:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>

#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>

#include <plat/gpio-cfg.h>
#include <mach/gpio-bank-n.h>
#include <mach/gpio-bank-l.h>


static unsigned button_major;
static dev_t button_id;  /*设备号*/

static struct cdev button_cdev;
static struct class *button_class;
static struct device *button_device;

static int myButton_open(struct inode * inode, struct file * file)
{
 
return 0;
}

static int myButton_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_pos)
{
return 0;

}

struct file_operations button_fiops=
{
.owner = THIS_MODULE,
.open  = myButton_open,
.write = myButton_write,


};

static int myButton_init(void)
{
    alloc_chrdev_region(&button_id, 0, 2,"myButton_drv" );/*动态分配设备号,存在变量button_id里*/  
	button_major = MAJOR(button_id);
	 
	cdev_init(&button_cdev, &button_fiops); /*初始化设备,看cdev_init源码知,这行代码其实质是:&button_dev->ops = &button_fiops*/
	cdev_add(&button_cdev, button_id, 2);  /*这行代码的实质是:&button_cdev->dev = button_id , &button_cdev->count = 2*/
    /*以上几行代码就是在填充结构体struct device变量button_cdev*/
	button_class = class_create(THIS_MODULE, "myButton");
	button_device = device_create(button_class, NULL, MKDEV(button_major , 0), NULL, "myButton0");  /*创建设备节点/dev/myButton0*/
	button_device = device_create(button_class, NULL, MKDEV(button_major , 1), NULL, "myButton1");  /*创建设备节点/dev/myButton1*/
  
    printk("myButton init succeed\n");
    return 0;
}
static void myButton_exit(void)
{
/* 以下几行是撤销资源,跟上面那个init函数时反着来的*/
device_destroy(button_class, MKDEV(button_major , 0));
device_destroy(button_class, MKDEV(button_major , 1));
class_destroy(button_class);
cdev_del(&button_cdev);
unregister_chrdev_region(button_id, 2);

printk("myButton exit succeed\n");

}

module_init(myButton_init);
module_exit(myButton_exit);
MODULE_LICENSE("GPL");

Makefil文件如下:

obj-m := mybutton_drv.o
KDIR :=/home/lijunliang/windows-files/linux-tiny6410
all :
	make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm
clean :
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值