2023年7月13日 星期四 Linux驱动作业

news2024/9/23 19:14:38

1.使用platform驱动实现代码实现如下要求

a.应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断)
例如number=0 按下按键number=1再次按下按键number=0d.在按下按键的时候需要同时将1ed1的状态取反
t
e.驱动中需要编写字符设备驱动f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中

应用程序


#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
 
 int number;
int main(int argc, char const *argv[])
{
    int buf[128] = {0};
    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        read(fd,&number,sizeof(number));//读取数据
        printf("number:%d\n",number);
    }
 
    close(fd);
 
    return 0;
}

驱动程序


#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/io.h>

struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0; // 次设备号的起始值
dev_t devno;
struct class *cls;
struct device *dev;
// 定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
struct device_node *dnode;
int number = 0;
int i;
/*myplatform{
        compatible = "hqyj,myplatform";
        reg=<0X12345678 0X400>;
        interrupt-parent=<&gpiof>;
        interrupts=<9 0>;   //9表示引用中断父节点时的索引信息  0表示默认设置
           led1=<&gpioe 10 0>;

    };*/
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
    number = !number;
    gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
    condition = 1;                   // 表示硬件数据就绪
    wake_up_interruptible(&wq_head); // 唤醒休眠的进程
    return IRQ_HANDLED;
}
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    if (sizeof(number) < size)
        size = sizeof(number);
    wait_event_interruptible(wq_head, condition); // 将进程切换为休眠

    ret = copy_to_user(ubuf, &number, size);
    if (ret)
    {
        printk("copy_tO_user filed\n");
        return -EIO;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    condition = 0; // 表示下一次硬件数据没有准备好
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .read = mycdev_read,
    .release = mycdev_close,
};
// probe函数,匹配设备成功执行
int pdrv_probe(struct platform_device *pdev)
{
    int ret, i;
    // 初始化等待队列头
    init_waitqueue_head(&wq_head);
    // 1.分配字符设备驱动对象空间  cdev_alloc
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请字符设备驱动对象空间失败\n");
        ret = -EFAULT;
        goto out1;
    }
    printk("字符设备驱动对象申请成功\n");
    // 2.字符设备驱动对象部分初始化  cdev_init
    cdev_init(cdev, &fops);
    // 3.申请设备号  register_chrdev_region/alloc_chrdev_region
    if (major > 0) // 静态申请设备号
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, 3, "myled");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major = MAJOR(devno); // 根据设备号得到主设备号
        minor = MINOR(devno); // 根据设备号得到次设备号
    }
    printk("申请设备号成功\n");
    // 4.注册字符设备驱动对象  cdev_add()
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");
    // 5.向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");
    // 6.向上提交设备节点
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交节点信息失败\n");
            ret = -PTR_ERR(dev);
            goto out5;
        }
    }
    printk("向上提交设备节点信息成功\n");
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    // 获取设备信息
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (res == NULL)
    {
        printk("获取资源失败\n");
        return -ENXIO;
    }
    printk("获取资源信息成功 %x\n", res->start);
    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0)
    {
        printk("获取中断资源失败\n");
        return irqno;
    }
    printk("中断类型资源为%d\n", irqno);
    // 注册中断
    ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key", NULL);
    if (ret)
    {
        printk("注册驱动失败\n");
        return ret;
    }
    printk("key1中断注册成功\n");
    // 获取gpio信息
    // pdev->dev.of_node  设备树匹配之后会把设备树节点结构体首地址赋值给dev的of_node成员
    gpiono = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono))
    {
        printk("解析GPIO信息失败\n");
        return -PTR_ERR(gpiono);
    }
    // 亮灯
    gpiod_set_value(gpiono, 1);
    return 0;

out5:
    for (--i; i >= 0; i--)
    {
        // 销毁上面提交的设备信息
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
    kfree(cdev);
out1:
    return ret;
}
// remove 设备和驱动分离时执行
int pdrv_remove(struct platform_device *pdev)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    // 1.销毁设备信息  device_destroy
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 2.销毁目录  class_destroy
    class_destroy(cls);
    // 3.注销对象  cdev_del()
    cdev_del(cdev);
    // 4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major, minor), 3);
    // 5.释放对象空间  kfree()
    kfree(cdev);
    free_irq(irqno, 0);
    // 灭灯
    gpiod_set_value(gpiono, 0);
    // s释放gpio信息
    gpiod_put(gpiono);
    return 0;
}
// 构建设备树匹配的表
struct of_device_id oftable[] = {
    {
        .compatible = "hqyj,myplatform",
    },
    {
        .compatible = "hqyj,myplatform1",
    },
    {},
};

struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "aaaaa",
        .of_match_table = oftable, // 设置设备树匹配
    },

};

// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

运行结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/749128.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Tik Tok你不知道的那些知识?

TikTok是一款短视频社交平台&#xff0c;由中国公司字节跳动&#xff08;ByteDance&#xff09;开发和运营。它让用户可以通过手机拍摄、编辑和分享15秒至60秒的短视频&#xff0c;涵盖了各种内容&#xff0c;包括音乐、舞蹈、喜剧、唱歌、游戏等。TikTok以其简单易用和丰富多样…

Meteor code must always run within a Fiber 报错解决办法

报错&#xff1a; 这样的写法会出现这个报错 大概的意思就是说&#xff0c;目前你这个函数不是运行在meteor的环境中&#xff0c;所以要使用Meteor.bindEnvironment&#xff0c;来改变函数运行的上下文 解决办法&#xff1a;

87. 把字符串转换成整数

目录 链接&#xff1a; 题目&#xff1a; 思路&#xff1a; 代码&#xff1a; 图片&#xff1a; 链接&#xff1a; 原题链接 题目&#xff1a; 请你写一个函数 StrToInt&#xff0c;实现把字符串转换成整数这个功能。 当然&#xff0c;不能使用 atoi 或者其他类似的库函数…

摆脱基础设施束缚,亚马逊云科技提出生成式AI方法论

“未来近在咫尺&#xff0c;只不过时隐时现 (The future is here, its just not evenly distributed yet.)”--亚马逊云科技全球产品副总裁Matt Wood博士引用“赛博朋克之父” William Gibson 的一句名言来表达生成式AI的发展现状。 自去年底ChatGPT惊艳众人开始&#xff0c;这…

云原生高性能API网关,选Apache APISIX还是Nginx Plus

文章首发地址 Apache APISIX 对比 Nginx Plus APISIX 和 Nginx Plus 都是高性能的 API 网关&#xff0c;具有类似的特点&#xff0c;如可扩展性、插件化、负载均衡、反向代理等。下面对 APISIX 和 Nginx Plus 进行对比&#xff1a; 开源授权&#xff1a;APISIX 是 Apache 开…

Java阶段五Day05

Java阶段五Day05 文章目录 Java阶段五Day05问题解析无法启动Naocs Nacos服务注册发现Nacos运行架构nacos-server是一个服务进程 配置注册服务端客户端csmall-for-jsd-business-adapter 整合nacos-clientyaml详细配置注册信息在nacos中的内存状态多实例注册服务抓取&#xff08;…

五大引擎全新升级!轻流 5.0 正式发布

轻流的5.0版本&#xff0c;一个“陪伴企业成长的一站式开发平台”&#xff0c;它将更加灵活、更加开放&#xff0c;同时更加低门槛。 ——轻流联合创始人&CPO 严琦东 7月6日&#xff0c;在一年一度的无代码无边界 202376Day 轻流无代码探索者大会上&#xff0c;轻流联合创…

SpringMVC实现对页面的访问和跳转~

初识MVC: MVC是一种软件架构的思想&#xff0c;将软件按照模型&#xff0c;视图&#xff0c;控制器来划分 M&#xff1a;Model&#xff0c;模型层&#xff0c;指工程中的JavaBean,作用是处理数据 JavaBean分为两类&#xff1a; 一类称为实体类Bean:专门存储业务数据的&…

Java 中线程相关的各种锁

一、Java对象与锁 1、对象结构 2、对象头的 Mark Word 二、锁介绍 1、概念和种类 1、乐观锁 不加锁&#xff0c;在使用数据时判断数据是不是最新。常用CAS算法实现 2、自旋锁 与 适应性自旋锁 两者并不是锁&#xff0c;而是锁提供的处理方式。 自旋锁&#xff08;JDK1.4&a…

Sentinel 熔断与限流

