驱动开发(中断)

头文件:

#ifndef __LED_H__
#define __LED_H__#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR  0X50006014
#define PHY_LED1_RCC 0X50000A28#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR  0X50007014
#define PHY_LED2_RCC  0X50000A28#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR  0X50006014
#define PHY_LED3_RCC  0X50000A28
//风扇
#define PHY_FAN_MODER 0X50006000
#define PHY_FAN_ODR  0X50006014
#define PHY_FAN_RCC 0X50000A28
//蜂鸣器
#define PHY_BUZZER_MODER 0X50003000
#define PHY_BUZZER_ODR  0X50003014
#define PHY_BUZZER_RCC 0X50000A28
//马达
#define PHY_MOTOR_MODER 0X50007000
#define PHY_MOTOR_ODR  0X50007014
#define PHY_MOTOR_RCC 0X50000A28
//功能码
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
#define FAN_ON _IO('f',1)
#define FAN_OFF _IO('f',0)
#define BUZZER_ON _IO('b',1)
#define BUZZER_OFF _IO('b',0)
#define MOTOR_ON _IO('m',1)
#define MOTOR_OFF _IO('m',0)
#endif

 

应用层程序:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{char buf[128] = {0};int led1_fd, led2_fd, led3_fd;// 打开led设备文件led1_fd = open("/dev/mycdev0", O_RDWR);if (led1_fd < 0){printf("打开led1设备文件失败\n");exit(-1);}led2_fd = open("/dev/mycdev1", O_RDWR);if (led2_fd < 0){printf("打开led2设备文件失败\n");exit(-1);}led3_fd = open("/dev/mycdev2", O_RDWR);if (led3_fd < 0){printf("打开led3设备文件失败\n");exit(-1);}int a, b;while (1){printf("请选择要控制的器件: 1(led灯1) 2 (led灯2) 3 (led灯3) ");scanf("%d", &a);printf("请输入指令:0(关闭) 1 (打开)");scanf("%d", &b);switch (b){case 1:switch (a){case 1:ioctl(led1_fd, LED_ON, a);break;case 2:ioctl(led2_fd, LED_ON, a);break;case 3:ioctl(led3_fd, LED_ON, a);break;}break;case 0:switch (a){case 1:ioctl(led1_fd, LED_OFF, a);break;case 2:ioctl(led2_fd, LED_OFF, a);break;case 3:ioctl(led3_fd, LED_OFF, a);break;}break;}}close(led1_fd);close(led2_fd);close(led3_fd);return 0;
}

