4

I am using an IMU sensor called LSM6DSL with the iio drivers. They work fine if I display the raw values with the command:

cat /sys/bus/iio/devices/iio:device0/in_accel_x_raw

Then I decided to use the libiio so I can read all these values from a C program :

struct iio_context *context = iio_create_local_context();
struct iio_device *device = iio_context_get_device(context, 1);
struct iio_channel *chan = iio_device_get_channel(device, 0);
iio_channel_enable(chan);
if (iio_channel_is_scan_element(chan) == true)
    printf("OK\n");
struct iio_channel *chan2 = iio_device_get_channel(device, 1);
iio_channel_enable(chan2);
struct iio_buffer *buff = iio_device_create_buffer(device, 1, true);
if (buff == NULL)
{
  printf("Error: %s\n", strerror(errno));
  return (1);
}

And this is the result :

OK
Error: Device or resource busy

Am I missing something? Let me know if you need more informations.

  • 1
    It's hard to say what is the reason. Can be anything from permissions issue (try using root?) to library bug. You should debug your program + libiio (using `gdb`, `strace` or just add `printf` traces). Just put in a break point before calling the `iio_device_create_buffer()` and step down until you find where exactly the `-EBUSY` error occurs. You may need to use [debug symbols](https://wiki.debian.org/DebugPackage) to debug libiio. If you need our help from there -- throw an update here, mentioning exact line of code where `-EBUSY` is set in libiio. – Sam Protsenko May 18 '17 at 15:23
  • Also, grepping the sources of libiio by `EBUSY` word is a good idea. Assuming this error was returned by libiio function and not by syscall/libc function, like `open()` or `fopen()`, you may get a pretty good idea what is going on. – Sam Protsenko May 18 '17 at 15:32
  • 1
    @SamProtsenko, btw, there is a trick which you can use to find the culprit line, i.e. `#undef EINVAL; #define EINVAL __LINE__`. Perhaps I have to write an article about it :-) – 0andriy May 18 '17 at 18:54
  • @0andriy Neat hack :) But in this case you'd have to rebuild libiio to take advantage of that trick. And there is still a chance that `-EBUSY` comes from some syscall or libc call. So I'd incline to old good debugging here. – Sam Protsenko May 18 '17 at 21:13
  • @0andriy Speaking of debugging techniques, the one I find the most useful is something I call "intellectual grep". It means: 1) Locate all places in code that could lead to the issue, using `grep`/`cscope`/ whatever. 2) Use your knowledge and logic to shrink possible causes further, which often gives you the answer immediately, eliminating the need for actual debugging. 3) If you still have several possible causes -- a few `printk`/`printf` traces are usually enough to quickly figure out where is the actual cause. The only problem with this method is to force yourself to use it more often :) – Sam Protsenko May 18 '17 at 21:24
  • Thanks for your answers guys, I'm gonna try to debug it with gdb. But I don't understand where do you want me to use printf, because using it in my program isn't useful, but if I have to place it in the library source I'll have to recompile it each times. By the way I noticed something weird when I used the IIO tool "iio_generic_buffer" if you know it, using flags -agn . It enables all my channels and it's supposed to write some values but it doesn't. It's like it's waiting for something to happen before it can write. –  May 19 '17 at 08:18
  • Ok I have some news: I was calling all of these functions after initializing the ncurses library so I can display the infos nicely. I moved them at the top of the main and it worked succefully, but I have another problem. Reading the values with the refill_buffer function returns the error code 110 which means that a timeout happened. I believe this has something to do with what I said in the comment just before, and I think it is related to something called "interrupt" in the device tree but I didn't manage do fix it. –  May 19 '17 at 09:46

1 Answers1

2

I guess I found the answer, and I didn't pay attention to the effects of the ncurses library (sorry for not mentioning that I was using it).

I moved these functions before the initialization of ncurses and now the buffer is created successful.