172 lines
4.3 KiB
C
172 lines
4.3 KiB
C
/*
|
|
* bottomhalf.c - 起始与后续部分中断处理
|
|
*
|
|
* 基于 Stefan Wendler (devnull@kaltpost.de) 提供的 RPi 示例
|
|
* 来自:
|
|
* https://github.com/wendlers/rpi-kmod-samples
|
|
*
|
|
* 按下一个按钮以点亮 LED,按下另一个按钮以熄灭它
|
|
*/
|
|
|
|
#include <linux/delay.h>
|
|
#include <linux/gpio.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/module.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/init.h>
|
|
|
|
/* 宏 DECLARE_TASKLET_OLD 的存在是为了确保兼容
|
|
* 详细信息: https://lwn.net/Articles/830964/
|
|
*/
|
|
#ifndef DECLARE_TASKLET_OLD
|
|
#define DECLARE_TASKLET_OLD(arg1, arg2) DECLARE_TASKLET(arg1, arg2, 0L)
|
|
#endif
|
|
|
|
static int button_irqs[] = { -1, -1 };
|
|
|
|
/* 定义 LED 的 GPIO。
|
|
* TODO: 根据你的板子更改 GPIO 编号。
|
|
*/
|
|
static struct gpio leds[] = { { 1, GPIOF_OUT_INIT_LOW, "LED 1" } };
|
|
|
|
/* 定义按钮的 GPIO。
|
|
* TODO: 根据你的板子更改 GPIO 编号。
|
|
*/
|
|
static struct gpio buttons[] = { { 2, GPIOF_IN, "LED 1 打开按钮" },
|
|
{ 3, GPIOF_IN, "LED 1 关闭按钮" } };
|
|
|
|
/* 包含一些复杂处理的任务队列函数 */
|
|
static void bottomhalf_tasklet_fn(unsigned long data)
|
|
{
|
|
pr_info("底半部任务队列开始\n");
|
|
/* 执行一些耗时的操作 */
|
|
mdelay(500);
|
|
pr_info("底半部任务队列结束\n");
|
|
}
|
|
|
|
static DECLARE_TASKLET_OLD(buttontask, bottomhalf_tasklet_fn);
|
|
|
|
/* 按钮按下时触发的中断函数 */
|
|
static irqreturn_t button_isr(int irq, void *data)
|
|
{
|
|
/* 立即执行一些操作 */
|
|
if (irq == button_irqs[0] && !gpio_get_value(leds[0].gpio))
|
|
gpio_set_value(leds[0].gpio, 1); // 点亮 LED
|
|
else if (irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
|
|
gpio_set_value(leds[0].gpio, 0); // 熄灭 LED
|
|
|
|
/* 将剩余工作交给调度程序处理 */
|
|
tasklet_schedule(&buttontask);
|
|
|
|
return IRQ_HANDLED; // 表示中断已经处理完毕
|
|
}
|
|
|
|
static int __init bottomhalf_init(void)
|
|
{
|
|
int ret = 0;
|
|
|
|
pr_info("%s\n", __func__);
|
|
|
|
/* 请求 LED 的 GPIO */
|
|
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
|
|
|
|
if (ret) {
|
|
pr_err("无法请求 LED 的 GPIOs: %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
/* 请求按钮的 GPIO */
|
|
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
|
|
|
|
if (ret) {
|
|
pr_err("无法请求按钮的 GPIOs: %d\n", ret);
|
|
goto fail1;
|
|
}
|
|
|
|
pr_info("当前按钮1的值: %d\n", gpio_get_value(buttons[0].gpio));
|
|
|
|
/* 获取按钮 1 的中断号 */
|
|
ret = gpio_to_irq(buttons[0].gpio);
|
|
|
|
if (ret < 0) {
|
|
pr_err("无法请求中断: %d\n", ret);
|
|
goto fail2;
|
|
}
|
|
|
|
button_irqs[0] = ret;
|
|
|
|
pr_info("成功请求了按钮1的中断号 # %d\n", button_irqs[0]);
|
|
|
|
/* 注册按钮 1 的中断 */
|
|
ret = request_irq(button_irqs[0], button_isr,
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
"gpio模块#按钮1", NULL);
|
|
|
|
if (ret) {
|
|
pr_err("无法请求中断: %d\n", ret);
|
|
goto fail2;
|
|
}
|
|
|
|
/* 获取按钮 2 的中断号 */
|
|
ret = gpio_to_irq(buttons[1].gpio);
|
|
|
|
if (ret < 0) {
|
|
pr_err("无法请求中断: %d\n", ret);
|
|
goto fail2;
|
|
}
|
|
|
|
button_irqs[1] = ret;
|
|
|
|
pr_info("成功请求了按钮2的中断号 # %d\n", button_irqs[1]);
|
|
|
|
/* 注册按钮 2 的中断 */
|
|
ret = request_irq(button_irqs[1], button_isr,
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
"gpio模块#按钮2", NULL);
|
|
|
|
if (ret) {
|
|
pr_err("无法请求中断: %d\n", ret);
|
|
goto fail3;
|
|
}
|
|
|
|
return 0;
|
|
|
|
/* 清理已经设置的资源 */
|
|
fail3:
|
|
free_irq(button_irqs[0], NULL);
|
|
|
|
fail2:
|
|
gpio_free_array(buttons, ARRAY_SIZE(buttons));
|
|
|
|
fail1:
|
|
gpio_free_array(leds, ARRAY_SIZE(leds));
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void __exit bottomhalf_exit(void)
|
|
{
|
|
int i;
|
|
|
|
pr_info("%s\n", __func__);
|
|
|
|
/* 释放中断 */
|
|
free_irq(button_irqs[0], NULL);
|
|
free_irq(button_irqs[1], NULL);
|
|
|
|
/* 关闭所有 LED */
|
|
for (i = 0; i < ARRAY_SIZE(leds); i++)
|
|
gpio_set_value(leds[i].gpio, 0);
|
|
|
|
/* 注销 GPIO */
|
|
gpio_free_array(leds, ARRAY_SIZE(leds));
|
|
gpio_free_array(buttons, ARRAY_SIZE(buttons));
|
|
}
|
|
|
|
module_init(bottomhalf_init);
|
|
module_exit(bottomhalf_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("起始与后续部分中断处理");
|
|
|