歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 驅動01.LED,led驅動電源

驅動01.LED,led驅動電源

日期:2017/3/3 17:41:25   编辑:學習Linux

驅動01.LED,led驅動電源

驅動01.LED,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     else
42     {
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");

http://xxxxxx/Linuxjc/1185553.html TechArticle

Copyright © Linux教程網 All Rights Reserved