/// <summary> /// /// </summary> /// <param name="address"></param> /// <returns></returns> public byte ReadByte(byte address) { //Console.WriteLine(" spiBufTx = {0} {1} {2}", CMD_READ, address, 0); Marshal.Copy(new byte[] { CMD_READ, address, 0 }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength); Marshal.Copy(new byte[] { 0, 0, 0 }, 0, this._rxBufferPtr, HardwareSpiDevice.TxRxBufferLength); // build the command var cmd = SpiDev.SPI_IOC_MESSAGE(1); // build the spi transfer structure var spi = new SpiDev.spi_ioc_transfer { tx_buf = (UInt64)this._txBufferPtr.ToInt64(), rx_buf = (UInt64)this._rxBufferPtr.ToInt64(), len = HardwareSpiDevice.TxRxBufferLength, delay_usecs = this.SpiDelay, speed_hz = this.MaxSpeedHz, bits_per_word = (byte)this.BitsPerWord }; // call the native method var result = IoCtl.ioctl(this.DeviceHandle, cmd, ref spi); if (result < 0) { var error = Mono.Unix.Native.Stdlib.GetLastError(); } // return the result. every byte transmitted results in a // data or dummy byte received, so we have to skip the // leading dummy bytes to read out actual data bytes. var bufOut = new byte[HardwareSpiDevice.TxRxBufferLength]; Marshal.Copy(this._txBufferPtr, bufOut, 0, bufOut.Length); Marshal.Copy(this._rxBufferPtr, bufOut, 0, bufOut.Length); return(bufOut[2]); }
/// <summary> /// Write a value to the SPI bus. /// </summary>< /// <param name="address"></param> /// <param name="value"></param> public void WriteByte(byte address, byte value) { byte buffer = SPI_SLAVE_ID; buffer |= (SPI_SLAVE_ADDR << 1 & SPI_SLAVE_MSG_END) | SPI_SLAVE_WRITE; Marshal.Copy(new byte[] { buffer, address, value }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength); Marshal.Copy(new byte[] { 0, 0, 0 }, 0, this._rxBufferPtr, HardwareSpiDevice.TxRxBufferLength); // build the command var cmd = SpiDev.SPI_IOC_MESSAGE(1); // build the spi transfer structure var spi = new SpiDev.spi_ioc_transfer { tx_buf = (UInt64)this._txBufferPtr.ToInt64(), rx_buf = (UInt64)this._rxBufferPtr.ToInt64(), len = HardwareSpiDevice.TxRxBufferLength, delay_usecs = this.SpiDelay, speed_hz = this.MaxSpeedHz, bits_per_word = (byte)this.BitsPerWord }; // call the native method var result = IoCtl.ioctl(this.DeviceHandle, cmd, ref spi); if (result < 0) { var error = Mono.Unix.Native.Stdlib.GetLastError(); } }
public void SetMaxSpeedHz(uint maxSpeedHz) { var value = maxSpeedHz; var result = 0; result = IoCtl.ioctl(this.DeviceHandle, SpiDev.SPI_IOC_WR_MAX_SPEED_HZ, ref value); if (result < 0) { throw new InvalidOperationException(string.Format("Failed to set max speed hz - error {0}", result)); } // every tx results in an rx, so we have to read after every write result = IoCtl.ioctl(this.DeviceHandle, SpiDev.SPI_IOC_RD_MAX_SPEED_HZ, ref value); if (result < 0) { throw new System.InvalidOperationException(string.Format("Failed to get max speed hz - error {0}.", result)); } this.MaxSpeedHz = maxSpeedHz; }
public void SetBitsPerWord(byte bitsPerWord) { var value = (uint)bitsPerWord; var result = 0; result = IoCtl.ioctl(this.DeviceHandle, SpiDev.SPI_IOC_WR_BITS_PER_WORD, ref value); if (result < 0) { throw new InvalidOperationException(string.Format("Failed to set bits per word - error {0}", result)); } // every tx results in an rx, so we have to read after every write result = IoCtl.ioctl(this.DeviceHandle, SpiDev.SPI_IOC_RD_BITS_PER_WORD, ref value); if (result < 0) { throw new InvalidOperationException(string.Format("Failed to get bits per word - error {0}", result)); } this.BitsPerWord = bitsPerWord; }
public void SetMode(uint mode) { var value = mode; var result = 0; result = IoCtl.ioctl(this.DeviceHandle, SpiDev.SPI_IOC_WR_MODE, ref value); if (result < 0) { throw new InvalidOperationException(string.Format("Failed to set mode - error {0}", result)); } // every tx results in an rx, so we have to read after every write) result = IoCtl.ioctl(this.DeviceHandle, SpiDev.SPI_IOC_RD_MODE, ref value); if (result < 0) { throw new InvalidOperationException(string.Format("Failed to get mode - error {0}", result)); } this.Mode = mode; }