0%

Linux驱动学习实战(5) 写出流水灯

设计思路

  将流水灯的三个灯作为一组,写入设备树中。在驱动程序中开启一个每 0.5s 触发的中断,在中断里每次移动一位,让 GPIO 实现流水灯效果。

硬件分析

  我们将二极管的正极接在 IO 口上,负极接在 GND,因此当 IO 口使用推挽输出模式输出高电平时,二极管将被点亮。正极分别接在板子上的 12、14、16。查阅原理图可以得知,其分别对应 GPIO01_IO04/8/6。

编写代码

  打开 imx6ull-hcw-emmc.dtsi 文件,在 iomuxc 节点下添加 pinctrl_flowingled,并写入如下内容:

1
2
3
4
5
6
7
pinctrl_flowingled: flowingledgrp{
fsl,pins = <
MX6UL_PAD_GPIO1_IO04__GPIO1_IO04 0x10B0
MX6UL_PAD_GPIO1_IO08__GPIO1_IO08 0x10B0
MX6UL_PAD_GPIO1_IO06__GPIO1_IO06 0x10B0
>;
};

  在根节点下添加 flowingled 设备,写入如下内容,写好后检查是否由 pin 被占用:

1
2
3
4
5
6
7
8
9
10
11
12
flowingled{
#address-cells = <1>;
#size-cells = <1>;
compatible = "hcw-flowingled";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_flowingled>;
flowingled-gpios = <&gpio1 4 GPIO_ACTIVE_HIGH>,
<&gpio1 8 GPIO_ACTIVE_HIGH>,
<&gpio1 6 GPIO_ACTIVE_HIGH>;

status = "okay";
};

  新建文件夹,新建 C 文件,接下来我们一步一步写出完整的驱动代码!

  我们先将大体框架搭建完毕,写入以下代码

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* @Author: 胡城玮
* @FilePath: flowingled.c
* @Date: 2026-03-11
* @Description:
* @Version: 0.1
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/file.h>
#include <linux/platform_device.h>
#include <linux/irq.h>

#define FLOWINGLED_CNT 1
#define FLOWINGLED_NAME "hcw-flowingled"
#define FLOWINGLED_ON 1
#define FLOWINGLED_OFF 0

struct flowingled_dev{
dev_t devid;
struct cdev cdev;
struct class *class;
struct device *device;
int major;
struct device_node *node;

int gpio[3];
int led_num;

struct timer_list timer;
unsigned int period_ms; // 控制闪烁周期
unsigned int cur_led; // 当前灯号
unsigned int dir; // 闪烁方向
unsigned int running; // 是否运行
};

struct flowingled_dev flowingled;

static int flowingled_open(struct inode *inode, struct file *filp){
filp->private_data = &flowingled;
return 0;
}

static ssize_t flowingled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[2];
unsigned char led_running_flag;

retvalue = copy_from_user(databuf,buf,cnt);
if(retvalue < 0)
{
printk("kernel write failed\r\n");
return -EFAULT;
}

led_running_flag = databuf[0];

return 0;
}

static struct file_operations flowingled_fops = {
.owner = THIS_MODULE,
.open = flowingled_open,
.write = flowingled_write
};

static int flowingled_probe(struct platform_device *dev)
{
printk("driver and device matched");

if(flowingled.major){
flowingled.devid = MKDEV(flowingled.major, 0);
register_chrdev_region(flowingled.devid, FLOWINGLED_CNT, FLOWINGLED_NAME);
}
else
{
alloc_chrdev_region(&flowingled.devid, 0, FLOWINGLED_CNT, FLOWINGLED_NAME);
flowingled.major = MAJOR(flowingled.devid);
}

cdev_init(&flowingled.cdev, &flowingled_fops);
cdev_add(&flowingled.cdev, flowingled.devid,FLOWINGLED_CNT);

flowingled.class = class_create(THIS_MODULE, FLOWINGLED_NAME);
if(IS_ERR(flowingled.class))
{
return PTR_ERR(flowingled.class);
}

flowingled.device = device_create(flowingled.class , NULL, flowingled.devid, NULL, FLOWINGLED_NAME);
if(IS_ERR(flowingled.device))
{
return PTR_ERR(flowingled.device);
}

flowingled.node = of_find_node_by_path("/flowingled");
if(flowingled.node == NULL)
{
printk("flowingled does not found\r\n");
return -EINVAL;
}

for (int i = 0; i < 3; i++)
{
char buf[8];
flowingled.gpio[i] = of_get_named_gpio(flowingled.node, "flowingled-gpios", i);
if (flowingled.gpio[i] < 0) {
printk("can't get flowingled-gpios[%d]\n", i);
return flowingled.gpio[i];
}
sprintf(buf ,"LED%d", i);
gpio_request(flowingled.gpio[i],buf);
gpio_direction_output(flowingled.gpio[i],0);
}
return 0;
}

