Skip to content

zephyr: buffer streaming over RTIO - #1631

Open
MaureenHelm wants to merge 9 commits into
analogdevicesinc:mainfrom
MaureenHelm:zephyr-streaming
Open

MaureenHelm wants to merge 9 commits into
analogdevicesinc:mainfrom
MaureenHelm:zephyr-streaming

Conversation

@MaureenHelm

Copy link
Copy Markdown
Collaborator

PR Description

Adds buffer support to the Zephyr backend, so clients can stream samples from Zephyr devices through the standard libiio block API. Sensors are the first device class. It is gated by CONFIG_LIBIIO_STREAMING (default n).

Zephyr's sensor streaming is built on RTIO, which works much like io_uring:

  • A reader submits requests to a submission queue and collects results from a completion queue.
  • Requests can draw their data buffers from a mempool owned by the RTIO context. This is similar to io_uring provided buffers.
  • A request can be multishot. It stays armed and posts a completion every time the device fires a trigger, such as a FIFO watermark. This is similar to io_uring multishot receives.

The libiio backend uses this model directly:

  • Dequeue submits reads and decodes completions into the block. For a multishot device, it drains the completions that have built up since the last dequeue.
  • Cancel or disable cancels the outstanding request.

Design decisions

  • No dynamic allocation. Block memory, buffer descriptors and RTIO mempools are sized at build time from devicetree and Kconfig. A client can exhaust the budget but can never make the device allocate.
  • Devicetree picks the pacing. A device with stream-triggers is armed with a multishot request. A device without them is filled by on-demand reads.
  • Processed samples. Zephyr decoders emit q31 values in SI units, so samples are packed as 32-bit milli-units, not raw codes plus a scale.
  • Class-agnostic core. Drivers implement a few optional iio_device ops. RTIO and the buffer lifecycle live in stream.c.
  • Not supported yet: output buffers, cyclic buffers, and more than one open buffer per device.

Future work

PR Type

  • Bug fix (a change that fixes an issue)
  • New feature (a change that adds new functionality)
  • Breaking change (a change that affects other repos or cause CIs to fail)

PR Checklist

  • I have conducted a self-review of my own code changes
  • I have commented new code, particularly complex or unclear areas
  • I have checked that I did not introduce new warnings or errors (CI output)
  • I have checked that components that use libiio did not get broken
  • I have updated the documentation accordingly (GitHub Pages, READMEs, etc)

The thread, mutex and condvar pool defaults only covered the network
transport, so a USB build fell back to the single-interpreter sizes and
had too few threads for its pipes. Give USB the network defaults.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Port the attribute path from sensor_sample_fetch()/sensor_channel_get()
to sensor_read() and the sensor decoder API.

Channels are tracked by struct sensor_chan_spec, which also keys the
cached value so two channels of the same type no longer alias. Decoders
that only answer for SENSOR_CHAN_ACCEL_XYZ, such as the ADXL345's, fall
back to decoding XYZ and picking out the requested axis.

Give the read's RTIO context two SQEs: the executor posts a completion
before it frees the SQE, so a reader that outranks the completing
thread would otherwise find none free on its next read.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Register an input buffer for every device that has scan elements, so
the context XML describes the buffer explicitly. The first scan element
decides its direction.

Opening the buffer still fails with -ENOSYS until the buffer ops are
implemented.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Check that the backend gives a buffer only to a device with scan
elements, and that the first scan element sets the buffer's direction.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Implement the buffer and block ops behind a new LIBIIO_STREAMING option,
default n, for iio_device drivers that return streaming resources from a
new get_stream op. Implement it, with submit_read, frame_count and
decode, for the sensor class.

Blocks come from a per-device k_mem_slab sized at build time by the new
buffer-block-count and buffer-block-size-max devicetree properties, and
buffer descriptors from a system-wide slab, so a client cannot make the
device allocate more than was budgeted. Devices that cannot stream
reserve nothing.

Each dequeue submits reads until the block is full, packing every
enabled channel in milli-units at the offset libiio's iio_block_first()
expects, saturated to the channel's width. Sensor channels become 32
bits wide so full-scale readings fit. Frames a capture holds past
the end of the block are kept for the next dequeue. A capture that does
not arrive within a new Kconfig timeout fails the dequeue, and
cancelling or disabling the buffer wakes a blocked dequeue and waits
for it.

Captures use a mempool RTIO context of their own, so an attribute read
cannot take one. That context allows one open buffer per device; a
second open fails with -EBUSY. The thread, mutex and condvar pool
defaults grow to cover an open buffer.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Add the libiio.stream ztest with a fixture sensor whose decoder only
answers for the combined XYZ channel and yields known frames. Cover the
block budget, sample packing, scan offsets against libiio's
iio_block_first(), and one-shot capture including its refusals,
cancellation and timeout.

Before each test, check that no block or device is still held, so a
leak fails the next test by name.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Devices that list stream-triggers in devicetree are armed with a
multishot RTIO request on the first dequeue, so samples arrive at the
device's own trigger rate instead of the client's dequeue rate.

Stopping the stream waits, within a new Kconfig timeout, for the
cancelled request to complete. A stream the device ends on a full
mempool is re-armed once, so the overrun shows as a gap in the samples.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Configure the accelerometers on iio_pmod_acl, iio_eval_adxl362_ardz and
iio_x_nucleo_iks4a1 to stream on their FIFO watermark.

The lsm6dsv16x interrupt is wired to INT1 on D5, not INT2 on D4 as the
upstream shield overlay describes, so override int1-gpios and drdy-pin.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>
Add a pytest suite that streams each supported accelerometer shield,
alone and two at once, through the Python bindings over IIOD USB, and
checks the sample rate and magnitude. Captured samples are logged to a
CSV file in the build directory.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Maureen Helm <maureen.helm@analog.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant