当前位置: 首页 > news >正文

arm内核驱动-中断

先介绍个东西   ctags

这个工具可以像keil一样在工程里查找跳转,帮我们找到我们想要的东西。

安装教程可以找到,这里只讲怎么用。

在工程目录(包含所有你会用到的头文件等)下,先加载这个命令,可能要等待一会

然后在我们写代码过程中,比如下面

以按键中断为例

按键K1中断源为EINT8

与裸机不同,操作系统下,很多东西别人已经写好了

我们在用到中断时

直接CTRL  +  ]   就会帮你搜索跳转到所要查找的文件里。CRTL  +  O回跳。我们也可以通过这个方式找到头文件。

回到中断

在操作系统中,我们不需要用很多的寄存器去配置中断。我们只需要按步骤配置就行了

1.注册中断  request_irq

参数:

        irq:中断号,就是我们上面第一张图所显示的,这里是K1的EINT8

        handler:中断回调函数,懂的都懂

        flags:中断行为、触发方式

下降沿触发

关闭其他中断(后面要用其他中断需要开启)

后面两个参数就是中断的名字和我们给回调函数传的参数,不在赘述。

回调函数

read函数

当我们调用read函数时,会进入wake_event阻塞。然后按键产生的中断会唤醒。

这里用的wake_even_interruptible而不用wake_event,如果是wake_event,在进入底层后,程序调度无法影响到它

用wake_even_interruptible这个卡死就可以被中断,也可以用wake_even_timeout,设置超时时间,当时间到了还在之前的地方就会被强制打断,上图红字所示 。

所以整个程序,中断前卡死在read那里,按一下按键就执行一次回调函数。

代码:

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <asm/string.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/irqreturn.h>
#include <mach/irqs.h>
#include <linux/wait.h>
#include <linux/sched.h>#define MAJOR_NUM 247
#define MINOR_NUM 0
#define DEV_NAME "eint8_key"
static wait_queue_head_t wq;
static int condition = 0;irqreturn_t eint8_handler(int irq, void *arg)
{condition = 1;wake_up_interruptible(&wq);printk("irq = %d    arg = %d\n", irq, *(int *)arg);return IRQ_HANDLED;
}static int open(struct inode *node, struct file *file)
{printk("kernrl open \n");return 0;
}static int read(struct file *file, char __user *buf, size_t len, loff_t *loff)
{condition = 0;wait_event_interruptible(wq, condition);printk("kernrl read \n");return 0;
}static int write(struct file *file, const char __user *buf, size_t len, loff_t *loff)
{printk("kernrl write \n");return 0;
}static int close(struct inode *node, struct file *file)
{printk("kernrl close \n");return 0;
}static dev_t dev_num;
static struct file_operations fops =
{.owner = THIS_MODULE,.open = open,.read = read,.write = write,.release = close
};
static struct cdev dev;
static unsigned int args = 100;static int __init exit_key_init(void)
{int ret = 0;dev_num = MKDEV(MAJOR_NUM, MINOR_NUM);ret = cdev_add(&dev, dev_num, 1);if(ret < 0)goto err_add_failed;cdev_init(&dev, &fops);ret = register_chrdev_region(dev_num, 1, DEV_NAME);if(ret < 0)goto err2_register_failed;ret = request_irq(IRQ_EINT8, eint8_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED, "exit_key", &args);if(ret < 0)goto err_request_irq_failed;printk("eint8_key_init       /\n");return 0;err_add_failed:printk("cdev_add failed\n");cdev_del(&dev);return ret;err2_register_failed:printk("register_chrdev_region failed\n");unregister_chrdev_region(dev_num, 1);cdev_del(&dev);return ret;err_request_irq_failed:printk("err_request_irq failed\n");disable_irq(IRQ_EINT8);free_irq(IRQ_EINT8, &args);unregister_chrdev_region(dev_num, 1);cdev_del(&dev);return ret;}static void __exit key_exit(void)
{disable_irq(IRQ_EINT8);free_irq(IRQ_EINT8, &args);unregister_chrdev_region(dev_num, 1);cdev_del(&dev);printk("led_exit ....\n");
}module_init(exit_key_init);
module_exit(key_exit);

http://www.lryc.cn/news/339024.html

相关文章:

  • 第十五届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组
  • kotlin编译版本
  • 【C#】 删除首/尾部字符
  • 第十五篇【传奇开心果系列】Python自动化办公库技术点案例示例:深度解读Python 自动化处理图像在各行各业的应用场景
  • 什么是MOV视频格式?如何把MP4视频转MOV视频格式?
  • 整理的微信小程序日历(单选/多选/筛选)
  • Unity 人形骨骼动画模型嘴巴张开
  • Python爬虫-京东商品评论数据
  • 实况窗助力美团打造鸿蒙原生外卖新体验,用户可实时掌握外卖进展
  • Go语言mac环境搭建详解
  • 房贷还款(C语言)
  • 系统架构最佳实践 -- 供应链系统架构
  • SpringCloudAlibaba-整合sentinel(四)
  • 【研发日记】Matlab/Simulink软件优化(一)——动态内存负荷压缩
  • python使用Flask框架开发API
  • 使用hexo+gitee从零搭建个人博客
  • 绝地求生:杜卡迪来了,这些摩托车技巧不学一下吗?
  • openstack安装dashboard后登录网页显示404错误
  • c# Xml 和 Json 转换方法记录
  • OpenHarmony南向开发案例:【智能垃圾桶】
  • 每日一题---OJ题: 旋转数组
  • 基于单链表的通讯录C语言实现
  • 【深度学习】YOLO-World: Real-Time Open-Vocabulary Object Detection,目标检测
  • debian安装和基本使用
  • nvm安装详细教程(安装nvm、node、npm、cnpm、yarn及环境变量配置)
  • 优优嗨聚集团:如何优雅地解决个人债务问题,一步步走向财务自由
  • SpringCloud实用篇(四)——Nacos
  • 【嵌入式基础知识学习】AD/DA—数模/模数转换
  • Swift中的结构体
  • Selenium - java - 屏幕截图