static int flowingled_remove(struct platform_device *dev)
{
for(int i = 0;i < FLOWINGLED_CNT; i++)
{
gpio_set_value(flowingled.gpio[i],0);
}

cdev_del(&flowingled.cdev);

unregister_chrdev_region(flowingled.devid, FLOWINGLED_CNT);
device_destroy(flowingled.class, flowingled.devid);
class_destroy(flowingled.class);
return 0;
}

static const struct of_device_id flowing_of_match[] = {
{.compatible = "hcw-flowingled"},
{}
};

static struct platform_driver flowingled_drv = {
.driver = {
.name = "imx6u-flowingled",
.of_match_table = flowing_of_match,
},
.probe = flowingled_probe,
.remove = flowingled_remove
};

static int __init flowingled_init(void)
{
return platform_driver_register(&flowingled_drv);
}

static void __exit flowingled_exit(void)
{
platform_driver_unregister(&flowingled_drv);
}

module_init(flowingled_init);
module_exit(flowingled_exit);

MODULE_AUTHOR("hcw");
MODULE_LICENSE("GPL");

  编译,并将 ko 文件装载到根文件系统中,试图装载,但出现以下报错:

图 1 IO被占用

  阅读报错信息,可以看到原本我们要用的 GPIO01_IO08 被占用。回到原理图,观察,也许我们可以临时关闭 USB_OTG1 来实现 3 个 IO 口的流水灯功能,修改设备树,重新编译、装载。没有报错了!(实际上博主在这一步消耗了大量时间,最终使用了1、2、4 三个 IO 口),终于无报错设备驱动匹配成功!

图 2 GPIO原理图

  接下来我们进一步写更详细的驱动代码,我想的是当用户输入 1 时,流水灯开启,当用户输入 0 时,流水灯关闭。主要就是开启一个固定时间的定时中断,在中断里调整 GPIO 的高低电平。首先写下回调函数:

1
2
3
4
static void timer_callbacl(struct timer_list *t)
{
mod_timer(&flowingled.timer, jiffies + msecs_to_jiffies(500));
}

  接着,在 probe 函数中写下初始化,并在 remove 函数中删除定时器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static int flowingled_probe(struct platform_device *dev)
{
·····

timer_setup(&flowingled.timer, timer_callbacl, 0);

mod_timer(&flowingled.timer, jiffies + msecs_to_jiffies(500));

return 0;
}

static int flowingled_remove(struct platform_device *dev)
{
for(int i = 0;i < FLOWINGLED_CNT; i++)
{
gpio_set_value(flowingled.gpio[i],0);
}

del_timer_sync(&flowingled.timer);

······
}

  接下来我们来完成流水灯的具体功能。最终代码如下:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* @Author: 胡城玮
* @FilePath: flowingled.c
* @Date: 2026-03-11
* @Description:
* @Version: 0.1
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/file.h>
#include <linux/platform_device.h>
#include <linux/irq.h>

#define FLOWINGLED_CNT 1
#define FLOWINGLED_NAME "hcw-flowingled"
#define FLOWINGLED_ON 1
#define FLOWINGLED_OFF 0

struct flowingled_dev{
dev_t devid;
struct cdev cdev;
struct class *class;
struct device *device;
int major;
struct device_node *node;

int gpio[3];
int led_num;

struct timer_list timer;
unsigned int period_ms; // 控制闪烁周期
unsigned int cur_led; // 当前灯号
unsigned int dir; // 闪烁方向
unsigned int running; // 是否运行
};

struct flowingled_dev flowingled;

