嵌入式Linux学习: platform 设备驱动实验

在Linux中,Platform(平台)机制是一个重要的设备驱动管理框架,它主要在Linux 2.6内核及以后的版本中引入。Platform机制的主要目的是提供一种统一的方式来管理那些不直接挂靠在传统物理总线(如USB、PCI、I2C、SPI等)上的设备,如SoC(系统芯片)内部集成的外设控制器或挂接在SoC内存空间的外设。以下是对Linux中Platform机制的简要介绍:

1. Platform总线的概念

  • 虚拟总线:Platform总线是一种虚拟的、抽象出来的总线,实际中并不存在这样的物理总线。它是Linux设备驱动模型为了保持设备驱动的统一性而虚拟出来的。
  • 作用:通过将那些不依赖于传统物理总线的设备映射到Platform总线上,Linux能够对这些设备进行统一的管理,从而保持设备驱动模型的完整性和一致性。

2. Platform机制的主要组成部分

  • platform_bus:这是一个全局的bus_type结构体实例,用于描述Platform总线。它包含了总线的基本信息和匹配函数等。
  • platform_device:描述Platform总线上的设备。它包含了设备的名称、ID、资源(如I/O端口、中断号等)以及设备特定的信息(如platform_data)。
  • platform_driver:描述与Platform设备相匹配的驱动程序。它包含了用于探测(probe)、移除(remove)、挂起(suspend)和恢复(resume)设备的函数指针,以及指向设备驱动名称或ID表的指针。

3. Platform机制的工作流程

  • 设备注册:首先,需要将Platform设备通过platform_device_register函数注册到系统中。在注册过程中,系统会为设备分配必要的资源,并在sysfs中创建相应的设备节点。
  • 驱动注册:接着,将Platform驱动通过platform_driver_register函数注册到系统中。在注册过程中,系统会检查是否有与当前驱动匹配的设备,如果有,则调用驱动的probe函数进行设备的初始化和配置。
  • 设备与驱动的匹配:Platform机制通过设备名称或ID表来匹配设备和驱动。如果设备名称与驱动中指定的名称相同,或者设备的ID与驱动ID表中的某个条目匹配,则认为该设备与驱动相匹配。
  • 驱动加载与设备初始化:一旦设备和驱动匹配成功,系统就会调用驱动的probe函数来加载驱动并初始化设备。在probe函数中,驱动可以获取设备资源、配置设备寄存器以及注册中断等。

4. 匹配规则

在这里插入图片描述
在这里插入图片描述
相关函数使用方法这里不再叙述,请自行查找。

在这里插入图片描述

5. Platform机制的优点

  • 统一性:Linux内核支持多种类型的硬件设备,这些设备可能通过不同的物理总线(如USB、PCI、SPI等)连接,也可能直接集成在SoC(系统芯片)内部,没有明确的物理总线接口。Platform机制提供了一种统一的方式来管理这些不同类型的设备,无论是挂靠在物理总线上的还是集成在SoC内部的,都可以通过Platform总线进行统一管理。
  • 灵活性:Platform机制允许设备和驱动分别注册,并通过匹配机制动态地建立联系。这使得驱动的开发和部署更加灵活。
  • 可移植性:Platform机制将驱动的实现和资源分离,使得驱动代码更加独立和可移植。

综上所述,Linux中的Platform机制是一种重要的设备驱动管理框架,它通过虚拟的Platform总线来统一管理那些不依赖于传统物理总线的设备,从而提高了设备驱动的统一性、灵活性和可移植性。

6. 实验代码

本次实验所用的是韦东山I.MX6U开发板,实验功能是无设备树的platform来驱动led。

platform_device_led.c文件
#include "linux/ioport.h"
#include "linux/printk.h"
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define GPIO5_DR_BASE 0x020AC000
#define GPIO5_GDIR_BASE 0X020AC004
#define SW_MUX_GPIO5_IO03_BASE 0x02290014

#define REGISTER_LEN 3


void led_release(struct device *dev){
    printk("led_release\r\n");
}
static struct resource led_resources[] = {
   [0] = {
    .start =    GPIO5_DR_BASE,
    .end   =    GPIO5_DR_BASE + REGISTER_LEN -1,
    .flags =    IORESOURCE_MEM,
   },
    [1] = {
    .start =    GPIO5_GDIR_BASE,
    .end   =    GPIO5_GDIR_BASE + REGISTER_LEN -1,
    .flags =    IORESOURCE_MEM,
   },
    [2] = {
    .start =    SW_MUX_GPIO5_IO03_BASE,
    .end   =    SW_MUX_GPIO5_IO03_BASE + REGISTER_LEN -1,
    .flags =    IORESOURCE_MEM,
   },
};

static struct platform_device led_device = {
    .name   = "imx6ull_led",
    .id     = -1,
    .dev    = {
            .release = led_release,
    },
    .num_resources = ARRAY_SIZE(led_resources),
    .resource =   led_resources,
};

