平台设备驱动:

  包含BUS(总线)、DEVICE、DRIVER。

  DEVICE:硬件相关的代码

  DRIVER:比较稳定的代码  

  BUS有一个driver链表和device链表。

    ①把device放入bus的device链表中

    ②从bus的drv链表中取出每一个drv,用bus的match函数判断drv能否支持dev。

    ③如果可以执行,则调用probe函数。

  driver和device类似。

device驱动程序代码:

/**
* file name: led_dev.c
*/
#include <linux/platform_device.h>
#include <asm/mach/arch.h>
#include <asm/mach/map.h> /* 分配注册一个平台device结构体 */
static struct resource led_resources[] = {
[] = {
.start= 0x56000050,
.end= 0x56000050 + - ,
.flags= IORESOURCE_MEM,
},
[] = {
.start= , //第4个引脚
.end= ,
.flags= IORESOURCE_IRQ,
},
}; void led_release(struct device * dev)
{ } static struct platform_device led_dev = {
.name= "myled",
.id= -,
.resource= led_resources,
.num_resources= ARRAY_SIZE(led_resources),
.dev = {.release = led_release,},
}; static int led_dev_init(void)
{
platform_device_register(&led_dev); return ;
} static void led_dev_exit(void)
{
platform_device_unregister(&led_dev);
return ;
} module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");

driver驱动程序代码:

/**
* file name:led_driver.c
*/
#include <linux/platform_device.h>
#include <asm/mach/arch.h>
#include <asm/mach/map.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <linux/fs.h> static int major; struct class *led_class;
struct class_device *led_class_dev;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin; static int led_open(struct inode *inode, struct file *file)
{
*gpio_con &= ~(0x3<<(pin*));
*gpio_con |= 0x1<<(pin*);
return ;
} ssize_t led_write(struct file *file, const char *buff, size_t len, loff_t *pos )
{
int val;
copy_from_user(&val, buff, );
val &= 0x1;
*gpio_dat &= ~(<<pin);
*gpio_dat |= (val<<pin); return ;
} struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
}; static int led_probe(struct platform_device *pdev)
{
struct resource *res; printk("led drv probe\n"); res = platform_get_resource(pdev,IORESOURCE_MEM,);
gpio_con = ioremap(res->start,res->end-res->start + );
gpio_dat = gpio_con + ; res = platform_get_resource(pdev,IORESOURCE_IRQ,);
pin = res->start; major = register_chrdev(,"myled", &led_fops );
led_class = class_create(THIS_MODULE,"myled");
led_class_dev = class_device_create(led_class,NULL,MKDEV(major,),NULL,"led%d",pin); return ;
} static int led_remove(struct platform_device *pdev)
{
iounmap(gpio_con);
class_device_unregister(led_class_dev);
class_destroy(led_class);
unregister_chrdev( major,"myled");
printk("led remove\n");
return ;
} struct platform_driver led_drv = {
.driver={
.owner=THIS_MODULE,
.name = "myled",
},
.probe = led_probe,
.remove= led_remove,
}; static int led_drv_init(void)
{
platform_driver_register(&led_drv);
return ;
} static void led_drv_exit(void)
{
platform_driver_unregister(&led_drv);
} module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");

sd

嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动的更多相关文章

  1. 驱动程序分层分离概念_总线驱动设备模型_P

    分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...

  2. 嵌入式linux的学习之路[转]

    我认为的一条学习嵌入式Linux的路: 1)学习 Linux系统安装. 常用命令.应用程序安装. 2) 学习 Linux 下的 C 编程.这本书必学<UNIX 环境高级编程>.<UN ...

  3. linux字符设备驱动学习笔记(一):简单的字符设备驱动

    最近在鼓捣lnux字符设备驱动,在网上搜集的各种关于linux设备驱动的代码和注释,要么是针对2.4的,要么是错误百出,根本就不能运行成功,真希望大家在发博客的时候能认真核对下代码的正确性,特别是要把 ...

  4. Linux-2.6驱动程序分层分离概念

    下面以一个按键的实验作为驱动分离时间简单学习: #include <linux/module.h> #include <linux/version.h> #include &l ...

  5. 【Linux高级驱动】linux设备驱动模型之平台设备驱动机制

    [1:引言: linux字符设备驱动的基本编程流程] 1.实现模块加载函数  a.申请主设备号    register_chrdev(major,name,file_operations);  b.创 ...

  6. Linux驱动之平台设备驱动模型简析(驱动分离分层概念的建立)

    Linux设备模型的目的:为内核建立一个统一的设备模型,从而有一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...

  7. Linux Platform devices 平台设备驱动

    设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196 本文主要参考:http://www.wowotech.net/devi ...

  8. Qt 学习之路 2(29):绘制设备

    Qt 学习之路 2(29):绘制设备 豆子 2012年12月3日 Qt 学习之路 2 28条评论 绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说 ...

  9. 【Linux高级驱动】平台设备驱动机制的编程流程与编译进内核

    [平台设备驱动机制的编程流程] [如何将驱动静态的编译进内核镜像] 1.添加资源(dev-led.c) 1.1:一般来说,系统习惯上将资源放在arch/arm/plat-samsung/目录中 cp ...

随机推荐

  1. JAVA collection集合之 扑克牌游戏

    主要内容:这里使用collection集合,模拟香港电影中大佬们玩的扑克牌游戏. 1.游戏规则:两个玩家每人手中发两张牌,进行比较.比较每个玩家手中牌最大的点数,大小由A-2,点数大者获胜.如果点数相 ...

  2. [转载]C#使用Interlocked进行原子操作

    原文链接:王旭博客 » C# 使用Interlocked进行原子操作 什么是原子操作? 原子(atom)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operat ...

  3. CSS3关于background-size属性

    bachground-size属性就是定义背景图片的大小,其值有:auto , 像素值 , 百分比 , cover  ,  contain . background-size: auto,默认值,以图 ...

  4. CSS3D效果

    效果如本博客中右边呢个浅色框框,来自webpack首页(IE绕路0_0) github地址:http://wjf444128852.github.io/demo02/css3/css3d/ 思路: 1 ...

  5. 关于jquery中用函数来设置css样式

    关于jquery中用函数来设置css样式 2016-12-21 23:27:55 1.jquery语法 $(selector).css(name,function(index,value)) name ...

  6. Android app被系统kill的场景

    何时发生 当我们的app被切到后台的时候,比如用户按下了home键或者切换到了别的应用,总之是我们的app不再和用户交互了,这个时候对于我们的app来说就是什么事情都可能发生的时候了,因为系统会认为你 ...

  7. iOS开发 解决UITapGestureRecognizer手势与UITableView的点击事件的冲突

    该篇文章摘自我的新浪博客,原文地址为: http://blog.sina.com.cn/s/blog_dcc636350102wavx.html UITableView 拥有属于自己的点击事件,在将一 ...

  8. Petya勒索木马

    同事小学妹神好奇心,在陌生群里下载了个软件,接下来就是自动重启无法开机. 找我一看,凭我专业帮妹纸装系统多年的经验,起初也不觉得有啥困难,兼容模式下重启,接下来出现这个: 按下any key后: 试了 ...

  9. django中html过滤器filter

    http://blog.csdn.net/iloveyin/article/details/49560559 safe让Html标签以及一些特殊符号(如<)生效,下面以例子说明: # value ...

  10. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分的解决办法方案

    最近操作系统不断地报异常或错误,可能是用得太久了,而刚好工作比较多又不想重新安装系统,但用一段时间后(一天大概有两次)系统就出现各种问题导致无法继续使用真是痛苦万分啊,今天又来了这样的错误: 此实现不 ...