嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
平台设备驱动:
包含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驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动的更多相关文章
- 驱动程序分层分离概念_总线驱动设备模型_P
分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...
- 嵌入式linux的学习之路[转]
我认为的一条学习嵌入式Linux的路: 1)学习 Linux系统安装. 常用命令.应用程序安装. 2) 学习 Linux 下的 C 编程.这本书必学<UNIX 环境高级编程>.<UN ...
- linux字符设备驱动学习笔记(一):简单的字符设备驱动
最近在鼓捣lnux字符设备驱动,在网上搜集的各种关于linux设备驱动的代码和注释,要么是针对2.4的,要么是错误百出,根本就不能运行成功,真希望大家在发博客的时候能认真核对下代码的正确性,特别是要把 ...
- Linux-2.6驱动程序分层分离概念
下面以一个按键的实验作为驱动分离时间简单学习: #include <linux/module.h> #include <linux/version.h> #include &l ...
- 【Linux高级驱动】linux设备驱动模型之平台设备驱动机制
[1:引言: linux字符设备驱动的基本编程流程] 1.实现模块加载函数 a.申请主设备号 register_chrdev(major,name,file_operations); b.创 ...
- Linux驱动之平台设备驱动模型简析(驱动分离分层概念的建立)
Linux设备模型的目的:为内核建立一个统一的设备模型,从而有一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...
- Linux Platform devices 平台设备驱动
设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196 本文主要参考:http://www.wowotech.net/devi ...
- Qt 学习之路 2(29):绘制设备
Qt 学习之路 2(29):绘制设备 豆子 2012年12月3日 Qt 学习之路 2 28条评论 绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说 ...
- 【Linux高级驱动】平台设备驱动机制的编程流程与编译进内核
[平台设备驱动机制的编程流程] [如何将驱动静态的编译进内核镜像] 1.添加资源(dev-led.c) 1.1:一般来说,系统习惯上将资源放在arch/arm/plat-samsung/目录中 cp ...
随机推荐
- python之网络编程
本地的进程间通信(IPC)有很多种方式,但可以总结为下面4类: 消息传递(管道.FIFO.消息队列) 同步(互斥量.条件变量.读写锁.文件和写记录锁.信号量) 共享内存(匿名的和具名的) 远程过程调用 ...
- 安卓模拟器genymotion连接eclipse成功但是不显示其中项目
安卓模拟器困了我两三天了,原装模拟器比较慢,忍受不了,查到安卓模拟器的神器——genymotion 按照网上的步骤一步步都安装完毕,最后打开后发现,genymotion界面里面没有找到新建的工程, 这 ...
- php实现设计模式之 中介者模式
<?php /* * 中介者模式:用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用从而使其耦合松散,而且可以独立地改变它们之间的交互 */ /* * 以一个同学qq群为例说明, ...
- 关于依赖注入IOC/DI的感想
之前一直不明白依赖注入有什么好处,甚至觉得它是鸡肋,现在想想,当时真是可笑. 这个想法正如同说接口是没有用处一样. 当整个项目非常庞大,各个方法之间的调用非常复杂,那么,可以想象一下,假设说没有任何的 ...
- java中 try return finally return
finally块里面的代码一般都是会执行的,除非执行 System.exit(int),停止虚拟机,断电. 1.若try代码块里面有return ,假设要return 的值 是A,A为基本类型或者被f ...
- java多线程-线程池
线程池(Thread Pool)对于限制应用程序中同一时刻运行的线程数很有用.因为每启动一个新线程都会有相应的性能开销,每个线程都需要给栈分配一些内存等等. 我们可以把并发执行的任务传递给一个线程池, ...
- 咱小谈CLR
1.什么是CLR CLR(Common Language Runtime)公共语言远行时,是一个可由多种编程语言使用的“远行时”.CLR的核心功能(比如内存管理.程序集加载.安全性.异常处理和线程同步 ...
- CSS3与页面布局学习笔记(六)——CSS3新特性(阴影、动画、渐变、变形( transform)、透明、伪元素等)
一.阴影 1.1.文字阴影 text-shadow<length>①: 第1个长度值用来设置对象的阴影水平偏移值.可以为负值 <length>②: 第2个长度值用来设置对象的阴 ...
- JavaScript中this指向的简单理解
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- 偷偷发请求的ajax
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...