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