diff options
Diffstat (limited to 'tools/iio/generic_buffer.c')
-rw-r--r-- | tools/iio/generic_buffer.c | 115 |
1 files changed, 75 insertions, 40 deletions
diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 4eebb6616e5c..9f7b85bf6ada 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c | |||
@@ -51,14 +51,33 @@ int size_from_channelarray(struct iio_channel_info *channels, int num_channels) | |||
51 | if (bytes % channels[i].bytes == 0) | 51 | if (bytes % channels[i].bytes == 0) |
52 | channels[i].location = bytes; | 52 | channels[i].location = bytes; |
53 | else | 53 | else |
54 | channels[i].location = bytes - bytes%channels[i].bytes | 54 | channels[i].location = bytes - bytes % channels[i].bytes |
55 | + channels[i].bytes; | 55 | + channels[i].bytes; |
56 | |||
56 | bytes = channels[i].location + channels[i].bytes; | 57 | bytes = channels[i].location + channels[i].bytes; |
57 | i++; | 58 | i++; |
58 | } | 59 | } |
60 | |||
59 | return bytes; | 61 | return bytes; |
60 | } | 62 | } |
61 | 63 | ||
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 | |||
62 | void print2byte(uint16_t input, struct iio_channel_info *info) | 81 | void print2byte(uint16_t input, struct iio_channel_info *info) |
63 | { | 82 | { |
64 | /* First swap if incorrect endian */ | 83 | /* First swap if incorrect endian */ |
@@ -136,9 +155,9 @@ void print8byte(uint64_t input, struct iio_channel_info *info) | |||
136 | /** | 155 | /** |
137 | * process_scan() - print out the values in SI units | 156 | * process_scan() - print out the values in SI units |
138 | * @data: pointer to the start of the scan | 157 | * @data: pointer to the start of the scan |
139 | * @channels: information about the channels. Note | 158 | * @channels: information about the channels. |
140 | * size_from_channelarray must have been called first to fill the | 159 | * Note: size_from_channelarray must have been called first |
141 | * location offsets. | 160 | * to fill the location offsets. |
142 | * @num_channels: number of channels | 161 | * @num_channels: number of channels |
143 | **/ | 162 | **/ |
144 | void process_scan(char *data, | 163 | void process_scan(char *data, |
@@ -150,6 +169,10 @@ void process_scan(char *data, | |||
150 | for (k = 0; k < num_channels; k++) | 169 | for (k = 0; k < num_channels; k++) |
151 | switch (channels[k].bytes) { | 170 | switch (channels[k].bytes) { |
152 | /* only a few cases implemented so far */ | 171 | /* only a few cases implemented so far */ |
172 | case 1: | ||
173 | print1byte(*(uint8_t *)(data + channels[k].location), | ||
174 | &channels[k]); | ||
175 | break; | ||
153 | case 2: | 176 | case 2: |
154 | print2byte(*(uint16_t *)(data + channels[k].location), | 177 | print2byte(*(uint16_t *)(data + channels[k].location), |
155 | &channels[k]); | 178 | &channels[k]); |
@@ -170,15 +193,15 @@ void process_scan(char *data, | |||
170 | 193 | ||
171 | void print_usage(void) | 194 | void print_usage(void) |
172 | { | 195 | { |
173 | printf("Usage: generic_buffer [options]...\n" | 196 | fprintf(stderr, "Usage: generic_buffer [options]...\n" |
174 | "Capture, convert and output data from IIO device buffer\n" | 197 | "Capture, convert and output data from IIO device buffer\n" |
175 | " -c <n> Do n conversions\n" | 198 | " -c <n> Do n conversions\n" |
176 | " -e Disable wait for event (new data)\n" | 199 | " -e Disable wait for event (new data)\n" |
177 | " -g Use trigger-less mode\n" | 200 | " -g Use trigger-less mode\n" |
178 | " -l <n> Set buffer length to n samples\n" | 201 | " -l <n> Set buffer length to n samples\n" |
179 | " -n <name> Set device name (mandatory)\n" | 202 | " -n <name> Set device name (mandatory)\n" |
180 | " -t <name> Set trigger name\n" | 203 | " -t <name> Set trigger name\n" |
181 | " -w <n> Set delay between reads in us (event-less mode)\n"); | 204 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
182 | } | 205 | } |
183 | 206 | ||
184 | int main(int argc, char **argv) | 207 | int main(int argc, char **argv) |
@@ -213,6 +236,7 @@ int main(int argc, char **argv) | |||
213 | num_loops = strtoul(optarg, &dummy, 10); | 236 | num_loops = strtoul(optarg, &dummy, 10); |
214 | if (errno) | 237 | if (errno) |
215 | return -errno; | 238 | return -errno; |
239 | |||
216 | break; | 240 | break; |
217 | case 'e': | 241 | case 'e': |
218 | noevents = 1; | 242 | noevents = 1; |
@@ -225,6 +249,7 @@ int main(int argc, char **argv) | |||
225 | buf_len = strtoul(optarg, &dummy, 10); | 249 | buf_len = strtoul(optarg, &dummy, 10); |
226 | if (errno) | 250 | if (errno) |
227 | return -errno; | 251 | return -errno; |
252 | |||
228 | break; | 253 | break; |
229 | case 'n': | 254 | case 'n': |
230 | device_name = optarg; | 255 | device_name = optarg; |
@@ -245,8 +270,8 @@ int main(int argc, char **argv) | |||
245 | } | 270 | } |
246 | } | 271 | } |
247 | 272 | ||
248 | if (device_name == NULL) { | 273 | if (!device_name) { |
249 | printf("Device name not set\n"); | 274 | fprintf(stderr, "Device name not set\n"); |
250 | print_usage(); | 275 | print_usage(); |
251 | return -1; | 276 | return -1; |
252 | } | 277 | } |
@@ -254,9 +279,10 @@ int main(int argc, char **argv) | |||
254 | /* Find the device requested */ | 279 | /* Find the device requested */ |
255 | dev_num = find_type_by_name(device_name, "iio:device"); | 280 | dev_num = find_type_by_name(device_name, "iio:device"); |
256 | if (dev_num < 0) { | 281 | if (dev_num < 0) { |
257 | printf("Failed to find the %s\n", device_name); | 282 | fprintf(stderr, "Failed to find the %s\n", device_name); |
258 | return dev_num; | 283 | return dev_num; |
259 | } | 284 | } |
285 | |||
260 | printf("iio device number being used is %d\n", dev_num); | 286 | printf("iio device number being used is %d\n", dev_num); |
261 | 287 | ||
262 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); | 288 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
@@ -264,7 +290,7 @@ int main(int argc, char **argv) | |||
264 | return -ENOMEM; | 290 | return -ENOMEM; |
265 | 291 | ||
266 | if (!notrigger) { | 292 | if (!notrigger) { |
267 | if (trigger_name == NULL) { | 293 | if (!trigger_name) { |
268 | /* | 294 | /* |
269 | * Build the trigger name. If it is device associated | 295 | * Build the trigger name. If it is device associated |
270 | * its name is <device_name>_dev[n] where n matches | 296 | * its name is <device_name>_dev[n] where n matches |
@@ -281,13 +307,16 @@ int main(int argc, char **argv) | |||
281 | /* Verify the trigger exists */ | 307 | /* Verify the trigger exists */ |
282 | trig_num = find_type_by_name(trigger_name, "trigger"); | 308 | trig_num = find_type_by_name(trigger_name, "trigger"); |
283 | if (trig_num < 0) { | 309 | if (trig_num < 0) { |
284 | printf("Failed to find the trigger %s\n", trigger_name); | 310 | fprintf(stderr, "Failed to find the trigger %s\n", |
311 | trigger_name); | ||
285 | ret = trig_num; | 312 | ret = trig_num; |
286 | goto error_free_triggername; | 313 | goto error_free_triggername; |
287 | } | 314 | } |
315 | |||
288 | printf("iio trigger number being used is %d\n", trig_num); | 316 | printf("iio trigger number being used is %d\n", trig_num); |
289 | } else | 317 | } else { |
290 | printf("trigger-less mode selected\n"); | 318 | printf("trigger-less mode selected\n"); |
319 | } | ||
291 | 320 | ||
292 | /* | 321 | /* |
293 | * Parse the files in scan_elements to identify what channels are | 322 | * Parse the files in scan_elements to identify what channels are |
@@ -295,8 +324,8 @@ int main(int argc, char **argv) | |||
295 | */ | 324 | */ |
296 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); | 325 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
297 | if (ret) { | 326 | if (ret) { |
298 | printf("Problem reading scan element information\n"); | 327 | fprintf(stderr, "Problem reading scan element information\n" |
299 | printf("diag %s\n", dev_dir_name); | 328 | "diag %s\n", dev_dir_name); |
300 | goto error_free_triggername; | 329 | goto error_free_triggername; |
301 | } | 330 | } |
302 | 331 | ||
@@ -314,13 +343,16 @@ int main(int argc, char **argv) | |||
314 | 343 | ||
315 | if (!notrigger) { | 344 | if (!notrigger) { |
316 | printf("%s %s\n", dev_dir_name, trigger_name); | 345 | printf("%s %s\n", dev_dir_name, trigger_name); |
317 | /* Set the device trigger to be the data ready trigger found | 346 | /* |
318 | * above */ | 347 | * Set the device trigger to be the data ready trigger found |
348 | * above | ||
349 | */ | ||
319 | ret = write_sysfs_string_and_verify("trigger/current_trigger", | 350 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
320 | dev_dir_name, | 351 | dev_dir_name, |
321 | trigger_name); | 352 | trigger_name); |
322 | if (ret < 0) { | 353 | if (ret < 0) { |
323 | printf("Failed to write current_trigger file\n"); | 354 | fprintf(stderr, |
355 | "Failed to write current_trigger file\n"); | ||
324 | goto error_free_buf_dir_name; | 356 | goto error_free_buf_dir_name; |
325 | } | 357 | } |
326 | } | 358 | } |
@@ -332,10 +364,14 @@ int main(int argc, char **argv) | |||
332 | 364 | ||
333 | /* Enable the buffer */ | 365 | /* Enable the buffer */ |
334 | ret = write_sysfs_int("enable", buf_dir_name, 1); | 366 | ret = write_sysfs_int("enable", buf_dir_name, 1); |
335 | if (ret < 0) | 367 | if (ret < 0) { |
368 | fprintf(stderr, | ||
369 | "Failed to enable buffer: %s\n", strerror(-ret)); | ||
336 | goto error_free_buf_dir_name; | 370 | goto error_free_buf_dir_name; |
371 | } | ||
372 | |||
337 | scan_size = size_from_channelarray(channels, num_channels); | 373 | scan_size = size_from_channelarray(channels, num_channels); |
338 | data = malloc(scan_size*buf_len); | 374 | data = malloc(scan_size * buf_len); |
339 | if (!data) { | 375 | if (!data) { |
340 | ret = -ENOMEM; | 376 | ret = -ENOMEM; |
341 | goto error_free_buf_dir_name; | 377 | goto error_free_buf_dir_name; |
@@ -349,13 +385,12 @@ int main(int argc, char **argv) | |||
349 | 385 | ||
350 | /* Attempt to open non blocking the access dev */ | 386 | /* Attempt to open non blocking the access dev */ |
351 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); | 387 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); |
352 | if (fp == -1) { /* If it isn't there make the node */ | 388 | if (fp == -1) { /* TODO: If it isn't there make the node */ |
353 | ret = -errno; | 389 | ret = -errno; |
354 | printf("Failed to open %s\n", buffer_access); | 390 | fprintf(stderr, "Failed to open %s\n", buffer_access); |
355 | goto error_free_buffer_access; | 391 | goto error_free_buffer_access; |
356 | } | 392 | } |
357 | 393 | ||
358 | /* Wait for events 10 times */ | ||
359 | for (j = 0; j < num_loops; j++) { | 394 | for (j = 0; j < num_loops; j++) { |
360 | if (!noevents) { | 395 | if (!noevents) { |
361 | struct pollfd pfd = { | 396 | struct pollfd pfd = { |
@@ -372,25 +407,22 @@ int main(int argc, char **argv) | |||
372 | } | 407 | } |
373 | 408 | ||
374 | toread = buf_len; | 409 | toread = buf_len; |
375 | |||
376 | } else { | 410 | } else { |
377 | usleep(timedelay); | 411 | usleep(timedelay); |
378 | toread = 64; | 412 | toread = 64; |
379 | } | 413 | } |
380 | 414 | ||
381 | read_size = read(fp, | 415 | read_size = read(fp, data, toread * scan_size); |
382 | data, | ||
383 | toread*scan_size); | ||
384 | if (read_size < 0) { | 416 | if (read_size < 0) { |
385 | if (errno == EAGAIN) { | 417 | if (errno == EAGAIN) { |
386 | printf("nothing available\n"); | 418 | fprintf(stderr, "nothing available\n"); |
387 | continue; | 419 | continue; |
388 | } else | 420 | } else { |
389 | break; | 421 | break; |
422 | } | ||
390 | } | 423 | } |
391 | for (i = 0; i < read_size/scan_size; i++) | 424 | for (i = 0; i < read_size / scan_size; i++) |
392 | process_scan(data + scan_size*i, | 425 | process_scan(data + scan_size * i, channels, |
393 | channels, | ||
394 | num_channels); | 426 | num_channels); |
395 | } | 427 | } |
396 | 428 | ||
@@ -404,11 +436,13 @@ int main(int argc, char **argv) | |||
404 | ret = write_sysfs_string("trigger/current_trigger", | 436 | ret = write_sysfs_string("trigger/current_trigger", |
405 | dev_dir_name, "NULL"); | 437 | dev_dir_name, "NULL"); |
406 | if (ret < 0) | 438 | if (ret < 0) |
407 | printf("Failed to write to %s\n", dev_dir_name); | 439 | fprintf(stderr, "Failed to write to %s\n", |
440 | dev_dir_name); | ||
408 | 441 | ||
409 | error_close_buffer_access: | 442 | error_close_buffer_access: |
410 | if (close(fp) == -1) | 443 | if (close(fp) == -1) |
411 | perror("Failed to close buffer"); | 444 | perror("Failed to close buffer"); |
445 | |||
412 | error_free_buffer_access: | 446 | error_free_buffer_access: |
413 | free(buffer_access); | 447 | free(buffer_access); |
414 | error_free_data: | 448 | error_free_data: |
@@ -424,6 +458,7 @@ error_free_channels: | |||
424 | error_free_triggername: | 458 | error_free_triggername: |
425 | if (datardytrigger) | 459 | if (datardytrigger) |
426 | free(trigger_name); | 460 | free(trigger_name); |
461 | |||
427 | error_free_dev_dir_name: | 462 | error_free_dev_dir_name: |
428 | free(dev_dir_name); | 463 | free(dev_dir_name); |
429 | 464 | ||