Skip to content

Commit a1a198f

Browse files
committed
treewide: docs: Update to the new API
Update documentation across the tree to reflect the buffer_stream API changes. Add missing doxygen comments for new public functions in the header file, update the Python bindings documentation and the test README. In mainpage.dox, beyond updating to the new buffer_stream/block API, also fix references to legacy v0.x APIs (such as iio_buffer_refill, iio_buffer_push, iio_buffer_foreach_sample, iio_buffer_first/step/end) that were already gone but still documented. Signed-off-by: Nuno Sá <nuno.sa@analog.com>
1 parent 55b08a4 commit a1a198f

File tree

4 files changed

+87
-37
lines changed

4 files changed

+87
-37
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BufferStream
2+
==================
3+
4+
Members
5+
--------------
6+
.. autoclass:: iio.BufferStream
7+
:members:

bindings/python/doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Components
3939

4040
context
4141
buffer
42+
buffer_stream
4243
device
4344
channel
4445
trigger

mainpage.dox

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Separately, the IIO Library also includes a set of test examples and utilities,
1616
The 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
1717

1818
@section code_model Code Model
19-
The basic bricks of the libiio API are the iio_context, iio_device, iio_channel and iio_buffer classes.
19+
The basic bricks of the libiio API are the iio_context, iio_device, iio_channel, iio_buffer, iio_buffer_stream and iio_block classes.
2020

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

@@ -133,44 +133,83 @@ To see if one device is associated with a trigger, use iio_device_get_trigger().
133133
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.
134134

135135
@section capture_upload Capturing or uploading samples
136-
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.
136+
The process of capturing samples from the hardware and uploading samples to the hardware is done using the iio_buffer, iio_buffer_stream and iio_block objects.
137137

138-
@subsection create_buffer Enabling channels and creating the Buffer object
138+
@subsection create_buffer Enabling channels and opening a Buffer stream
139139
The very first step is to enable the capture channels that we want to use, and disable those that we don't need.
140-
This is done with the functions iio_channel_enable() and iio_channel_disable().
141-
Note that the channels will really be enabled or disabled when the iio_buffer object is created.
140+
This is done by creating an iio_channels_mask with iio_create_channels_mask() and then calling iio_channel_enable() for the desired channels.
142141

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

145-
Once 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().
146-
This call will fail if no channels have been enabled, or for triggered buffers, if the trigger has not been assigned.
144+
Buffers are pre-created during device context creation. To obtain a buffer, use iio_device_get_buffer(). To open it for streaming, call iio_buffer_open() with the channels mask, which returns an iio_buffer_stream object.
145+
This call will fail if no channels have been enabled in the mask, or for triggered buffers, if the trigger has not been assigned.
147146

148-
When the object is no more needed, it can be destroyed with iio_buffer_destroy().
147+
When the stream is no longer needed, it can be closed with iio_buffer_close().
149148

150-
@subsection refill Refilling the Buffer (input devices only)
151-
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.
149+
@subsection streaming Streaming with blocks
150+
Data is transferred to and from the hardware using iio_block objects. There are two approaches: manual block management or the high-level iio_stream API.
152151

