blob: 61fcb3c812a24807bef124b9ea9384a65bba95c8 [file] [log] [blame]
Paul Cercueil847eded2014-09-12 14:43:41 +02001/**
2@mainpage
3
4@tableofcontents
5
6@section license License
7Libiio has been developed and is released under the terms of the GNU Lesser General Public
Robin Getz3c366402020-01-28 10:04:05 -05008License, version 2 or (at your option) any later version.
9This open-source license allows anyone to use the library for proprietary or
10open-source, commercial or non-commercial applications.
Paul Cercueil847eded2014-09-12 14:43:41 +020011
Robin Getz3c366402020-01-28 10:04:05 -050012Seperately, the IIO Library also includes a set of test examples and utilities, (collectively known as iio-utils) which are developed and released under the terms of the GNU General Public License, version 2 or (at your option) any later version.
13
14The full terms of the library license can be found at: http://opensource.org/licenses/LGPL-2.1 and the iio-utils license can be found at: https://opensource.org/licenses/GPL-2.0
Paul Cercueil847eded2014-09-12 14:43:41 +020015
16@section code_model Code Model
17The basic bricks of the libiio API are the iio_context, iio_device, iio_channel and iio_buffer classes.
18
19![Caption text](doc/codemodel.svg)
20
21- A iio_context object may contain zero or more iio_device objects.
22 A iio_device object is associated with only one iio_context.
23 This object represents an instance of the library.
24- A iio_device object may contain zero or more iio_channel objects. A iio_channel object is associated with only one iio_device.
25- A iio_device object may be associated with one iio_buffer object, and a iio_buffer object is associated with only one iio_device.
26
27@section creating_context Creating a context
28Different 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.
29Each function will result in a different backend being used.
30
31Those functions are:
32- iio_create_local_context(): Create a "local" context, to use the IIO devices connected to the system (typically for cross-compiled applications).
33- iio_create_network_context(): Create a "network" context that will work with a remotely connected target.
34
35Note that every function that compose the API of libiio will work independently of the function that was used to create the iio_context object.
36
37The iio_context object can later be destroyed with iio_context_destroy().
38
39@subsection navigation Navigation
40@subsubsection device_obj Device objects
41- From the iio_context object, you can obtain the number of available devices with iio_context_get_devices_count().
42- Then, each iio_device object can be accessed with iio_context_get_device().
43- Alternatively, it is possible to lookup for a device name or ID with iio_context_find_device().
44
45Each iio_device object has an ID that can be used as identifier. This ID can be retrieved with iio_device_get_id().
46It optionally also has a name, that can be retrieved with iio_device_get_name().
47
48@subsubsection channel_obj Channel objects
49- From the iio_device object, you can obtain the number of available channels with iio_device_get_channels_count().
50- Then, each iio_channel object can be accessed with iio_device_get_channel().
51- Alternatively, it is possible to lookup for a channel name or ID with iio_device_find_channel().
52
53Each iio_channel can be either input, or output. This information can be retrieved with iio_channel_is_output().
54As for the Device objects, the iio_channel object features an ID and optionally a name.
55The ID can be obtained with iio_channel_get_id(), and the name can be obtained with iio_channel_get_name().
56Important note: two iio_channel can have the same ID, as long as one is input and the other is output.
57
58@subsection list_params Parameters
59Different kinds of parameters are available: parameters that apply to a iio_device, and parameters that apply to one or more iio_channel.
60- The number of device-specific parameters can be obtained with iio_device_get_attrs_count(). Each attribute name can be obtained with iio_device_get_attr().
61- The number of channel-specific attributes can be obtained with iio_channel_get_attrs_count(). Each attribute name can be obtained with iio_channel_get_attr().
62
63Alternatively, it is possible to lookup for the name of an attribute with iio_device_find_attr() and iio_channel_find_attr().
64
65@section reading_modify_params Reading and modifying parameters
66
67@subsection read_param Reading a parameter
68Read device-specific attributes with those functions:
69- iio_device_attr_read()
70- iio_device_attr_read_all()
71- iio_device_attr_read_bool()
72- iio_device_attr_read_longlong()
73- iio_device_attr_read_double()
74
75Read channel-specific attributes with those functions:
76- iio_channel_attr_read()
77- iio_channel_attr_read_all()
78- iio_channel_attr_read_bool()
79- iio_channel_attr_read_longlong()
80- iio_channel_attr_read_double()
81
82Read debug attributes with those functions:
83- iio_device_debug_attr_read()
84- iio_device_debug_attr_read_all()
85- iio_device_debug_attr_read_bool()
86- iio_device_debug_attr_read_longlong()
87- iio_device_debug_attr_read_double()
88
89@subsection write_param Modifying a parameter
90Write device-specific attributes with those functions:
91- iio_device_attr_write()
92- iio_device_attr_write_all()
93- iio_device_attr_write_bool()
94- iio_device_attr_write_longlong()
95- iio_device_attr_write_double()
96
97Write channel-specific attributes with those functions:
98- iio_channel_attr_write()
99- iio_channel_attr_write_all()
100- iio_channel_attr_write_bool()
101- iio_channel_attr_write_longlong()
102- iio_channel_attr_write_double()
103
104Write debug attributes with those functions:
Lars-Peter Clausen36296d12016-04-12 19:24:49 +0200105- iio_device_debug_attr_write()
106- iio_device_debug_attr_write_all()
107- iio_device_debug_attr_write_bool()
108- iio_device_debug_attr_write_longlong()
109- iio_device_debug_attr_write_double()
Paul Cercueil847eded2014-09-12 14:43:41 +0200110
111@section trigger Triggers
112Some devices, mostly low-speed ADCs and DACs, require a trigger to be set for the capture or upload process to work.
113
Morten Fyhn Amundsen556932e2018-04-19 09:36:54 +0200114In libiio, triggers are just regular iio_device objects. To check if an iio_device can be used as a trigger, you can use iio_device_is_trigger().
Paul Cercueil847eded2014-09-12 14:43:41 +0200115
116To see if one device is associated with a trigger, use iio_device_get_trigger().
117
118To 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.
119
120@section capture_upload Capturing or uploading samples
121The 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.
122
123@subsection create_buffer Enabling channels and creating the Buffer object
124The very first step is to enable the capture channels that we want to use, and disable those that we don't need.
125This is done with the functions iio_channel_enable() and iio_channel_disable().
126Note that the channels will really be enabled or disabled when the iio_buffer object is created.
127
128Also, not all channels can be enabled. To know whether or not one channel can be enabled, use iio_channel_is_scan_element().
129
Robin Getzeddbb3d2018-10-30 17:57:43 -0400130Once the channels have been enabled, and triggers assigned (for triggered buffers) the iio_buffer object can be created from the iio_device object that will be used, with the function iio_device_create_buffer().
131This call will fail if no channels have been enabled, or for triggered buffers, if the trigger has not been assigned.
Paul Cercueil847eded2014-09-12 14:43:41 +0200132
133When the object is no more needed, it can be destroyed with iio_buffer_destroy().
134
135@subsection refill Refilling the Buffer (input devices only)
136If 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.
137
138@subsection read_write Reading or writing samples to the Buffer
139Libiio offers various ways to interact with the iio_buffer object.
140
141@subsubsection memcpy Direct copy
142If you already have a buffer of samples, correctly interleaved and in the format that the hardware expects,
143it is possible to copy the samples directly into the iio_buffer object using `memcpy`:
144
145~~~{.c}
146size_t iio_buf_size = iio_buffer_end(buffer) - iio_buffer_start(buffer);
147size_t count = MAX(sizeof(samples_buffer), iio_buf_size);
148memcpy(iio_buffer_start(buffer), samples_buffer, count);
149~~~
150
151Using `memcpy` to copy samples from the iio_buffer is <b>not recommended</b>.
152When capturing samples from an input device, you cannot assume that the iio_buffer object contains only the samples you're interested in.
153
154@subsubsection iterating_cb Iterating over the buffer with a callback
155Libiio provides a way to iterate over the buffer by registering a callback function, with the iio_buffer_foreach_sample() function.
156
157The callback function will be called for each "sample slot" of the buffer,
158which will contain a valid sample if the buffer has been refilled,
159or correspond to an area where a sample should be stored if using an output device.
160
161~~~{.c}
162ssize_t sample_cb(const struct iio_channel *chn, void *src, size_t bytes, void *d)
163{
164 /* Use "src" to read or write a sample for this channel */
165}
166
167int main(void)
168{
169 ...
170 iio_buffer_foreach_sample(buffer, sample_cb, NULL);
171 ...
172}
173~~~
174
175Note that the callback will be called in the order that the samples appear in the buffer,
176and only for samples that correspond to channels that were enabled.
177
178@subsubsection iterating_for Iterating on the samples with a for loop
179This method allows you to iterate over the samples slots that correspond to one channel.
180As such, it is interesting if you want to process the data channel by channel.
181
182It basically consists in a for loop that uses the functions iio_buffer_first(), iio_buffer_step() and iio_buffer_end():
183
184~~~{.c}
185for (void *ptr = iio_buffer_first(buffer, channel);
186 ptr < iio_buffer_end(buffer);
187 ptr += iio_buffer_step(buffer)) {
188 /* Use "ptr" to read or write a sample for this channel */
189}
190~~~
191
192@subsubsection deinterleave Extracting from/to a second buffer
193
194Finally, it is possible to use the iio_channel_read() and iio_channel_read_raw()
195functions to read samples from the iio_buffer to a second byte array.
196The samples will be deinterleaved if needed.
197The "raw" variant will only deinterleave the samples, while the other variant will deinterleave and convert the samples.
198
199For output devices, the iio_channel_write() and iio_channel_write_raw() functions are also available.
200The "raw" variant will only interleave the samples (if needed), while the other variant will interleave and convert the samples
201back to their hardware format.
202
203@subsection convert Convert the samples from/to hardware format
204The raw stream of samples generally isn't in a format that can be directly used in algorithms.
205Some operations, like endianness conversion and bit-shifting of the samples, have to be performed first.
206
207Libiio offers two functions that can be used to convert samples:
208- iio_channel_convert(), to convert from the hardware format
209- iio_channel_convert_inverse(), to convert to the hardware format.
210
211Those two functions should always be used when manipulating the samples of the iio_buffer.
212The exception is when iio_channel_read() or iio_channel_write() are used, as the conversion is then done internally.
213
214@subsection push Submitting the Buffer (output devices only)
215When all the samples have been written to the iio_buffer object, you can submit the buffer to the hardware with a call to iio_buffer_push().
216As soon as the buffer has been submitted, it can be re-used to store new samples.
217
218If the iio_buffer object has been created with the "cyclic" parameter set, and the kernel driver supports cyclic buffers,
219the submitted buffer will be repeated until the iio_buffer is destroyed, and no subsequent call to iio_buffer_push() will be allowed.
220
221@section advanced Advanced options
222
223@subsection userdata Register and retrieve a pointer
224The iio_device and iio_channel allow you to register a pointer, that can then be retrieved at a later moment.
225- A pointer can be registered with a iio_device object using iio_device_set_data(), and can be retrieved with iio_device_get_data().
226- A pointer can be registered with a iio_channel object using iio_channel_set_data(), and can be retrieved with iio_channel_get_data().
227
228@subsection debug_attr Debug attributes
229Some IIO devices provide debug parameters, but their presence is optional. In a similar way than with regular device parameters,
230the number of debug parameters can be obtained with iio_device_get_debug_attrs_count(). Each individual parameter can be retrieved with iio_device_get_debug_attr().
231Alternatively, it is possible to lookup for the name of a debug attribute with iio_device_find_debug_attr().
232
233Those debug parameters can be read using the following functions:
234- iio_device_debug_attr_read(),
235- iio_device_debug_attr_read_all(),
236- iio_device_debug_attr_read_bool(),
237- iio_device_debug_attr_read_longlong(),
238- iio_device_debug_attr_read_double().
239
240Those debug parameters can be written using the following functions:
241- iio_device_debug_attr_write(),
242- iio_device_debug_attr_write_all(),
243- iio_device_debug_attr_write_bool(),
244- iio_device_debug_attr_write_longlong(),
245- iio_device_debug_attr_write_double().
246
247@subsection registers Reading and writing registers
248As for debug attributes, some IIO devices also offer the possibility to read and write hardware registers directly.
249In libiio, this can be done with two functions, iio_device_reg_read() and iio_device_reg_write().
250
Lucas Magasweran7e7b6b52016-09-15 16:26:20 -0700251@section abi Application Binary Interface
252
253The libiio ABI tries to be both backwards and forwards compatible.
254This means applications compiled against an older version will work fine with a newer dynamically linked library. Applications compiled against a newer version will work fine with an older dynamically linked library so long as they don't access any new features. Applications using new features should ensure the libiio version is compatible by using iio_library_get_version() to avoid undefined behavior.
255
256
Paul Cercueil847eded2014-09-12 14:43:41 +0200257*/