-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi_gpio_irc_module.c
461 lines (391 loc) · 10.9 KB
/
rpi_gpio_irc_module.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* file: rpi_gpio_irc_module.c
*
* Driver for processing events on GPIO inputs and evaluation
* quadrature encoded signals to position value
*
* Copyright (C) 2014 Radek Meciar
* Copyright (C) 2014 Pavel Pisa
*
* More information in bachelor thesis
* Motor control with Raspberry Pi board and Linux
* https://support.dce.felk.cvut.cz/mediawiki/images/1/10/Bp_2014_meciar_radek.pdf
* Supervisor: Pavel Pisa <pisa@cmp.felk.cvut.cz>
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive
* for more details.
*/
/*
IRC inputs are mapped to GPIO 7, 8, 23, 24 on RPI P1.
The channel A is mapped to inputs 7 and 8, channel B to 23 and 24.
Mapping of each signal to two inputs (one configured for IRQ on
signal rising edge and another for falling edge)
simplifies evaluation because there is no need to read actual
GPIO values which posses considerable overhead through
Linux generic GPIO infrastructue.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#define IRC1_GPIO 23 /* GPIO 3 -> IRC channel A */
#define IRC3_GPIO 24
#if 1
#define IRC2_GPIO 25 /* GPIO 2 -> IRC channel B */
#define IRC4_GPIO 27
#else
#define IRC2_GPIO 7 /* GPIO 2 -> IRC channel B */
#define IRC4_GPIO 8
#endif
/* #define IRQ_GPIO 25 input not used for this variant of processing */
#define IRC1_NAME "GPIO23_irc1_chA"
#define IRC2_NAME "GPIO7_irc2_chB"
#define IRC3_NAME "GPIO24_irc3_chA"
#define IRC4_NAME "GPI08_irc4_chB"
/* #define IRQ_name "GPIO23_irq" not used */
#define IRC_DIRECTION_DOWN -1
#define IRC_DIRECTION_UP 1
#define IRC_INPUT_LOW 0
#define DEVICE_NAME "irc"
struct gpio_irc_state {
atomic_t used_count;
volatile uint32_t position;
volatile char prev_phase;
volatile char direction;
int irc_gpio[4];
const char *irc_gpio_name[4];
unsigned int irc_irq_num[4];
};
struct gpio_irc_state gpio_irc_0 = {
.irc_gpio = {IRC1_GPIO, IRC2_GPIO, IRC3_GPIO, IRC4_GPIO},
.irc_gpio_name = {IRC1_NAME, IRC2_NAME, IRC3_NAME, IRC4_NAME},
};
int dev_major;
static struct class *irc_class;
/*
* irc_irq_handlerAR:
* GPIO IRC 1 (= 3) rising edge handler - direction determined from IRC 2 (= 4).
*/
static irqreturn_t irc_irq_handlerAR(int irq, void *dev)
{
struct gpio_irc_state *ircst = (struct gpio_irc_state *)dev;
if (ircst->prev_phase == 0) {
ircst->position++;
ircst->prev_phase = 1;
ircst->direction = IRC_DIRECTION_UP;
return IRQ_HANDLED;
}
if (ircst->prev_phase == 3) {
ircst->position--;
ircst->prev_phase = 2;
ircst->direction = IRC_DIRECTION_DOWN;
return IRQ_HANDLED;
}
if (gpio_get_value(ircst->irc_gpio[1]) == IRC_INPUT_LOW) {
ircst->position++;
ircst->prev_phase = 1;
ircst->direction = IRC_DIRECTION_UP;
} else {
ircst->position--;
ircst->prev_phase = 2;
ircst->direction = IRC_DIRECTION_DOWN;
}
return IRQ_HANDLED;
}
/*
* irc_irq_handlerAF:
* GPIO IRC 3 (= 1) faling edge handler - direction determined from IRC 2 (= 4).
*/
static irqreturn_t irc_irq_handlerAF(int irq, void *dev)
{
struct gpio_irc_state *ircst = (struct gpio_irc_state *)dev;
if (ircst->prev_phase == 2) {
ircst->position++;
ircst->prev_phase = 3;
ircst->direction = IRC_DIRECTION_UP;
return IRQ_HANDLED;
}
if (ircst->prev_phase == 1) {
ircst->position--;
ircst->prev_phase = 0;
ircst->direction = IRC_DIRECTION_DOWN;
return IRQ_HANDLED;
}
if (gpio_get_value(ircst->irc_gpio[1]) != IRC_INPUT_LOW) {
ircst->position++;
ircst->prev_phase = 3;
ircst->direction = IRC_DIRECTION_UP;
} else {
ircst->position--;
ircst->prev_phase = 0;
ircst->direction = IRC_DIRECTION_DOWN;
}
return IRQ_HANDLED;
}
/*
* irc_irq_handlerBF:
* GPIO IRC 2 (= 4) falling edge handler - direction determined from IRC 1 (= 3).
*/
static irqreturn_t irc_irq_handlerBF(int irq, void *dev)
{
struct gpio_irc_state *ircst = (struct gpio_irc_state *)dev;
if (ircst->prev_phase == 3) {
ircst->position++;
ircst->prev_phase = 0;
ircst->direction = IRC_DIRECTION_UP;
return IRQ_HANDLED;
}
if (ircst->prev_phase == 2) {
ircst->position--;
ircst->prev_phase = 1;
ircst->direction = IRC_DIRECTION_DOWN;
return IRQ_HANDLED;
}
if (gpio_get_value(ircst->irc_gpio[0]) == IRC_INPUT_LOW) {
ircst->position++;
ircst->prev_phase = 0;
ircst->direction = IRC_DIRECTION_UP;
} else {
ircst->position--;
ircst->prev_phase = 1;
ircst->direction = IRC_DIRECTION_DOWN;
}
return IRQ_HANDLED;
}
/*
* irc_irq_handlerBR:
* GPIO IRC 4 (= 2) rising edge handler - direction determined from IRC 1 (= 3).
*/
static irqreturn_t irc_irq_handlerBR(int irq, void *dev)
{
struct gpio_irc_state *ircst = (struct gpio_irc_state *)dev;
if (ircst->prev_phase == 1) {
ircst->position++;
ircst->prev_phase = 2;
ircst->direction = IRC_DIRECTION_UP;
return IRQ_HANDLED;
}
if (ircst->prev_phase == 0) {
ircst->position--;
ircst->prev_phase = 3;
ircst->direction = IRC_DIRECTION_DOWN;
return IRQ_HANDLED;
}
if (gpio_get_value(ircst->irc_gpio[0]) != IRC_INPUT_LOW) {
ircst->position++;
ircst->prev_phase = 2;
ircst->direction = IRC_DIRECTION_UP;
} else {
ircst->position--;
ircst->prev_phase = 3;
ircst->direction = IRC_DIRECTION_DOWN;
}
return IRQ_HANDLED;
}
/*
* irc_read:
* file operation processing read systemcall for /dev/irc0 device
* it returns accumulated position to the calling process buffer
*/
ssize_t irc_read(struct file *file, char *buffer, size_t length, loff_t *offset)
{
struct gpio_irc_state *ircst = (struct gpio_irc_state *)file->private_data;
int bytes_to_copy;
int ret;
uint32_t pos;
if (length < sizeof(uint32_t)) {
pr_debug("Trying to read less bytes than a irc message,\n");
pr_debug("this will always return zero.\n");
return 0;
}
pos = ircst->position;
ret = copy_to_user(buffer, &pos, sizeof(uint32_t));
buffer += sizeof(uint32_t);
bytes_to_copy = length-sizeof(uint32_t);
if (ret)
return -EFAULT;
return length - bytes_to_copy;
}
/*
* irc_open:
* file operation called at /dev/irc0 device open
* it records number of active device users
*/
int irc_open(struct inode *inode, struct file *file)
{
int dev_minor = MINOR(inode->i_rdev);
struct gpio_irc_state *ircst = &gpio_irc_0;
if (dev_minor > 0)
pr_err("There is no hardware support for the device file with minor nr.: %d\n",
dev_minor);
atomic_inc(&ircst->used_count);
file->private_data = ircst;
return 0;
}
/*
*irc_relese:
* file operation called at /dev/irc0 device close/release time
*/
int irc_relase(struct inode *inode, struct file *file)
{
struct gpio_irc_state *ircst = (struct gpio_irc_state *)file->private_data;
if (atomic_dec_and_test(&ircst->used_count))
pr_debug("Last irc user finished\n");
return 0;
}
/*
*Define file operations for device IRC
*/
const struct file_operations irc_fops = {
.owner = THIS_MODULE,
.read = irc_read,
.write = NULL,
/* .poll = irc_poll,*/
.open = irc_open,
.release = irc_relase,
};
void gpio_irc_free_irq_fn(struct gpio_irc_state *ircst)
{
int i;
for (i = 0; i < 4; i++)
free_irq(ircst->irc_irq_num[i], ircst);
}
void gpio_irc_free_fn(struct gpio_irc_state *ircst)
{
int i;
for (i = 0; i < 4; i++)
gpio_free(ircst->irc_gpio[i]);
}
/*
* gpio_irc_setup_inputs:
* Configure inputs as sources and connect interrupt handlers
* GPIO 2, 3, 4, 23 and 24 are configured as inputs
*/
int gpio_irc_setup_inputs(struct gpio_irc_state *ircst)
{
int i;
for (i = 0; i < 4; i++) {
if (gpio_request(ircst->irc_gpio[i], ircst->irc_gpio_name[i]) != 0) {
pr_err("failed request %s\n", ircst->irc_gpio_name[i]);
goto error_gpio_request;
}
}
for (i = 0; i < 4; i++) {
if (gpio_direction_input(ircst->irc_gpio[i]) != 0) {
pr_err("failed set direction input %s\n", ircst->irc_gpio_name[i]);
gpio_irc_free_fn(ircst);
return (-1);
}
}
return 0;
error_gpio_request:
while (i > 0)
gpio_free(ircst->irc_gpio[--i]);
return -1;
}
/*
* gpio_irc_init:
* Module initialization.
*/
static int gpio_irc_init(void)
{
int i;
int res;
int dev_minor = 0;
int pom = 0;
struct gpio_irc_state *ircst = &gpio_irc_0;
struct device *this_dev;
pr_notice("gpio_irc init started\n");
pr_notice("variant without table (4x IRQ on 4 GPIO) - FAST\n");
pr_notice("for peripheral variant 2\n");
irc_class = class_create(THIS_MODULE, DEVICE_NAME);
res = register_chrdev(dev_major, DEVICE_NAME, &irc_fops);
if (res < 0) {
pr_err("Error registering driver.\n");
class_destroy(irc_class);
return -ENODEV;
/*goto register_error;*/
}
if (dev_major == 0)
dev_major = res;
this_dev = device_create(irc_class, NULL, MKDEV(dev_major, dev_minor),
NULL, "irc%d", dev_minor);
if (IS_ERR(this_dev)) {
pr_err("problem to create device \"irc%d\" in the class \"irc\"\n",
dev_minor);
return (-1);
}
pom = gpio_irc_setup_inputs(ircst);
if (pom == -1) {
pr_err("Inicializace GPIO se nezdarila");
return (-1);
}
ircst->prev_phase = -1;
for (i = 0; i < 4; i++) {
int irq_num;
irq_num = gpio_to_irq(ircst->irc_gpio[i]);
if (irq_num < 0) {
pr_err("failed get IRQ number %s\n", ircst->irc_gpio_name[i]);
gpio_irc_free_fn(ircst);
return (-1);
}
ircst->irc_irq_num[i] = (unsigned int)irq_num;
}
if (request_irq(ircst->irc_irq_num[0], irc_irq_handlerAR,
IRQF_TRIGGER_RISING, "irc1_irqAS", ircst) != 0) {
pr_err("failed request IRQ for %s\n", ircst->irc_gpio_name[0]);
gpio_irc_free_fn(ircst);
return (-1);
}
if (request_irq(ircst->irc_irq_num[2], irc_irq_handlerAF,
IRQF_TRIGGER_FALLING, "irc3_irqAN", ircst) != 0) {
pr_err("failed request IRQ for %s\n", ircst->irc_gpio_name[2]);
free_irq(ircst->irc_irq_num[0], ircst);
gpio_irc_free_fn(ircst);
return (-1);
}
if (request_irq(ircst->irc_irq_num[1], irc_irq_handlerBF,
IRQF_TRIGGER_FALLING, "irc2_irqBS", ircst) != 0) {
pr_err("failed request IRQ for %s\n", ircst->irc_gpio_name[1]);
free_irq(ircst->irc_irq_num[0], ircst);
free_irq(ircst->irc_irq_num[2], ircst);
gpio_irc_free_fn(ircst);
return (-1);
}
if (request_irq(ircst->irc_irq_num[3], irc_irq_handlerBR,
IRQF_TRIGGER_RISING, "irc4_irqBN", ircst) != 0) {
pr_err("failed request IRQ for %s\n", ircst->irc_gpio_name[3]);
free_irq(ircst->irc_irq_num[0], ircst);
free_irq(ircst->irc_irq_num[2], ircst);
free_irq(ircst->irc_irq_num[1], ircst);
gpio_irc_free_fn(ircst);
return (-1);
}
pr_notice("gpio_irc init done\n");
return 0;
}
/*
* gpio_irc_exist:
* Called when module is removed.
*/
static void gpio_irc_exit(void)
{
struct gpio_irc_state *ircst = &gpio_irc_0;
int dev_minor = 0;
gpio_irc_free_irq_fn(ircst);
gpio_irc_free_fn(ircst);
device_destroy(irc_class, MKDEV(dev_major, dev_minor));
class_destroy(irc_class);
unregister_chrdev(dev_major, DEVICE_NAME);
pr_notice("gpio_irc modul closed\n");
}
module_init(gpio_irc_init);
module_exit(gpio_irc_exit);
MODULE_LICENSE("GPL");
MODULE_VERSION("1.1");
MODULE_DESCRIPTION("gpio_irc module for incremetal/quadrature signals input processing");
MODULE_AUTHOR("Radek Meciar");