153-
@subsection read_write Reading or writing samples to the Buffer
154-
Libiio offers various ways to interact with the iio_buffer object.
152+
@subsubsection manual_blocks Manual block management
153+
Create one or more blocks from the buffer stream with iio_buffer_stream_create_block(). Each block represents a contiguous region of memory for sample data.
154+
155+
To start data flow, call iio_buffer_stream_start(). Then use iio_block_enqueue() to submit a block for I/O, and iio_block_dequeue() to wait for the transfer to complete. To stop, call iio_buffer_stream_stop() or iio_buffer_stream_cancel().
156+
157+
~~~{.c}
158+
struct iio_buffer *buf = iio_device_get_buffer(dev, 0);
159+
struct iio_buffer_stream *stream = iio_buffer_open(buf, mask);
160+
struct iio_block *block = iio_buffer_stream_create_block(stream, block_size);
161+
162+
iio_buffer_stream_start(stream);
163+
164+
/* Streaming loop */
165+
while (!stop) {
166+
iio_block_enqueue(block, 0, false);
167+
iio_block_dequeue(block, false);
168+
/* Process samples via iio_block_start()/iio_block_end() */
169+
}
170+
171+
iio_block_destroy(block);
172+
iio_buffer_close(stream);
173+
~~~
174+
175+
@subsubsection stream_api High-level Stream API
176+
The iio_stream API simplifies streaming by managing blocks internally. Create a stream with iio_buffer_create_stream(), specifying the number of blocks and the number of samples per block. Then iterate by calling iio_stream_get_next_block() in a loop.
177+
178+
~~~{.c}
179+
struct iio_buffer *buf = iio_device_get_buffer(dev, 0);
180+
struct iio_stream *stream = iio_buffer_create_stream(buf, 4, num_samples, mask);
181+
182+
while (!stop) {
183+
const struct iio_block *block = iio_stream_get_next_block(stream);
184+
/* Process samples via iio_block_start()/iio_block_end() */
185+
}
186+
187+
iio_stream_destroy(stream);
188+
~~~
189+
190+
To abort a running stream from another thread or signal context, use iio_stream_cancel(). Destroying the stream with iio_stream_destroy() will also close the underlying buffer stream.
191+
192+
@subsection read_write Reading or writing samples to a Block
193+
Libiio offers various ways to interact with the iio_block object.
155194

156195
@subsubsection memcpy Direct copy
157196
If you already have a buffer of samples, correctly interleaved and in the format that the hardware expects,
158-
it is possible to copy the samples directly into the iio_buffer object using `memcpy`:
197+
it is possible to copy the samples directly into the iio_block object using `memcpy`:
159198

160199
~~~{.c}
161-
size_t iio_buf_size = iio_buffer_end(buffer) - iio_buffer_start(buffer);
162-
size_t count = MAX(sizeof(samples_buffer), iio_buf_size);
163-
memcpy(iio_buffer_start(buffer), samples_buffer, count);
200+
size_t block_size = iio_block_end(block) - iio_block_start(block);
201+
size_t count = MIN(sizeof(samples_buffer), block_size);
202+
memcpy(iio_block_start(block), samples_buffer, count);
164203
~~~
165204

166-
Using `memcpy` to copy samples from the iio_buffer is <b>not recommended</b>.
167-
When capturing samples from an input device, you cannot assume that the iio_buffer object contains only the samples you're interested in.
205+
Using `memcpy` to copy samples from the iio_block is <b>not recommended</b>.
206+
When capturing samples from an input device, you cannot assume that the iio_block object contains only the samples you're interested in.
168207

169-
@subsubsection iterating_cb Iterating over the buffer with a callback
170-
Libiio provides a way to iterate over the buffer by registering a callback function, with the iio_buffer_foreach_sample() function.
208+
@subsubsection iterating_cb Iterating over a block with a callback
209+
Libiio provides a way to iterate over a block by registering a callback function, with the iio_block_foreach_sample() function.
171210

172-
The callback function will be called for each "sample slot" of the buffer,
173-
which will contain a valid sample if the buffer has been refilled,
211+
The callback function will be called for each "sample slot" of the block,
212+
which will contain a valid sample if the block has been dequeued from an input device,
174213
or correspond to an area where a sample should be stored if using an output device.
175214

