Skip to content
Draft
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
17 changes: 7 additions & 10 deletions src/devices/Mcp23xxx/SpiAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ public SpiAdapter(SpiDevice device, int deviceAddress)
/// <inheritdoc/>
public override void Read(byte registerAddress, Span<byte> buffer)
{
// Include OpCode and Register Address.
Span<byte> writeBuffer = stackalloc byte[]
{
GetOpCode(_deviceAddress, isReadCommand: true),
registerAddress
};
int fullLength = buffer.Length + 2;

Span<byte> readBuffer = stackalloc byte[buffer.Length + 2];
// Include OpCode and Register Address.
// writeBuffer must be the same length as readBuffer for TransferFullDuplex.
Span<byte> writeBuffer = stackalloc byte[fullLength];
writeBuffer[0] = GetOpCode(_deviceAddress, isReadCommand: true);
writeBuffer[1] = registerAddress;

// Should this also contain the op code and register?
// Why are we transferring full duplex if we only really
// need to read?
Span<byte> readBuffer = stackalloc byte[fullLength];
_device.TransferFullDuplex(writeBuffer, readBuffer);

// First 2 bytes are from sending OpCode and Register Address.
Expand Down
5 changes: 4 additions & 1 deletion src/devices/Mcp23xxx/tests/Mcp23xxxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public SpiDeviceMock(int ports)

public override void TransferFullDuplex(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
Write(writeBuffer);
// Only pass the command bytes (OpCode + RegisterAddress) to Write.
// Remaining bytes in writeBuffer are don't-care padding for full-duplex reads
// and should not be written as register data.
Write(writeBuffer.Slice(0, Math.Min(writeBuffer.Length, 2)));
Read(readBuffer);
}

Expand Down