Open
Support zero-length I2C writes to emit a Start/Stop bus transaction#2582
Conversation
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
raffaeler
approved these changes
Jul 1, 2026
Contributor
There was a problem hiding this comment.
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.Writeto treatbuffer.Length == 0as a real bus transaction by passing a non-null pointer withlen = 0intoWriteReadCore. - Update
I2cDevice.WriteXML 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Writepreviously produced no bus activity (orError 22on the file-transfer path) because an empty span yields a null pointer underfixed, andWriteReadCoreskips write messages when the write pointer is null.Changes
UnixI2cBus.Write— On an empty buffer, pass a non-null placeholder pointer with length 0 soWriteReadCoreemits a zero-length write message (START + address + STOP). Non-empty writes are unchanged. The file-transfer fallback bus is covered automatically, since its existingwriteBuffer != nullbranch now runs with length 0.I2cDevice.Write— Documented that an empty buffer generates a bus transaction without transferring data bytes.Usage
Notes
No automated test is included: this path requires real
/dev/i2chardware, and the internalUnixI2cBushas no existing mockable test seam. Behavior was verified with a standalone harness — an empty write emits alen=0message; normal writes are unaffected.