Skip to content

drivers: add optional full-duplex transceive to DatagramDevice - #1077

Open
matpala wants to merge 3 commits into
ZigEmbeddedGroup:mainfrom
matpala:drivers/datagram-transceive
Open

matpala wants to merge 3 commits into
ZigEmbeddedGroup:mainfrom
matpala:drivers/datagram-transceive

Conversation

@matpala

@matpala matpala commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Adds optional full-duplex transfers to DatagramDevice, as discussed in #1069 and requested in #637.

  • Adds transceive(src, dst) and transceivev(write_chunks, read_chunks). Total write/read lengths must match; chunk boundaries may differ. Both return error.Unsupported when transceivev_fn is absent.
  • The new vtable entry defaults to null, preserving existing named-field initializers. The !void return type avoids the AVR issue affecting non-void error-union constants in Zig 0.17.0 (zig#37099).
  • TestDevice simulates the transfer with write/read, asserts equal total lengths when asserts are enabled, and returns IoError for a short reply.
  • Adds drivers.assert, controlled by a compile-time flag and able to panic in any optimization mode. MicroBuild forwards each firmware's asserts option, keeping it in sync with microzig.assert; standalone builds enable it with -Dasserts=true.
  • Adds tests for both methods, differing chunk boundaries, short replies, and unsupported devices. README documents the API and capability detection with @hasDecl for concrete types.

No port adapter implements the operation in this PR. I plan to add ATmega328P support in a follow-up for the nRF24L01+ driver, to receive STATUS alongside each command.

Comment thread drivers/src/base/DatagramDevice.zig Outdated

fn transceivev(ctx: *anyopaque, write_chunks: []const []const u8, read_chunks: []const []u8) (WriteError || ReadError)!void {
const read_len = chunks_len(read_chunks);
std.debug.assert(chunks_len(write_chunks) == read_len);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use microzig.assert. We often have to compile in release small, so the regular assert function doesn't give us enough flexibility on when to enable it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The standalone drivers module has no microzig import configured and runs its own tests independently. Since this is in TestDevice, I replaced the assertion with an error.IoError return before any transfer, so the check stays on in every build mode, and added a length-mismatch test that verifies nothing was sent.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, make an assert function for the driver framework that can be configured and comptime like the microzig assert. Pass down the flag that activates it in MicroBuild so they're enabled at the same time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added drivers.assert with a compile-time asserts flag: it panics on failure in any optimization mode when enabled and does nothing otherwise. MicroBuild forwards each firmware's asserts option, keeping it in sync with microzig.assert. Standalone driver tests enable it with zig build test -Dasserts=true from drivers/.

TestDevice.transceivev now uses it for the length precondition; I removed the test expecting IoError for mismatched lengths.

@matpala
matpala force-pushed the drivers/datagram-transceive branch from 8b98a62 to d9e4737 Compare October 12, 2026 17:58

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.

3 participants