/// <summary> /// Opens a SPI slave device. /// </summary> /// <param name="bus">The SPI bus number.</param> /// <param name="chip">The SPI chip select number.</param> public SpiDevice(int bus, int chip) { var ret = NativeSpi.Open(bus, chip, out _handle); if (ret != Internals.Errors.ErrorCode.None) { throw ExceptionFactory.CreateException(ret); } }
/// <summary> /// Writes the bytes data to the SPI slave device. /// </summary> /// <param name="data">The data buffer to write.</param> public void Write(byte[] data) { var length = Convert.ToUInt32(data.Length); var ret = NativeSpi.Write(_handle, data, length); if (ret != Internals.Errors.ErrorCode.None) { throw ExceptionFactory.CreateException(ret); } }
/// <summary> /// Reads the bytes data from the SPI slave device. /// </summary> /// <param name="buffer">The Data buffer.</param> public void Read(byte[] buffer) { var length = Convert.ToUInt32(buffer.Length); var ret = NativeSpi.Read(_handle, buffer, length); if (ret != Internals.Errors.ErrorCode.None) { throw ExceptionFactory.CreateException(ret); } }
/// <summary> /// Disposes the Spi. /// </summary> protected virtual void Dispose(bool disposing) { if (_disposed) { return; } NativeSpi.Close(_handle); _disposed = true; }
/// <summary> /// Exchanges the bytes data to the SPI slave device. /// writeBuffer.Length and readBuffer.Length must be equal. /// </summary> /// <param name="writeBuffer">Array containing data to write to the device.</param> /// <param name="readBuffer">Array containing data read from the dievice.</param> public void TransferSequential(byte[] writeBuffer, byte[] readBuffer) { if (writeBuffer.Length != readBuffer.Length) { throw new Exception("writeBuffer.Length is not equal to readBuffer.Length"); } var buffersLength = Convert.ToUInt32(writeBuffer.Length); var ret = NativeSpi.Transfer(_handle, writeBuffer, readBuffer, buffersLength); if (ret != Internals.Errors.ErrorCode.None) { throw ExceptionFactory.CreateException(ret); } }