diff options
Diffstat (limited to 'drivers/staging/iio/Documentation/generic_buffer.c')
-rw-r--r-- | drivers/staging/iio/Documentation/generic_buffer.c | 95 |
1 files changed, 35 insertions, 60 deletions
diff --git a/drivers/staging/iio/Documentation/generic_buffer.c b/drivers/staging/iio/Documentation/generic_buffer.c index 3cc18ab4ebfd..f82894f42d27 100644 --- a/drivers/staging/iio/Documentation/generic_buffer.c +++ b/drivers/staging/iio/Documentation/generic_buffer.c @@ -27,6 +27,7 @@ #include <sys/dir.h> #include <linux/types.h> #include <string.h> +#include <poll.h> #include "iio_utils.h" /** @@ -53,6 +54,24 @@ int size_from_channelarray(struct iio_channel_info *channels, int num_channels) return bytes; } +void print2byte(int input, struct iio_channel_info *info) +{ + /* shift before conversion to avoid sign extension + of left aligned data */ + input = input >> info->shift; + if (info->is_signed) { + int16_t val = input; + val &= (1 << info->bits_used) - 1; + val = (int16_t)(val << (16 - info->bits_used)) >> + (16 - info->bits_used); + printf("%05f ", val, + (float)(val + info->offset)*info->scale); + } else { + uint16_t val = input; + val &= (1 << info->bits_used) - 1; + printf("%05f ", ((float)val + info->offset)*info->scale); + } +} /** * process_scan() - print out the values in SI units * @data: pointer to the start of the scan @@ -70,25 +89,8 @@ void process_scan(char *data, switch (infoarray[k].bytes) { /* only a few cases implemented so far */ case 2: - if (infoarray[k].is_signed) { - int16_t val = *(int16_t *) - (data - + infoarray[k].location); - if ((val >> infoarray[k].bits_used) & 1) - val = (val & infoarray[k].mask) | - ~infoarray[k].mask; - printf("%05f ", ((float)val + - infoarray[k].offset)* - infoarray[k].scale); - } else { - uint16_t val = *(uint16_t *) - (data + - infoarray[k].location); - val = (val & infoarray[k].mask); - printf("%05f ", ((float)val + - infoarray[k].offset)* - infoarray[k].scale); - } + print2byte(*(uint16_t *)(data + infoarray[k].location), + &infoarray[k]); break; case 8: if (infoarray[k].is_signed) { @@ -132,10 +134,9 @@ int main(int argc, char **argv) int datardytrigger = 1; char *data; - size_t read_size; - struct iio_event_data dat; + ssize_t read_size; int dev_num, trig_num; - char *buffer_access, *buffer_event; + char *buffer_access; int scan_size; int noevents = 0; char *dummy; @@ -210,7 +211,7 @@ int main(int argc, char **argv) */ ret = build_channel_array(dev_dir_name, &infoarray, &num_channels); if (ret) { - printf("Problem reading scan element information \n"); + printf("Problem reading scan element information\n"); goto error_free_triggername; } @@ -251,54 +252,32 @@ int main(int argc, char **argv) } ret = asprintf(&buffer_access, - "/dev/device%d:buffer0:access0", + "/dev/device%d:buffer0", dev_num); if (ret < 0) { ret = -ENOMEM; goto error_free_data; } - ret = asprintf(&buffer_event, "/dev/device%d:buffer0:event0", dev_num); - if (ret < 0) { - ret = -ENOMEM; - goto error_free_buffer_access; - } /* Attempt to open non blocking the access dev */ fp = open(buffer_access, O_RDONLY | O_NONBLOCK); if (fp == -1) { /*If it isn't there make the node */ printf("Failed to open %s\n", buffer_access); ret = -errno; - goto error_free_buffer_event; - } - /* Attempt to open the event access dev (blocking this time) */ - fp_ev = fopen(buffer_event, "rb"); - if (fp_ev == NULL) { - printf("Failed to open %s\n", buffer_event); - ret = -errno; - goto error_close_buffer_access; + goto error_free_buffer_access; } /* Wait for events 10 times */ for (j = 0; j < num_loops; j++) { if (!noevents) { - read_size = fread(&dat, - 1, - sizeof(struct iio_event_data), - fp_ev); - switch (dat.id) { - case IIO_EVENT_CODE_RING_100_FULL: - toread = buf_len; - break; - case IIO_EVENT_CODE_RING_75_FULL: - toread = buf_len*3/4; - break; - case IIO_EVENT_CODE_RING_50_FULL: - toread = buf_len/2; - break; - default: - printf("Unexpecteded event code\n"); - continue; - } + struct pollfd pfd = { + .fd = fp, + .events = POLLIN, + }; + + poll(&pfd, 1, -1); + toread = buf_len; + } else { usleep(timedelay); toread = 64; @@ -320,22 +299,18 @@ int main(int argc, char **argv) /* Stop the ring buffer */ ret = write_sysfs_int("enable", buf_dir_name, 0); if (ret < 0) - goto error_close_buffer_event; + goto error_close_buffer_access; /* Disconnect from the trigger - just write a dummy name.*/ write_sysfs_string("trigger/current_trigger", dev_dir_name, "NULL"); -error_close_buffer_event: - fclose(fp_ev); error_close_buffer_access: close(fp); error_free_data: free(data); error_free_buffer_access: free(buffer_access); -error_free_buffer_event: - free(buffer_event); error_free_buf_dir_name: free(buf_dir_name); error_free_triggername: |