设为首页 加入收藏

TOP

Linux-2.6驱动程序分层分离概念(一)
2019-09-01 23:08:27 】 浏览:50
Tags:Linux-2.6 驱动程序 分层 分离 概念

下面以一个按键的实验作为驱动分离时间简单学习:

 1 #include <linux/module.h>
 2 #include <linux/version.h>
 3 
 4 #include <linux/init.h>
 5 
 6 #include <linux/kernel.h>
 7 #include <linux/types.h>
 8 #include <linux/interrupt.h>
 9 #include <linux/list.h>
10 #include <linux/timer.h>
11 #include <linux/init.h>
12 #include <linux/serial_core.h>
13 #include <linux/platform_device.h>
14 
15 
16 /* 分配/设置/注册一个platform_device */
17 
18 static struct resource led_resource[] = {
19     [0] = {
20         .start = 0x56000050,
21         .end   = 0x56000050 + 8 - 1,
22         .flags = IORESOURCE_MEM,
23     },
24     [1] = {
25         .start = 5,
26         .end   = 5,
27         .flags = IORESOURCE_IRQ,
28     }
29 
30 };
31 
32 static void led_release(struct device * dev)
33 {
34 }
35 
36 
37 static struct platform_device led_dev = {
38     .name         = "myled",
39     .id       = -1,
40     .num_resources    = ARRAY_SIZE(led_resource),
41     .resource     = led_resource,
42     .dev = { 
43         .release = led_release, 
44     },
45 };
46 
47 static int led_dev_init(void)
48 {
49     platform_device_register(&led_dev);
50     return 0;
51 }
52 
53 static void led_dev_exit(void)
54 {
55     platform_device_unregister(&led_dev);
56 }
57 
58 module_init(led_dev_init);
59 module_exit(led_dev_exit);
60 
61 MODULE_LICENSE("GPL");
led_dev.c
  1 /* 分配/设置/注册一个platform_driver */
  2 
  3 #include <linux/module.h>
  4 #include <linux/version.h>
  5 
  6 #include <linux/init.h>
  7 #include <linux/fs.h>
  8 #include <linux/interrupt.h>
  9 #include <linux/irq.h>
 10 #include <linux/sched.h>
 11 #include <linux/pm.h>
 12 #include <linux/sysctl.h>
 13 #include <linux/proc_fs.h>
 14 #include <linux/delay.h>
 15 #include <linux/platform_device.h>
 16 #include <linux/input.h>
 17 #include <linux/irq.h>
 18 #include <asm/uaccess.h>
 19 #include <asm/io.h>
 20 
 21 static int major;
 22 
 23 
 24 static struct class *cls;
 25 static volatile unsigned long *gpio_con;
 26 static volatile unsigned long *gpio_dat;
 27 static int pin;
 28 
 29 static int led_open(struct inode *inode, struct file *file)
 30 {
 31     //printk("first_drv_open\n");
 32     /* 配置为输出 */
 33     *gpio_con &= ~(0x3<<(pin*2));
 34     *gpio_con |= (0x1<<(pin*2));
 35     return 0;    
 36 }
 37 
 38 static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
 39 {
 40     int val;
 41 
 42     //printk("first_drv_write\n");
 43 
 44     copy_from_user(&val, buf, count); //    copy_to_user();
 45 
 46     if (val == 1)
 47     {
 48         // 点灯
 49         *gpio_dat &= ~(1<<pin);
 50     }
 51     else
 52     {
 53         // 灭灯
 54         *gpio_dat |= (1<<pin);
 55     }
 56     
 57     return 0;
 58 }
 59 
 60 
 61 static struct file_operations led_fops = {
 62     .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
 63     .open   =   led_open,     
 64     .write    =    led_write,       
 65 };
 66 
 67 static int led_probe(struct platform_device *pdev)
 68 {
 69     struct resource        *res;
 70 
 71     /* 根据platform_device的资源进行ioremap */
 72     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 73     gpio_con = ioremap(res->start, res->end - res->start + 1);
 74     gpio_dat = gpio_con + 1;
 75 
 76     res = platform_get_resource(pdev, IORESOURCE_IRQ, 0)
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Deepin15.8系统下安装QorIQ Linux.. 下一篇helpera64开发板下制作ubuntu roo..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目