驱动开发——中断号与中断编程

更新时间:2023-05-16 10:36:10 阅读: 评论:0

驱动开发——中断号与中断编程
⽬录:
  1、中断号
  2、获取中断号
  3、实现中断处理
  4、中断编程—实现字符设备驱动框架
  5、驱动实现将硬件数据传递给数据
  6、⽰例
1、中断号
  中断号是系统分配给每个中断源的代号,以便识别和处理。在采⽤向量中断⽅式的中断系统中,CPU必须通过它才可以找到中断服务程序的⼊⼝地址,实现程序的转移。
  在ARM裸机中实现中断需要配置:
1 I/O⼝为中断模式,触发⽅式,I/O⼝中断使能
2设置GIC中断使能,分发配置,分发总使能,CPU外部中断接⼝使能,中断优先级
  在linux内核中实现中断,只需要知道:
1中断号是什么,怎么得到中断号
2中断处理⽅法
 2、获取中断号的⽅法:
  1)宏定义
    在没有设备树的内核中,中断号定义为宏,IRQ_EINT
  2)设备树⽂件中
    arch/arm/boot/dts/exynos4412-fs4412.dts
  1)看原理图,芯⽚⼿册找到中断源对应的中断号SPI Port  No
  2)进⼊设备树,在arch/arm/boot/dts/exynos4x12-pinctrl.dtsi中
1  gpx1: gpx1 {
2                    gpio-controller;
3                    #gpio-cells = <2>;
4
5                    interrupt-controller;  //中断控制器
6                    interrupt-parent = <&gic>;  //继承于gic
7                    interrupts = <0240>, <0250>, <0260>, <0270>,
8                                  <0280>, <0290>, <0300>, <0310>;
9                    #interrupt-cells = <2>; //⼦继承的interrupts的长度
10            };
  括号中的24、 25等对应于SPI Port No,以上是系统中已经定义好的节点
在编程中,需要定义⾃⼰的节点,⽤来描述按键,打开可编辑的设备树⽂件:
arch/arm/boot/dts/exynos4412-fs4412.dts,进⼊⽂件。
  3)定义节点,描述当前设备⽤的中断号
1 key_int_node{
2            compatible = "test_key";
3            interrupt-parent = <&gpx1>;  //继承于gpx1
4            interrupts = <24>;      //2表⽰第⼏个中断号,4表⽰触发⽅式为下降沿
5        };              //interrupts⾥长度由⽗母的-cell决定
  再举个栗⼦,设置k4 --- GPX3_2(XEINT26) 的节点,中断号
1 key_int_node{
2              compatible = "test_key";
3              interrupt-parent = <&gpx3>;  //继承于gpx3
4              interrupts = <24>;      //2表⽰第2个中断号,4表⽰触发⽅式为下降沿
5          };
    中断号的定位⽅法:
    看I/O引脚,GPX1_2,中断号就是GPX1⾥⾯的第2个
says
  4)编译设备树:make dtbs
    更新设备树⽂件: cp  -raf arch/arm/boot/dts/exynos4412-fs4412.dtb  /tftpboot/    查看定义的节点:在根⽬录的 proc/device-tree/⽬录下  poss
3、实现中断处理⽅法
  在驱动中通过代码获取到中断号,并且申请中断
  先看⼀下中断相关的函数:
