gpio 子系统

这篇具有很好参考价值的文章主要介绍了gpio 子系统。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

linux GPIO子系统

Linux的GPIO子系统驱动框架的组成部分

Linux的GPIO子系统驱动框架由三个主要部分组成:GPIO控制器驱动程序、平台驱动程序和GPIO字符设备驱动程序

GPIO控制器驱动程序
GPIO控制器驱动程序是与硬件相关的代码,用于处理GPIO控制器与系统总线之间的通信。该部分代码通常由芯片厂商提供,以与特定的GPIO硬件交互。这些驱动程序通常以platform_driver结构体的形式提供,其注册和卸载函数被称为platform_driver_probe和platform_driver_remove。

**平台驱动程序**
平台驱动程序是用于与硬件平台交互的代码,用于识别GPIO硬件并将其与相关的GPIO控制器驱动程序关联起来。平台驱动程序通常以platform_device结构体的形式提供,其注册和卸载函数被称为platform_device_add和platform_device_del。

平台驱动程序的主要任务是向系统注册GPIO控制器设备,与GPIO控制器驱动程序进行绑定,并为其分配资源。平台驱动程序还需要为GPIO控制器分配适当的内存,并将其映射到内核地址空间中。

**GPIO字符设备驱动程序**
GPIO字符设备驱动程序提供了用户空间API,使应用程序能够使用GPIO。该驱动程序通常是通过字符设备驱动程序实现的。用户可以通过/sys/class/gpio目录下的文件系统接口,通过读取和写入GPIO寄存器,控制GPIO。

这些部分一起构成了Linux的GPIO子系统驱动框架,使开发人员能够与GPIO硬件交互,并在用户空间中访问GPIO。

驱动例程

以下是一个GPIO设备在设备树中的示例,以及相应的驱动程序。

设备树节点示例:

/ {
    gpio_example {
        compatible = "example,gpio";
        gpio-num = <4>;
        direction = "out";
    };
};

在这个设备树节点中,我们定义了一个名为gpio_example的节点,它包含了以下属性:

compatible:用于指定设备的兼容性字符串,可以用于匹配设备驱动程序。
gpio-num:指定要使用的GPIO编号。
direction:指定GPIO的方向,可以是“in”或“out”。

设备驱动程序示例:

#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>

static int gpio_example_probe(struct platform_device *pdev)
{
    int ret, gpio_num;
    const char *dir;

    // 从设备树中获取GPIO编号和方向
    struct device_node *np = pdev->dev.of_node;
    ret = of_property_read_u32(np, "gpio-num", &gpio_num);
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to read gpio-num property\n");
        return ret;
    }
    ret = of_property_read_string(np, "direction", &dir);
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to read direction property\n");
        return ret;
    }

    // 请求GPIO
    ret = gpio_request(gpio_num, "gpio_example");
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to request GPIO\n");
        return ret;
    }

    // 设置GPIO方向
    if (strcmp(dir, "out") == 0) {
        gpio_direction_output(gpio_num, 0);
    } else if (strcmp(dir, "in") == 0) {
        gpio_direction_input(gpio_num);
    } else {
        dev_err(&pdev->dev, "invalid direction property: %s\n", dir);
        gpio_free(gpio_num);
        return -EINVAL;
    }

    // 在platform_device中保存GPIO编号
    platform_set_drvdata(pdev, (void *)gpio_num);

    dev_info(&pdev->dev, "GPIO device driver initialized\n");

    return 0;
}

static int gpio_example_remove(struct platform_device *pdev)
{
    int gpio_num = (int)platform_get_drvdata(pdev);

    // 释放GPIO
    gpio_free(gpio_num);

    dev_info(&pdev->dev, "GPIO device driver removed\n");

    return 0;
}

static const struct of_device_id gpio_example_of_match[] = {
    { .compatible = "example,gpio" },
    {},
};
MODULE_DEVICE_TABLE(of, gpio_example_of_match);

static struct platform_driver gpio_example_driver = {
    .probe = gpio_example_probe,
    .remove = gpio_example_remove,
    .driver = {
        .name = "gpio_example",
        .of_match_table = gpio_example_of_match,
    },
};

module_platform_driver(gpio_example_driver);

在此示例程序中,我们使用了of_property_read_u32和 of_property_read_string函数从设备树节点中读取GPIO编号和方向属性的值。然后,我们使用gpio_request函数请求GPIO并设置它的方向,如果方向属性无效,将释放GPIO并返回错误。

