/// <summary> /// Close the SPI port /// </summary> public void CloseChannel() { if (ftHandle == IntPtr.Zero) { return; } SPIDllDriver.SPI_CloseChannel(ftHandle); ftHandle = IntPtr.Zero; }
/// <summary> /// Get the Information of Channel /// </summary> /// <param name="channel">The index of channel</param> /// <exception cref="FTDIException">When Get channel information failed</exception> /// <returns>The Device Info Class</returns> public DeviceInfo GetChannelInfo(uint channel) { FTDeviceListInfoNode node = new FTDeviceListInfoNode(); status = SPIDllDriver.SPI_GetChannelInfo(channel, ref node); if (status != FTStatus.OK) { throw new FTDIException(status); } return(node); }
public void SPIInit(uint clock = 30000000, byte latency = 1, SPIConfigOption options = SPIConfigOption.MODE0 | SPIConfigOption.CS_DBUS3 | SPIConfigOption.CS_ACTIVELOW, uint pin = 0) { channelconfig = new ChannelConfig(); channelconfig.ClockRate = clock; channelconfig.LatencyTimer = latency; channelconfig.configOptions = (uint)options; channelconfig.Pin = pin; status = SPIDllDriver.SPI_InitChannel(ftHandle, ref channelconfig); if (status != FTStatus.OK) { throw new FTDIException(status, "Error in initializing spi channel"); } }
/// <summary> /// Read From SPI device /// </summary> /// <param name="length">The length of data to be read</param> /// <param name="buffer">The data to be stored</param> /// <exception cref="FTDIException">When failed to read from the spi channel.</exception> /// <returns>the real transfered length</returns> public uint Read(uint length, out byte[] buffer) { buffer = new byte[length]; uint sizetransfered = 0; if (ftHandle == IntPtr.Zero) { throw new NullReferenceException("The SPI Interface is not initialized!"); } status = SPIDllDriver.SPI_Read(ftHandle, ref buffer, (uint)length, ref sizetransfered, (uint)(SPITransferOption.SIZE_IN_BYTES | SPITransferOption.CHIPSELECT_ENABLE | SPITransferOption.CHIPSELECT_DISABLE)); if (status != FTStatus.OK) { throw new FTDIException(status, "Error is Reading From SPI bus!"); } return(sizetransfered); }
/// <summary> /// Send data to SPI bus /// </summary> /// <param name="buffer">The data to be send</param> /// <param name="length">The length of data to be send, this number must smaller than the legnth of data</param> /// <returns>The real length send out</returns> public uint Write(byte[] buffer, uint length = 0) { uint sizetransfered = 0; if (buffer.Length < length) { throw new ArgumentOutOfRangeException("length", "The length given in arguments is out of data length"); } if (length == 0) { length = (uint)buffer.Length; } if (ftHandle == IntPtr.Zero) { throw new NullReferenceException("The SPI Interface is not initialized!"); } status = SPIDllDriver.SPI_Write(ftHandle, buffer, length, ref sizetransfered, (uint)(SPITransferOption.SIZE_IN_BYTES | SPITransferOption.CHIPSELECT_ENABLE | SPITransferOption.CHIPSELECT_DISABLE)); if (status != FTStatus.OK) { throw new FTDIException(status, "Error is Send data to SPI bus!"); } return(sizetransfered); }
/// <summary> /// Read Write data on SPI /// </summary> /// <param name="indata">data send to SPI</param> /// <param name="outdata">data read from SPI</param> /// <param name="length">the lenght to send/receive </param> /// <returns>the real numbers of data sent/received </returns> public uint ReadWrite(byte[] indata, out byte[] outdata, uint length) { uint sizetransfered = 0; outdata = new byte[length]; if (indata.Length < length) { throw new ArgumentOutOfRangeException("length", "The length given in arguments is out of data length"); } if (length == 0) { length = (uint)indata.Length; } if (ftHandle == IntPtr.Zero) { throw new NullReferenceException("The SPI Interface is not initialized!"); } status = SPIDllDriver.SPI_ReadWrite(ftHandle, indata, ref outdata, length, ref sizetransfered, (uint)(SPITransferOption.SIZE_IN_BYTES | SPITransferOption.CHIPSELECT_ENABLE | SPITransferOption.CHIPSELECT_DISABLE)); if (status != FTStatus.OK) { throw new FTDIException(status, "Error in ReadWrite data on SPI bus!"); } return(sizetransfered); }