驱动程序:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include "head.h"
struct cdev* mycdev;
struct class* cls;
struct device* dev_d;
unsigned int major = 500;
unsigned int minor = 0;
dev_t devno;
char kbuf[128] = {0};
wait_queue_head_t wq_head;
int condition = 0;
struct device_node *dev,*led_dev;
unsigned int irqno1,irqno2,irqno3;
struct gpio_desc* gpiono1,*gpiono2,*gpiono3;
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{int a = inode->i_rdev;file->private_data = (void *)MINOR(a);printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
long mychrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{	unsigned int a;printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);a = (unsigned int)file->private_data;switch(a){case 0:if(cmd == LED_ON){gpiod_set_value(gpiono1,1);}else{gpiod_set_value(gpiono1,0);}break;case 1:if(cmd == LED_ON){gpiod_set_value(gpiono2,1);}else{gpiod_set_value(gpiono2,0);}break;case 2:if(cmd == LED_ON){gpiod_set_value(gpiono3,1);}else{gpiod_set_value(gpiono3,0);}break;}return 0;
}
irqreturn_t myirq_handler(int irq,void *dev)
{gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));return IRQ_HANDLED;
}
irqreturn_t myirq2_handler(int irq,void *dev)
{gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));return IRQ_HANDLED;
}
irqreturn_t myirq3_handler(int irq,void *dev)
{gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));return IRQ_HANDLED;
}
//对设备文件进行操作的结构体
struct file_operations fops = {.open = mycdev_open,//打开.unlocked_ioctl = mychrdev_ioctl,//硬件功能的选择.release = mycdev_close,//关闭
};
static int __init mycdev_init(void)
{int i;major = register_chrdev(0,"mycdev",&fops);if(major<0){printk("注册设备驱动失败\n");return major;}printk("注册设备驱动成功major=%d\n",major);//************自动申请设备节点*********************//1、向上提交目录cls = class_create(THIS_MODULE, "mycdev");if(IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录成功\n");// 2、向上提交设备节点信息for (i = 0; i < 3; i++){dev_d = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);if (IS_ERR(dev_d)){printk("提交led设备信息失败\n");return -PTR_ERR(dev_d);}}printk("提交设备信息成功\n");/***********************************///解析设备树的节点int ret;dev = of_find_node_by_name(NULL,"myirq");if(dev == NULL){printk("解析设备树节点失败\n");return -EIO;}printk("解析myirq设备树节点成功\n");led_dev = of_find_node_by_name(NULL,"leds");if(led_dev == NULL){printk("解析led设备树节点失败\n");return -EIO;}printk("解析led设备树节点成功\n");gpiono1 = gpiod_get_from_of_node(led_dev,"led1-gpios",0,GPIOD_OUT_LOW,NULL);if(IS_ERR(gpiono1)){printk("申请gpio对象失败\n");return -PTR_ERR(gpiono1);}gpiono2 = gpiod_get_from_of_node(led_dev,"led2-gpios",0,GPIOD_OUT_LOW,NULL);if(IS_ERR(gpiono2)){printk("申请gpio2对象失败\n");return -PTR_ERR(gpiono2);}gpiono3 = gpiod_get_from_of_node(led_dev,"led3-gpios",0,GPIOD_OUT_LOW,NULL);if(IS_ERR(gpiono3)){printk("申请gpio3对象失败\n");return -PTR_ERR(gpiono3);}//根据设备树节点解析出软中断号irqno1 = irq_of_parse_and_map(dev,0);if(!irqno1){printk("解析led1软中断号失败\n");return -ENXIO;}printk("解析软led1中断号成功irqno=%d\n",irqno1);irqno2 = irq_of_parse_and_map(dev,1);if(!irqno2){printk("解析led2软中断号失败\n");return -ENXIO;}printk("解析led2软中断号成功irqno=%d\n",irqno2);irqno3 = irq_of_parse_and_map(dev,2);if(!irqno3){printk("解析led3软中断号失败\n");return -ENXIO;}printk("解析led3软中断号成功irqno=%d\n",irqno3);//注册中断ret = request_irq(irqno1,myirq_handler,IRQF_TRIGGER_FALLING,"key1",NULL);if(ret){printk("key1软中断号注册失败\n");return ret;}printk("key1软中断号注册成功\n");ret = request_irq(irqno2,myirq2_handler,IRQF_TRIGGER_FALLING,"key2",NULL);if(ret){printk("key2软中断号注册失败\n");return ret;}printk("key2软中断号注册成功\n");ret = request_irq(irqno3,myirq3_handler,IRQF_TRIGGER_FALLING,"key3",NULL);if(ret){printk("key3软中断号注册失败\n");return ret;}printk("key3软中断号注册成功\n");return 0;
}
static void __exit mycdev_exit(void)
{int i;//灭灯gpiod_set_value(gpiono1,0);//释放gpio编号gpiod_put(gpiono1);//灭灯gpiod_set_value(gpiono2,0);//释放gpio编号gpiod_put(gpiono2);//灭灯gpiod_set_value(gpiono3,0);//释放gpio编号gpiod_put(gpiono3);//注销中断号free_irq(irqno1,NULL);free_irq(irqno2,NULL);free_irq(irqno3,NULL);//销毁节点信息for (i = 0; i < 3; i++){device_destroy(cls, MKDEV(major, i));}//销毁目录信息class_destroy(cls);//注销字符设备驱动unregister_chrdev(major,"mycdev");}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

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

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

相关文章

在word的文本框内使用Endnote引用文献,如何保证引文编号按照上下文排序

问题 如下图所示&#xff0c;我在word中插入了一个文本框&#xff08;为了插图&#xff09;&#xff0c;然后文本框内有引用&#xff0c;结果endnote自动将文本框内的引用优先排序&#xff0c;变成文献[1]了&#xff0c;而事实上应该是[31]。请问如何能让文本框内的排序也自动…

html5设置不缓存

<meta http-equiv"Cache-Control" content"no-cache, no-store, must-revalidate"> <meta http-equiv"Pragma" content"no-cache"> <meta http-equiv"Expires" content"0"> 使用meta元素的htt…

maven install命令:将包安装在本地仓库,供本地的其它工程或者模块依赖

说明 有时候&#xff0c;自己本地的maven工程依赖于本地的其它工程&#xff0c;或者manven工程中的一个模块依赖于另外的模块&#xff0c;可以执行maven的install命令&#xff0c;将被依赖的包安装在maven本地仓库。 示例 一个工程包含几个模块&#xff0c;模块之间存在依赖…

第一个maven项目(IDEA生成)

第一个maven项目&#xff08;IDEA生成&#xff09; 步骤1 配置Project SDK 步骤2 配置maven File->Settings搜索maven

JS中常用的数组拷贝技巧

我们都知道&#xff0c;数组也是属于对象&#xff0c;在JS中对象的存储方式则是引用的方式。我们想要拷贝一个数组&#xff0c;就不能只是变量之前的赋值拷贝&#xff0c;这样他们将共享同一个引用&#xff0c;而数组又具有可变性&#xff0c;所以无法将原数组和拷贝的数组的数…

风辞远的科技茶屋:来自未来的信号枪

很久之前&#xff0c;有位朋友问我&#xff0c;现在科技资讯这么发达了&#xff0c;你们还写啊写做什么呢&#xff1f; 我是这么看的。最终能够凝结为资讯的那个新闻点&#xff0c;其实是一系列事情最终得出的结果&#xff0c;而这个结果又会带来更多新的结果。其中这些“得出”…

[QT编程系列-35]:数据存储 - JSON格式配置数据的存储与通知

目录 1. QJsonObject 2 QJsonDocument 3 JSON本文格式 4. JSON示例 5. JASON配置文件示例 1. QJsonObject QJsonObject 是Qt的类之一&#xff0c;用于表示 JSON 对象。 JSON&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0…

kagNet:对常识推理的知识感知图网络 2023 AAAI 8.4+8.5

这里写目录标题 摘要介绍概述问题陈述推理流程 模式图基础概念识别模式图构造概念网通过寻找路径来匹配子图基于KG嵌入的路径修剪 知识感知图网络图卷积网络&#xff08;GCN&#xff09;关系路径编码分层注意机制 实验数据集和实验步骤比较方法KAGNET是实施细节性能比较和分析与…

Netty:ByteBuf的readerIndex和writerIndex

说明 io.netty.buffer.ByteBuf的数据索引从0开始。ByteBuf保存一个readerIndex和一个writerIndex变量。readerIndex用于读取操作&#xff0c;writerIndex用于写入操作。 0 < readerIndex < writerIndex < capacity < maxCapacity 示例 获取当前的readerIndex和w…

mysql授权

1.dml权限 mysql> grant select,delete,update,insert,create on netcentre.* to ln_sale% identified by password; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show grants for ln_sale%; ------------------------------------------------------------…

python GUI nicegui初识一(登录界面创建)

最近尝试了python的nicegui库&#xff0c;虽然可能也有一些不足&#xff0c;但个人感觉对于想要开发不过对ui设计感到很麻烦的人来说是很友好的了&#xff0c;毕竟nicegui可以利用TailwindCSS和Quasar进行ui开发&#xff0c;并且也支持定制自己的css样式。 这里记录一下自己利…

【Spring框架】Spring事务

目录 Spring中事务的实现编程式事务声明式事务Transactional 作⽤范围Transactional 参数说明注意事项Transactional ⼯作原理 MySQL 事务隔离级别Spring 事务隔离级别事务传播机制 Spring中事务的实现 Spring中事务操作分为两类&#xff1a; 1.编程式事务 2.声明式事务 编程…

Abaqus 中最常用的子程序有哪些 硕迪科技

在ABAQUS中&#xff0c;用户定义的子程序是一种重要的构件&#xff0c;可以将其插入到Abaqus分析中以增强该软件的功能和灵活性。这些子程序允许用户在分析过程中添加自定义材料模型、边界条件、初始化、加载等特定操作&#xff0c;以便更精准地模拟分析中的现象和现象。ABAQUS…

小白电脑装机(自用)

几个月前买了配件想自己装电脑&#xff0c;结果最后无法成功点亮&#xff0c;出现的问题是主板上的DebugLED黄灯常亮&#xff0c;即DRAM灯亮。对于微星主板的Debug灯&#xff0c;其含义这篇博文中有说明。 根据另一篇博文&#xff0c;有两种可能。 我这边曾将内存条和主板一块…

作为一名3年经验的java开发,对未来发展的思考

前言 已经好久没有更新博客了&#xff0c;一直在工作中忙碌&#xff0c;下班后不是学习就是运动&#xff0c;也逐渐忘记了自己以前有写博客的习惯&#xff0c;想起3年前刚毕业那会找工作的焦头烂额&#xff0c;每天发博客的目的是为了刺激自己能够更加努力上进&#xff0c;告诉…

MISRA 2012学习笔记(1)-Directives

文章目录 说明Directives2 编译与构建Dir 2.1 3 需求可追溯性Dir 3.1 4 代码设计Dir 4.1Dir 4.2Dir 4.3Dir 4.4Dir 4.5Dir 4.6Dir 4.7Dir 4.8Dir 4.9Dir 4.10Dir 4.11Dir 4.12Dir 4.13 说明 以下等级一般分为三种&#xff0c;建议&#xff0c;必要&#xff0c;强制 建议&#…

mongodb-win32-x86_64-2008plus-ssl-3.6.23-signed.msi

Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。C:\Users\Administrator>cd C:\MongoDB\Server\3.6\binC:\MongoDB\Server\3.6\bin> C:\MongoDB\Server\3.6\bin> C:\MongoDB\Server\3.6\bin>mongod --dbpath C:\Mongo…

c语言每日一练(2)

前言&#xff1a; 每日一练系列&#xff0c;每一期都包含5道选择题&#xff0c;2道编程题&#xff0c;博主会尽可能详细地进行讲解&#xff0c;令初学者也能听的清晰。每日一练系列会持续更新&#xff0c;暑假时三天之内必有一更&#xff0c;到了开学之后&#xff0c;将看学业情…

Nginx启动报错- Failed to start The nginx HTTP and reverse proxy server

根据日志&#xff0c;仍然出现 “bind() to 0.0.0.0:8888 failed (13: Permission denied)” 错误。这意味着 Nginx 仍然无法绑定到 8888 端口&#xff0c;即使使用 root 权限。 请执行以下操作来进一步排查问题&#xff1a; 确保没有其他进程占用 8888 端口&#xff1a;使用以…

Java-JUC(七)

1.Java中实现多线程有几种方法 创建线程的常用的几种方式&#xff1a; 继承Thread类 实现Runnable接口 &#xff08;重写run方法&#xff0c;无返回值&#xff09; 实现Callable接口&#xff08; JDK1.5>,重写call方法&#xff0c;可以自定义返回值 &#xff09; 线程池方…