2023年7月23日 星期三 Linux驱动作业

news2025/2/22 13:29:25

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

a.应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断)
例如number=0 按下按键number=1再次按下按键number=0
d.在按下按键的时候需要同时将1ed1的状态取反
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/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/of.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/io.h>

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;
unsigned int gpiono;
unsigned int irqno;
int number = 0;
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
    number = !number;
    gpio_set_value(gpiono, !gpio_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,
};

static int __init mycdev_init(void)
{
    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");
    // 解析设备树节点
    dnode = of_find_node_by_name(NULL, "myhomework");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("设备树节点解析成功\n");
    // 获取软中断号
    irqno = irq_of_parse_and_map(dnode, 0);
    if (!irqno)
    {
        printk("软中断号获取失败\n");
        return -ENOMEM;
    }
    printk("软中断号获取成功irqno=%d\n", irqno);
    // 注册中断
    ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key", NULL);
    if (ret)
    {
        printk("注册驱动失败\n");
        return ret;
    }
    printk("key1中断注册成功\n");
    // 根据设备树节点解析led1gpio编号
    gpiono = of_get_named_gpio(dnode, "led1", 0);
    if (gpiono < 0)
    {
        printk("gpio编号解析失败\n");
    }
    printk("解析gpio编号成功gpiono=%d\n", gpiono);
    // 申请gpio编号
    ret = gpio_request(gpiono, NULL);
    if (ret < 0)
    {
        printk("申请gpio编号失败\n");
        goto out6;
    }
    // led1对应的gpio的管脚为输出
    gpio_direction_output(gpiono, 0);
    gpio_set_value(gpiono, 1);
    return 0;
out6:
    // 释放gpio编号
    gpio_free(gpiono);
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;
}
static void __exit mycdev_exit(void)
{
    // 1.销毁设备信息  device_destroy
    int i;
    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);
    gpio_free(gpiono);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

运行结果

在这里插入图片描述

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

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

相关文章

FlinkCDC第四部分-同步mysql到mysql,ctrl就完事~(flink版本1.17.1)

本文介绍了不同源单表-单表同步&#xff0c;不同源多表-单表同步。 注&#xff1a;此版本支持火焰图 Flink版本&#xff1a;1.17.1 环境&#xff1a;Linux CentOS 7.0、jdk1.8 基础文件&#xff1a; flink-1.17.1-bin-scala_2.12.tgz、 flink-connector-jdbc-3.0.0-1.16.…

LeetCode·每日一题·2544. 交替数字和·模拟

作者&#xff1a;小迅 链接&#xff1a;https://leetcode.cn/problems/alternating-digit-sum/solutions/2341276/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-7fjq/ 来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 著作权归作者所有。商业转载请联系作者获得授权&#…

产品经理学习画原型(三)

1.布尔运算的使用 合并必须选择两个及以上元件 合并后&#xff1a; 组合&#xff1a; 去除顶层&#xff1a;

【基于FPGA的芯片设计】32位RISC-V存储器

实验板卡&#xff1a;xc7a100tlc sg324-2L&#xff0c;共20个开关 实验要求

述往思来,砥砺前行—易我文化系列课《企业发展历程》讲座圆满举行

2023年6月9日&#xff0c;易我文化系列课《企业发展历程》讲座如期举行并取得圆满成功。 此次讲座邀请了易我客服总监——喻涛为大家授课&#xff0c;旨在让员工对易我的历史、发展以及未来有更全面的认识&#xff0c;引导员工从易我发展史中汲取奋进力量&#xff0c;增强集体…

【kubernetes系列】Kubernetes之ServiceAccount

概述 Service Account是什么呢&#xff0c;顾名思义&#xff0c;服务账号&#xff0c;一种给服务使用的账号&#xff0c;它不是给Kubernetes的集群的用户&#xff08;系统管理员、运维人员、租户用户等&#xff09;使用&#xff0c;而是给运行在Pod里的进程用的&#xff0c;它…

【雕爷学编程】Arduino动手做(113)---5110液晶屏模块2

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…

基于springboot+Redis的前后端分离项目(八)-【黑马点评】

&#x1f381;&#x1f381;资源文件分享 链接&#xff1a;https://pan.baidu.com/s/1189u6u4icQYHg_9_7ovWmA?pwdeh11 提取码&#xff1a;eh11 好友关注&Feed流 &#xff08;一&#xff09;好友关注-关注和取消关注(二)好友关注-共同关注&#xff08;三&#xff09; 好友…

飞行动力学 - 第7节-part1-起飞性能 之 基础点摘要

