Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/System.Device.Gpio/System/Device/I2c/I2cDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public virtual unsafe void WriteByte(byte value)
/// <param name="buffer">
/// The buffer that contains the data to be written to the I2C device.
/// The data should not include the I2C device address.
/// 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 +73 to +74
/// </param>
public abstract void Write(ReadOnlySpan<byte> buffer);

Expand Down
11 changes: 11 additions & 0 deletions src/System.Device.Gpio/System/Device/I2c/UnixI2cBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ internal unsafe void Write(int deviceAddress, ReadOnlySpan<byte> buffer)
throw new ArgumentException($"{nameof(buffer)} length is too long.", nameof(buffer));
}

if (buffer.Length == 0)
{
// 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.
Comment on lines +126 to +129
byte placeholder = 0;
WriteReadCore((ushort)deviceAddress, &placeholder, null, 0, 0);
return;
}

fixed (byte* writeBufferPointer = buffer)
{
WriteReadCore((ushort)deviceAddress, writeBufferPointer, null, (ushort)buffer.Length, 0);
Expand Down
Loading