public static bool VerifyPage(EEPROM_BUFFER r, int pageIndex) { int totalErrorCount = 0; for (var i = 0; i < EEPROM_24LCXXX.DEFAULT_PAGE_SIZE; i++) { var expected = i; if (pageIndex == 2) { expected = NEW_WRITTEN_VALUE_1; } if (pageIndex == 3) { expected = NEW_WRITTEN_VALUE_2; } if (r.Buffer[i] != expected) { System.Console.WriteLine("Failed Page:{0} [{1}] = {2}, expected {3}", pageIndex, i, r.Buffer[i], expected); totalErrorCount += 1; } } return(totalErrorCount == 0); }
public EEPROM_BUFFER ReadSector(int sector4kStart, int len, bool optimize = false) { //this.WaitForOperation(); sector4kStart = sector4kStart * this.SectorSize; #if !NUSBIO2 // With Nusbio2 with high speed SPI by default if (optimize) { return(ReadPageOptimized(sector4kStart, len)); } #endif var rb = new EEPROM_BUFFER(); var tmpBuffer = new List <byte>() { READ, (byte)(sector4kStart >> 16), (byte)(sector4kStart >> 8), (byte)(sector4kStart & 0xFF) }; var buffer = base.GetEepromApiDataBuffer((int)len); tmpBuffer.AddRange(buffer); var r = this.SpiTransfer(tmpBuffer, true); if (!r.Succeeded) { return(rb); } rb.Buffer = r.Buffer.Skip(4).ToArray(); rb.Succeeded = r.Succeeded; return(rb); }
static void ReadAndVerifyEEPROMPageInBatch(int numberOfPageToRead) { Console.Clear(); var totalErrorCount = 0; var t = Stopwatch.StartNew(); var batchSize = 64; var pageIndex = 0; byte[] buf; for (var batchIndex = 0; batchIndex < numberOfPageToRead; batchIndex += batchSize) { Console.WriteLine("Reading page {0}", batchIndex); var r = _eeprom.ReadPage(batchIndex * _eeprom.PAGE_SIZE, _eeprom.PAGE_SIZE * batchSize); if (r.Succeeded) { for (var b = 0; b < batchSize; b++) { buf = r.GetPage(b, _eeprom.PAGE_SIZE); var tmpBuf = new EEPROM_BUFFER { Buffer = buf }; EEPROM_24LC256.VerifyPage(tmpBuf, pageIndex); pageIndex += 1; } } else { Console.WriteLine("ReadBuffer failure"); } } t.Stop(); Console.WriteLine("{0} error(s), Time:{1}, {2:0.00} kb/s", totalErrorCount, t.ElapsedMilliseconds, _eeprom.MaxByte * 1.0 / t.ElapsedMilliseconds ); Console.WriteLine("Hit enter key"); Console.ReadLine(); }
public EEPROM_BUFFER ReadPageOptimized(int addr, int len = -1) { if (len == -1) { len = this.PAGE_SIZE; } var eb = new EEPROM_BUFFER(); 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); }