1 a,获取到中断号码:
2int get_irqno_from_node(void)
3    {
4// 获取到设备树中的节点
5struct device_node *np = of_find_node_by_path("/key_int_node");
6if(np){
7            printk("find node ok\n");
8        }el{
9            printk("find node failed\n");
覆盖层10        }
11
12// 通过节点去获取到中断号码
13int irqno = irq_of_par_and_map(np, 0);
14        printk("irqno = %d\n", irqno);
15
16return irqno;
17    }
18 b,申请中断
19int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, const char * name, void * dev) 20参数1: irq    设备对应的中断号
21参数2: handler    中断的处理函数
22            typedef irqreturn_t (*irq_handler_t)(int, void *);
23参数3:flags    触发⽅式
24#define IRQF_TRIGGER_NONE    0x00000000  //内部控制器触发中断的时候的标志
25#define IRQF_TRIGGER_RISING    0x00000001 //上升沿
26#define IRQF_TRIGGER_FALLING    0x00000002 //下降沿
27#define IRQF_TRIGGER_HIGH    0x00000004  // ⾼点平
28#define IRQF_TRIGGER_LOW    0x00000008 //低电平触发
29参数4:name    中断的描述,⾃定义,主要是给⽤户查看的ending什么意思
30            /proc/interrupts
31参数5:dev    传递给参数2中函数指针的值
32返回值:正确为0,错误⾮0
33
34
35参数2的赋值:即中断处理函数
36    irqreturn_t key_irq_handler(int irqno, void *devid)
37    {
38return IRQ_HANDLED;
39    }
43
44 c, 释放中断:
45void free_irq(unsigned int irq, void *dev_id)
46参数1:设备对应的中断号
47参数2:与request_irq中第5个参数保持⼀致
代码实现获取中断号,并注册中断,按下按键引发中断,打印信息
characterize
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/fs.h>
4 #include <linux/device.h>
5 #include <asm/uaccess.h>
6 #include <asm/io.h>
7 #include <linux/slab.h>
8 #include <linux/of.h>
9 #include <linux/of_irq.h>
10 #include <linux/interrupt.h>
11
12int irqno;    //中断号
13
14
15 irqreturn_t key_irq_handler(int irqno, void *devid)
16 {
17    printk("----------%s---------",__FUNCTION__);
18return IRQ_HANDLED;
19 }
20
21
22//获取中断号
23int get_irqno_from_node(void)
24 {
25//获取设备树中的节点
26struct device_node *np = of_find_node_by_path("/key_int_node");
27if(np){
28        printk("find node success\n");
29    }el{
30        printk("find node failed\n");
31    }
32
33//通过节点去获取中断号
34int irqno = irq_of_par_and_map(np, 0);
35    printk("iqrno = %d",irqno);
36
37return irqno;
38 }
39
40
41
42static int __init key_drv_init(void)
43 {
44//演⽰如何获取到中断号
45int ret;
46
47    irqno = get_irqno_from_node();
48
49    ret = request_irq(irqno, key_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 50"key3_eint10", NULL);
51if(ret != 0)
52    {
53        printk("request_irq error\n");
54return ret;
55    }
56
57return0;
58 }
59
60static void __exit key_drv_exit(void)
61 {
62    free_irq(irqno, NULL);  //free_irq与request_irq的最后⼀个参数⼀致
63 }
64
65
66
67 module_init(key_drv_init);
吸血鬼日记2电视剧
68 module_exit(key_drv_exit);
69
70 MODULE_LICENSE("GPL");
key_drv.c
测试效果:
按键按下,打印信息,但出现了按键抖动
cat /proc/interrupt
  4、中断编程 --- 字符设备驱动框架
1// 1,设定⼀个全局的设备对象
2 key_dev = kzalloc(sizeof(struct key_desc),  GFP_KERNEL);
3
4// 2,申请主设备号
5 key_dev->dev_major = register_chrdev(0, "key_drv", &key_fops);
6
7// 3,创建设备节点⽂件
8 key_dev->cls = class_create(THIS_MODULE, "key_cls");
9 key_dev->dev = device_create(key_dev->cls, NULL, MKDEV(key_dev->dev_major,0), NULL, "key0"); 10
11// 4,硬件初始化:
12        a.地址映射
13        b.中断申请
  5、驱动实现将硬件所产⽣的数据传递给⽤户
  1)硬件如何获取数据
key:按下和抬起:1/0
读取key对应的gpio的状态,可以判断按下还是抬起i have a dream 马丁路德金
读取key对应gpio的寄存器--数据寄存器
//读取数据寄存器
int value = readl(key_dev->reg_ba + 4) & (1<<2);
  2)驱动传递数据给⽤户