最后,我们使用platform_set_drvdata函数将GPIO编号保存在platform_device结构体中,以便在remove函数中使用。在remove函数中,我们使用platform_get_drvdata函数获取保存在platform_device中的GPIO编号,并使用gpio_free函数释放GPIO。

该驱动程序还包括一个of_device_id数组,用于在设备树中匹配设备兼容性字符串。然后,我们将这个数组传递给platform_driver结构体的driver字段中,以便内核可以自动加载此驱动程序并匹配适当的设备节点。

紧接着在驱动实现read write 接口

下面是例子

#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

#define BUF_SIZE 256

static int gpio_num;
static char gpio_direction;

static int gpio_example_open(struct inode *inode, struct file *filp)
{
    // 获取GPIO方向
    if (gpio_direction == 'o') {
        gpio_direction_output(gpio_num, 0);
    } else {
        gpio_direction_input(gpio_num);
    }

    return 0;
}

static ssize_t gpio_example_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
    char kbuf[BUF_SIZE];

    if (count > BUF_SIZE) {
        return -EINVAL;
    }

    if (copy_from_user(kbuf, buf, count)) {
        return -EFAULT;
    }

    if (gpio_direction == 'o') {
        if (kbuf[0] == '1') {
            gpio_set_value(gpio_num, 1);
        } else {
            gpio_set_value(gpio_num, 0);
        }
    }

    return count;
}

static const struct file_operations gpio_example_fops = {
    .owner = THIS_MODULE,
    .open = gpio_example_open,
    .write = gpio_example_write,
};

static int gpio_example_probe(struct platform_device *pdev)
{
    int ret;
    const char *dir;

    // 从设备树中获取GPIO编号和方向
    struct device_node *np = pdev->dev.of_node;
    ret = of_property_read_u32(np, "gpio-num", &gpio_num);
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to read gpio-num property\n");
        return ret;
    }
    ret = of_property_read_string(np, "direction", &dir);
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to read direction property\n");
        return ret;
    }
    gpio_direction = dir[0];

    // 请求GPIO
    ret = gpio_request(gpio_num, "gpio_example");
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to request GPIO\n");
        return ret;
    }

    // 注册字符设备
    ret = register_chrdev(0, "gpio_example", &gpio_example_fops);
    if (ret < 0) {
        dev_err(&pdev->dev, "failed to register char device\n");
        gpio_free(gpio_num);
        return ret;
    }

    dev_info(&pdev->dev, "GPIO device driver initialized\n");

    return 0;
}

static int gpio_example_remove(struct platform_device *pdev)
{
    // 取消注册字符设备
    unregister_chrdev(0, "gpio_example");

    // 释放GPIO
    gpio_free(gpio_num);

    dev_info(&pdev->dev, "GPIO device driver removed\n");

    return 0;
}

static const struct of_device_id gpio_example_of_match[] = {
    { .compatible = "example,gpio" },
    {},
};
MODULE_DEVICE_TABLE(of, gpio_example_of_match);

static struct platform_driver gpio_example_driver = {
    .probe = gpio_example_probe,
    .remove = gpio_example_remove,
    .driver = {
        .name = "gpio_example",
        .of_match_table = gpio_example_of_match,
        .owner = THIS_MODULE,
    },
};

module_platform_driver(gpio_example_driver);

MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("GPIO device driver");
MODULE_LICENSE("GPL");


在这个代码中,我们使用了module_platform_driver宏,它自动将gpio_example_driver注册为平台驱动程序,并处理模块的加载和卸载。我们还定义了模块的作者,描述和许可证信息。文章来源地址https://www.toymoban.com/news/detail-499222.html

