10、中断下半部:工作队列传参实验
那么如何用工作队列给中断下文传参呢 ?
一、工作队列传参
在Linux系统中,工作队列可以像这样传递参数:
- 定义数据结构
C
struct work_data {
struct work_struct test_work; // 内置的工作项结构
int a;
int b;
};
struct work_data test_workqueue_work; // 创建一个工作项实例
1
2
3
4
5
6
2
3
4
5
6
- 初始化工作队列和参数 在模块加载函数里:
C
// 创建名为"test_workqueue"的工作队列
test_workqueue = create_workqueue("test_workqueue");
// 初始化工作项,指定处理函数为test_work
INIT_WORK(&test_workqueue_work.test_work, test_work);
// 给参数赋初始值
test_workqueue_work.a = 1;
test_workqueue_work.b = 2;
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 触发工作队列 当发生中断时:
C
// 将工作项加入队列等待执行
queue_work(test_workqueue, &test_workqueue_work.test_work);
1
2
2
- 处理函数实现
C
void test_work(struct work_struct *work)
{
struct work_data *pdata;
// 通过指针找到包含work_struct的整个结构体
pdata = container_of(work, struct work_data, test_work);
// 输出参数值
printk("a的值是:%d\n", pdata->a);
printk("b的值是:%d\n", pdata->b);
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
工作原理说明:
- 我们把需要传递的参数(a和b)和工作项结构体打包成一个结构体work_data
- 当中断发生时,把工作项加入队列
- 系统执行时,通过container_of宏找到原始结构体,就能访问到参数a和b的值
- 这个方法让工作项能安全地携带自定义参数执行任务
整个过程就像快递运输:
- 准备一个包裹(work_data结构体)
- 把需要的东西(a和b参数)放进包裹
- 把包裹寄到快递站(工作队列)
- 收件人(处理函数)通过包裹标签(container_of)找到包裹里的东西
二、驱动案例
说明:
::: 硬件IO表:https://lceda001.feishu.cn/wiki/UN0pw7Y1KixKirkMh0rcP3Tandc
操作第七个管脚触发中断,对应GPIO1_A4 ::: ::: 引脚编号计算:
公式1:GPIO 小组编号计算公式:number = group * 8 + X
其中:group为小组号(A为0,B为1,C为2,D为3)
公式2:GPIO pin 脚计算公式:pin = bank * 32 + number
其中:bank 为组号,number由公式1计算得来
故:GPIO1_A4
1*32+0*8+4 = 36 :::
Makefiel:
C
export ARCH=arm64
export CROSS_COMPILE=/home/book/rk/tspi/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
obj-m +=interrupt.o #此处要和你的驱动源文件同名
KDIR := /home/book/rk/tspi/kernel #这里是你的内核目录
PWD ?= $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules #make#操作
clean:
make -C $(KDIR) M=$(PWD) clean #make clean操作
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
驱动代码:
C++
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
int irq;
struct work_data
{
struct work_struct test_work;
int a;
int b;
};
struct work_data test_workqueue_work;
struct workqueue_struct *test_workqueue;
// 工作项处理函数
void test_work(struct work_struct *work)
{
struct work_data *pdata;
pdata = container_of(work, struct work_data, test_work);
printk("a is %d", pdata->a);
printk("b is %d", pdata->b);
}
// 中断处理函数
irqreturn_t test_interrupt(int irq, void *args)
{
printk("This is test_interrupt\n");
// 提交工作项到工作队列
queue_work(test_workqueue, &test_workqueue_work.test_work);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int interrupt_irq_init(void)
{
int ret;
irq = gpio_to_irq(36); // 将GPIO映射为中断号
printk("irq is %d\n", irq);
// 请求中断
ret = request_irq(irq, test_interrupt, IRQF_TRIGGER_RISING, "test", NULL);
if (ret < 0)
{
printk("request_irq is error\n");
return -1;
}
// 创建工作队列
test_workqueue = create_workqueue("test_workqueue");
// 初始化工作项
INIT_WORK(&test_workqueue_work.test_work, test_work);
test_workqueue_work.a = 1;
test_workqueue_work.b = 2;
return 0;
}
static void interrupt_irq_exit(void)
{
free_irq(irq, NULL); // 释放中断
cancel_work_sync(&test_workqueue_work.test_work); // 取消工作项
flush_workqueue(test_workqueue); // 刷新工作队列
destroy_workqueue(test_workqueue); // 销毁工作队列
printk("bye bye\n");
}
module_init(interrupt_irq_init);
module_exit(interrupt_irq_exit);
MODULE_LICENSE("GPL");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
驱动加载:insmod interrupt.ko
拉高GPIO: