public void Send_ReadRxBuffer_Instruction(byte instructionFormat, RxBufferAddressPointer addressPointer, int byteCount) { byte[] expectedWriteBuffer = new byte[byteCount + 1]; expectedWriteBuffer[0] = instructionFormat; var mcp25xxxSpiDevice = new Mcp25xxxSpiDevice(); Mcp25xxx mcp25xxx = new Mcp25625(mcp25xxxSpiDevice); mcp25xxx.ReadRxBuffer(addressPointer, byteCount); Assert.Equal(expectedWriteBuffer, mcp25xxxSpiDevice.LastWriteBuffer); Assert.Equal(byteCount, mcp25xxxSpiDevice.LastReadBuffer.Length - 1); }
private static void ReadRxBuffer(Mcp25xxx mcp25xxx, RxBufferAddressPointer addressPointer, int byteCount) { byte[] data = mcp25xxx.ReadRxBuffer(addressPointer, byteCount); Console.Write($"{addressPointer,10}: "); foreach (byte value in data) { Console.Write($"0x{value:X2} "); } Console.WriteLine(); }
/// <summary> /// When reading a receive buffer, reduces the overhead of a normal READ /// command by placing the Address Pointer at one of four locations for the receive buffer. /// </summary> /// <param name="addressPointer">The Address Pointer to one of four locations for the receive buffer.</param> /// <param name="byteCount">Number of bytes to read. This must be one or more to read.</param> /// <returns>The value of address read.</returns> public byte[] ReadRxBuffer(RxBufferAddressPointer addressPointer, int byteCount = 1) { if (byteCount < 1) { throw new ArgumentException($"Invalid number of bytes {byteCount}.", nameof(byteCount)); } const int StackThreshold = 31; // Usually won't read more than this at a time. Span <byte> writeBuffer = byteCount < StackThreshold ? stackalloc byte[byteCount + 1] : new byte[byteCount + 1]; Span <byte> readBuffer = byteCount < StackThreshold ? stackalloc byte[byteCount + 1] : new byte[byteCount + 1]; // This instruction has a base value of 0x90. // The 2nd and 3rd bits are used for the pointer address for reading. writeBuffer[0] = (byte)((byte)InstructionFormat.ReadRxBuffer | ((byte)addressPointer << 1)); _spiDevice.TransferFullDuplex(writeBuffer, readBuffer); return(readBuffer.Slice(1).ToArray()); }