-
Notifications
You must be signed in to change notification settings - Fork 17
/
hc-sro4.c
361 lines (298 loc) · 8.23 KB
/
hc-sro4.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
/* Precise measurements of time delta between sending a trigger signal
* to the HC-SRO4 distance sensor and receiving the echo signal from
* the sensor back. This has to be precise in the usecs range. We
* use trigger interrupts to measure the signal, so no busy wait :)
*
* This supports an (in theory) unlimited number of HC-SRO4 devices.
* To add a device, do a (as root):
*
* # echo 23 24 1000 > /sys/class/distance-sensor/configure
*
* (23 is the trigger GPIO, 24 is the echo GPIO and 1000 is a timeout in
* milliseconds)
*
* Then a directory appears with a file measure in it. To measure, do a
*
* # cat /sys/class/distance-sensor/distance_23_24/measure
*
* You'll receive the length of the echo signal in usecs. To convert (roughly)
* to centimeters multiply by 17150 and divide by 1e6.
*
* To deconfigure the device, do a
*
* # echo -23 24 > /sys/class/distance-sensor/configure
*
* (normally not needed).
*
* DO NOT attach your HC-SRO4's echo pin directly to the raspberry, since
* it runs with 5V while raspberry expects 3V on the GPIO inputs.
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timekeeping.h>
#include <linux/gpio/consumer.h>
#include <linux/mutex.h>
#include <linux/device.h>
#include <linux/sysfs.h>
#include <linux/interrupt.h>
#include <linux/kdev_t.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/gpio/driver.h>
#include <linux/delay.h>
#include <linux/sched.h>
struct hc_sro4 {
int gpio_trig;
int gpio_echo;
struct gpio_desc *trig_desc;
struct gpio_desc *echo_desc;
struct timeval time_triggered;
struct timeval time_echoed;
int echo_received;
int device_triggered;
struct mutex measurement_mutex;
wait_queue_head_t wait_for_echo;
unsigned long timeout;
struct list_head list;
};
static LIST_HEAD(hc_sro4_devices);
static DEFINE_MUTEX(devices_mutex);
static struct hc_sro4 *create_hc_sro4(int trig, int echo, unsigned long timeout)
/* must be called with devices_mutex held */
{
struct hc_sro4 *new;
int err;
new = kmalloc(sizeof(*new), GFP_KERNEL);
if (new == NULL)
return ERR_PTR(-ENOMEM);
new->gpio_echo = echo;
new->gpio_trig = trig;
new->echo_desc = gpio_to_desc(echo);
if (new->echo_desc == NULL) {
kfree(new);
return ERR_PTR(-EINVAL);
}
new->trig_desc = gpio_to_desc(trig);
if (new->trig_desc == NULL) {
kfree(new);
return ERR_PTR(-EINVAL);
}
err = gpiod_direction_input(new->echo_desc);
if (err < 0) {
kfree(new);
return ERR_PTR(err);
}
err = gpiod_direction_output(new->trig_desc, 0);
if (err < 0) {
kfree(new);
return ERR_PTR(err);
}
gpiod_set_value(new->trig_desc, 0);
mutex_init(&new->measurement_mutex);
init_waitqueue_head(&new->wait_for_echo);
new->timeout = timeout;
list_add_tail(&new->list, &hc_sro4_devices);
return new;
}
static irqreturn_t echo_received_irq(int irq, void *data)
{
struct hc_sro4 *device = (struct hc_sro4 *) data;
int val;
struct timeval irq_tv;
do_gettimeofday(&irq_tv);
if (!device->device_triggered)
return IRQ_HANDLED;
if (device->echo_received)
return IRQ_HANDLED;
val = gpiod_get_value(device->echo_desc);
if (val == 1) {
device->time_triggered = irq_tv;
} else {
device->time_echoed = irq_tv;
device->echo_received = 1;
wake_up_interruptible(&device->wait_for_echo);
}
return IRQ_HANDLED;
}
/* devices_mutex must be held by caller, so nobody deletes the device
* before we lock it.
*/
static int do_measurement(struct hc_sro4 *device,
unsigned long long *usecs_elapsed)
{
long timeout;
int irq;
int ret;
if (!mutex_trylock(&device->measurement_mutex)) {
mutex_unlock(&devices_mutex);
return -EBUSY;
}
mutex_unlock(&devices_mutex);
msleep(60);
/* wait 60 ms between measurements.
* now, a while true ; do cat measure ; done should work
*/
irq = gpiod_to_irq(device->echo_desc);
if (irq < 0)
return -EIO;
device->echo_received = 0;
device->device_triggered = 0;
ret = request_any_context_irq(irq, echo_received_irq,
IRQF_SHARED | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"hc_sro4", device);
if (ret < 0)
goto out_mutex;
gpiod_set_value(device->trig_desc, 1);
udelay(10);
device->device_triggered = 1;
gpiod_set_value(device->trig_desc, 0);
ret = gpiochip_lock_as_irq(gpiod_to_chip(device->echo_desc),
device->gpio_echo);
if (ret < 0)
goto out_irq;
timeout = wait_event_interruptible_timeout(device->wait_for_echo,
device->echo_received, device->timeout);
if (timeout == 0)
ret = -ETIMEDOUT;
else if (timeout < 0)
ret = timeout;
else {
*usecs_elapsed =
(device->time_echoed.tv_sec - device->time_triggered.tv_sec) * 1000000 +
(device->time_echoed.tv_usec - device->time_triggered.tv_usec);
ret = 0;
}
/* TODO: unlock_as_irq */
out_irq:
free_irq(irq, device);
out_mutex:
mutex_unlock(&device->measurement_mutex);
return ret;
}
static ssize_t sysfs_do_measurement(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct hc_sro4 *sensor = dev_get_drvdata(dev);
unsigned long long usecs_elapsed;
int status;
mutex_lock(&devices_mutex);
status = do_measurement(sensor, &usecs_elapsed);
if (status < 0)
return status;
return sprintf(buf, "%lld\n", usecs_elapsed);
}
DEVICE_ATTR(measure, 0444, sysfs_do_measurement, NULL);
static struct attribute *sensor_attrs[] = {
&dev_attr_measure.attr,
NULL,
};
static const struct attribute_group sensor_group = {
.attrs = sensor_attrs
};
static const struct attribute_group *sensor_groups[] = {
&sensor_group,
NULL
};
static ssize_t sysfs_configure_store(struct class *class,
struct class_attribute *attr,
const char *buf, size_t len);
static struct class_attribute hc_sro4_class_attrs[] = {
__ATTR(configure, 0200, NULL, sysfs_configure_store),
__ATTR_NULL,
};
static struct class hc_sro4_class = {
.name = "distance-sensor",
.owner = THIS_MODULE,
.class_attrs = hc_sro4_class_attrs
};
static struct hc_sro4 *find_sensor(int trig, int echo)
{
struct hc_sro4 *sensor;
list_for_each_entry(sensor, &hc_sro4_devices, list) {
if (sensor->gpio_trig == trig &&
sensor->gpio_echo == echo)
return sensor;
}
return NULL;
}
static int match_device(struct device *dev, const void *data)
{
return dev_get_drvdata(dev) == data;
}
static int remove_sensor(struct hc_sro4 *rip_sensor)
/* must be called with devices_mutex held. */
{
struct device *dev;
dev = class_find_device(&hc_sro4_class, NULL, rip_sensor, match_device);
if (dev == NULL)
return -ENODEV;
mutex_lock(&rip_sensor->measurement_mutex);
/* wait until measurement has finished */
list_del(&rip_sensor->list);
kfree(rip_sensor); /* TODO: ?? double free ?? */
device_unregister(dev);
put_device(dev);
return 0;
}
static ssize_t sysfs_configure_store(struct class *class,
struct class_attribute *attr,
const char *buf, size_t len)
{
int add = buf[0] != '-';
const char *s = buf;
int trig, echo, timeout;
struct hc_sro4 *new_sensor, *rip_sensor;
int err;
if (buf[0] == '-' || buf[0] == '+')
s++;
if (add) {
if (sscanf(s, "%d %d %d", &trig, &echo, &timeout) != 3)
return -EINVAL;
mutex_lock(&devices_mutex);
if (find_sensor(trig, echo)) {
mutex_unlock(&devices_mutex);
return -EEXIST;
}
new_sensor = create_hc_sro4(trig, echo, timeout);
mutex_unlock(&devices_mutex);
if (IS_ERR(new_sensor))
return PTR_ERR(new_sensor);
device_create_with_groups(class, NULL, MKDEV(0, 0), new_sensor,
sensor_groups, "distance_%d_%d", trig, echo);
} else {
if (sscanf(s, "%d %d", &trig, &echo) != 2)
return -EINVAL;
mutex_lock(&devices_mutex);
rip_sensor = find_sensor(trig, echo);
if (rip_sensor == NULL) {
mutex_unlock(&devices_mutex);
return -ENODEV;
}
err = remove_sensor(rip_sensor);
mutex_unlock(&devices_mutex);
if (err < 0)
return err;
}
return len;
}
static int __init init_hc_sro4(void)
{
return class_register(&hc_sro4_class);
}
static void exit_hc_sro4(void)
{
struct hc_sro4 *rip_sensor, *tmp;
mutex_lock(&devices_mutex);
list_for_each_entry_safe(rip_sensor, tmp, &hc_sro4_devices, list) {
remove_sensor(rip_sensor); /* ignore errors */
}
mutex_unlock(&devices_mutex);
class_unregister(&hc_sro4_class);
}
module_init(init_hc_sro4);
module_exit(exit_hc_sro4);
MODULE_AUTHOR("Johannes Thoma");
MODULE_DESCRIPTION("Distance measurement for the HC-SRO4 ultrasonic distance sensor");
MODULE_LICENSE("GPL");