文章目录 1 是什么&#xff1f;2 特征3 特性4 与Hystrix的区别5 两个部分6 应用6.1 依赖6.2 配置文件 7 流量配置规则7.1 直接&#xff08;默认&#xff09;7.2 关联7.3 Warm Up 预热7.4 排队等待 8 熔断降级8.1 概述RT(平均响应时间&#xff0c;秒级)异常比列(秒级)异常数(分钟…

python-cv2模块安装

1.自动安装 如果网络环境好&#xff1a; pip install opencv-python2.卸载与安装指定版本 卸载opencv pip uninstall opencv-python安装指定版本的cv 指定版本为&#xff1a;4.5.4.60 pip install opencv-python 4.5.4.603.下载安装包安装 从官网下载正确安装包安装&#x…

【Linux工具】编译器、调式器、项目自动化构建工具以及git的使用2(make/makefile和git的基本使用)

【Linux工具】编译器、调式器、项目自动化构建工具以及git的使用2&#xff08;make/makefile和git的基本使用&#xff09; 目录 【Linux工具】编译器、调式器、项目自动化构建工具以及git的使用2&#xff08;make/makefile和git的基本使用&#xff09;背景make和makefile的用法…

Proxy-Reflect使用详解

1 监听对象的操作 2 Proxy类基本使用 3 Proxy常见捕获器 4 Reflect介绍和作用 5 Reflect的基本使用 6 Reflect的receiver Proxy-监听对象属性的操作(ES5) 通过es5的defineProperty来给对象中的某个参数添加修改和获取时的响应式。 单独设置defineProperty是只能一次设置一…

AppStorage, OnboardingView 的示例

1. AppStorage 数据简单存储的实现 /// 应用程序数据简单存储 struct AppStorageBootcamp: View {//State var currentUserName: String?AppStorage("name") var currentUserName: String?var body: some View {VStack(spacing: 20) {Text(currentUserName ?? &…

Ghostscript开源PDF库中发现关键漏洞

在Linux中广泛使用的PostScript语言和PDF文件开源解释器Ghostscript被发现存在严重远程代码执行漏洞。 该漏洞被标记为CVE-2023-3664&#xff0c;CVSS v3评级为9.8&#xff0c;影响10.01.2之前的所有Ghostscript版本&#xff0c;10.01.2是三周前发布的最新版本。 据Kroll公司…

深入理解netfilter和iptables

目录 Netfilter的设计与实现 内核数据包处理流 netfilter钩子 钩子触发点 NF_HOOK宏与Netfilter裁定 回调函数与优先级 iptables 内核空间模块 xt_table的初始化 ipt_do_table() 复杂度与更新延时 用户态的表&#xff0c;链与规则 conntrack Netfilter(结合iptable…

基于C语言设计的足球信息查询系统

完整资料进入【数字空间】查看——baidu搜索"writebug" 需求分析与概要设计 2.1 项目说明 我们小组的选题主要是面向足球爱好者&#xff0c;在普通社交软件的基础之上&#xff0c;围绕足球的主题展开设计&#xff0c;以便于他们能够更好的交流相关的话题&#xff…

高效编程的捷径:HbuilderX的独特之处

目录 引言HbuilderX的功能HbuilderX的优点HbuilderX的缺点总结 HBuilderX 官网 引言 在当今科技发展日新月异的时代&#xff0c;软件开发已成为一个极富挑战性且高需求的领域。为了在竞争激烈的市场中脱颖而出&#xff0c;程序员们需要掌握一系列高效编程的技巧和工具。在这个过…

谈一谈LLM在推荐域的一些理解

作者&#xff1a;陈祖龙(葬青) 一、前言 最近大模型真的很火&#xff0c;从个人到公司&#xff0c;各行各业都在学习大模型、总结大模型和尝试应用大模型。大模型其实不是一个新的产物&#xff0c;已经在NLP发展了很多年。ChatGPT的诞生&#xff0c;经验的效果震惊了所有人&…

ES系列--es初探

一、前言 一般传统数据库&#xff0c;全文检索都实现的很鸡肋&#xff0c;因为一般也没人用数据库存文本字段。进 行全文检索需要扫描整个表&#xff0c;如果数据量大的话即使对 SQL 的语法优化&#xff0c;也收效甚微。建 立了索引&#xff0c;但是维护起来也很麻烦&#xff0…