/*
    设备加载
*/
static int __init led_init(void)
{
    // 注册platform设备
   return platform_device_register(&led_device);
}

/*
     设备卸载 
*/
static void __exit led_exit(void)
{
    // 卸载platform设备
    platform_device_unregister(&led_device);
}


module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pumpk1n");

platform_driver_led.c文件
#include "asm-generic/errno-base.h"
#include "asm-generic/int-ll64.h"
#include "linux/compiler.h"
#include "linux/err.h"
#include "linux/export.h"
#include "linux/ioport.h"
#include "linux/kdev_t.h"
#include "linux/leds.h"
#include "linux/printk.h"
#include "linux/stddef.h"
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define LEDDEV_CNT  1 // 设备号长度
#define LEDDEV_NAME "plat_led" // 设备名
#define LED_OFF     0
#define LED_ON      1   


/*

#define GPIO5_DR_BASE 0x020AC000
#define GPIO5_GDIR_BASE 0X020AC004
#define SW_MUX_GPIO5_IO03_BASE 0x02290014

*/

/* 寄存器名 */
static void __iomem *GPIO5_DR;
static void __iomem *GPIO5_GDIR;
static void __iomem *SW_MUX_GPIO5_IO03;


/* leddev 设备结构体 */
struct leddev_dev{
    dev_t devid; // 设备号
    struct cdev cdev; // cdev
    struct class *clazz;  // 类
    struct device *device; // 设备
    int major;   //主设备号
};

struct leddev_dev led_dev;

void led_switch(u8 sta){
    u32 val;
    if(sta == LED_ON)
    {
        val = readl(GPIO5_DR);
        val &= ~(1 << 3);
        writel(val, GPIO5_DR);
    }
    else if(sta == LED_OFF)
    {
        val = readl(GPIO5_DR);
        val |= (1 << 3);
        writel(val, GPIO5_DR);
    }
}


static int led_open(struct inode *inode, struct file *file)
{
    file->private_data = &led_dev;
    return 0;
}

ssize_t led_write (struct file *file, const char __user *buf, 
                size_t size, loff_t *offset){
                u_char dataBuf[1];
                u_char led_sta;
                int ret;

                ret  =  copy_from_user(dataBuf, buf, size);
                if(ret < 0){
                    return -EFAULT;
                }

                led_sta = dataBuf[0];
                if(led_sta == LED_ON)
                    led_switch(LED_ON);
                else if(led_sta == LED_OFF)
                    led_switch(LED_OFF);
                return 0;
}


static const struct file_operations led_fops = {
	.owner		= THIS_MODULE,
	.open		= led_open,
    .write      = led_write,
};


/* platform的probe函数,
   当驱动与设备匹配成功后执行此函数
 */
static int led_probe(struct platform_device *pdev){
     
     int i;
     u32 val;
     int resize[3];
     struct resource *led_source[3];

     printk("led driver and device has matched!\r\n");
     /* 1.获取资源 */
     for(i = 0; i < 3; i++)
     {
        led_source[i] = platform_get_resource(pdev,IORESOURCE_MEM, i);
        if(!led_source[i])
        { // 资源为空
            dev_err(&pdev->dev, "No MEM resource for always on\r\n");
            return -ENXIO;
        }
        resize[i] = resource_size(led_source[i]);
     }

     /* 寄存器地址映射 */
      GPIO5_DR = ioremap(led_source[0]->start, resize[0]);
      GPIO5_GDIR = ioremap(led_source[1]->start, resize[1]);
      SW_MUX_GPIO5_IO03 = ioremap(led_source[2]->start,resize[2]);


      val = readl(GPIO5_GDIR);
      val |= (1 << 3);
      writel(val, GPIO5_GDIR); // 输出模式

      /* 注册字符设备驱动 */
      if(led_dev.major)
      {
        led_dev.devid = MKDEV(led_dev.major, 0);
        register_chrdev_region(led_dev.devid, LEDDEV_CNT, LEDDEV_NAME);
      }else{
        alloc_chrdev_region(&led_dev.devid, 0, LEDDEV_CNT,LEDDEV_NAME);
        led_dev.major = MAJOR(led_dev.devid);
      }

      /* 初始化CDEV */
      led_dev.cdev.owner = THIS_MODULE;
      cdev_init(&led_dev.cdev, &led_fops);

      /* 添加一个CDEV */
      cdev_add(&led_dev.cdev, led_dev.devid,LEDDEV_CNT);

      /* 创建类 */
      led_dev.clazz = class_create(THIS_MODULE, LEDDEV_NAME); // /dev/plat_led
      if(IS_ERR(led_dev.clazz))
      {
        return PTR_ERR(led_dev.clazz);
      }

      /* 创建设备 */
     led_dev.device = device_create(led_dev.clazz,NULL,led_dev.devid,NULL,LEDDEV_NAME);
    if(IS_ERR(led_dev.device))
    {
        return PTR_ERR(led_dev.device);
    }
    return 0;
}

