Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 1 | /* Industrialio buffer test code. |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is primarily intended as an example application. |
| 10 | * Reads the current buffer setup from sysfs and starts a short capture |
| 11 | * from the specified device, pretty printing the result after appropriate |
| 12 | * conversion. |
| 13 | * |
| 14 | * Command line parameters |
| 15 | * generic_buffer -n <device_name> -t <trigger_name> |
| 16 | * If trigger name is not specified the program assumes you want a dataready |
| 17 | * trigger associated with the device and goes looking for it. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <unistd.h> |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 23 | #include <dirent.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <stdio.h> |
| 26 | #include <errno.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/dir.h> |
| 29 | #include <linux/types.h> |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 30 | #include <string.h> |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 31 | #include <poll.h> |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 32 | #include <endian.h> |
Peter Meerwald | bb23378 | 2012-06-25 23:12:17 +0200 | [diff] [blame] | 33 | #include <getopt.h> |
Peter Meerwald | 1bcdfbc | 2012-06-25 23:12:16 +0200 | [diff] [blame] | 34 | #include <inttypes.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 35 | #include "iio_utils.h" |
| 36 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 37 | /** |
| 38 | * size_from_channelarray() - calculate the storage size of a scan |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 39 | * @channels: the channel info array |
| 40 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 41 | * |
| 42 | * Has the side effect of filling the channels[i].location values used |
| 43 | * in processing the buffer output. |
| 44 | **/ |
| 45 | int size_from_channelarray(struct iio_channel_info *channels, int num_channels) |
| 46 | { |
| 47 | int bytes = 0; |
| 48 | int i = 0; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 49 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 50 | while (i < num_channels) { |
| 51 | if (bytes % channels[i].bytes == 0) |
| 52 | channels[i].location = bytes; |
| 53 | else |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 54 | channels[i].location = bytes - bytes % channels[i].bytes |
| 55 | + channels[i].bytes; |
| 56 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 57 | bytes = channels[i].location + channels[i].bytes; |
| 58 | i++; |
| 59 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 60 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 61 | return bytes; |
| 62 | } |
| 63 | |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 64 | void print2byte(uint16_t input, struct iio_channel_info *info) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 65 | { |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 66 | /* First swap if incorrect endian */ |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 67 | if (info->be) |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 68 | input = be16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 69 | else |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 70 | input = le16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 71 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 72 | /* |
| 73 | * Shift before conversion to avoid sign extension |
| 74 | * of left aligned data |
| 75 | */ |
Aya Mahfouz | 1525ecf | 2015-02-26 11:45:26 +0200 | [diff] [blame] | 76 | input >>= info->shift; |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 77 | input &= info->mask; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 78 | if (info->is_signed) { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 79 | int16_t val = (int16_t)(input << (16 - info->bits_used)) >> |
| 80 | (16 - info->bits_used); |
| 81 | printf("%05f ", ((float)val + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 82 | } else { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 83 | printf("%05f ", ((float)input + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 86 | |
| 87 | void print4byte(uint32_t input, struct iio_channel_info *info) |
| 88 | { |
| 89 | /* First swap if incorrect endian */ |
| 90 | if (info->be) |
| 91 | input = be32toh(input); |
| 92 | else |
| 93 | input = le32toh(input); |
| 94 | |
| 95 | /* |
| 96 | * Shift before conversion to avoid sign extension |
| 97 | * of left aligned data |
| 98 | */ |
| 99 | input >>= info->shift; |
| 100 | input &= info->mask; |
| 101 | if (info->is_signed) { |
| 102 | int32_t val = (int32_t)(input << (32 - info->bits_used)) >> |
| 103 | (32 - info->bits_used); |
| 104 | printf("%05f ", ((float)val + info->offset) * info->scale); |
| 105 | } else { |
| 106 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void print8byte(uint64_t input, struct iio_channel_info *info) |
| 111 | { |
| 112 | /* First swap if incorrect endian */ |
| 113 | if (info->be) |
| 114 | input = be64toh(input); |
| 115 | else |
| 116 | input = le64toh(input); |
| 117 | |
| 118 | /* |
| 119 | * Shift before conversion to avoid sign extension |
| 120 | * of left aligned data |
| 121 | */ |
| 122 | input >>= info->shift; |
| 123 | input &= info->mask; |
| 124 | if (info->is_signed) { |
| 125 | int64_t val = (int64_t)(input << (64 - info->bits_used)) >> |
| 126 | (64 - info->bits_used); |
| 127 | /* special case for timestamp */ |
| 128 | if (info->scale == 1.0f && info->offset == 0.0f) |
| 129 | printf("%" PRId64 " ", val); |
| 130 | else |
| 131 | printf("%05f ", |
| 132 | ((float)val + info->offset) * info->scale); |
| 133 | } else { |
| 134 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 135 | } |
| 136 | } |
| 137 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 138 | /** |
| 139 | * process_scan() - print out the values in SI units |
| 140 | * @data: pointer to the start of the scan |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 141 | * @channels: information about the channels. |
| 142 | * Note: size_from_channelarray must have been called first |
| 143 | * to fill the location offsets. |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 144 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 145 | **/ |
| 146 | void process_scan(char *data, |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 147 | struct iio_channel_info *channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 148 | int num_channels) |
| 149 | { |
| 150 | int k; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 151 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 152 | for (k = 0; k < num_channels; k++) |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 153 | switch (channels[k].bytes) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 154 | /* only a few cases implemented so far */ |
| 155 | case 2: |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 156 | print2byte(*(uint16_t *)(data + channels[k].location), |
| 157 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 158 | break; |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 159 | case 4: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 160 | print4byte(*(uint32_t *)(data + channels[k].location), |
| 161 | &channels[k]); |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 162 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 163 | case 8: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 164 | print8byte(*(uint64_t *)(data + channels[k].location), |
| 165 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 166 | break; |
| 167 | default: |
| 168 | break; |
| 169 | } |
| 170 | printf("\n"); |
| 171 | } |
| 172 | |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 173 | void print_usage(void) |
| 174 | { |
| 175 | printf("Usage: generic_buffer [options]...\n" |
| 176 | "Capture, convert and output data from IIO device buffer\n" |
| 177 | " -c <n> Do n conversions\n" |
| 178 | " -e Disable wait for event (new data)\n" |
| 179 | " -g Use trigger-less mode\n" |
| 180 | " -l <n> Set buffer length to n samples\n" |
| 181 | " -n <name> Set device name (mandatory)\n" |
| 182 | " -t <name> Set trigger name\n" |
| 183 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
| 184 | } |
| 185 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 186 | int main(int argc, char **argv) |
| 187 | { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 188 | unsigned long num_loops = 2; |
| 189 | unsigned long timedelay = 1000000; |
| 190 | unsigned long buf_len = 128; |
| 191 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 192 | int ret, c, i, j, toread; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 193 | int fp; |
| 194 | |
| 195 | int num_channels; |
| 196 | char *trigger_name = NULL, *device_name = NULL; |
| 197 | char *dev_dir_name, *buf_dir_name; |
| 198 | |
| 199 | int datardytrigger = 1; |
| 200 | char *data; |
Jonathan Cameron | c77b381 | 2011-04-15 18:56:00 +0100 | [diff] [blame] | 201 | ssize_t read_size; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 202 | int dev_num, trig_num; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 203 | char *buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 204 | int scan_size; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 205 | int noevents = 0; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 206 | int notrigger = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 207 | char *dummy; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 208 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 209 | struct iio_channel_info *channels; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 210 | |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 211 | while ((c = getopt(argc, argv, "c:egl:n:t:w:")) != -1) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 212 | switch (c) { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 213 | case 'c': |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 214 | errno = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 215 | num_loops = strtoul(optarg, &dummy, 10); |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 216 | if (errno) |
| 217 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 218 | |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 219 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 220 | case 'e': |
| 221 | noevents = 1; |
| 222 | break; |
| 223 | case 'g': |
| 224 | notrigger = 1; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 225 | break; |
| 226 | case 'l': |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 227 | errno = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 228 | buf_len = strtoul(optarg, &dummy, 10); |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 229 | if (errno) |
| 230 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 231 | |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 232 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 233 | case 'n': |
| 234 | device_name = optarg; |
| 235 | break; |
| 236 | case 't': |
| 237 | trigger_name = optarg; |
| 238 | datardytrigger = 0; |
| 239 | break; |
| 240 | case 'w': |
| 241 | errno = 0; |
| 242 | timedelay = strtoul(optarg, &dummy, 10); |
| 243 | if (errno) |
| 244 | return -errno; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 245 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 246 | case '?': |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 247 | print_usage(); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 248 | return -1; |
| 249 | } |
| 250 | } |
| 251 | |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 252 | if (device_name == NULL) { |
| 253 | printf("Device name not set\n"); |
| 254 | print_usage(); |
Michael Hennerich | 065896e | 2011-02-24 16:34:50 +0100 | [diff] [blame] | 255 | return -1; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 256 | } |
Michael Hennerich | 065896e | 2011-02-24 16:34:50 +0100 | [diff] [blame] | 257 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 258 | /* Find the device requested */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 259 | dev_num = find_type_by_name(device_name, "iio:device"); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 260 | if (dev_num < 0) { |
| 261 | printf("Failed to find the %s\n", device_name); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 262 | return dev_num; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 263 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 264 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 265 | printf("iio device number being used is %d\n", dev_num); |
| 266 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 267 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
| 268 | if (ret < 0) |
| 269 | return -ENOMEM; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 270 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 271 | if (!notrigger) { |
| 272 | if (trigger_name == NULL) { |
| 273 | /* |
| 274 | * Build the trigger name. If it is device associated |
| 275 | * its name is <device_name>_dev[n] where n matches |
| 276 | * the device number found above. |
| 277 | */ |
| 278 | ret = asprintf(&trigger_name, |
| 279 | "%s-dev%d", device_name, dev_num); |
| 280 | if (ret < 0) { |
| 281 | ret = -ENOMEM; |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 282 | goto error_free_dev_dir_name; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | /* Verify the trigger exists */ |
| 287 | trig_num = find_type_by_name(trigger_name, "trigger"); |
| 288 | if (trig_num < 0) { |
| 289 | printf("Failed to find the trigger %s\n", trigger_name); |
Hartmut Knaack | e83a47c | 2015-05-31 14:39:57 +0200 | [diff] [blame] | 290 | ret = trig_num; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 291 | goto error_free_triggername; |
| 292 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 293 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 294 | printf("iio trigger number being used is %d\n", trig_num); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 295 | } else { |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 296 | printf("trigger-less mode selected\n"); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 297 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * Parse the files in scan_elements to identify what channels are |
| 301 | * present |
| 302 | */ |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 303 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 304 | if (ret) { |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 305 | printf("Problem reading scan element information\n"); |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 306 | printf("diag %s\n", dev_dir_name); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 307 | goto error_free_triggername; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Construct the directory name for the associated buffer. |
| 312 | * As we know that the lis3l02dq has only one buffer this may |
| 313 | * be built rather than found. |
| 314 | */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 315 | ret = asprintf(&buf_dir_name, |
| 316 | "%siio:device%d/buffer", iio_dir, dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 317 | if (ret < 0) { |
| 318 | ret = -ENOMEM; |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 319 | goto error_free_channels; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 320 | } |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 321 | |
| 322 | if (!notrigger) { |
| 323 | printf("%s %s\n", dev_dir_name, trigger_name); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 324 | /* |
| 325 | * Set the device trigger to be the data ready trigger found |
| 326 | * above |
| 327 | */ |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 328 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
| 329 | dev_dir_name, |
| 330 | trigger_name); |
| 331 | if (ret < 0) { |
| 332 | printf("Failed to write current_trigger file\n"); |
| 333 | goto error_free_buf_dir_name; |
| 334 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Setup ring buffer parameters */ |
| 338 | ret = write_sysfs_int("length", buf_dir_name, buf_len); |
| 339 | if (ret < 0) |
| 340 | goto error_free_buf_dir_name; |
| 341 | |
| 342 | /* Enable the buffer */ |
| 343 | ret = write_sysfs_int("enable", buf_dir_name, 1); |
| 344 | if (ret < 0) |
| 345 | goto error_free_buf_dir_name; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 346 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 347 | scan_size = size_from_channelarray(channels, num_channels); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 348 | data = malloc(scan_size * buf_len); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 349 | if (!data) { |
| 350 | ret = -ENOMEM; |
| 351 | goto error_free_buf_dir_name; |
| 352 | } |
| 353 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 354 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 355 | if (ret < 0) { |
| 356 | ret = -ENOMEM; |
| 357 | goto error_free_data; |
| 358 | } |
| 359 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 360 | /* Attempt to open non blocking the access dev */ |
| 361 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 362 | if (fp == -1) { /* TODO: If it isn't there make the node */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 363 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 364 | printf("Failed to open %s\n", buffer_access); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 365 | goto error_free_buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 368 | for (j = 0; j < num_loops; j++) { |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 369 | if (!noevents) { |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 370 | struct pollfd pfd = { |
| 371 | .fd = fp, |
| 372 | .events = POLLIN, |
| 373 | }; |
| 374 | |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 375 | ret = poll(&pfd, 1, -1); |
| 376 | if (ret < 0) { |
| 377 | ret = -errno; |
| 378 | goto error_close_buffer_access; |
| 379 | } else if (ret == 0) { |
| 380 | continue; |
| 381 | } |
| 382 | |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 383 | toread = buf_len; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 384 | } else { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 385 | usleep(timedelay); |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 386 | toread = 64; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 387 | } |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 388 | |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 389 | read_size = read(fp, data, toread * scan_size); |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 390 | if (read_size < 0) { |
Hartmut Knaack | 8749948 | 2015-05-31 14:39:56 +0200 | [diff] [blame] | 391 | if (errno == EAGAIN) { |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 392 | printf("nothing available\n"); |
| 393 | continue; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 394 | } else { |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 395 | break; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 396 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 397 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 398 | for (i = 0; i < read_size / scan_size; i++) |
| 399 | process_scan(data + scan_size * i, channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 400 | num_channels); |
| 401 | } |
| 402 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 403 | /* Stop the buffer */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 404 | ret = write_sysfs_int("enable", buf_dir_name, 0); |
| 405 | if (ret < 0) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 406 | goto error_close_buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 407 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 408 | if (!notrigger) |
| 409 | /* Disconnect the trigger - just write a dummy name. */ |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 410 | ret = write_sysfs_string("trigger/current_trigger", |
| 411 | dev_dir_name, "NULL"); |
| 412 | if (ret < 0) |
| 413 | printf("Failed to write to %s\n", dev_dir_name); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 414 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 415 | error_close_buffer_access: |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 416 | if (close(fp) == -1) |
| 417 | perror("Failed to close buffer"); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 418 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 419 | error_free_buffer_access: |
| 420 | free(buffer_access); |
Hartmut Knaack | a71bfb4 | 2015-05-31 14:39:41 +0200 | [diff] [blame] | 421 | error_free_data: |
| 422 | free(data); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 423 | error_free_buf_dir_name: |
| 424 | free(buf_dir_name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 425 | error_free_channels: |
| 426 | for (i = num_channels - 1; i >= 0; i--) { |
| 427 | free(channels[i].name); |
| 428 | free(channels[i].generic_name); |
| 429 | } |
| 430 | free(channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 431 | error_free_triggername: |
| 432 | if (datardytrigger) |
| 433 | free(trigger_name); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame^] | 434 | |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 435 | error_free_dev_dir_name: |
| 436 | free(dev_dir_name); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 437 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 438 | return ret; |
| 439 | } |