public bool WriteRange(uint address, byte[] data, ProgrammingModes prgMode, bool doVerify) { if (data == null || data.Length == 0) { return(false); } var dataLength = data.Length; var start = address; var sSize = GetSectorSize(address); var dataBlock = new byte[sSize]; while (dataLength >= sSize) { Buffer.BlockCopy(data, (int)(address - start), dataBlock, 0, (int)sSize); if (!Program(address / 2, dataBlock, prgMode)) { return(false); } address += sSize; dataLength -= (int)sSize; sSize = GetSectorSize(address); if (dataBlock.Length != sSize) { Array.Resize(ref dataBlock, (int)sSize); } } return(doVerify && Verify(start, data) || !doVerify); }
private bool Program(uint address, byte[] data, ProgrammingModes prgMode) { uint blockSIZE; var sSize = GetSectorSize(address * 2); Debug.Assert(data.Length == sSize); Debug.Assert((address & sSize / 2 - 1) == 0); SendCommand(NORInformation.ChipType == NORInfo.ChipTypes.SamsungK8Q2815Uqb && address >= 0x400000 ? FlashCommand.NOR2NdDieEnable : FlashCommand.NOR2NdDieDisable); FlashCommand flashCommand; switch(prgMode) { case ProgrammingModes.WordMode: blockSIZE = 0x2000; // 8KB blocks flashCommand = FlashCommand.NORWriteBlockWord; break; case ProgrammingModes.UnlockBypassMode: blockSIZE = 0x2000; // 8KB blocks flashCommand = FlashCommand.NORWriteBlockUbm; break; case ProgrammingModes.WriteBufferMode: blockSIZE = 0x8000; // 32KB blocks flashCommand = FlashCommand.NORWriteBlockWbp; break; default: return false; } var oData = ReadSector(address, sSize); if(ByteArraysEqual(oData, data)) return true; if(!ByteArrayHasSameValues(oData, 0xff)) EraseSector(address); for(uint i = 0; i < sSize; i += blockSIZE) { var retries = WriteRetries; while(retries != 0) { SetAddress(address + (i / 2)); SendCommand(flashCommand); Flush(); WriteBlock(ByteArrayCopy(data, i, blockSIZE)); // read write status byte // 'K' = okay, 'T' = timeout error when writing, 'R' = Teensy receive buffer timeout, 'V' = Verification error var res = Read(); if(res == 75) break; SendCommand(FlashCommand.NORReset); Ping(); retries -= 1; Debug.WriteLine(string.Format("({0}. Retry) {1}", WriteRetries - retries, res == 84 ? "RY/BY timeout error while writing!" : string.Format("Received unknown error! (Got 0x{0:X})", res))); } //end while if(retries != 0) continue; Debug.WriteLine("Flashing failed"); return false; } //end for return true; }
private bool Program(uint address, byte[] data, ProgrammingModes prgMode) { uint blockSIZE; var sSize = GetSectorSize(address * 2); Debug.Assert(data.Length == sSize); Debug.Assert((address & sSize / 2 - 1) == 0); SendCommand(NORInformation.ChipType == NORInfo.ChipTypes.SamsungK8Q2815Uqb && address >= 0x400000 ? FlashCommand.NOR2NdDieEnable : FlashCommand.NOR2NdDieDisable); FlashCommand flashCommand; switch (prgMode) { case ProgrammingModes.WordMode: blockSIZE = 0x2000; // 8KB blocks flashCommand = FlashCommand.NORWriteBlockWord; break; case ProgrammingModes.UnlockBypassMode: blockSIZE = 0x2000; // 8KB blocks flashCommand = FlashCommand.NORWriteBlockUbm; break; case ProgrammingModes.WriteBufferMode: blockSIZE = 0x8000; // 32KB blocks flashCommand = FlashCommand.NORWriteBlockWbp; break; default: return(false); } var oData = ReadSector(address, sSize); if (ByteArraysEqual(oData, data)) { return(true); } if (!ByteArrayHasSameValues(oData, 0xff)) { EraseSector(address); } for (uint i = 0; i < sSize; i += blockSIZE) { var retries = WriteRetries; while (retries != 0) { SetAddress(address + (i / 2)); SendCommand(flashCommand); Flush(); WriteBlock(ByteArrayCopy(data, i, blockSIZE)); // read write status byte // 'K' = okay, 'T' = timeout error when writing, 'R' = Teensy receive buffer timeout, 'V' = Verification error var res = Read(); if (res == 75) { break; } SendCommand(FlashCommand.NORReset); Ping(); retries -= 1; Debug.WriteLine(string.Format("({0}. Retry) {1}", WriteRetries - retries, res == 84 ? "RY/BY timeout error while writing!" : string.Format("Received unknown error! (Got 0x{0:X})", res))); } //end while if (retries != 0) { continue; } Debug.WriteLine("Flashing failed"); return(false); } //end for return(true); }
public bool WriteRange(uint address, byte[] data, ProgrammingModes prgMode, bool doVerify) { if(data == null || data.Length == 0) return false; var dataLength = data.Length; var start = address; var sSize = GetSectorSize(address); var dataBlock = new byte[sSize]; while(dataLength >= sSize) { Buffer.BlockCopy(data, (int) (address - start), dataBlock, 0, (int) sSize); if(!Program(address / 2, dataBlock, prgMode)) return false; address += sSize; dataLength -= (int) sSize; sSize = GetSectorSize(address); if(dataBlock.Length != sSize) Array.Resize(ref dataBlock, (int) sSize); } return doVerify && Verify(start, data) || !doVerify; }