如何在一个内核模块中包含三个 IRQ 处理程序?
How to include three IRQ handlers in one kernel module?
我目前正在学习 Derek Molloy 在他的书中的示例“探索 Raspberry Pi - 使用嵌入式 Linux 连接到真实世界”。我在 清单 16-3 中使用了示例,不幸的是我无法在网上找到它。
该示例包含用于单个中断的内核模块代码。它从 GPIO 17 的按钮读取信号,然后发送中断以打开 GPIO 27 的 LED。本书使用默认的 GPIO 引脚编号,所以我也这样做。
我想做的是修改代码以包含另外 2 个按钮-LED 对。我想这样做:
- GPIO 17转on/off GPIO 23转
- GPIO 27转on/off GPIO 24转
- GPIO 22转on/offGPIO 25
这是我使用的修改后的代码。
static unsigned int gpioDevice1 = 17;
static unsigned int gpioDevice2 = 27;
static unsigned int gpioDevice3 = 22;
static unsigned int gpioButton1 = 24;
static unsigned int gpioButton2 = 23;
static unsigned int gpioButton3 = 25;
static unsigned int irqNumber1On;
static unsigned int irqNumber2On;
static unsigned int irqNumber3On;
static unsigned int buttonCounter1 = 0;
static unsigned int buttonCounter2 = 0;
static unsigned int buttonCounter3 = 0;
static unsigned int totalCounter = 0;
static bool devOn1 = 0; // Initial state of devices
static bool devOn2 = 0;
static bool devOn3 = 0;
// prototype for the custom IRQ handler function, function below. Should I use IRQF_SHARED here?
static irq_handler_t rpi3_gpio_irq_handler_1(unsigned int irq, void *dev_id, struct pt_regs *regs);
static irq_handler_t rpi3_gpio_irq_handler_2(unsigned int irq, void *dev_id, struct pt_regs *regs);
static irq_handler_t rpi3_gpio_irq_handler_3(unsigned int irq, void *dev_id, struct pt_regs *regs);
/** LKM initialization function */
static int __init rpi3_gpio_init(void) {
int result1On = 0;
int result2On = 0;
int result3On = 0;
printk(KERN_INFO "GPIO_TEST: Initializing the GPIO_TEST LKM\n");
/* GPIO validation on the three devices */
if (!gpio_is_valid(gpioDevice1) || !gpio_is_valid(gpioDevice2) || !gpio_is_valid(gpioDevice3)) {
printk(KERN_INFO "GPIO_TEST: invalid GPIO for Devices\n");
return -ENODEV; //wouldn't using ENXIO is more appropriate than ENODEV?
}
/* Configuring GPIO pins for the pairs */
gpio_request(gpioDevice1, "sysfs"); // request LED GPIO
gpio_direction_output(gpioDevice1, devOn1); // set in output mode
gpio_export(gpioDevice1, false); // appears in /sys/class/gpio
// false prevents in/out change
gpio_request(gpioDevice2, "sysfs");
gpio_direction_output(gpioDevice2, devOn2);
gpio_export(gpioDevice2, false);
gpio_request(gpioDevice3, "sysfs");
gpio_direction_output(gpioDevice3, devOn3);
gpio_export(gpioDevice3, false);
gpio_request(gpioButton1, "sysfs"); // set up gpioButton1
gpio_direction_input(gpioButton1); // set up as input
gpio_set_debounce(gpioButton1, 200); // debounce delay of 200ms to avoid erratic and uncontrolled interrupt
gpio_export(gpioButton1, false); // appears in /sys/class/gpio
gpio_request(gpioButton2, "sysfs");
gpio_direction_input(gpioButton2);
gpio_set_debounce(gpioButton2, 200);
gpio_export(gpioButton2, false);
gpio_request(gpioButton3, "sysfs");
gpio_direction_input(gpioButton3);
gpio_set_debounce(gpioButton3, 200);
gpio_export(gpioButton3, false);
printk(KERN_INFO "GPIO_TEST: button1 value is currently: %d\n", gpio_get_value(gpioButton1));
irqNumber1On = gpio_to_irq(gpioButton1); // map GPIO to IRQ number 189?
printk(KERN_INFO "GPIO_TEST: button1 mapped to IRQ: %d\n", irqNumber1On);
printk(KERN_INFO "GPIO_TEST: button2 value is currently: %d\n", gpio_get_value(gpioButton2));
irqNumber2On = gpio_to_irq(gpioButton2); // map GPIO to IRQ number 190?
printk(KERN_INFO "GPIO_TEST: button2 mapped to IRQ: %d\n", irqNumber2On);
printk(KERN_INFO "GPIO_TEST: button3 value is currently: %d\n", gpio_get_value(gpioButton3));
irqNumber3On = gpio_to_irq(gpioButton3); // map GPIO to IRQ number 191?
printk(KERN_INFO "GPIO_TEST: button3 mapped to IRQ: %d\n", irqNumber3On);
/* Interrupt lines when tactile button is pressed */
result1On = request_irq(irqNumber1On, // interrupt number requested
(irq_handler_t) rpi3_gpio_irq_handler_1, // handler function
// TO DO: Insert IRQF_SHARED here?
IRQF_TRIGGER_RISING, // on rising edge (press, not release)
"rpi3_gpio_handler", // used in /proc/interrupts
NULL); // *dev_id for shared interrupt lines shouldn't be NULL
printk(KERN_INFO "GPIO_TEST: IRQ request result for device 1 is: %d\n", result1On);
return result1On;
result2On = request_irq(irqNumber2On,
(irq_handler_t) rpi3_gpio_irq_handler_2,
IRQF_TRIGGER_RISING,
"rpi3_gpio_handler",
NULL);
printk(KERN_INFO "GPIO_TEST: IRQ request result for device 2 is: %d\n", result2On);
return result2On;
result3On = request_irq(irqNumber3On,
(irq_handler_t) rpi3_gpio_irq_handler_3,
IRQF_TRIGGER_RISING,
"rpi3_gpio_handler",
NULL);
printk(KERN_INFO "GPIO_TEST: IRQ request result for device 3 is: %d\n", result3On);
return result3On;
}
static void __exit rpi3_gpio_exit(void) {
printk(KERN_INFO "GPIO_TEST: button 1 value is currently: %d\n", gpio_get_value(gpioButton1));
printk(KERN_INFO "GPIO_TEST: button 1 was pressed %d times\n", buttonCounter1);
printk(KERN_INFO "GPIO_TEST: button 2 value is currently: %d\n", gpio_get_value(gpioButton2));
printk(KERN_INFO "GPIO_TEST: button 2 was pressed %d times\n", buttonCounter2);
printk(KERN_INFO "GPIO_TEST: button 3 value is currently: %d\n", gpio_get_value(gpioButton3));
printk(KERN_INFO "GPIO_TEST: button 3 was pressed %d times\n", buttonCounter3);
printk(KERN_INFO "GPIO_TEST: in total the buttons was pressed %d times\n", totalCounter);
gpio_set_value(gpioDevice1, 0); // turn the LED off
gpio_unexport(gpioDevice1); // unexport the LED GPIO
free_irq(irqNumber1On, NULL); // free the IRQ number, no *dev_id?
gpio_unexport(gpioButton1); // unexport the Button GPIO
gpio_free(gpioDevice1); // free the LED GPIO
gpio_free(gpioButton1); // free the Button GPIO
gpio_set_value(gpioDevice2, 0);
gpio_unexport(gpioDevice2);
free_irq(irqNumber2On, NULL);
gpio_unexport(gpioButton2);
gpio_free(gpioDevice2);
gpio_free(gpioButton2);
gpio_set_value(gpioDevice3, 0);
gpio_unexport(gpioDevice3);
free_irq(irqNumber3On, NULL);
gpio_unexport(gpioButton3);
gpio_free(gpioDevice3);
gpio_free(gpioButton3);
printk(KERN_INFO "GPIO_TEST: Goodbye from the LKM!\n");
}
/** GPIO IRQ Handler functions */
static irq_handler_t rpi3_gpio_irq_handler_1(unsigned int irq, void *dev_id, struct pt_regs *regs) {
devOn1 = !devOn1; // invert the LED state
gpio_set_value(gpioDevice1, devOn1); // set LED accordingly
printk(KERN_INFO "GPIO_TEST: Interrupt! (button 1 is %d)\n",
gpio_get_value(gpioButton1));
buttonCounter1++;
totalCounter++; // global counter
return (irq_handler_t) IRQ_HANDLED; // announce IRQ handled
}
static irq_handler_t rpi3_gpio_irq_handler_2(unsigned int irq, void *dev_id, struct pt_regs *regs) {
devOn2 = !devOn2;
gpio_set_value(gpioDevice2, devOn2);
printk(KERN_INFO "GPIO_TEST: Interrupt! (button 2 is %d)\n",
gpio_get_value(gpioButton2));
buttonCounter2++;
totalCounter++;
return (irq_handler_t) IRQ_HANDLED;
}
static irq_handler_t rpi3_gpio_irq_handler_3(unsigned int irq, void *dev_id, struct pt_regs *regs) {
devOn3 = !devOn3;
gpio_set_value(gpioDevice3, devOn3);
printk(KERN_INFO "GPIO_TEST: Interrupt! (button 3 is %d)\n",
gpio_get_value(gpioButton3));
buttonCounter3++;
totalCounter++;
return (irq_handler_t) IRQ_HANDLED;
}
module_init(rpi3_gpio_init);
module_exit(rpi3_gpio_exit);
为了获取 IRQ 编号,我在示例中使用了 gpio_to_irq()
,因为我不知道哪些值是有效编号。
第一对效果很好,但无论我按多少次按钮,其他对都不起作用。当我用 cat /proc/interrupts
检查 IRQ 号时
好像只有第一个得到IRQ号,就是189。假设,其他两个应该可能得到 190 和 191,但它们不存在。
printk()
函数也只显示 irqnumber1On
的行,而 irqnumber2On
和 irqnumber3On
的行没有出现。
我给中断设置了 NULL dev_id 因为我不知道如何 give/read 按钮的 ID。我尝试了随机数字组合,例如 500、505 和 550,但终端显示 warning: passing argument 5 of 'request_irq' makes pointer from integer without a cast
.
那么,我在这里做错了什么?我坚持了很长一段时间。我应该尝试使用 IRQF_SHARED
吗?但每个中断(或本例中的按钮)都需要特定的 dev_id
。菜鸟脑子里觉得不太可能
PS: 我知道代码看起来乱糟糟的,但请多多包涵。
PPS:如果认为有必要,我可以删除部分代码。
此语句后的任何代码:
return result1On;
永远不会被执行。
这意味着其他两个按钮中断将永远不会被处理
我目前正在学习 Derek Molloy 在他的书中的示例“探索 Raspberry Pi - 使用嵌入式 Linux 连接到真实世界”。我在 清单 16-3 中使用了示例,不幸的是我无法在网上找到它。
该示例包含用于单个中断的内核模块代码。它从 GPIO 17 的按钮读取信号,然后发送中断以打开 GPIO 27 的 LED。本书使用默认的 GPIO 引脚编号,所以我也这样做。
我想做的是修改代码以包含另外 2 个按钮-LED 对。我想这样做:
- GPIO 17转on/off GPIO 23转
- GPIO 27转on/off GPIO 24转
- GPIO 22转on/offGPIO 25
这是我使用的修改后的代码。
static unsigned int gpioDevice1 = 17;
static unsigned int gpioDevice2 = 27;
static unsigned int gpioDevice3 = 22;
static unsigned int gpioButton1 = 24;
static unsigned int gpioButton2 = 23;
static unsigned int gpioButton3 = 25;
static unsigned int irqNumber1On;
static unsigned int irqNumber2On;
static unsigned int irqNumber3On;
static unsigned int buttonCounter1 = 0;
static unsigned int buttonCounter2 = 0;
static unsigned int buttonCounter3 = 0;
static unsigned int totalCounter = 0;
static bool devOn1 = 0; // Initial state of devices
static bool devOn2 = 0;
static bool devOn3 = 0;
// prototype for the custom IRQ handler function, function below. Should I use IRQF_SHARED here?
static irq_handler_t rpi3_gpio_irq_handler_1(unsigned int irq, void *dev_id, struct pt_regs *regs);
static irq_handler_t rpi3_gpio_irq_handler_2(unsigned int irq, void *dev_id, struct pt_regs *regs);
static irq_handler_t rpi3_gpio_irq_handler_3(unsigned int irq, void *dev_id, struct pt_regs *regs);
/** LKM initialization function */
static int __init rpi3_gpio_init(void) {
int result1On = 0;
int result2On = 0;
int result3On = 0;
printk(KERN_INFO "GPIO_TEST: Initializing the GPIO_TEST LKM\n");
/* GPIO validation on the three devices */
if (!gpio_is_valid(gpioDevice1) || !gpio_is_valid(gpioDevice2) || !gpio_is_valid(gpioDevice3)) {
printk(KERN_INFO "GPIO_TEST: invalid GPIO for Devices\n");
return -ENODEV; //wouldn't using ENXIO is more appropriate than ENODEV?
}
/* Configuring GPIO pins for the pairs */
gpio_request(gpioDevice1, "sysfs"); // request LED GPIO
gpio_direction_output(gpioDevice1, devOn1); // set in output mode
gpio_export(gpioDevice1, false); // appears in /sys/class/gpio
// false prevents in/out change
gpio_request(gpioDevice2, "sysfs");
gpio_direction_output(gpioDevice2, devOn2);
gpio_export(gpioDevice2, false);
gpio_request(gpioDevice3, "sysfs");
gpio_direction_output(gpioDevice3, devOn3);
gpio_export(gpioDevice3, false);
gpio_request(gpioButton1, "sysfs"); // set up gpioButton1
gpio_direction_input(gpioButton1); // set up as input
gpio_set_debounce(gpioButton1, 200); // debounce delay of 200ms to avoid erratic and uncontrolled interrupt
gpio_export(gpioButton1, false); // appears in /sys/class/gpio
gpio_request(gpioButton2, "sysfs");
gpio_direction_input(gpioButton2);
gpio_set_debounce(gpioButton2, 200);
gpio_export(gpioButton2, false);
gpio_request(gpioButton3, "sysfs");
gpio_direction_input(gpioButton3);
gpio_set_debounce(gpioButton3, 200);
gpio_export(gpioButton3, false);
printk(KERN_INFO "GPIO_TEST: button1 value is currently: %d\n", gpio_get_value(gpioButton1));
irqNumber1On = gpio_to_irq(gpioButton1); // map GPIO to IRQ number 189?
printk(KERN_INFO "GPIO_TEST: button1 mapped to IRQ: %d\n", irqNumber1On);
printk(KERN_INFO "GPIO_TEST: button2 value is currently: %d\n", gpio_get_value(gpioButton2));
irqNumber2On = gpio_to_irq(gpioButton2); // map GPIO to IRQ number 190?
printk(KERN_INFO "GPIO_TEST: button2 mapped to IRQ: %d\n", irqNumber2On);
printk(KERN_INFO "GPIO_TEST: button3 value is currently: %d\n", gpio_get_value(gpioButton3));
irqNumber3On = gpio_to_irq(gpioButton3); // map GPIO to IRQ number 191?
printk(KERN_INFO "GPIO_TEST: button3 mapped to IRQ: %d\n", irqNumber3On);
/* Interrupt lines when tactile button is pressed */
result1On = request_irq(irqNumber1On, // interrupt number requested
(irq_handler_t) rpi3_gpio_irq_handler_1, // handler function
// TO DO: Insert IRQF_SHARED here?
IRQF_TRIGGER_RISING, // on rising edge (press, not release)
"rpi3_gpio_handler", // used in /proc/interrupts
NULL); // *dev_id for shared interrupt lines shouldn't be NULL
printk(KERN_INFO "GPIO_TEST: IRQ request result for device 1 is: %d\n", result1On);
return result1On;
result2On = request_irq(irqNumber2On,
(irq_handler_t) rpi3_gpio_irq_handler_2,
IRQF_TRIGGER_RISING,
"rpi3_gpio_handler",
NULL);
printk(KERN_INFO "GPIO_TEST: IRQ request result for device 2 is: %d\n", result2On);
return result2On;
result3On = request_irq(irqNumber3On,
(irq_handler_t) rpi3_gpio_irq_handler_3,
IRQF_TRIGGER_RISING,
"rpi3_gpio_handler",
NULL);
printk(KERN_INFO "GPIO_TEST: IRQ request result for device 3 is: %d\n", result3On);
return result3On;
}
static void __exit rpi3_gpio_exit(void) {
printk(KERN_INFO "GPIO_TEST: button 1 value is currently: %d\n", gpio_get_value(gpioButton1));
printk(KERN_INFO "GPIO_TEST: button 1 was pressed %d times\n", buttonCounter1);
printk(KERN_INFO "GPIO_TEST: button 2 value is currently: %d\n", gpio_get_value(gpioButton2));
printk(KERN_INFO "GPIO_TEST: button 2 was pressed %d times\n", buttonCounter2);
printk(KERN_INFO "GPIO_TEST: button 3 value is currently: %d\n", gpio_get_value(gpioButton3));
printk(KERN_INFO "GPIO_TEST: button 3 was pressed %d times\n", buttonCounter3);
printk(KERN_INFO "GPIO_TEST: in total the buttons was pressed %d times\n", totalCounter);
gpio_set_value(gpioDevice1, 0); // turn the LED off
gpio_unexport(gpioDevice1); // unexport the LED GPIO
free_irq(irqNumber1On, NULL); // free the IRQ number, no *dev_id?
gpio_unexport(gpioButton1); // unexport the Button GPIO
gpio_free(gpioDevice1); // free the LED GPIO
gpio_free(gpioButton1); // free the Button GPIO
gpio_set_value(gpioDevice2, 0);
gpio_unexport(gpioDevice2);
free_irq(irqNumber2On, NULL);
gpio_unexport(gpioButton2);
gpio_free(gpioDevice2);
gpio_free(gpioButton2);
gpio_set_value(gpioDevice3, 0);
gpio_unexport(gpioDevice3);
free_irq(irqNumber3On, NULL);
gpio_unexport(gpioButton3);
gpio_free(gpioDevice3);
gpio_free(gpioButton3);
printk(KERN_INFO "GPIO_TEST: Goodbye from the LKM!\n");
}
/** GPIO IRQ Handler functions */
static irq_handler_t rpi3_gpio_irq_handler_1(unsigned int irq, void *dev_id, struct pt_regs *regs) {
devOn1 = !devOn1; // invert the LED state
gpio_set_value(gpioDevice1, devOn1); // set LED accordingly
printk(KERN_INFO "GPIO_TEST: Interrupt! (button 1 is %d)\n",
gpio_get_value(gpioButton1));
buttonCounter1++;
totalCounter++; // global counter
return (irq_handler_t) IRQ_HANDLED; // announce IRQ handled
}
static irq_handler_t rpi3_gpio_irq_handler_2(unsigned int irq, void *dev_id, struct pt_regs *regs) {
devOn2 = !devOn2;
gpio_set_value(gpioDevice2, devOn2);
printk(KERN_INFO "GPIO_TEST: Interrupt! (button 2 is %d)\n",
gpio_get_value(gpioButton2));
buttonCounter2++;
totalCounter++;
return (irq_handler_t) IRQ_HANDLED;
}
static irq_handler_t rpi3_gpio_irq_handler_3(unsigned int irq, void *dev_id, struct pt_regs *regs) {
devOn3 = !devOn3;
gpio_set_value(gpioDevice3, devOn3);
printk(KERN_INFO "GPIO_TEST: Interrupt! (button 3 is %d)\n",
gpio_get_value(gpioButton3));
buttonCounter3++;
totalCounter++;
return (irq_handler_t) IRQ_HANDLED;
}
module_init(rpi3_gpio_init);
module_exit(rpi3_gpio_exit);
为了获取 IRQ 编号,我在示例中使用了 gpio_to_irq()
,因为我不知道哪些值是有效编号。
第一对效果很好,但无论我按多少次按钮,其他对都不起作用。当我用 cat /proc/interrupts
好像只有第一个得到IRQ号,就是189。假设,其他两个应该可能得到 190 和 191,但它们不存在。
printk()
函数也只显示 irqnumber1On
的行,而 irqnumber2On
和 irqnumber3On
的行没有出现。
我给中断设置了 NULL dev_id 因为我不知道如何 give/read 按钮的 ID。我尝试了随机数字组合,例如 500、505 和 550,但终端显示 warning: passing argument 5 of 'request_irq' makes pointer from integer without a cast
.
那么,我在这里做错了什么?我坚持了很长一段时间。我应该尝试使用 IRQF_SHARED
吗?但每个中断(或本例中的按钮)都需要特定的 dev_id
。菜鸟脑子里觉得不太可能
PS: 我知道代码看起来乱糟糟的,但请多多包涵。
PPS:如果认为有必要,我可以删除部分代码。
此语句后的任何代码:
return result1On;
永远不会被执行。
这意味着其他两个按钮中断将永远不会被处理