Skip to content

Support zero-length I2C writes to emit a Start/Stop bus transaction#2582

Open
raffaeler with Copilot wants to merge 2 commits into
mainfrom
copilot/i2c-support-start-stop-without-sending
Open

Support zero-length I2C writes to emit a Start/Stop bus transaction#2582
raffaeler with Copilot wants to merge 2 commits into
mainfrom
copilot/i2c-support-start-stop-without-sending

Conversation

Copilot AI commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Some devices (e.g. the PN532 NFC reader) are woken up by an I2C transaction that carries no data — a bare START + device address + STOP. An empty Write previously produced no bus activity (or Error 22 on the file-transfer path) because an empty span yields a null pointer under fixed, and WriteReadCore skips write messages when the write pointer is null.

Changes

  • UnixI2cBus.Write — On an empty buffer, pass a non-null placeholder pointer with length 0 so WriteReadCore emits a zero-length write message (START + address + STOP). Non-empty writes are unchanged. The file-transfer fallback bus is covered automatically, since its existing writeBuffer != null branch now runs with length 0.
  • I2cDevice.Write — Documented that an empty buffer generates a bus transaction without transferring data bytes.

Usage

// Wake up a device that pings on an empty transaction (e.g. PN532)
device.Write(ReadOnlySpan<byte>.Empty);

Notes

No automated test is included: this path requires real /dev/i2c hardware, and the internal UnixI2cBus has no existing mockable test seam. Behavior was verified with a standalone harness — an empty write emits a len=0 message; normal writes are unaffected.

@dotnet-policy-service dotnet-policy-service Bot added the area-System.Device.Gpio Contains types for using general-purpose I/O (GPIO) pins label Jul 1, 2026
Copilot AI changed the title [WIP] Add I2C support for Start and Stop without sending bytes Support zero-length I2C writes to emit a Start/Stop bus transaction Jul 1, 2026
Copilot AI requested a review from raffaeler July 1, 2026 14:21
@raffaeler raffaeler marked this pull request as ready for review July 1, 2026 14:35
Copilot AI review requested due to automatic review settings July 1, 2026 14:35

Copilot AI left a comment

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.

Pull request overview

Enables “empty I2C write” semantics on Unix so callers can intentionally emit a START + address + STOP transaction (no data bytes), which some devices use as a wake-up signal.

Changes:

  • Update UnixI2cBus.Write to treat buffer.Length == 0 as a real bus transaction by passing a non-null pointer with len = 0 into WriteReadCore.
  • Update I2cDevice.Write XML documentation to mention empty-buffer behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/System.Device.Gpio/System/Device/I2c/UnixI2cBus.cs Adds explicit empty-write handling so WriteReadCore emits a zero-length write message instead of skipping the write.
src/System.Device.Gpio/System/Device/I2c/I2cDevice.cs Updates Write(ReadOnlySpan<byte>) documentation to describe empty-buffer semantics.

Comment on lines +73 to +74
/// An empty buffer generates a transaction on the bus (Start condition, device address and Stop
/// condition) without transferring any data byte, which some devices require to be woken up.
Comment on lines +126 to +129
// An empty write still generates a transaction on the bus (Start condition, device
// address, Stop condition) without transferring any data byte. Some devices (e.g. the
// PN532 NFC reader) rely on this to be woken up. A non-null pointer is required so that
// WriteReadCore emits a zero-length write message instead of skipping it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Device.Gpio Contains types for using general-purpose I/O (GPIO) pins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I2C to support Start and Stop without sending/reading bytes

3 participants