/// <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(); } }
/// <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]); }
static bool ClearDfrFrameBuffer(IntPtr deviceHandle) { return(IoCtl.DeviceIoControl( deviceHandle, DfrHostIo.IOCTL_DFR_CLEAR_FRAMEBUFFER, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero )); }
public void DumpBuffer(long sectorBegin, long sectorEnd, ref byte[] buffer) { long distanceToMove = sectorBegin * BytesPerSector; if (sectorEnd > TotalSectors) { throw new Exception("DataReceiver.DumpBuffer_PhysicalDrive: Last sector is bigger than disk size"); } uint numBytesToRead = (uint)((sectorEnd - sectorBegin) * BytesPerSector); IoCtl.SetFilePointerEx(Handle, distanceToMove, out long moveToHigh, 0); IoCtl.ReadFile(Handle, buffer, numBytesToRead, out uint numBytesRead, IntPtr.Zero); //Array.Copy(KOSTIL, 0, buffer, 512, numBytesToRead); }
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; }
private void Open() { try { Handle = IoCtl.CreateFile( @"\\.\PHYSICALDRIVE" + DiskId.ToString(), IoCtl.GENERIC_READ, 0, IntPtr.Zero, IoCtl.OPEN_EXISTING, 0, IntPtr.Zero); if (Handle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } catch (Exception ex) { throw ex; } }
static unsafe bool DrawBitmap(IntPtr deviceHandle, string file, ushort x, ushort y) { ushort width = 0, height = 0; var bResult = false; try { using (var bitmap = new Bitmap(file)) { if (bitmap.Width > ushort.MaxValue || bitmap.Height > ushort.MaxValue) { System.Console.Write("Image too large"); return(false); } width = (ushort)bitmap.Width; height = (ushort)bitmap.Height; int requestSize = bitmap.Width * bitmap.Height * 3 + Marshal.SizeOf(typeof(DFR_HOSTIO_UPDATE_FRAMEBUFFER_HEADER)); // Memory stream for FrameBuffer update request IntPtr requestPtr = Marshal.AllocHGlobal(requestSize); if (requestPtr == IntPtr.Zero) { System.Console.Write("Failed to allocate memory for FrameBuffer"); return(false); } byte *requestBytePtr = (byte *)requestPtr.ToPointer(); UnmanagedMemoryStream requestStream = new UnmanagedMemoryStream(requestBytePtr, requestSize, requestSize, FileAccess.Write); using (var binaryWriter = new BinaryWriter(requestStream)) { binaryWriter.Write(x); binaryWriter.Write(y); binaryWriter.Write(width); binaryWriter.Write(height); binaryWriter.Write(DfrHostIo.DFR_FRAMEBUFFER_FORMAT); binaryWriter.Write((uint)0); for (int w = 0; w < bitmap.Width; w++) { for (int h = bitmap.Height - 1; h >= 0; h--) { var pixel = bitmap.GetPixel(w, h); binaryWriter.Write(pixel.R); binaryWriter.Write(pixel.G); binaryWriter.Write(pixel.B); } } binaryWriter.Flush(); bResult = IoCtl.DeviceIoControl( deviceHandle, DfrHostIo.IOCTL_DFR_UPDATE_FRAMEBUFFER, requestPtr, requestSize, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero ); } Marshal.FreeHGlobal(requestPtr); } } catch (Exception exc) { System.Console.Write($"Exception caught: {exc}"); } return(bResult); }
unsafe static void Main(string[] args) { if (args.Length < 1) { ShowHelp(); return; } // Find DFR Device var instancePath = DfrDeviceDiscovery.FindDfrDevice(); if (string.IsNullOrEmpty(instancePath)) { System.Console.WriteLine("No DFR device found"); return; } System.Console.WriteLine($"Found DFR instance {instancePath}"); var deviceHandle = IoCtl.CreateFile( instancePath, FileAccess.Write, FileShare.None, IntPtr.Zero, FileMode.Open, FileOptions.None, IntPtr.Zero ); if (deviceHandle == IntPtr.Zero) { System.Console.WriteLine("Failed to open DFR device"); return; } bool bResult = false; bool commandFound = true; switch (args[0].ToLowerInvariant()) { case "clear": bResult = ClearDfrFrameBuffer(deviceHandle); break; case "draw": ushort x = 0, y = 0; // Optional X if (args.Length >= 3) { if (ushort.TryParse(args[2], out ushort tX)) { x = tX; } } // Optional Y if (args.Length >= 4) { if (ushort.TryParse(args[3], out ushort tY)) { y = tY; } } bResult = DrawBitmap(deviceHandle, args[1], x, y); break; default: ShowHelp(); commandFound = false; break; } if (!bResult && commandFound) { System.Console.WriteLine("Failed to run the command"); } IoCtl.CloseHandle(deviceHandle); }