public override EEPROM_BUFFER ReadPage(int addr, int len = EEPROM_BASE_CLASS.DEFAULT_PAGE_SIZE) { var r = new EEPROM_BUFFER(len); #if NUSBIO2 var inBuffer = new List <byte>() { (byte)(pageAddr >> 8), (byte)(pageAddr & 0xFF) }; r.Buffer = new byte[len]; // Must pre allocate the buffer for now r.Succeeded = Program.HelloWorld.__I2C_Helper_WriteRead(base.DeviceId, inBuffer.ToArray(), r.Buffer) == 1; return(r); #else #if OPTIMIZE_I2C_CALL // This method is faster because the I2C write and read operations are // combined in one USB buffer r.Succeeded = this._i2c.Send16BitsAddressAndReadBuffer(addr, len, r.Buffer); #else // Slower method because we have one USB operation for the I2C write // and one USB operation for the I2C read // The transfer of the data per say is the same var tmpArray = new byte[2]; if (this._i2c.WriteBuffer(new byte[2] { (byte)(addr >> 8), (byte)(addr & 0xFF) })) { r.Succeeded = this._i2c.ReadBuffer(len, r.Buffer); } #endif #endif return(r); }
public static EEPROM_BUFFER ReadPageOptimized_SendReadData (GpioSequence spiSeq, int startByteToSkip, int byteToRead, SPIEngine spi) { var nusbio = spi.Nusbio; var eb = new EEPROM_BUFFER(); if (spiSeq.Send(nusbio)) { var inBuffer = spiSeq.GetInputBuffer(nusbio).ToList(); if (startByteToSkip == 0) { inBuffer.RemoveAt(0); // Extra byte from somewhere } if (startByteToSkip > 0) { var offSetToSkip = SPIEngine.BYTES_PER_BIT * 8 * startByteToSkip; // Skip the first 3 bytes, bitbanging the command and 16 bit address inBuffer = inBuffer.GetRange(offSetToSkip, inBuffer.Count - offSetToSkip); } var buffer = __spi_buf_r(byteToRead, inBuffer, spi); for (var i = 0; i < startByteToSkip; i++) // Remove the last startByteToSkip because they were not yet sent { buffer.RemoveAt(buffer.Count - 1); } eb.Buffer = buffer.ToArray(); eb.Succeeded = true; } return(eb); }
/* * Write enable * 10011000 * Write Disable * 10000000 * Write * //dt1 = 00101000 | ((adrs & 0000011100000000) >> 8) ; * //dt2 = (adrs & 0000000011111111) ; * Read * //dt1 = 00110000 | ((adrs & 0000011100000000) >> 8) ; * //dt2 = (adrs & 0b0000000011111111) ; */ public override EEPROM_BUFFER ReadPage(int addr, int len = -1) { if (len == -1) { len = PAGE_SIZE; } var eb = new EEPROM_BUFFER(); int dt1, dt2; //dt1 = 0b00110000 | ((adrs & 0b0000011100000000) >> 8) ; //dt2 = (adrs & 0b0000000011111111) ; dt1 = 0x30 | ((addr & 0x700) >> 8); dt2 = (addr & 0xFF); var spiBufferWrite = new List <byte>() { (byte)dt1, (byte)dt2 }; var spiBufferRead = GetEepromApiDataBuffer(len); var buffer = new List <byte>(); buffer.AddRange(spiBufferWrite); buffer.AddRange(spiBufferRead); var r = this.SpiTransfer(buffer); if (r.Succeeded) { eb.Succeeded = true; eb.Buffer = r.Buffer.GetRange(spiBufferWrite.Count, r.Buffer.Count - spiBufferWrite.Count).ToArray(); } return(eb); }
public override EEPROM_BUFFER ReadPage(int addr, int len = -1) { if (len == -1) { len = PAGE_SIZE; } var eb = new EEPROM_BUFFER(); var spiBufferWrite = GetEepromApiReadBuffer(addr); var spiBufferRead = GetEepromApiDataBuffer(len); var buffer = new List <byte>(); buffer.AddRange(spiBufferWrite); buffer.AddRange(spiBufferRead); var r = this.SpiTransfer(buffer); if (r.Succeeded) { eb.Succeeded = true; eb.Buffer = r.Buffer.GetRange(spiBufferWrite.Length, r.Buffer.Count - spiBufferWrite.Length).ToArray(); } return(eb); }
/// <summary> /// Read pages using an optimized way to transfer page /// Transfer around 20Kb/s. /// /// About optimizing transfer with Nusbio /// /// There are 2 optimizations /// 1) - We send 64 0 from the master to the slave to force the slave to send the data /// Since we know it all 0 the data line need to be set only once. We therefore save 1 cycle out of 3. /// The cycle saved are used to request more data in the limited 2024 cycles buffer that we hace in one /// USB Transaction /// </summary> /// <param name="addr"></param> /// <param name="len"></param> /// <returns></returns> public EEPROM_BUFFER ReadPageOptimized(int addr, int len = -1) { if (len == -1) { len = this.PAGE_SIZE; } var eb = new EEPROM_BUFFER(); //int byteSent = 0; var nusbio = this._spi.Nusbio; var spi = this._spi; var spiBufferCmdAddr = this.GetEepromApiReadBuffer(addr); var spiBufferDummyData = this.GetEepromApiDataBuffer(len); var buffer = new List <byte>(); buffer.AddRange(spiBufferCmdAddr); // Command + 16 bit Address buffer.AddRange(spiBufferDummyData); // Dummy data to be sent which will force the EEPROM to send the right data back var startByteToSkip = spiBufferCmdAddr.Length; var finalBuffer = new List <byte>(); this._spi.Select(); // Set select now so it is part of the bit banging sequence try { var byteBitBanged = 0; var spiSeq = new GpioSequence(nusbio.GetGpioMask(), nusbio.GetTransferBufferSize()); // Convert the 3 Command Bytes + the 64 0 Bytes into a bit banging buffer // The 64 bytes part is optimized since all the value are 0 we just need to set // the data line once for (var bx = 0; bx < buffer.Count; bx++) { for (byte bit = (1 << 7); bit > 0; bit >>= 1) // MSB - Most significant bit first { spiSeq.ClockBit(nusbio[spi.ClockGpio], nusbio[spi.MosiGpio], WinUtil.BitUtil.IsSet(buffer[bx], bit), compactData: (bx >= spiBufferCmdAddr.Length) // For simplicity do not compact the first 3 commands byte, so we know exactly where the data start after the first 3 bytes ); } byteBitBanged++; if (spiSeq.IsSpaceAvailable(8 * 2)) // If we only have left space to compute 1 byte or less { var peb = ReadPageOptimized_SendReadData(spiSeq, startByteToSkip, byteBitBanged, this._spi); if (peb.Succeeded) { finalBuffer.AddRange(peb.Buffer); spiSeq = new GpioSequence(nusbio.GetGpioMask(), nusbio.GetTransferBufferSize()); startByteToSkip = 0; // We skipped it, let's forget about it byteBitBanged = 0; } else { return(eb); // failed } } } var peb2 = ReadPageOptimized_SendReadData(spiSeq, startByteToSkip, byteBitBanged, this._spi); if (peb2.Succeeded) { finalBuffer.AddRange(peb2.Buffer); eb.Buffer = finalBuffer.ToArray(); eb.Succeeded = true; } else { return(eb); // failed } } finally { this._spi.Unselect(); } return(eb); }