/* 移除platform驱动执行 */
int led_remove(struct platform_device * pdevice){

    iounmap(GPIO5_DR);
    iounmap(GPIO5_GDIR);
    iounmap(SW_MUX_GPIO5_IO03);
    
    cdev_del(&led_dev.cdev);
    unregister_chrdev_region(led_dev.devid,LEDDEV_CNT);
    device_destroy(led_dev.clazz, led_dev.devid);
    class_destroy(led_dev.clazz);

    return 0;
}


/* platform_driver结构体 */
static struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ull_led",
    },
    .probe    = led_probe,
    .remove   = led_remove
};

static int __init leddriver_init(void)
{
    return platform_driver_register(&led_driver);
}


static void __exit leddriver_exit(void)
{
    platform_driver_unregister(&led_driver);
}

module_init(leddriver_init);
module_exit(leddriver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pumpk1n");
测试文件: led_test.c

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
/***************************************************************
 描述 : platform 驱动驱测试 APP。
***************************************************************/
#define LEDOFF 0
#define LEDON 1

/*
* @description : main 主程序
* @param - argc : argv 数组元素个数
* @param - argv : 具体参数
* @return : 0 成功;其他 失败
*/
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[2];
if(argc != 3){
 printf("Error Usage!\r\n");
return -1;
}

filename = argv[1];
/* 打开 led 驱动 */
 fd = open(filename, O_RDWR);
if(fd < 0){
 printf("file %s open failed!\r\n", argv[1]);
 return -1;
}

databuf[0] = atoi(argv[2]); /* 要执行的操作:打开或关闭 */
 retvalue = write(fd, databuf, sizeof(databuf));
 if(retvalue < 0){
printf("LED Control Failed!\r\n");
 close(fd);
return -1;
 }

retvalue = close(fd); /* 关闭文件 */
 if(retvalue < 0){
printf("file %s close failed!\r\n", argv[1]);
 return -1;
}
return 0;
}

编译 led_test.c文件为 led_test可执行文件

arm-buildroot-linux-gnueabihf-gcc led_test.c -o led_test

Makefile文件
KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
CURRENT_PATH :=  `pwd`

obj-m := platform_device_led.o platform_driver_led.o

build: kernel_modules

kernel_modules:
	$(MAKE) -C $(KERN_DIR) M=$(CURRENT_PATH) modules
clean:
	$(MAKE) -C $(KERN_DIR) M=$(CURRENT_PATH) clean
挂载网络文件系统
book@100ask:~/100ask_imx6ull-sdk/Linux-4.9.88/hello_drv/platform_demo$  cp platform_driver_led.ko platform_device_led.ko led_test ~/nfs_rootfs/
[root@100ask:/mnt]# mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
设置交叉编译工具链

在串口连接终端里配置

export ARCH=arm
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin

查看设备是否存在

[root@100ask:/mnt]# ls /dev/plat_led
/dev/plat_led
[root@100ask:/mnt]# ls /mnt/ 
drivers_projects  hello  input_demo  led_drv.ko  led_test  platform_device_led.ko  platform_driver_led.ko  platform_led.ko

在开发板上加载驱动程序

insmod /mnt/platform_driver_led.ko
insmod /mnt/platform_device_led.ko

测试

/mnt/led_test /dev/plat_led 1     ## LED点亮
/mnt/led_test /dev/plat_led 0     ## LED熄灭

相关推荐

  1. 嵌入linux驱动开发之网络设备驱动

    2024-07-22 02:42:02       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 02:42:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 02:42:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 02:42:02       45 阅读
  4. Python语言-面向对象

    2024-07-22 02:42:02       55 阅读

热门阅读

  1. Xcode代码静态分析:构建无缺陷代码的秘诀

    2024-07-22 02:42:02       18 阅读
  2. 半监督学习 原理与代码实例讲解

    2024-07-22 02:42:02       19 阅读
  3. 初学Mybatis之 CRUD 增删改查

    2024-07-22 02:42:02       13 阅读
  4. TensorFlow 的基本概念和使用场景

    2024-07-22 02:42:02       16 阅读
  5. Windows图形界面(GUI)-DLG-C/C++ - 列表视图(ListView)

    2024-07-22 02:42:02       19 阅读
  6. 大规模语言模型从理论到实践 高效模型微调

    2024-07-22 02:42:02       17 阅读
  7. [11 Qt CMake开发]

    2024-07-22 02:42:02       21 阅读
  8. 显示隐式-》初始化 & 类型转换

    2024-07-22 02:42:02       16 阅读
  9. ReentrantLock源码

    2024-07-22 02:42:02       17 阅读
  10. 向量数据库Milvus

    2024-07-22 02:42:02       20 阅读
  11. 在 ASP.NET Core Web API 中实现审计跟踪

    2024-07-22 02:42:02       14 阅读
  12. leetcode-56. 合并区间

    2024-07-22 02:42:02       17 阅读
  13. 后端开发: 如何去使用公共组件

    2024-07-22 02:42:02       14 阅读