static void timer_callbacl(struct timer_list *t)
{
if(flowingled.running)
{
flowingled.cur_led ++;
if(flowingled.cur_led > 2)
flowingled.cur_led = 0;
else if(flowingled.cur_led < 0)
flowingled.cur_led = 2;

for(int i = 0;i < 3;i++)
{
if(i == flowingled.cur_led)
{
gpio_set_value(flowingled.gpio[i],1);
continue;
}
gpio_set_value(flowingled.gpio[i],0);
}
}
mod_timer(&flowingled.timer, jiffies + msecs_to_jiffies(500));
}

static int flowingled_open(struct inode *inode, struct file *filp){
filp->private_data = &flowingled;
return 0;
}

static ssize_t flowingled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[2];
unsigned char led_running_flag;

struct flowingled_dev *dev = (struct flowingled_dev *)filp->private_data;

retvalue = copy_from_user(databuf,buf,cnt);
if(retvalue < 0)
{
printk("kernel write failed\r\n");
return -EFAULT;
}

led_running_flag = databuf[0];

if(led_running_flag == FLOWINGLED_ON)
{
dev->running = 1;
}
else if(led_running_flag == FLOWINGLED_OFF)
{
dev->running = 0;
}

return 0;
}

static struct file_operations flowingled_fops = {
.owner = THIS_MODULE,
.open = flowingled_open,
.write = flowingled_write
};

static int flowingled_probe(struct platform_device *dev)
{
printk("driver and device matched");

if(flowingled.major){
flowingled.devid = MKDEV(flowingled.major, 0);
register_chrdev_region(flowingled.devid, FLOWINGLED_CNT, FLOWINGLED_NAME);
}
else
{
alloc_chrdev_region(&flowingled.devid, 0, FLOWINGLED_CNT, FLOWINGLED_NAME);
flowingled.major = MAJOR(flowingled.devid);
}

cdev_init(&flowingled.cdev, &flowingled_fops);
cdev_add(&flowingled.cdev, flowingled.devid,FLOWINGLED_CNT);

flowingled.class = class_create(THIS_MODULE, FLOWINGLED_NAME);
if(IS_ERR(flowingled.class))
{
return PTR_ERR(flowingled.class);
}

flowingled.device = device_create(flowingled.class , NULL, flowingled.devid, NULL, FLOWINGLED_NAME);
if(IS_ERR(flowingled.device))
{
return PTR_ERR(flowingled.device);
}

flowingled.node = of_find_node_by_path("/flowingled");
if(flowingled.node == NULL)
{
printk("flowingled does not found\r\n");
return -EINVAL;
}

for (int i = 0; i < 3; i++)
{
char buf[8];
flowingled.gpio[i] = of_get_named_gpio(flowingled.node, "flowingled-gpios", i);
if (flowingled.gpio[i] < 0) {
printk("can't get flowingled-gpios[%d]\n", i);
return flowingled.gpio[i];
}
sprintf(buf ,"LED%d", i);
gpio_request(flowingled.gpio[i],buf);
gpio_direction_output(flowingled.gpio[i],0);
}

timer_setup(&flowingled.timer, timer_callbacl, 0);

mod_timer(&flowingled.timer, jiffies + msecs_to_jiffies(500));

flowingled.running = 0;
flowingled.cur_led = 0;

return 0;
}

static int flowingled_remove(struct platform_device *dev)
{
for(int i = 0;i < 3; i++)
{
gpio_set_value(flowingled.gpio[i],0);
}

del_timer_sync(&flowingled.timer);

cdev_del(&flowingled.cdev);

unregister_chrdev_region(flowingled.devid, FLOWINGLED_CNT);
device_destroy(flowingled.class, flowingled.devid);
class_destroy(flowingled.class);
return 0;
}

static const struct of_device_id flowing_of_match[] = {
{.compatible = "hcw-flowingled"},
{}
};

static struct platform_driver flowingled_drv = {
.driver = {
.name = "imx6u-flowingled",
.of_match_table = flowing_of_match,
},
.probe = flowingled_probe,
.remove = flowingled_remove
};

static int __init flowingled_init(void)
{
return platform_driver_register(&flowingled_drv);
}

static void __exit flowingled_exit(void)
{

platform_driver_unregister(&flowingled_drv);
}

module_init(flowingled_init);
module_exit(flowingled_exit);

MODULE_AUTHOR("hcw");
MODULE_LICENSE("GPL");

  装载 ko 文件,运行测试程序向设备写入 1,成功点亮流水灯!