飞行动力学 - 第7节-part1-起飞性能 之 基础点摘要 1. 气动特性2. 起飞性能3. 性能指标3.1 地面滑跑阶段3.2 起飞滑跑距离估算 4. 跑道4.1 编号4.2 等级 5. 参考资料 1. 气动特性 起飞不仅需要考虑升力&#xff0c;还需要在有限跑道长度上加速&#xff0c;因此襟翼放出的角度不…

3Ds max入门教程:创建尼亚加拉大瀑布模型

推荐&#xff1a; NSDT场景编辑器助你快速搭建可二次开发的3D应用场景 初学者在3ds Max中为尼亚加拉大瀑布建模 这次您将学习通过几个简单的步骤在3ds max中对尼亚加拉大瀑布&#xff08;从远处看起来很逼真&#xff09;进行建模。所以&#xff0c;让我们开始吧&#xff01; …

JavaScript定时器(setTimeout和setInterval)的用法技巧

1.满足特定条件时才执行后续步骤。 核心代码&#xff1a; //初始化let isPass false;//只有满足特定条件&#xff08;在这里为&#xff1a;isPass为true时&#xff09;时&#xff0c;才执行下一步操作let timeout setInterval(() > {if (isPass) {//这里写要执行的后续方…

Kubernetes 入门篇上

❖ Kubernetes核心概念 ❖ 快速部署一个Kubernetes集群 ❖ Kubernetes将弃用Docker&#xff01; ❖ kubectl管理工具 ❖ Kubernetes基本资源概念 ❖ 资源编排&#xff08;YAML&#xff09; ❖ 深入理解Pod对象&#xff1a;基本管理 ❖ 深入理解Pod对象&#xff1a;调度 …

Chapter 6: Loops and Iterations | Python for Everybody 讲义笔记_En

文章目录 Python for Everybody课程简介Loops and IterationsUpdating variablesThe while statementInfinite loopsFinishing iterations with continueDefinite loops using forLoop patternsCounting and summing loopsMaximum and minimum loopsDebuggingGlossary Python f…

Chapter 7: Strings | Python for Everybody 讲义笔记_En

文章目录 Python for Everybody课程简介StringsA string is a sequenceGetting the length of a string using lenTraversal through a string with a loopString slicesStrings are immutableLooping and countingThe in operatorString comparisonString methodsParsing stri…

x86平台实时Windows机器视觉EtherCAT运动控制器VPLC711

一、市场背景 简单易用&#xff0c;功能强大&#xff0c;正运动技术持续专注智能制造核心控制器的产品与平台的突破&#xff01; 随着智能制造的兴起&#xff0c;制造型企业正面临着日益激烈的市场竞争和对生产效率与产品品质提升的迫切需求&#xff0c;以满足市场的要求。同…

详解自定义类型:结构体,枚举,联合

目录 结构体 结构体基础知识 结构的自引用 结构体内存对齐 结构体大小计算 存在内存对齐的原因 设计结构体时的技巧 修改默认对齐数 结构体实现位段&#xff08;位段的填充&可移植性&#xff09; 什么是位段 位段的内存分配 位段的跨平台问题 位段的应用 枚…

做数据库内核开发的人员很少吗?

是的&#xff0c;相对于其他领域的软件开发&#xff0c;数据库内核开发人员的数量确实相对较少。这是因为数据库内核开发是一项高度专业化和复杂的任务&#xff0c;需要深入理解数据库系统的原理、算法和底层技术。 我这里刚好有嵌入式、单片机、plc的资料需要可以私我或在评论…

和鲸数据科学专家平台正式成立,凝聚专家资源推进产业数字化升级

2015年&#xff0c;大数据与人工智能从技术到公众认知均迎来重大突破&#xff0c;全国首批“数据科学与大数据技术”专业获批 同年&#xff0c;和鲸科技的前身科赛网 Kesci 正式成立&#xff0c;从数据竞赛社区出发为“数据人”提供实践与成长的平台。 2023年&#xff0c;数据…

sql语句汇总

最近项目中接触到了mySql,把经常用到的MySql语句记录下来&#xff0c;方便以后随时查阅。 1.密码加密 表结构如下 INSERT INTO tbl_userinfo ( vc_accname,vc_username,vc_pwd,vc_phone,i_role_id,dt_creatTime) VALUES (%s,%s,AES_ENCRYPT((%s), Wang),%s,%d,NOW())该表主…

Shiro教程(二):Springboot整合Shiro全网最全教程

Shiro教程&#xff08;二&#xff09;&#xff1a;Springboot整合Shiro全网最全教程 1、SpringBoot整合Shiro环境搭建 新建Module 新建springboot项目 选择一个依赖就行&#xff0c;因为这里idea版本的问题&#xff0c;SpringBoot的版本最低就是2.7.13。可以等项目创建成功之后…