public MemoryDump16BitLE ReadFlash(uint startAddress, uint size, BlockProcessedDelegate cb) { BlockProcessInfo info = new BlockProcessInfo(); ushort[] buffer = new ushort[size]; info.Mode = eProgramMode.Read; info.BlockCount = size; for (uint i = 0; i < size; i++) { buffer[i] = ReadProgramWord(startAddress + i); if (cb != null) { info.BlockNum = i; cb(info); if (info.Cancel) { return(null); } } } MemoryDump16BitLE dump = new MemoryDump16BitLE(); dump.StartAddress = startAddress; dump.Data = buffer; return(dump); }
public void ProgramFlash(MemoryDump16BitLE dump, BlockProcessedDelegate cb) { BlockProcessInfo info = new BlockProcessInfo(); uint posFlash = 0; uint startAddress = dump.StartAddress; // create data struct, aligned to flash size uint dataSize = (uint)(((dump.Length + (AtmelType.PageSizeFlash - 1)) / AtmelType.PageSizeFlash) * AtmelType.PageSizeFlash); ushort[] data = new ushort[dataSize]; for (int pos = 0; pos < dataSize; pos++) { if (pos < dump.Length) { data[pos] = dump.Data[pos]; } else { data[pos] = 0xFFFF; } } // first erase the chip ChipErase(); info.Mode = eProgramMode.Program; info.BlockCount = (uint)data.Length; // then flash the given data while (posFlash < data.Length) { uint posTemp = 0; if (cb != null) { info.BlockNum = posFlash; cb(info); if (info.Cancel) { return; } } // skip empty pages if (!IsEmptyPage(data, posFlash, AtmelType.PageSizeFlash)) { // program byte-wise if (SlowProgramming) { while (posTemp < AtmelType.PageSizeFlash && posFlash + posTemp < data.Length) { LoadProgramMemoryPage(posTemp, data[posFlash + (posTemp++)]); } } else { uint blockSize = TransferBlockSize; while (posTemp < AtmelType.PageSizeFlash && posFlash + posTemp < data.Length) { if (posTemp + blockSize > AtmelType.PageSizeFlash) { blockSize = AtmelType.PageSizeFlash - posTemp; } LoadProgramMemoryPageMulti(posTemp, data, posFlash + posTemp, blockSize); posTemp += blockSize; } } // program page to flash WriteProgramMemoryPage(startAddress + posFlash); WaitReady(100); } posFlash += AtmelType.PageSizeFlash; } }