Level11day7作业

按键驱动之申请中断实验

1. 设备驱动代码

//设备驱动代码
//------------------key_drv.c------------------
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>

int irqno;

irqreturn_t key_irq_handler(int irqno, void *devid)
{
	printk("--------------%s------------------",__FUNCTION__);

	return IRQ_HANDLED;
}



int get_irqno_from_node(void)
{
	//获取到设备树中的节点

	struct device_node *np = of_find_node_by_path("/key_int_node");
	
	if(np){
		printk("find node ok\n");
	}
	else{
		printk("find node failed\n");
	}
	//通过节点去获取到中断号码
	irqno = irq_of_parse_and_map(np,0);
	
	printk("irqno = %d\n",irqno);

	return irqno;
}

static int __init key_drv_init(void)
{
	//演示如何获取中断号
	int irqno,ret;

	irqno = get_irqno_from_node();

	ret = request_irq(irqno, key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "IRQ_EINT10", NULL);

	if(ret != 0)
	{
		printk("request_irq ERROR!\n");
		return ret;
	}
	return 0;
}

static void __exit key_drv_exit(void)
{
	free_irq(irqno,NULL);	
}


module_init(key_drv_init);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");

2.Makefile代码

ROOTFS_DIR = /home/linux/Level11

#APP_NAME = led_test

CROSS_COMPILE = arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc

ifeq ($(KERNELRELEASE), )

KERNEL_DIR = /home/linux/Level10/day6/linux-5.4.79
CUR_DIR = $(shell pwd)

all :
		make -C  $(KERNEL_DIR) M=$(CUR_DIR) modules -j6 ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
		#$(CC) $(APP_NAME).c -o $(APP_NAME)

clean :
		make -C  $(KERNEL_DIR) M=$(CUR_DIR) clean
		clear

install:
		cp -raf  *.ko  $(APP_NAME)   ./1/
		
else
obj-m += key_drv.o

endif

3.效果展示:

[root@farsight ]# insmod *.ko
[   17.570120] key_drv: loading out-of-tree module taints kernel.
[   17.575433] find node ok
[   17.577120] irqno = 107
[root@farsight ]# cat /proc/interrupts
           CPU0       CPU1       CPU2       CPU3
 36:          0          0          0          0     GIC-0  89 Level     mct_comp_irq
 37:      20031       3406       1122       6958     GIC-0  28 Level     MCT
 38:          0          0          0          0  COMBINER  18 Edge      arm-pmu
 39:          0          0          0          0  COMBINER  26 Edge      arm-pmu
...
106:      19237          0          0          0  exynos4210_wkup_irq_chip   6 Level     eth0
107:          0          0          0          0  exynos4210_wkup_irq_chip   2 Edge      IRQ_EINT10
IPI0:          0          1          1          1  CPU wakeup interrupts
...
按下按键K3
[root@farsight ]# [ 2979.408843] --------------key_irq_handler------------------
[ 2983.202843] --------------key_irq_handler------------------

[root@farsight ]#