嵌入式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 ...
随机推荐
- 【C语言学习趣事】_33_关于C语言和C++语言中的取余数(求模)的计算_有符号和无符号数的相互转换问题
最近再次复习C++语言,用的教材是<C++ Primer>这本教材, 看到第二章的时候,里面有个问题困扰了我. 于是想上网查查怎么回事, 结果看了很久都没有得到一个满意的答案. 书上有这么 ...
- 第 31 章 项目实战-PC 端固定布局[5]
学习要点: 1.底部区域 2.说明区域 3.版权及证件区 主讲教师:李炎恢 本章主要开始使用学习用 HTML5 和 CSS3 来构建 Web 页面,第一个项目采用 PC 端固定布局来实现. 一.底部区 ...
- bootstrap分页
使用bootstrap分页插件,如下代码是html代码 <title>分页</title> <link href="page/bootstrap-3.3.5-d ...
- 容器--TreeMap
一.概述 在Map的实现中,除了我们最常见的KEY值无序的HashMap之外,还有KEY有序的Map,比较常用的有两类,一类是按KEY值的大小有序的Map,这方面的代表是TreeMap,另外一种就保持 ...
- 【工匠大道】Git的使用总结
初衷是想将一些常用的代码整理在博客园上,但是考虑到博客园上的代码量多,需要折叠,折叠后就不能直接修改,于是想到了 大家都常用的 gitHub来进行代码的管理. 其实之前我是用过 Osa的git的,但是 ...
- N-Tier Entity Framework开源项目介绍
N-Tier Entity Framework是一个基于微软Entity Framework的N层.NET解决方案. 并且与以下这此技术点无缝集成了: § WCF RIA Ser ...
- phpstorm10.0.3破解版安装教程及汉化方法
phpstorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查.不但是php开发的利器,前端开发也是毫不逊色的.下面记录php ...
- 工业串口和网络软件通讯平台(SuperIO 2.1)更新发布
SuperIO 2.1下载 一.SuperIO 的特点: 1) 能够很快的构建自己的通讯平台软件,包括主程序. 2) 设备模块化开发,通过配制文件挂载,即可在平台软件下运行. 3) 设备 ...
- iOS 修改状态栏字体的颜色
在实际开发中,状态栏有时,需要我们自己设置: 比如: 默认状态栏 假如我们开发的view是黑色的,那么效果如图: 状态栏是白底黑字,下面的view是黑底? 这样子真的好吗?说好的和谐社会呢?说好的开发 ...
- this的作用--转载
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...