在中断处理中填充数据:
key_dev-&de = KEY_ENTER;
key_dev->event.value = 0;
在xxx_read中奖数据传递给⽤户
ret = copy_to_ur(buf, &key_dev->event,  count);
  3)⽤户获取数据
while(1)
{
read(fd, &event, sizeof(struct key_event));
de == KEY_ENTER)
{
if(event.value)
{
printf("APP__ key enter presd\n");
}el{
printf("APP__ key enter up\n");
}
}
}
  6、⽰例:
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/of.h>
4 #include <linux/of_irq.h>
5 #include <linux/interrupt.h>
6 #include <linux/slab.h>
7 #include <linux/fs.h>
8 #include <linux/device.h>
9 #include <linux/kdev_t.h>
10 #include <linux/err.h>
11 #include <linux/device.h>
12 #include <asm/io.h>
13 #include <asm/uaccess.h>
14
15
16#define GPXCON_REG 0X11000C20  //不可以从数据寄存器开始映射,要配置寄存器
17#define KEY_ENTER  28
18
19//0、设计⼀个描述按键的数据的对象
20struct key_event{
21int code;    //按键类型:home,esc,enter
22int value;  //表状态,按下,松开
23 };
24
25//1、设计⼀个全局对象——— 描述key的信息
26struct key_desc{
27    unsigned int dev_major;
28int irqno;  //中断号
29struct class  *cls;
袭击的意思30struct device *dev;
31void *reg_ba;
32struct key_event event;
33 };
34
35struct key_desc *key_dev;
36
37
38 irqreturn_t key_irq_handler(int irqno, void *devid)
39 {
40    printk("----------%s---------",__FUNCTION__);
41
42int value;
43//读取按键状态
44    value = readl(key_dev->reg_ba + 4) & (0x01<<2);
45
46if(value){
47        printk("key3 up\n");
免费英语在线翻译
48        key_dev-&de  = KEY_ENTER;
49        key_dev->event.value = 0;
50    }el{
51        printk("key3 down\n");
52        key_dev-&de  = KEY_ENTER;
53        key_dev->event.value = 1;
54    }
55return IRQ_HANDLED;
56 }
57
58
59//获取中断号
60int get_irqno_from_node(void)
61 {
62int irqno;
63//获取设备树中的节点
64struct device_node *np = of_find_node_by_path("/key_int_node");
65if(np){
66        printk("find node success\n");
67    }el{
68        printk("find node failed\n");
69    }
70
71//通过节点去获取中断号
72    irqno = irq_of_par_and_map(np, 0);
73    printk("iqrno = %d",key_dev->irqno);
74
75return irqno;
76 }
77
78 ssize_t key_drv_read (struct file * filp, char __ur * buf, size_t count, loff_t * fops)
79 {
80//printk("----------%s---------",__FUNCTION__);
世界名校公开课81int ret;
82    ret = copy_to_ur(buf, &key_dev->event, count);
83if(ret > 0)
84    {
85        printk("copy_to_ur error\n");
86return -EFAULT;
87    }
88
89//传递给⽤户数据后,将数据清除,否则APP每次读都是第⼀次的数据
90    memt(&key_dev->event, 0, sizeof(key_dev->event));
91return count;
92 }
93
94 ssize_t key_drv_write (struct file *filp, const char __ur * buf, size_t count, loff_t * fops)
95 {
96    printk("----------%s---------",__FUNCTION__);
97return0;
98 }
99
100int key_drv_open (struct inode * inode, struct file *filp)
101 {
102    printk("----------%s---------",__FUNCTION__);
103return0;
104 }
105
106int key_drv_clo (struct inode *inode, struct file *filp)
107 {
108    printk("----------%s---------",__FUNCTION__);

本文发布于:2023-05-16 10:36:10,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/652974.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:中断   设备   实现   数据   获取   节点   按键
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图