Skip to content

Commit

Permalink
Rework Erase operation (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes committed Jun 28, 2023
1 parent 2a53448 commit 7303b1c
Showing 1 changed file with 83 additions and 48 deletions.
131 changes: 83 additions & 48 deletions nanoFramework.Tools.DebugLibrary.Shared/NFDevice/NanoDeviceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ public bool Erase(
IProgress<MessageWithProgress> progress = null,
IProgress<string> log = null)
{
bool fReset = false;
bool requestBooter = false;

if (DebugEngine == null)
Expand All @@ -379,17 +378,19 @@ public bool Erase(
{
log?.Report("Connecting to nanoBooter...");

fReset = DebugEngine.IsConnectedTonanoCLR;

if (!ConnectToNanoBooter())
if (DebugEngine.IsConnectedTonanoCLR)
{
log?.Report("*** ERROR: request to connect to nanoBooter failed ***");
// executing CLR so need to reset to nanoBooter
if (!ConnectToNanoBooter())
{
log?.Report("*** ERROR: request to connect to nanoBooter failed ***");

return false;
}
return false;
}

// flag request to launch nanoBooter
requestBooter = true;
// flag request to launch nanoBooter
requestBooter = true;
}
}

if (DebugEngine.FlashSectorMap.Count == 0)
Expand All @@ -399,13 +400,6 @@ public bool Erase(
return false;
}

if (requestBooter)
{
log?.Report("Getting connection source...");

DebugEngine.GetConnectionSource();
}

if (DebugEngine.IsConnectedTonanoCLR)
{
var deviceState = DebugEngine.GetExecutionMode();
Expand Down Expand Up @@ -496,56 +490,97 @@ public bool Erase(

foreach (Commands.Monitor_FlashSectorMap.FlashSectorData flashSectorData in eraseSectors)
{
var sectorSize = flashSectorData.NumBlocks * flashSectorData.BytesPerBlock;
if (DebugEngine.IsConnectedTonanoCLR)
{
// connected to CLR, OK to erase per sector

log?.Report($"Erasing sector @ 0x{flashSectorData.StartAddress:X8}...");
progress?.Report(new MessageWithProgress($"Erasing sector @ 0x{flashSectorData.StartAddress:X8}...", current, totalBytes));
var sectorSize = flashSectorData.NumBlocks * flashSectorData.BytesPerBlock;

(AccessMemoryErrorCodes ErrorCode, bool Success) = DebugEngine.EraseMemory(
flashSectorData.StartAddress,
sectorSize);
log?.Report($"Erasing sector @ 0x{flashSectorData.StartAddress:X8}...");
progress?.Report(new MessageWithProgress($"Erasing sector @ 0x{flashSectorData.StartAddress:X8}...", current, totalBytes));

if (!Success)
{
log?.Report($"Error erasing sector @ 0x{flashSectorData.StartAddress:X8}.");
progress?.Report(new MessageWithProgress(""));
(AccessMemoryErrorCodes ErrorCode, bool Success) = DebugEngine.EraseMemory(
flashSectorData.StartAddress,
sectorSize);

// don't bother continuing
return false;
}
if (!Success)
{
log?.Report($"Error erasing sector @ 0x{flashSectorData.StartAddress:X8}.");
progress?.Report(new MessageWithProgress(""));

// check the error code returned
if (ErrorCode != AccessMemoryErrorCodes.NoError)
{
// operation failed
log?.Report($"Error erasing sector @ 0x{flashSectorData.StartAddress:X8}. Error code: {ErrorCode}.");
progress?.Report(new MessageWithProgress(""));
// don't bother continuing
return false;
}

// don't bother continuing
return false;
// check the error code returned
if (ErrorCode != AccessMemoryErrorCodes.NoError)
{
// operation failed
log?.Report($"Error erasing sector @ 0x{flashSectorData.StartAddress:X8}. Error code: {ErrorCode}.");
progress?.Report(new MessageWithProgress(""));

// don't bother continuing
return false;
}

current += sectorSize;
}
else
{
// connected to nanoBooter so need to erase per sector
for (int blockIndex = 0; blockIndex < flashSectorData.NumBlocks; blockIndex++)
{
var blockAddress = flashSectorData.StartAddress + (blockIndex * flashSectorData.BytesPerBlock);

log?.Report($"Erasing block @ 0x{blockAddress:X8}...");
progress?.Report(new MessageWithProgress($"Erasing block @ 0x{blockAddress:X8}...", current, totalBytes));

(AccessMemoryErrorCodes ErrorCode, bool Success) = DebugEngine.EraseMemory((uint)blockAddress,
flashSectorData.BytesPerBlock);

if (!Success)
{
log?.Report($"Error erasing block @ 0x{blockAddress:X8}.");
progress?.Report(new MessageWithProgress(""));

current += sectorSize;
// don't bother continuing
return false;
}

// check the error code returned
if (ErrorCode != AccessMemoryErrorCodes.NoError)
{
// operation failed
log?.Report($"Error erasing block @ 0x{blockAddress:X8}. Error code: {ErrorCode}.");
progress?.Report(new MessageWithProgress(""));

// don't bother continuing
return false;
}

current += flashSectorData.BytesPerBlock;
}
}
}

progress?.Report(new MessageWithProgress(""));

// reset device if we specifically entered nanoBooter to erase
if (fReset)
{
log?.Report("Executing memory...");
DebugEngine.ExecuteMemory(0);
}

// reboot if we are talking to the CLR
if (DebugEngine.IsConnectedTonanoCLR)
if (requestBooter)
{
log?.Report("Rebooting...");
progress?.Report(new MessageWithProgress("Rebooting..."));

RebootOptions rebootOptions = RebootOptions.ClrOnly;
DebugEngine.RebootDevice(RebootOptions.NormalReboot, log);
}
else if (DebugEngine.IsConnectedTonanoCLR)
{
// reboot if we are talking to the CLR

log?.Report("Rebooting nanoCLR...");
progress?.Report(new MessageWithProgress("Rebooting nanoCLR..."));

DebugEngine.RebootDevice(rebootOptions, log);
DebugEngine.RebootDevice(RebootOptions.ClrOnly, log);
}

progress?.Report(new MessageWithProgress(""));
Expand Down

0 comments on commit 7303b1c

Please sign in to comment.