176215
~~~{.c}
@@ -182,32 +221,35 @@ ssize_t sample_cb(const struct iio_channel *chn, void *src, size_t bytes, void *
182221
int main(void)
183222
{
184223
...
185-
iio_buffer_foreach_sample(buffer, sample_cb, NULL);
224+
iio_block_foreach_sample(block, mask, sample_cb, NULL);
186225
...
187226
}
188227
~~~
189228

190-
Note that the callback will be called in the order that the samples appear in the buffer,
229+
Note that the callback will be called in the order that the samples appear in the block,
191230
and only for samples that correspond to channels that were enabled.
192231

193232
@subsubsection iterating_for Iterating on the samples with a for loop
194-
This method allows you to iterate over the samples slots that correspond to one channel.
233+
This method allows you to iterate over the sample slots that correspond to one channel.
195234
As such, it is interesting if you want to process the data channel by channel.
196235

197-
It basically consists in a for loop that uses the functions iio_buffer_first(), iio_buffer_step() and iio_buffer_end():
236+
Use iio_block_first() to find the first sample for a given channel, iio_block_end() for the end,
237+
and iio_device_get_sample_size() for the step between consecutive samples:
198238

199239
~~~{.c}
200-
for (void *ptr = iio_buffer_first(buffer, channel);
201-
ptr < iio_buffer_end(buffer);
202-
ptr += iio_buffer_step(buffer)) {
240+
size_t step = iio_device_get_sample_size(dev, mask);
241+
242+
for (void *ptr = iio_block_first(block, channel);
243+
ptr < iio_block_end(block);
244+
ptr += step) {
203245
/* Use "ptr" to read or write a sample for this channel */
204246
}
205247
~~~
206248

207249
@subsubsection deinterleave Extracting from/to a second buffer
208250

209251
Finally, it is possible to use the iio_channel_read() and iio_channel_read_raw()
210-
functions to read samples from the iio_buffer to a second byte array.
252+
functions to read samples from an iio_block to a second byte array.
211253
The samples will be deinterleaved if needed.
212254
The "raw" variant will only deinterleave the samples, while the other variant will deinterleave and convert the samples.
213255

@@ -223,15 +265,15 @@ Libiio offers two functions that can be used to convert samples:
223265
- iio_channel_convert(), to convert from the hardware format
224266
- iio_channel_convert_inverse(), to convert to the hardware format.
225267

226-
Those two functions should always be used when manipulating the samples of the iio_buffer.
268+
Those two functions should always be used when manipulating the samples of an iio_block.
227269
The exception is when iio_channel_read() or iio_channel_write() are used, as the conversion is then done internally.
228270

229-
@subsection push Submitting the Buffer (output devices only)
230-
When 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().
231-
As soon as the buffer has been submitted, it can be reused to store new samples.
271+
@subsection push Enqueuing blocks (output devices only)
272+
When all the samples have been written to the iio_block object, you can submit the block to the hardware with a call to iio_block_enqueue().
273+
As soon as the block has been submitted, it can be dequeued and reused to store new samples.
232274

233-
If the iio_buffer object has been created with the "cyclic" parameter set, and the kernel driver supports cyclic buffers,
234-
the submitted buffer will be repeated until the iio_buffer is destroyed, and no subsequent call to iio_buffer_push() will be allowed.
275+
If the iio_block_enqueue() call is made with the "cyclic" parameter set, and the kernel driver supports cyclic buffers,
276+
the submitted block will be repeated until the buffer stream is closed, and no subsequent call to iio_block_enqueue() will be allowed.
235277

236278
@section advanced Advanced options
237279

tests/api/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The test suite covers all public APIs defined in `include/iio/iio.h`:
1212
- **Context Functions** (`test_context.c`) - Tests for `iio_context_*()` functions
1313
- **Device Functions** (`test_device.c`) - Tests for `iio_device_*()` functions
1414
- **Channel Functions** (`test_channel.c`) - Tests for `iio_channel_*()` functions
15-
- **Buffer Functions** (`test_buffer.c`) - Tests for `iio_buffer_*()` and `iio_device_create_buffer()`
15+
- **Buffer Functions** (`test_buffer.c`) - Tests for `iio_buffer_open()`, `iio_buffer_stream_*()`, and `iio_buffer_close()`
1616
- **Attribute Functions** (`test_attr.c`) - Tests for `iio_attr_*()` functions
1717
- **HWMON Support** (`test_hwmon.c`) - Tests for HWMON-specific functions
1818
- **Events** (`test_events.c`) - Tests for `iio_event_*()` functions

0 commit comments

Comments
 (0)