到了这里,关于gpio 子系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【IMX6ULL驱动开发学习】14.Linux驱动开发 - GPIO中断(设备树 + GPIO子系统)

    代码自取 【14.key_tree_pinctrl_gpios_interrupt】: https://gitee.com/chenshao777/imx6-ull_-drivers 主要接口函数: 1. of_gpio_count (获得GPIO的数量) 2. kzalloc (向内核申请空间) 3. of_get_gpio (获取GPIO子系统标号) 4. gpio_to_irq (根据GPIO子系统标号得到软件中断号) 5. request_irq (根据软件中断号

    2024年02月12日
    浏览(35)
  • RK3588 PWM调试记录---linux pwm子系统驱动框架

    RK3588一共有4组PWM,每组有4个通道,共可以产生4*4=16路PWM波形; PWM0 开始地址:0xfd8b0000 PWM1 开始地址:0xfebd0000 PWM2 开始地址:0xfebe0000 PWM3 开始地址:0xfebf0000 即每组PWM的地址空间是(0xfd8b0000-0xfebd0000=0x1000)64KB(0x1000/1024) RK3588的PWM支持捕获、连续和单次触发三种模式。 1.捕获模式

    2024年02月12日
    浏览(41)
  • 【嵌入式Linux内核驱动】SPI子系统 | 硬件原理 | 应用编程 | 内核驱动 | 总体框架

    1.1 SPI通信协议 SPI(Serial Peripheral Interface)是由Motorola公司开发的一种通用数据总线 四根通信线:SCK(Serial Clock)、MOSI(Master Output Slave Input)、MISO(Master Input Slave Output)、SS(Slave Select) 同步,全双工 支持总线挂载多设备(一主多从) 1.2 硬件连接 多NSS独立片选方式 菊花

    2024年02月16日
    浏览(48)
  • 【嵌入式Linux内核驱动】05_IIC子系统 | 硬件原理与常见面试问题 | 应用编程 | 内核驱动 | 总体框架

    1.1 IIC 基础 IIC协议简介—学习笔记_iic标准协议_越吃越胖的黄的博客-CSDN博客 I2C(Inter-Integrated Circuit)是一种串行通信协议,用于连接微控制器、传感器、存储器和其他外设。 I2C使用两条线(SDA和SCL)进行通信,可以连接多个设备,每个设备都有一个唯一的地址。I2C总线上的

    2024年02月09日
    浏览(35)
  • Linux GPIO 和 Pinctrl 子系统的使用(十四)

     个人名片: 🦁作者简介:学生 🐯个人主页:妄北y 🐧个人QQ:2061314755 🐻个人邮箱:2061314755@qq.com 🦉个人WeChat:Vir2021GKBS 🐼 本文由妄北y原创,首发CSDN 🎊🎊🎊 🐨座右铭:大多数人想要改造这个世界,但却罕有人想改造自己。 专栏导航: 妄北y系列专栏导航: C/C++的基

    2024年04月26日
    浏览(25)
  • 驱动开发作业3——GPIO子系统

    作业1:在内核模块中启用定时器,定时1s,让LED1以1s为周期实现流水灯  myled.c(驱动文件)    作业2:基于GPIO子系统完成LED灯驱动的注册,并利用应用程序测试  chrdevled.c(驱动文件) test.c(测试文件)

    2024年02月16日
    浏览(24)
  • Linux MMC 驱动子系统详解

    SD/SDIO/MMC 驱动是一种基于 SDMMC 和 SD SPI 主机驱动的协议级驱动程序,目前已支持 SD 存储器、SDIO 卡和 eMMC 芯片。 因为linux内核mmc子系统里面已经实现了这些协议,我们以后并不需要重新实现这些,只需要对协议有个简单的了解。 mmc是比较老的存储卡了,sd是mmc的替代者,sdi

    2024年02月06日
    浏览(28)
  • Linux驱动开发:SPI子系统

    MISO:主设备数据输入,从设备数据输出。 MOSI:主设备数据输出,从设备数据输入。 SCLK:时钟信号,由主设备产生。 CS:    从设备片选信号,由主设备控制。 CPOL(时钟极性) :   0:时钟起始位低电平      1:时钟起始为高电平   CPHA(时钟相位) :0:第一个时钟周期采样   1

    2024年02月06日
    浏览(37)
  • <Linux开发>驱动开发 -之-Linux INPUT 子系统

    <Linux开发>驱动开发 -之-Linux INPUT 子系统 交叉编译环境搭建: <Linux开发> linux开发工具-之-交叉编译环境搭建 uboot移植可参考以下: <Linux开发> -之-系统移植 uboot移植过程详细记录(第一部分) <Linux开发> -之-系统移植 uboot移植过程详细记录(第二部分) <Linux开发

    2024年02月09日
    浏览(35)
  • [驱动开发]gpio子系统及中断实现led亮灭

    编写LED灯的驱动,使用GPIO子系统,里面添加按键的中断处理 1.应用程序发送指令控制发光二极管亮灭 2.按键1按下,led1电位反转;按键2按下,led2电位反转;按键3按下,led3电位反转   

    2024年02月14日
    浏览(64)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包