首页 技术 正文
技术 2022年11月20日
0 收藏 431 点赞 4,424 浏览 3117 个字

bus_drv_dev模型:功能改写只需改dev硬件代码即可,drv不需改写。

LED例子

下面用一个点亮LED的例子来说明这个分离的的例子:

led_dev.c  定义这个平台设备的资源:

static struct resource led_resource[] = {

   [0] = {

    .start  = 0x56000010,  //GPFCON的物理地址

     .end    = 0x56000010 + 8 – 1,

      .flags  = IORESOURCE_MEM,

    },

   [1] = {

     .start  = 6,// F6引脚

    .end    = 6,

     .flags  = IORESOURCE_IRQ,

    },

};

定义一个平台设备:

struct platform_device device_led = {

    .name       = “myled”,

    .id         = -1,

     .num_resources  = ARRAY_SIZE(led_resource),

     .resource       = led_resource,

     .dev={

         .release = led_release,

    },

};

在入口函数中 注册 这个 “平台设备”

static int led_dev_init(void)

{

   platform_device_register(&device_led);

   return 0;  
}

出口函数是卸载这个平台设备

static void led_dev_exit(void)

{

   platform_device_unregister(&device_led);

}

led_drv.c  定义一个平台driver

static struct platform_driver led_drv = {

.probe      = led_probe,

.remove     = led_remove,

.driver     = {

   .name   = “myled”,

      }

};  

这里需要注意的是这个平台的name和dev的平台设备的名字要一致。如果平台设备和平台driver匹配的上,就会调用这个led_driver这个函数。

/* 分配/设置/注册一个platform_driver */

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>

static int major;

static struct class *cls;
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)
{
//printk(“first_drv_open\n”);
/* 配置为输出 */
*gpio_con &= ~(0x3<<(pin*2));
*gpio_con |= (0x1<<(pin*2));
return 0;
}

static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;

copy_from_user(&val, buf, count); //copy_to_user();

if (val == 1)
{
// 点灯
*gpio_dat &= ~(1<<pin);
}
else
{
// 灭灯
*gpio_dat |= (1<<pin);
}

return 0;
}

static struct file_operations led_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = led_open,
.write=led_write,
};

static int led_probe(struct platform_device *pdev)
{
struct resource*res;

/* 根据platform_device的资源进行ioremap */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
gpio_con = ioremap(res->start, res->end – res->start + 1);
gpio_dat = gpio_con + 1;

res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
pin = res->start;

/* 注册字符设备驱动程序 */

printk(“led_probe, found led\n”);

major = register_chrdev(0, “myled”, &led_fops);

cls = class_create(THIS_MODULE, “myled”);

class_device_create(cls, NULL, MKDEV(major, 0), NULL, “led”); /* /dev/led */

return 0;
}

static int led_remove(struct platform_device *pdev)
{
/* 卸载字符设备驱动程序 */
/* iounmap */
printk(“led_remove, remove led\n”);

class_device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, “myled”);
iounmap(gpio_con);

return 0;
}

struct platform_driver led_drv = {
.probe= led_probe,
.remove= led_remove,
.driver= {
.name= “myled”,
}
};

static int led_drv_init(void)
{
platform_driver_register(&led_drv);
return 0;
}

static void led_drv_exit(void)
{
platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE(“GPL”);

这个驱动程序分为左右两边,即dev与drv,在led_dev.中分配,设置,注册一个platform_device,在led_drv中分配,设置,注册一个platform_driver。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892