-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhall_speed.c
381 lines (309 loc) · 8.34 KB
/
hall_speed.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
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/ktime.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#define DRIVER_NAME "halls"
#define DRIVER_PREFIX DRIVER_NAME ": "
#define HALLS_MINOR 0
#define HALLS_MAX_DEVICES 1
#define WHEEL_DIAMETER 6
#define MAGNET_NUMBER 2
#define MIN_SPEED 20
/* Change this line to use different GPIO number
* 32 (PA0) + 8 (PA8) (J11.9 AT91SAM9G45-EKES)
* DO from sensor point of view
*/
#define HALL_DO_GPIO_NUM 40
#define HALL_DO_GPIO_NAME "halls_do"
#define PI 314
#define PI_COEFFICIENT 100
#define SPEED_STR_BUFF_SIZE 12
static uint wheel_diameter = WHEEL_DIAMETER;
module_param(wheel_diameter, uint, S_IRUGO);
MODULE_PARM_DESC(wheel_diameter, "Wheel diameter in cm");
static uint magnet_number = MAGNET_NUMBER;
module_param(magnet_number, uint, S_IRUGO);
MODULE_PARM_DESC(magnet_number, "Number of magnets on wheel");
static uint min_speed = MIN_SPEED;
module_param(min_speed, uint, S_IRUGO);
MODULE_PARM_DESC(min_speed, "Minimun speed in cm/s");
static uint hall_do_gpio_num = HALL_DO_GPIO_NUM;
module_param(hall_do_gpio_num, uint, S_IRUGO);
MODULE_PARM_DESC(hall_do_gpio_num,
"GPIO number to which Hall sensor digital output is connected");
struct halls {
uint do_gpio_num;
int do_gpio_irq;
ktime_t t1, t2;
unsigned int stop_time;
struct timer_list stop_timer;
struct class class;
struct device *sysfs_dev;
int major;
struct cdev cdev;
spinlock_t lock;
};
static struct halls halls;
static inline u32 get_speed(struct halls *hs)
{
u32 t_diff;
unsigned long flags;
spin_lock_irqsave(&hs->lock, flags);
t_diff = ktime_to_ns(hs->t1) && ktime_to_ns(hs->t2) ?
ktime_to_us(ktime_sub(hs->t2, hs->t1)) : 0;
spin_unlock_irqrestore(&hs->lock, flags);
return t_diff ? PI * wheel_diameter * USEC_PER_SEC / PI_COEFFICIENT /
magnet_number / t_diff : 0;
}
static inline u32 get_stop_detection_time(u32 min_speed)
{
return PI * MSEC_PER_SEC * wheel_diameter / PI_COEFFICIENT /
magnet_number / min_speed;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
static ssize_t speed_value_read(struct class *class, char *buf)
#else
static ssize_t speed_value_read(struct class *class,
struct class_attribute *attr, char *buf)
#endif
{
struct halls *hs = container_of(class, struct halls, class);
return sprintf(buf, "%d\n", get_speed(hs));
}
static struct class_attribute halls_class_attrs[] = {
__ATTR(value, S_IRUGO, speed_value_read, NULL),
__ATTR_NULL,
};
/* Interrupt handler of hall sensor DO signal */
static irqreturn_t halls_do_isr(int irq, void *data)
{
struct halls *hs = data;
/* When magnet goes past sensor the last one sets its DO to 0 */
if (!gpio_get_value(hs->do_gpio_num)) {
hs->t1 = hs->t2;
hs->t2 = ktime_get();
mod_timer(&hs->stop_timer, jiffies +
msecs_to_jiffies(hs->stop_time));
}
return IRQ_HANDLED;
}
void stop_timer_callback(unsigned long data)
{
unsigned long flags;
static const ktime_t ktime_zero;
struct halls *hs = (struct halls *)data;
spin_lock_irqsave(&hs->lock, flags);
hs->t1 = hs->t2 = ktime_zero;
spin_unlock_irqrestore(&hs->lock, flags);
}
static int halls_open(struct inode *inode, struct file *filp)
{
struct halls *hs = container_of(inode->i_cdev, struct halls, cdev);
filp->private_data = hs;
return 0;
}
static int halls_release(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t halls_read(struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
size_t max_len;
char speed_str[SPEED_STR_BUFF_SIZE];
struct halls *hs = filp->private_data;
max_len = snprintf(speed_str, SPEED_STR_BUFF_SIZE, "%u\n",
get_speed(hs));
if (max_len > SPEED_STR_BUFF_SIZE)
max_len = SPEED_STR_BUFF_SIZE;
return simple_read_from_buffer(buf, count, f_pos, speed_str, max_len);
}
static const struct file_operations halls_fops = {
.owner = THIS_MODULE,
.open = halls_open,
.release = halls_release,
.read = halls_read,
};
static int halls_check_params(void)
{
if (!wheel_diameter) {
pr_err(DRIVER_PREFIX "wrong value of wheel diameter\n");
return -1;
}
if (!magnet_number) {
pr_err(DRIVER_PREFIX "wrong value of magnets number\n");
return -1;
}
if (!min_speed) {
pr_err(DRIVER_PREFIX "wrong value of minimum speed\n");
return -1;
}
return 0;
}
static int halls_char_dev_create(void)
{
dev_t dev;
int ret;
ret = alloc_chrdev_region(&dev, HALLS_MINOR, HALLS_MAX_DEVICES,
DRIVER_NAME);
if (ret < 0) {
pr_err(DRIVER_PREFIX "failed to alloc major number, ret %d\n",
ret);
return ret;
}
halls.major = MAJOR(dev);
cdev_init(&halls.cdev, &halls_fops);
halls.cdev.owner = THIS_MODULE;
ret = cdev_add(&halls.cdev, dev, HALLS_MAX_DEVICES);
if (ret) {
pr_err(DRIVER_PREFIX "failed to add char device, ret %d\n",
ret);
unregister_chrdev_region(dev, HALLS_MAX_DEVICES);
return ret;
}
return 0;
}
static void halls_char_dev_destroy(void)
{
cdev_del(&halls.cdev);
unregister_chrdev_region(MKDEV(halls.major, HALLS_MINOR),
HALLS_MAX_DEVICES);
}
static int halls_sysfs_create(void)
{
int ret;
halls.class.name = DRIVER_NAME;
halls.class.owner = THIS_MODULE;
halls.class.class_attrs = halls_class_attrs;
ret = class_register(&halls.class);
if (ret) {
pr_err(DRIVER_PREFIX "failed to register class, ret %d\n", ret);
return ret;
}
halls.sysfs_dev = device_create(&halls.class, NULL, MKDEV(halls.major,
HALLS_MINOR), NULL, DRIVER_NAME "%d", HALLS_MINOR);
if (IS_ERR(halls.sysfs_dev)) {
pr_err(DRIVER_PREFIX "failed to create sysfs device, ret %ld\n",
PTR_ERR(halls.sysfs_dev));
class_unregister(&halls.class);
return PTR_ERR(halls.sysfs_dev);
}
return 0;
}
static void halls_sysfs_destroy(void)
{
device_destroy(&halls.class, MKDEV(halls.major, HALLS_MINOR));
class_unregister(&halls.class);
}
static int halls_gpio_init(void)
{
int ret;
halls.do_gpio_num = hall_do_gpio_num;
ret = gpio_request(halls.do_gpio_num, HALL_DO_GPIO_NAME);
if (ret) {
dev_err(halls.sysfs_dev, "failed to request GPIO, ret %d\n",
ret);
return ret;
}
ret = gpio_direction_input(halls.do_gpio_num);
if (ret) {
dev_err(halls.sysfs_dev,
"failed to set GPIO direction, ret %d\n", ret);
goto fail_gpio_setup;
}
ret = gpio_to_irq(halls.do_gpio_num);
if (ret < 0) {
dev_err(halls.sysfs_dev, "failed to get GPIO IRQ, %d\n", ret);
goto fail_gpio_setup;
} else
halls.do_gpio_irq = ret;
ret = request_irq(halls.do_gpio_irq, halls_do_isr,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
IRQF_DISABLED |
#endif
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
HALL_DO_GPIO_NAME, &halls);
if (ret) {
dev_err(halls.sysfs_dev, "failed to request IRQ, ret %d\n",
ret);
goto fail_gpio_setup;
}
return 0;
fail_gpio_setup:
gpio_free(halls.do_gpio_num);
return ret;
}
static void halls_gpio_cleanup(void)
{
free_irq(halls.do_gpio_irq, &halls);
gpio_free(halls.do_gpio_num);
}
static int halls_stop_timer_init(void)
{
int ret;
/* If there wasn't signal from hall sensor during stop_time then wheel
* has been stopped and we need set speed value to 0. stop_time
* is calculated from minimal speed parameter. The higher minimal speed,
* the lower stop detection time.
*/
halls.stop_time = get_stop_detection_time(min_speed);
setup_timer(&halls.stop_timer, stop_timer_callback,
(unsigned long)&halls);
ret = mod_timer(&halls.stop_timer, jiffies +
msecs_to_jiffies(halls.stop_time));
if (ret) {
dev_err(halls.sysfs_dev, "failed to setup stop timer %d\n",
ret);
return ret;
}
return 0;
}
static void halls_stop_timer_cleanup(void)
{
del_timer(&halls.stop_timer);
}
static int __init halls_init(void)
{
int ret;
if (halls_check_params())
return -1;
ret = halls_char_dev_create();
if (ret)
return ret;
spin_lock_init(&halls.lock);
ret = halls_sysfs_create();
if (ret)
goto fail_sysfs_create;
ret = halls_gpio_init();
if (ret)
goto fail_gpio_init;
ret = halls_stop_timer_init();
if (ret)
goto fail_timer_init;
return 0;
fail_timer_init:
halls_gpio_cleanup();
fail_gpio_init:
halls_sysfs_destroy();
fail_sysfs_create:
halls_char_dev_destroy();
return ret;
}
static void __exit halls_exit(void)
{
halls_stop_timer_cleanup();
halls_gpio_cleanup();
halls_sysfs_destroy();
halls_char_dev_destroy();
}
module_init(halls_init);
module_exit(halls_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Bogdan Bogush");
MODULE_DESCRIPTION("Hall effect speed sensor driver");