星空网 > 软件开发 > 操作系统

驱动01.LED

1.写出leds_open,leds_write函数
2.1告诉内核这几个函数的存在?定义一个结构体file_operations
2.2把这个结构体告诉内核?用register_chrdev(major,name,file_operations)
    //将主设备号与file_operations结构一起向内核注册
    //major=register_chrdev(0,name,file_operations)表示major是由系统自动分配的;
3.谁来调用register_chrdev?有驱动的入口函数first_drv_init
4.怎么知道入口函数就是first_drv_init?使用module_init函数来修饰入口函数,\
    内核启动时,先寻找module_init这个结构体。eg:module_init(first_drv_init)
5.当然,也会存在注销相应函数的操作;
    eg:unregister_chrdev(major, "first_drv")
    module_exit(first_drv_exit)
6.仿照其他程序加一些必要的头文件
7.如果要使驱动程序可以支持自动创建/dev/xxx,则驱动程序中需支持mdev机制。
    7.1由mdev根据系统信息创建设备节点(sys/class/firstdev),需要定义两个结构体
        static struct class *firstdrv_class;
        static struct class_device    *firstdrv_class_dev;
    7.2在first_drv_init内加入以下两条代码
        firstdrv_class = class_create(THIS_MODULE, "firstdrv");
        firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
    同理,在first_drv_exit内加入以下两条代码
        class_device_unregister(firstdrv_class_dev);
        class_destroy(firstdrv_class);
/*猜测:将firstdrv放在firstdrv_class这个结构体里面,然后根据这个使用class_device_create创建设备节点*/
8.由于驱动程序不能直接操作物理地址,需要操作虚拟地址,则需要一个物理地址到虚拟地址的映射。
查看2440手册,得出相应的物理地址,然后使用iorema函数完成映射,结束时使用iounmap撤销;
eg:gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);//用volatile防止编译器优化,必须每一次都来检测。
        iounmap(gpfcon);
9.最后加上MODULE_LICENSE("GPL");//务必在ko驱动中追加此声明,否则insmod驱动时将不能与/proc/kallsyms中的符号正常连接
    可以modinfo xxx.ko查看其依赖的模块,可知其中licens依赖于GPL
10.修改makefile的最后一行为:obj-m    += first_drv.o
     且把first_drv放在makefile对应的文件位置,执行make,得到first_drv.ko文件,使用insmod、rmmod、lsmod、modinfo实现对其操作;
11.测试驱动程序
    arm-linux-gcc -o firstdrvtest firstdrvtest.c
    根据测试程序进行操作即可;
12.因为使用的是2.6.22.6的内核,要使用3.4.5的gcc版本来交叉编译,否则无法运行。

 1 #include <linux/module.h> 2 #include <linux/kernel.h> 3 #include <linux/fs.h> 4 #include <linux/init.h> 5 #include <linux/delay.h> 6 #include <asm/uaccess.h> 7 #include <asm/irq.h> 8 #include <asm/io.h> 9 #include <asm/arch/regs-gpio.h>10 #include <asm/hardware.h>11 12 13 static struct class *ptFirstdrvClass;14 static struct class_device *ptFirstdrvClassDev;15 16 volatile unsigned long *pulgpfcon = NULL;17 volatile unsigned long *pulgpfdat = NULL;18 19 static int firstdrv_open(struct inode *inode, struct file *file)20 {  21     //printk("first_drv_open\n");22   /* 配置GPF4,5,6为输出 */23   *pulgpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));24   *pulgpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));25   return 0;26 }27 28 static ssize_t firstdrv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)29 {30   int val;31 32   //printk("first_drv_write\n");33 34   copy_from_user(&val, buf, count); //  copy_to_user();35 36   if (val == 1)37   {38     // 点灯39     *pulgpfdat &= ~((1<<4) | (1<<5) | (1<<6));40   }41   else42   {43     // 灭灯44     *pulgpfdat |= (1<<4) | (1<<5) | (1<<6);45   }46   47   return 0;48 }49 50 static struct file_operations firstdrv_ops ={51   .owner  = THIS_MODULE,  /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */52     .open  = firstdrv_open,   53   .write  =  firstdrv_write,  54 55 };56 57 int g_iMajor;58 static int firstdrv_init(void)59 {60   g_iMajor = register_chrdev(0,"first_drv",&firstdrv_ops);61   ptFirstdrvClass = class_create(THIS_MODULE, "firstdrv");62   ptFirstdrvClassDev = class_device_create(ptFirstdrvClass, NULL, MKDEV(g_iMajor, 0), NULL, "xyz"); /* /dev/xyz */63 64   pulgpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);65   pulgpfdat = pulgpfcon + 1;66 67   return 0;68 69 70 }71 72 static int firstdrv_exit(void)73 {74   unregister_chrdev(g_iMajor, "first_drv");75   class_device_unregister(ptFirstdrvClassDev);76   class_destroy(ptFirstdrvClass);77 78   iounmap(pulgpfcon);79 80   return 0;81   82 }  83 84 85 86 module_init(firstdrv_init);87 88 module_exit(firstdrv_exit);89 90 MODULE_LICENSE("GPL");

 




原标题:驱动01.LED

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

如何快速获取VATNumber号码:https://www.kjdsnews.com/a/1375586.html
如何快速获取VATNumber信息:https://www.kjdsnews.com/a/1375587.html
如何快速申请中国VAT税号:https://www.kjdsnews.com/a/1375588.html
如何快速申请VAT税号?:https://www.kjdsnews.com/a/1375589.html
亚马逊给部分商品贴特殊标签;速卖通3月周年庆大促正式开卖:https://www.kjdsnews.com/a/1375590.html
货之家:进保税仓有什么好处?跨境电商货物进保税仓:https://www.kjdsnews.com/a/1375591.html
淘宝给商家发了个“大红包” :https://www.kjdsnews.com/a/1836561.html
厦门曾厝垵家庭旅馆哪家最好?:https://www.vstour.cn/a/365183.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流