bool ISpiDriver.Xaction_nWrite16_nRead16(ref SpiXaction Transaction) { if (Transaction.WriteCount <= 0) { Debug.Assert(false, "Transaction.WriteCount is 0."); Trace.WriteLine("Error: The write buffer in a SPI transaction cannot be empty."); return(false); } if (Transaction.WritePtr == IntPtr.Zero) { Debug.Assert(false, "Transaction.write buffer is null."); Trace.WriteLine("Error: The write buffer in a SPI transaction cannot be null."); return(false); } if ((Transaction.ReadCount > 0) && (Transaction.ReadPtr == IntPtr.Zero)) { Debug.Assert(false, "Transaction.read buffer is null."); Trace.WriteLine("Error: The read buffer in a SPI transaction cannot be null."); return(false); } ushort[] writeData; int length = Transaction.ReadCount + Transaction.ReadStartOffset; if (length < Transaction.WriteCount) { length = Transaction.WriteCount; } writeData = new ushort[length]; // marshal the pointer to an array for (int i = 0; i < Transaction.WriteCount; i++) { writeData[i] = (ushort)Marshal.ReadInt16(Transaction.WritePtr, i * 2); } // repeat the last byte if neccessary to fill up writeData for (int i = Transaction.WriteCount; i < writeData.Length; i++) { writeData[i] = writeData[Transaction.WriteCount - 1]; } ushort[] readData = _spiDeviceCurrent.DeviceWrite(writeData); if (Transaction.ReadPtr != IntPtr.Zero) { Utility.MarshalUshort(Transaction.ReadPtr, Transaction.ReadCount, Transaction.ReadStartOffset, readData); } return(true); }
bool ISpiDriver.Xaction_nWrite16_nRead16(ref SpiXaction Transaction) { if (Transaction.WriteCount <= 0) { Debug.Assert(false, "Transaction.WriteCount is 0."); Trace.WriteLine("Error: The write buffer in a SPI transaction cannot be empty."); return(false); } ushort[] writeData; if (Transaction.ReadPtr == IntPtr.Zero) { writeData = new ushort[Transaction.WriteCount]; } else { if (Transaction.ReadStartOffset > Transaction.WriteCount || Transaction.ReadStartOffset < 0) { Debug.Assert(false, "ReadStartOffset is invalid"); Trace.WriteLine("Error: ReadStartOffset of the SPI transaction is invalid."); return(false); } writeData = new ushort[Transaction.ReadCount + Transaction.ReadStartOffset]; if (writeData.Length < Transaction.WriteCount) { Debug.Assert(false, "The read count (including offset) needs to be no less than the write count."); Trace.WriteLine("Error: Read count (including offset) should be no less than the write count in a SPI transaction."); return(false); } } // marshal the pointer to an array for (int i = 0; i < Transaction.WriteCount; i++) { writeData[i] = (ushort)Marshal.ReadInt16(Transaction.WritePtr, i * 2); } // repeat the last byte if neccessary to fill up writeData for (int i = Transaction.WriteCount; i < writeData.Length; i++) { writeData[i] = writeData[Transaction.WriteCount - 1]; } ushort[] readData = _spiDeviceCurrent.DeviceWrite(writeData); if (readData.Length != writeData.Length) { Debug.Assert(false, "read data length expected to be write data length"); Trace.WriteLine("Error: Read data and write data should have the same length in a SPI transaction."); return(false); } if (Transaction.ReadPtr != IntPtr.Zero) { Utility.MarshalUshort(Transaction.ReadPtr, Transaction.ReadCount, Transaction.ReadStartOffset, readData); } return(true); }