AnsweredAssumed Answered

iio_device_create_buffer fails

Question asked by jamesfitzsimons on Apr 9, 2018
Latest reply on May 11, 2018 by larsc

Hi all,

 

I am attempting to use libiio to communicate with a 9 dof IMU board. It has an adxl345 an itg-3200 gyro and HMC5883L magnetometer all accessed via i2c.

 

I have my device tree configured to enable the adxl345 and itg-3200 and their interrupts:

// -- i2c section --
fragment@5 {
   target = <&i2c1>; // i2c1 is numbered correctly
   __overlay__ {
      status = "okay";
      pinctrl-names = "default";
      pinctrl-0 = <>;

      // configuration start
      clock-frequency = <400000>;

      #address-cells = <1>;
      #size-cells = <0>;

 

      adxl345@53 { 
         compatible = "adi,adxl345"; 
         reg = <0x53>; 
         interrupt-parent = <&gpio0>; /* gpio0_3 is on &gpio0 */ 
         interrupts = <3 0>; /* gpio0_3, 0 is active low */ 
      };

 

      itg3200@68 { 
         compatible = "invensense,itg3200"; 
         reg = <0x68>; 
         interrupt-parent = <&gpio0>; /* gpio0_2 is on &gpio0 */ 
         interrupts = <2 0>; /* gpio0_2, 0 is active low */ 
      }; 
   };
};

 

I have written a short libiio test program to test the gyro, but it fails attempting to create the buffer, returning 0x0. Line in bold font below:

 

 

ssize_t sample_cb(const struct iio_channel *chn, void *src, size_t bytes, void *d)
{
      /* Use "src" to read or write a sample for this channel */
      short val;
      iio_channel_convert(chn, &val, (short*)src);
      printf("Got %i bytes, value = %d \n", bytes, val);
      printf("val = %f\n", val * 0.001214142);
      return 0;
}

 


int main (int argc, char **argv)
{
      // Streaming devices
      struct iio_device *dev;

      printf("* Acquiring IIO context\n");
      ASSERT((ctx = iio_create_default_context()) && "No context");
      ASSERT(iio_context_get_devices_count(ctx) > 0 && "No devices");

      

      printf("* Acquiring itg3200 device\n");
      dev = iio_context_find_device(ctx, "itg3200");
      ASSERT(dev != NULL && "No itg3200 dev found");
      int num_channels = iio_device_get_channels_count(dev);
      printf("* Number of channels = %i\n", num_channels);

      

      struct iio_channel* ch = iio_device_get_channel(dev, 1);
      printf("Channel id = %s\n", iio_channel_get_id(ch));
      iio_channel_enable(ch);
      printf("Channel is enabled = %s\n", iio_channel_is_enabled(ch) == true ? "true" : "false");

 

      // Hardware trigger

      struct iio_device *trigger;
      const char* trigger_str = "itg3200-dev1";
      printf("* Acquiring trigger %s\n", trigger_str);
      trigger = iio_context_find_device(ctx, trigger_str);
      if (!trigger || !iio_device_is_trigger(trigger)) {
            perror("No trigger found (try setting up the iio-trig-hrtimer module)");
            exit(1);
      }


      const struct iio_data_format *fmt = iio_channel_get_data_format(ch);
      size_t sample_size = fmt->length / 8;

      uint8_t *dst;
      int num_samples = 10;
      dst = (uint8_t*)malloc(sample_size * num_samples);
      iio_buffer *buffer = iio_device_create_buffer(dev, num_samples, false);
      ASSERT(buffer > 0 && "Failed to create buffer");


      iio_buffer_set_blocking_mode(buffer, true);
      ssize_t samples = iio_buffer_refill(buffer);
      printf("Have %i samples\n", samples);

      iio_buffer_foreach_sample(buffer, sample_cb, NULL);

 

      free(dst);
      iio_buffer_destroy(buffer);
      iio_context_destroy(ctx);
}

I was previously using libiio 0.10-3rcnee0~stretch+20170915 from the Debian repository and was getting a different error where the iio_buffer_refill call would always timeout (return -110). I've now build 0.14 from source so am using the most up to date release. I'm running the following kernel:

   Linux beaglebone 4.14.20-ti-r36 #1 SMP Mon Feb 19 18:00:35 UTC 2018 armv7l GNU/Linux

 

Any pointers on how to debug this or advice as to what I might be doing wrong would be greatly appreciated!

 

Cheers,

James Fitzsimons

Outcomes