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 | /** |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 38 | * enum autochan - state for the automatic channel enabling mechanism |
| 39 | */ |
| 40 | enum autochan { |
| 41 | AUTOCHANNELS_DISABLED, |
| 42 | AUTOCHANNELS_ENABLED, |
| 43 | AUTOCHANNELS_ACTIVE, |
| 44 | }; |
| 45 | |
| 46 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 47 | * size_from_channelarray() - calculate the storage size of a scan |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 48 | * @channels: the channel info array |
| 49 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 50 | * |
| 51 | * Has the side effect of filling the channels[i].location values used |
| 52 | * in processing the buffer output. |
| 53 | **/ |
| 54 | int size_from_channelarray(struct iio_channel_info *channels, int num_channels) |
| 55 | { |
| 56 | int bytes = 0; |
| 57 | int i = 0; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 58 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 59 | while (i < num_channels) { |
| 60 | if (bytes % channels[i].bytes == 0) |
| 61 | channels[i].location = bytes; |
| 62 | else |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 63 | channels[i].location = bytes - bytes % channels[i].bytes |
| 64 | + channels[i].bytes; |
| 65 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 66 | bytes = channels[i].location + channels[i].bytes; |
| 67 | i++; |
| 68 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 69 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 70 | return bytes; |
| 71 | } |
| 72 | |
Tiberiu Breana | e8d0927 | 2015-07-03 12:57:36 +0300 | [diff] [blame] | 73 | void print1byte(uint8_t input, struct iio_channel_info *info) |
| 74 | { |
| 75 | /* |
| 76 | * Shift before conversion to avoid sign extension |
| 77 | * of left aligned data |
| 78 | */ |
| 79 | input >>= info->shift; |
| 80 | input &= info->mask; |
| 81 | if (info->is_signed) { |
| 82 | int8_t val = (int8_t)(input << (8 - info->bits_used)) >> |
| 83 | (8 - info->bits_used); |
| 84 | printf("%05f ", ((float)val + info->offset) * info->scale); |
| 85 | } else { |
| 86 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 87 | } |
| 88 | } |
| 89 | |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 90 | void print2byte(uint16_t input, struct iio_channel_info *info) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 91 | { |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 92 | /* First swap if incorrect endian */ |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 93 | if (info->be) |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 94 | input = be16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 95 | else |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 96 | input = le16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 97 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Shift before conversion to avoid sign extension |
| 100 | * of left aligned data |
| 101 | */ |
Aya Mahfouz | 1525ecf | 2015-02-26 11:45:26 +0200 | [diff] [blame] | 102 | input >>= info->shift; |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 103 | input &= info->mask; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 104 | if (info->is_signed) { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 105 | int16_t val = (int16_t)(input << (16 - info->bits_used)) >> |
| 106 | (16 - info->bits_used); |
| 107 | printf("%05f ", ((float)val + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 108 | } else { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 109 | printf("%05f ", ((float)input + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 112 | |
| 113 | void print4byte(uint32_t input, struct iio_channel_info *info) |
| 114 | { |
| 115 | /* First swap if incorrect endian */ |
| 116 | if (info->be) |
| 117 | input = be32toh(input); |
| 118 | else |
| 119 | input = le32toh(input); |
| 120 | |
| 121 | /* |
| 122 | * Shift before conversion to avoid sign extension |
| 123 | * of left aligned data |
| 124 | */ |
| 125 | input >>= info->shift; |
| 126 | input &= info->mask; |
| 127 | if (info->is_signed) { |
| 128 | int32_t val = (int32_t)(input << (32 - info->bits_used)) >> |
| 129 | (32 - info->bits_used); |
| 130 | printf("%05f ", ((float)val + info->offset) * info->scale); |
| 131 | } else { |
| 132 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void print8byte(uint64_t input, struct iio_channel_info *info) |
| 137 | { |
| 138 | /* First swap if incorrect endian */ |
| 139 | if (info->be) |
| 140 | input = be64toh(input); |
| 141 | else |
| 142 | input = le64toh(input); |
| 143 | |
| 144 | /* |
| 145 | * Shift before conversion to avoid sign extension |
| 146 | * of left aligned data |
| 147 | */ |
| 148 | input >>= info->shift; |
| 149 | input &= info->mask; |
| 150 | if (info->is_signed) { |
| 151 | int64_t val = (int64_t)(input << (64 - info->bits_used)) >> |
| 152 | (64 - info->bits_used); |
| 153 | /* special case for timestamp */ |
| 154 | if (info->scale == 1.0f && info->offset == 0.0f) |
| 155 | printf("%" PRId64 " ", val); |
| 156 | else |
| 157 | printf("%05f ", |
| 158 | ((float)val + info->offset) * info->scale); |
| 159 | } else { |
| 160 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 161 | } |
| 162 | } |
| 163 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 164 | /** |
| 165 | * process_scan() - print out the values in SI units |
| 166 | * @data: pointer to the start of the scan |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 167 | * @channels: information about the channels. |
| 168 | * Note: size_from_channelarray must have been called first |
| 169 | * to fill the location offsets. |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 170 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 171 | **/ |
| 172 | void process_scan(char *data, |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 173 | struct iio_channel_info *channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 174 | int num_channels) |
| 175 | { |
| 176 | int k; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 177 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 178 | for (k = 0; k < num_channels; k++) |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 179 | switch (channels[k].bytes) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 180 | /* only a few cases implemented so far */ |
Tiberiu Breana | e8d0927 | 2015-07-03 12:57:36 +0300 | [diff] [blame] | 181 | case 1: |
| 182 | print1byte(*(uint8_t *)(data + channels[k].location), |
| 183 | &channels[k]); |
| 184 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 185 | case 2: |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 186 | print2byte(*(uint16_t *)(data + channels[k].location), |
| 187 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 188 | break; |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 189 | case 4: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 190 | print4byte(*(uint32_t *)(data + channels[k].location), |
| 191 | &channels[k]); |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 192 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 193 | case 8: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 194 | print8byte(*(uint64_t *)(data + channels[k].location), |
| 195 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 196 | break; |
| 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | printf("\n"); |
| 201 | } |
| 202 | |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 203 | static int enable_disable_all_channels(char *dev_dir_name, int enable) |
| 204 | { |
| 205 | const struct dirent *ent; |
| 206 | char scanelemdir[256]; |
| 207 | DIR *dp; |
| 208 | int ret; |
| 209 | |
| 210 | snprintf(scanelemdir, sizeof(scanelemdir), |
| 211 | FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name); |
| 212 | scanelemdir[sizeof(scanelemdir)-1] = '\0'; |
| 213 | |
| 214 | dp = opendir(scanelemdir); |
| 215 | if (!dp) { |
| 216 | fprintf(stderr, "Enabling/disabling channels: can't open %s\n", |
| 217 | scanelemdir); |
| 218 | return -EIO; |
| 219 | } |
| 220 | |
| 221 | ret = -ENOENT; |
| 222 | while (ent = readdir(dp), ent) { |
| 223 | if (iioutils_check_suffix(ent->d_name, "_en")) { |
| 224 | printf("%sabling: %s\n", |
| 225 | enable ? "En" : "Dis", |
| 226 | ent->d_name); |
| 227 | ret = write_sysfs_int(ent->d_name, scanelemdir, |
| 228 | enable); |
| 229 | if (ret < 0) |
| 230 | fprintf(stderr, "Failed to enable/disable %s\n", |
| 231 | ent->d_name); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (closedir(dp) == -1) { |
| 236 | perror("Enabling/disabling channels: " |
| 237 | "Failed to close directory"); |
| 238 | return -errno; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 243 | void print_usage(void) |
| 244 | { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 245 | fprintf(stderr, "Usage: generic_buffer [options]...\n" |
| 246 | "Capture, convert and output data from IIO device buffer\n" |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 247 | " -a Auto-activate all available channels\n" |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 248 | " -c <n> Do n conversions\n" |
| 249 | " -e Disable wait for event (new data)\n" |
| 250 | " -g Use trigger-less mode\n" |
| 251 | " -l <n> Set buffer length to n samples\n" |
| 252 | " -n <name> Set device name (mandatory)\n" |
| 253 | " -t <name> Set trigger name\n" |
| 254 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 257 | int main(int argc, char **argv) |
| 258 | { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 259 | unsigned long num_loops = 2; |
| 260 | unsigned long timedelay = 1000000; |
| 261 | unsigned long buf_len = 128; |
| 262 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 263 | int ret, c, i, j, toread; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 264 | int fp; |
| 265 | |
| 266 | int num_channels; |
| 267 | char *trigger_name = NULL, *device_name = NULL; |
| 268 | char *dev_dir_name, *buf_dir_name; |
| 269 | |
| 270 | int datardytrigger = 1; |
| 271 | char *data; |
Jonathan Cameron | c77b381 | 2011-04-15 18:56:00 +0100 | [diff] [blame] | 272 | ssize_t read_size; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 273 | int dev_num, trig_num; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 274 | char *buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 275 | int scan_size; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 276 | int noevents = 0; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 277 | int notrigger = 0; |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 278 | enum autochan autochannels = AUTOCHANNELS_DISABLED; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 279 | char *dummy; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 280 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 281 | struct iio_channel_info *channels; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 282 | |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 283 | while ((c = getopt(argc, argv, "ac:egl:n:t:w:")) != -1) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 284 | switch (c) { |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 285 | case 'a': |
| 286 | autochannels = AUTOCHANNELS_ENABLED; |
| 287 | break; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 288 | case 'c': |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 289 | errno = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 290 | num_loops = strtoul(optarg, &dummy, 10); |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 291 | if (errno) |
| 292 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 293 | |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 294 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 295 | case 'e': |
| 296 | noevents = 1; |
| 297 | break; |
| 298 | case 'g': |
| 299 | notrigger = 1; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 300 | break; |
| 301 | case 'l': |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 302 | errno = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 303 | buf_len = strtoul(optarg, &dummy, 10); |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 304 | if (errno) |
| 305 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 306 | |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 307 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 308 | case 'n': |
| 309 | device_name = optarg; |
| 310 | break; |
| 311 | case 't': |
| 312 | trigger_name = optarg; |
| 313 | datardytrigger = 0; |
| 314 | break; |
| 315 | case 'w': |
| 316 | errno = 0; |
| 317 | timedelay = strtoul(optarg, &dummy, 10); |
| 318 | if (errno) |
| 319 | return -errno; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 320 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 321 | case '?': |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 322 | print_usage(); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | } |
| 326 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 327 | if (!device_name) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 328 | fprintf(stderr, "Device name not set\n"); |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 329 | print_usage(); |
Michael Hennerich | 065896e | 2011-02-24 16:34:50 +0100 | [diff] [blame] | 330 | return -1; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 331 | } |
Michael Hennerich | 065896e | 2011-02-24 16:34:50 +0100 | [diff] [blame] | 332 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 333 | /* Find the device requested */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 334 | dev_num = find_type_by_name(device_name, "iio:device"); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 335 | if (dev_num < 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 336 | fprintf(stderr, "Failed to find the %s\n", device_name); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 337 | return dev_num; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 338 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 339 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 340 | printf("iio device number being used is %d\n", dev_num); |
| 341 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 342 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
| 343 | if (ret < 0) |
| 344 | return -ENOMEM; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 345 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 346 | if (!notrigger) { |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 347 | if (!trigger_name) { |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 348 | /* |
| 349 | * Build the trigger name. If it is device associated |
| 350 | * its name is <device_name>_dev[n] where n matches |
| 351 | * the device number found above. |
| 352 | */ |
| 353 | ret = asprintf(&trigger_name, |
| 354 | "%s-dev%d", device_name, dev_num); |
| 355 | if (ret < 0) { |
| 356 | ret = -ENOMEM; |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 357 | goto error_free_dev_dir_name; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Linus Walleij | 793d6b5 | 2016-03-24 09:39:14 +0100 | [diff] [blame] | 361 | /* Look for this "-devN" trigger */ |
| 362 | trig_num = find_type_by_name(trigger_name, "trigger"); |
| 363 | if (trig_num < 0) { |
| 364 | /* OK try the simpler "-trigger" suffix instead */ |
| 365 | free(trigger_name); |
| 366 | ret = asprintf(&trigger_name, |
| 367 | "%s-trigger", device_name); |
| 368 | if (ret < 0) { |
| 369 | ret = -ENOMEM; |
| 370 | goto error_free_dev_dir_name; |
| 371 | } |
| 372 | } |
| 373 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 374 | trig_num = find_type_by_name(trigger_name, "trigger"); |
| 375 | if (trig_num < 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 376 | fprintf(stderr, "Failed to find the trigger %s\n", |
| 377 | trigger_name); |
Hartmut Knaack | e83a47c | 2015-05-31 14:39:57 +0200 | [diff] [blame] | 378 | ret = trig_num; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 379 | goto error_free_triggername; |
| 380 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 381 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 382 | printf("iio trigger number being used is %d\n", trig_num); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 383 | } else { |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 384 | printf("trigger-less mode selected\n"); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 385 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * Parse the files in scan_elements to identify what channels are |
| 389 | * present |
| 390 | */ |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 391 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 392 | if (ret) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 393 | fprintf(stderr, "Problem reading scan element information\n" |
| 394 | "diag %s\n", dev_dir_name); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 395 | goto error_free_triggername; |
| 396 | } |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 397 | if (num_channels && autochannels == AUTOCHANNELS_ENABLED) { |
| 398 | fprintf(stderr, "Auto-channels selected but some channels " |
| 399 | "are already activated in sysfs\n"); |
| 400 | fprintf(stderr, "Proceeding without activating any channels\n"); |
| 401 | } |
| 402 | |
| 403 | if (!num_channels && autochannels == AUTOCHANNELS_ENABLED) { |
| 404 | fprintf(stderr, |
| 405 | "No channels are enabled, enabling all channels\n"); |
| 406 | |
| 407 | ret = enable_disable_all_channels(dev_dir_name, 1); |
| 408 | if (ret) { |
| 409 | fprintf(stderr, "Failed to enable all channels\n"); |
| 410 | goto error_free_triggername; |
| 411 | } |
| 412 | |
| 413 | /* This flags that we need to disable the channels again */ |
| 414 | autochannels = AUTOCHANNELS_ACTIVE; |
| 415 | |
| 416 | ret = build_channel_array(dev_dir_name, &channels, |
| 417 | &num_channels); |
| 418 | if (ret) { |
| 419 | fprintf(stderr, "Problem reading scan element " |
| 420 | "information\n" |
| 421 | "diag %s\n", dev_dir_name); |
| 422 | goto error_disable_channels; |
| 423 | } |
| 424 | if (!num_channels) { |
| 425 | fprintf(stderr, "Still no channels after " |
| 426 | "auto-enabling, giving up\n"); |
| 427 | goto error_disable_channels; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) { |
Linus Walleij | 53dabaf | 2015-08-10 10:55:08 +0200 | [diff] [blame] | 432 | fprintf(stderr, |
| 433 | "No channels are enabled, we have nothing to scan.\n"); |
| 434 | fprintf(stderr, "Enable channels manually in " |
| 435 | FORMAT_SCAN_ELEMENTS_DIR |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 436 | "/*_en or pass -a to autoenable channels and " |
| 437 | "try again.\n", dev_dir_name); |
Linus Walleij | 53dabaf | 2015-08-10 10:55:08 +0200 | [diff] [blame] | 438 | ret = -ENOENT; |
| 439 | goto error_free_triggername; |
| 440 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * Construct the directory name for the associated buffer. |
| 444 | * As we know that the lis3l02dq has only one buffer this may |
| 445 | * be built rather than found. |
| 446 | */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 447 | ret = asprintf(&buf_dir_name, |
| 448 | "%siio:device%d/buffer", iio_dir, dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 449 | if (ret < 0) { |
| 450 | ret = -ENOMEM; |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 451 | goto error_free_channels; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 452 | } |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 453 | |
| 454 | if (!notrigger) { |
| 455 | printf("%s %s\n", dev_dir_name, trigger_name); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 456 | /* |
| 457 | * Set the device trigger to be the data ready trigger found |
| 458 | * above |
| 459 | */ |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 460 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
| 461 | dev_dir_name, |
| 462 | trigger_name); |
| 463 | if (ret < 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 464 | fprintf(stderr, |
| 465 | "Failed to write current_trigger file\n"); |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 466 | goto error_free_buf_dir_name; |
| 467 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* Setup ring buffer parameters */ |
| 471 | ret = write_sysfs_int("length", buf_dir_name, buf_len); |
| 472 | if (ret < 0) |
| 473 | goto error_free_buf_dir_name; |
| 474 | |
| 475 | /* Enable the buffer */ |
| 476 | ret = write_sysfs_int("enable", buf_dir_name, 1); |
Irina Tirdea | e723149 | 2015-07-24 16:28:06 +0300 | [diff] [blame] | 477 | if (ret < 0) { |
| 478 | fprintf(stderr, |
| 479 | "Failed to enable buffer: %s\n", strerror(-ret)); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 480 | goto error_free_buf_dir_name; |
Irina Tirdea | e723149 | 2015-07-24 16:28:06 +0300 | [diff] [blame] | 481 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 482 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 483 | scan_size = size_from_channelarray(channels, num_channels); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 484 | data = malloc(scan_size * buf_len); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 485 | if (!data) { |
| 486 | ret = -ENOMEM; |
| 487 | goto error_free_buf_dir_name; |
| 488 | } |
| 489 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 490 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 491 | if (ret < 0) { |
| 492 | ret = -ENOMEM; |
| 493 | goto error_free_data; |
| 494 | } |
| 495 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 496 | /* Attempt to open non blocking the access dev */ |
| 497 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 498 | if (fp == -1) { /* TODO: If it isn't there make the node */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 499 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 500 | fprintf(stderr, "Failed to open %s\n", buffer_access); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 501 | goto error_free_buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 504 | for (j = 0; j < num_loops; j++) { |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 505 | if (!noevents) { |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 506 | struct pollfd pfd = { |
| 507 | .fd = fp, |
| 508 | .events = POLLIN, |
| 509 | }; |
| 510 | |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 511 | ret = poll(&pfd, 1, -1); |
| 512 | if (ret < 0) { |
| 513 | ret = -errno; |
| 514 | goto error_close_buffer_access; |
| 515 | } else if (ret == 0) { |
| 516 | continue; |
| 517 | } |
| 518 | |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 519 | toread = buf_len; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 520 | } else { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 521 | usleep(timedelay); |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 522 | toread = 64; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 523 | } |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 524 | |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 525 | read_size = read(fp, data, toread * scan_size); |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 526 | if (read_size < 0) { |
Hartmut Knaack | 8749948 | 2015-05-31 14:39:56 +0200 | [diff] [blame] | 527 | if (errno == EAGAIN) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 528 | fprintf(stderr, "nothing available\n"); |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 529 | continue; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 530 | } else { |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 531 | break; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 532 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 533 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 534 | for (i = 0; i < read_size / scan_size; i++) |
| 535 | process_scan(data + scan_size * i, channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 536 | num_channels); |
| 537 | } |
| 538 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 539 | /* Stop the buffer */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 540 | ret = write_sysfs_int("enable", buf_dir_name, 0); |
| 541 | if (ret < 0) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 542 | goto error_close_buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 543 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 544 | if (!notrigger) |
| 545 | /* Disconnect the trigger - just write a dummy name. */ |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 546 | ret = write_sysfs_string("trigger/current_trigger", |
| 547 | dev_dir_name, "NULL"); |
| 548 | if (ret < 0) |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 549 | fprintf(stderr, "Failed to write to %s\n", |
| 550 | dev_dir_name); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 551 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 552 | error_close_buffer_access: |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 553 | if (close(fp) == -1) |
| 554 | perror("Failed to close buffer"); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 555 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 556 | error_free_buffer_access: |
| 557 | free(buffer_access); |
Hartmut Knaack | a71bfb4 | 2015-05-31 14:39:41 +0200 | [diff] [blame] | 558 | error_free_data: |
| 559 | free(data); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 560 | error_free_buf_dir_name: |
| 561 | free(buf_dir_name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 562 | error_free_channels: |
| 563 | for (i = num_channels - 1; i >= 0; i--) { |
| 564 | free(channels[i].name); |
| 565 | free(channels[i].generic_name); |
| 566 | } |
| 567 | free(channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 568 | error_free_triggername: |
| 569 | if (datardytrigger) |
| 570 | free(trigger_name); |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 571 | error_disable_channels: |
| 572 | if (autochannels == AUTOCHANNELS_ACTIVE) { |
| 573 | ret = enable_disable_all_channels(dev_dir_name, 0); |
| 574 | if (ret) |
| 575 | fprintf(stderr, "Failed to disable all channels\n"); |
| 576 | } |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 577 | error_free_dev_dir_name: |
| 578 | free(dev_dir_name); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 579 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 580 | return ret; |
| 581 | } |