libiio  0.9
Library for interfacing with IIO devices
libiio Documentation

License

Libiio has been developed and is released under the terms of the GNU Lesser General Public License, version 2. This open-source license allows anyone to use the library for proprietary or open-source, commercial or non-commercial applications. This choice was motivated by the fact that Analog Devices is a company that principally sells hardware, and this library provides the clients with a better and easier way of using this hardware.

The full terms of the license can be found here: http://opensource.org/licenses/LGPL-2.1

Code Model

The basic bricks of the libiio API are the iio_context, iio_device, iio_channel and iio_buffer classes.

![Caption text](doc/codemodel.svg)

Creating a context

Different functions are available to create the iio_context object. Depending on what backends were enabled when compiling the library, some of them may not be available. Each function will result in a different backend being used.

Those functions are:

Note that every function that compose the API of libiio will work independently of the function that was used to create the iio_context object.

The iio_context object can later be destroyed with iio_context_destroy().

Navigation

Device objects

Each iio_device object has an ID that can be used as identifier. This ID can be retrieved with iio_device_get_id(). It optionally also has a name, that can be retrieved with iio_device_get_name().

Channel objects

Each iio_channel can be either input, or output. This information can be retrieved with iio_channel_is_output(). As for the Device objects, the iio_channel object features an ID and optionally a name. The ID can be obtained with iio_channel_get_id(), and the name can be obtained with iio_channel_get_name(). Important note: two iio_channel can have the same ID, as long as one is input and the other is output.

Parameters

Different kinds of parameters are available: parameters that apply to a iio_device, and parameters that apply to one or more iio_channel.

Alternatively, it is possible to lookup for the name of an attribute with iio_device_find_attr() and iio_channel_find_attr().

Reading and modifying parameters

Reading a parameter

Read device-specific attributes with those functions:

Read channel-specific attributes with those functions:

Read debug attributes with those functions:

Modifying a parameter

Write device-specific attributes with those functions:

Write channel-specific attributes with those functions:

Write debug attributes with those functions:

Triggers

Some devices, mostly low-speed ADCs and DACs, require a trigger to be set for the capture or upload process to work.

In libiio, triggers are just regular iio_device objects. To check if a iio_buffer can be used as a trigger, you can use iio_device_is_trigger().

To see if one device is associated with a trigger, use iio_device_get_trigger().

To assign one trigger to a iio_device, you can use iio_device_set_trigger(). If you want to disassociate a iio_device from its trigger, pass NULL to the "trigger" parameter of this function.

Capturing or uploading samples

The process of capturing samples from the hardware and uploading samples to the hardware is done using the functions that apply to the iio_buffer object.

Enabling channels and creating the Buffer object

The very first step is to enable the capture channels that we want to use, and disable those that we don't need. This is done with the functions iio_channel_enable() and iio_channel_disable(). Note that the channels will really be enabled or disabled when the iio_buffer object is created.

Also, not all channels can be enabled. To know whether or not one channel can be enabled, use iio_channel_is_scan_element().

Once the channels have been enabled, the iio_buffer object can be created from the iio_device object that will be used, with the function iio_device_create_buffer(). This call will fail if no channels have been enabled.

When the object is no more needed, it can be destroyed with iio_buffer_destroy().

Refilling the Buffer (input devices only)

If the Buffer object has been created from a device with input channels, then it must be updated first. This is done with the iio_buffer_refill() function.

Reading or writing samples to the Buffer

Libiio offers various ways to interact with the iio_buffer object.

Direct copy

If you already have a buffer of samples, correctly interleaved and in the format that the hardware expects, it is possible to copy the samples directly into the iio_buffer object using `memcpy`:

~~~{.c} size_t iio_buf_size = iio_buffer_end(buffer) - iio_buffer_start(buffer); size_t count = MAX(sizeof(samples_buffer), iio_buf_size); memcpy(iio_buffer_start(buffer), samples_buffer, count); ~~~

Using `memcpy` to copy samples from the iio_buffer is not recommended. When capturing samples from an input device, you cannot assume that the iio_buffer object contains only the samples you're interested in.

Iterating over the buffer with a callback

Libiio provides a way to iterate over the buffer by registering a callback function, with the iio_buffer_foreach_sample() function.

The callback function will be called for each "sample slot" of the buffer, which will contain a valid sample if the buffer has been refilled, or correspond to an area where a sample should be stored if using an output device.

~~~{.c} 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

 All Data Structures Files Functions Variables Enumerations