目录
注册一个中断并编写中断处理程序
[root@ecs tmp]# cat test.c
#include <linux/interrupt.h>
#include<linux/irq.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int irq=13; //13
// 自定义中断处理函数
static irqreturn_t myhandler(int data, void *dev_id)
{
printk("the data is :%d\n", data); //data是对应中断的中断号
printk("in the interrupt handler function\n");
return IRQ_HANDLED;
}
static int __init request_irq_init(void)
{
int result=0;
printk("into request_irq_init\n");
/*调用函数request_irq( )申请中断,irq指中断编号,irq_handler是中断处理函数,IRQF_DISABLED
是中断类型,“A_New_Device”指中断设备名,NULL指设备,设备为NULL说明设备不真实存在*/
result=request_irq(irq, myhandler, IRQF_DISABLED, "A_New_Device", NULL);
printk("the result of the request_irq is: %d\n", result); //显示申请结果
printk("out request_irq_init\n");
return 0;
}
static void __exit request_irq_exit(void)
{
free_irq(irq, NULL); //释放申请的中断
printk("Goodbye request_irq\n");
return;
}
module_init(request_irq_init);
module_exit(request_irq_exit);
编写Makefile
[root@ecs tmp]# cat Makefile
#
# Makefile for the IPVS TOA modules on top of IPv4.
#
obj-$(CONFIG_TOA) += test.o
default:
make CONFIG_TOA=m -C /lib/modules/`uname -r`/build M=`pwd` modules
debug:
make CONFIG_TOA=m -C /lib/modules/`uname -r`/build M=`pwd` modules EXTRA_CFLAGS="-DTOA_DBG_MSG"
clean:
rm -f .*.cmd *.o *.ko *.mod.c modules.order Module.symvers
rm -rf .tmp_versions
install:
test -d "$(DESTDIR)/lib/modules/`uname -r`/extra/net/toa" \
|| mkdir -p "$(DESTDIR)/lib/modules/`uname -r`/extra/net/toa"
cp *.ko "$(DESTDIR)/lib/modules/`uname -r`/extra/net/toa"
编译源码
[root@ecs tmp]# make
make CONFIG_TOA=m -C /lib/modules/`uname -r`/build M=`pwd` modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-1062.el7.x86_64'
CC [M] /tmp/test.o
Building modules, stage 2.
MODPOST 1 modules
CC /tmp/test.mod.o
LD [M] /tmp/test.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-1062.el7.x86_64'
[root@ecs tmp]#
插入刚生成的测试模块
[root@ecs tmp]# dmesg -c
[root@ecs tmp]# insmod test.ko
[root@ecs tmp]# dmesg
[3118569.100416] into request_irq_init
[3118569.100468] the result of the request_irq is: 0
[3118569.100470] out request_irq_init
[root@ecs tmp]#
查看/proc/interrupts中自己编写的中断
[root@ecs tmp]# cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 43 0 0 0 IO-APIC-edge timer
1: 254 0 0 0 IO-APIC-edge i8042
5: 41 0 0 0 IO-APIC-fasteoi ipmi_si
6: 3 0 0 0 IO-APIC-edge floppy
8: 0 0 0 0 IO-APIC-edge rtc0
9: 2 0 0 0 IO-APIC-fasteoi acpi
10: 311861 0 0 0 IO-APIC-fasteoi virtio2
11: 29 0 0 0 IO-APIC-fasteoi uhci_hcd:usb1
12: 15 0 0 0 IO-APIC-edge i8042
13: 0 0 0 0 IO-APIC-edge A_New_Device //此处是新注册的中断
14: 0 0 0 0 IO-APIC-edge ata_piix
15: 0 0 0 0 IO-APIC-edge ata_piix
24: 0 0 0 0 PCI-MSI-edge virtio3-config
25: 1463404 1102540 0 0 PCI-MSI-edge virtio3-req.0
26: 0 0 0 0 PCI-MSI-edge virtio0-config
27: 6 2345591 2201723 0 PCI-MSI-edge virtio0-input.0
28: 2 0 51 71 PCI-MSI-edge virtio0-output.0
29: 0 0 0 0 PCI-MSI-edge virtio1-config
30: 33 0 0 0 PCI-MSI-edge virtio1-virtqueues
NMI: 0 0 0 0 Non-maskable interrupts
LOC: 89430313 36831946 83767379 34099261 Local timer interrupts
SPU: 0 0 0 0 Spurious interrupts
PMI: 0 0 0 0 Performance monitoring interrupts
IWI: 849901 1889946 1328760 1777716 IRQ work interrupts
RTR: 0 0 0 0 APIC ICR read retries
RES: 4515561 5633273 4995706 3817982 Rescheduling interrupts
CAL: 914 786 827 783 Function call interrupts
TLB: 85275 233634 150226 286200 TLB shootdowns
TRM: 0 0 0 0 Thermal event interrupts
THR: 0 0 0 0 Threshold APIC interrupts
DFR: 0 0 0 0 Deferred Error APIC interrupts
MCE: 0 0 0 0 Machine check exceptions
MCP: 10396 10396 10396 10396 Machine check polls
ERR: 0
MIS: 0
PIN: 0 0 0 0 Posted-interrupt notification event
NPI: 0 0 0 0 Nested posted-interrupt event
PIW: 0 0 0 0 Posted-interrupt wakeup event
由于没有触发这个中断,所以中断处理函数中的代码没有执行,但是参考的连接Linux内核API request_irq|极客笔记在dmesg中会执行中断处理函数中的代码