bool ISpiDriver.Xaction_nWrite8_nRead8(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); } byte[] writeData; if (Transaction.ReadPtr == IntPtr.Zero) { writeData = new byte[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 byte[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.Copy(Transaction.WritePtr, writeData, 0, Transaction.WriteCount); // 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]; } byte[] 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.MarshalBytes(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); } 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_nWrite8_nRead8(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); } byte[] writeData; int length = Transaction.ReadCount + Transaction.ReadStartOffset; if (length < Transaction.WriteCount) { length = Transaction.WriteCount; } writeData = new byte[length]; Marshal.Copy(Transaction.WritePtr, writeData, 0, Transaction.WriteCount); // 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]; } byte[] readData = _spiDeviceCurrent.DeviceWrite(writeData); if (Transaction.ReadPtr != IntPtr.Zero) { Utility.MarshalBytes(Transaction.ReadPtr, Transaction.ReadCount, Transaction.ReadStartOffset, readData); } return(true); }
bool ISpiDriver.nWrite8_nRead8(ref SpiConfiguration Configuration, IntPtr Write8, int WriteCount, IntPtr Read8, int ReadCount, int ReadStartOffset) { Configuration configuration = SpiConfigurationToConfiguration(Configuration); bool res = false; try { if (StartTransaction(configuration)) { SpiXaction xaction = new SpiXaction(Write8, WriteCount, Read8, ReadCount, ReadStartOffset); res = ((ISpiDriver)this).Xaction_nWrite8_nRead8(ref xaction); } } finally { res &= EndTransaction(configuration); } return(res); }