Skip to content

Commit

Permalink
Fix erase memory command (#32)
Browse files Browse the repository at this point in the history
- improve ProcessMessage so the return is never null
- improve PerformRequestAsync so the return is never null
- improve code in EraseMemoryAsync to check for bad execution
- replace timeout values with constants in EraseMemoryAsync for clarity
- fix nanoframework/nf-Visual-Studio-extension/#107
- bump Nuget version to 0.4.0-preview017

Signed-off-by: José Simões <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes authored Jun 29, 2017
1 parent e5fa09d commit 9a34e4e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions source/USB Test App WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Button Content="Resolve Assemblies" HorizontalAlignment="Left" Margin="39,235,0,0" VerticalAlignment="Top" Width="75" Click="ResolveAssembliesButton_Click" />
<Button Content="Deploy Test" HorizontalAlignment="Left" Margin="154,235,0,0" VerticalAlignment="Top" Width="75" Click="DeployTestButton_Click" />
<Button Content="Flash Map" HorizontalAlignment="Left" Margin="39,275,0,0" VerticalAlignment="Top" Width="75" Click="FlashMapButton_Click" />
<Button Content="Resume Execution" HorizontalAlignment="Left" Margin="155,275,0,0" VerticalAlignment="Top" Width="75" Click="ResumeExecutionButton_Click" />


</Grid>
Expand Down
32 changes: 31 additions & 1 deletion source/USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private async void DeployTestButton_Click(object sender, RoutedEventArgs e)
try
{
// add mscorlib
string assemblyPath = @"..\..\..\packages\nanoFramework.CoreLibrary.1.0.0-preview020\lib\mscorlib.pe";
string assemblyPath = @"..\..\..\packages\nanoFramework.CoreLibrary.1.0.0-preview022\lib\mscorlib.pe";

using (FileStream fs = File.Open(assemblyPath, FileMode.Open, FileAccess.Read))
{
Expand Down Expand Up @@ -236,6 +236,36 @@ private async void FlashMapButton_Click(object sender, RoutedEventArgs e)

}

// enable button
(sender as Button).IsEnabled = true;
}
private async void ResumeExecutionButton_Click(object sender, RoutedEventArgs e)
{
// disable button
(sender as Button).IsEnabled = false;

try
{
await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(async () =>
{
// enable button
(sender as Button).IsEnabled = true;

var result = await (DataContext as MainViewModel).AvailableDevices[0].DebugEngine.ResumeExecutionAsync();

Debug.WriteLine("");
Debug.WriteLine("");
Debug.WriteLine($"resume execution: {result}");
Debug.WriteLine("");
Debug.WriteLine("");

}));
}
catch (Exception ex)
{

}

// enable button
(sender as Button).IsEnabled = true;
}
Expand Down
2 changes: 1 addition & 1 deletion source/USB Test App WPF/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<package id="PropertyChanged.Fody" version="2.1.2" targetFramework="net46" developmentDependency="true" />
<package id="PropertyChanging.Fody" version="1.28.0" targetFramework="net462" developmentDependency="true" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net46" />
<package id="nanoFramework.CoreLibrary" version="1.0.0-preview020" targetFramework="net46" />
<package id="nanoFramework.CoreLibrary" version="1.0.0-preview022" targetFramework="net46" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PackageId>nanoFramework.Tools.Debugger.Net</PackageId>
<PackageVersion>0.4.0-preview016</PackageVersion>
<PackageVersion>0.4.0-preview017</PackageVersion>
<Description>This .NET library provides a debug client for nanoFramework devices using USB or Serial connection to a board.</Description>
<Authors>nanoFramework project contributors</Authors>
<Title>nanoFramework debug library for .NET</Title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,16 @@ private OutgoingMessage CreateMessage(uint cmd, uint flags, object payload)

// typical max Flash erase times for STM32 parts with PSIZE set to 16bits are:
// 16kB sector: 600ms >> 38ms/kB
const int eraseTimeout16kSector = 600;

// 64kB sector: 1400ms >> 22ms/kB
const int eraseTimeout64kSector = 1400;

// 128kB sector: 2600ms >> 21ms/kB
const int eraseTimeout128kSector = 2600;

// this extra timeout is to account comm times and execution operation on the AccessMemory funtion
const int extraTimeoutForErase = 800;

// the erase memory command isn't aware of the sector(s) size it will end up erasing so we have to do an educated guess on how long that will take
// considering the worst case timming which is the erase of the smallest sector.
Expand All @@ -618,29 +626,29 @@ private OutgoingMessage CreateMessage(uint cmd, uint flags, object payload)
if (length <= (16 * 1024))
{
// timeout for 16kB sector
timeout = 600;
timeout = eraseTimeout16kSector + extraTimeoutForErase;
}
else if (length <= (64 * 1024))
{
// timeout for 64kB sector
timeout = 1400;
timeout = eraseTimeout64kSector + extraTimeoutForErase;
}
else if (length <= (128 * 1024))
{
// timeout for 128kB sector
timeout = 2600;
timeout = eraseTimeout128kSector + extraTimeoutForErase;
}
else
{
// timeout for anything above 128kB (multiple sectors)
timeout = (int)(length / (16 * 1024)) * 600 + 500;
timeout = (int)(length / (16 * 1024)) * eraseTimeout16kSector + 2 * extraTimeoutForErase;
}

IncomingMessage reply = await PerformRequestAsync(Commands.c_Monitor_EraseMemory, 0, cmd, 0, timeout);

Commands.Monitor_EraseMemory.Reply cmdReply = reply.Payload as Commands.Monitor_EraseMemory.Reply;

return (cmdReply.ErrorCode, IncomingMessage.IsPositiveAcknowledge(reply));
return (cmdReply?.ErrorCode ?? 0, IncomingMessage.IsPositiveAcknowledge(reply));
}

public async Task<bool> ExecuteMemoryAsync(uint address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal async Task<IncomingMessage> ProcessAsync(CancellationToken cancellation

Debug.WriteLine("cancel token");

return null;
return GetCompleteMessage();
}

switch (_state)
Expand Down Expand Up @@ -207,7 +207,7 @@ internal async Task<IncomingMessage> ProcessAsync(CancellationToken cancellation
// FIXME
// evaluate the purpose of this reply back to the NanoFramework device, the nanoCLR doesn't seem to have to handle this. In the end it looks like this does have any real purpose and will only be wasting CPU.
//await IncomingMessage.ReplyBadPacketAsync(m_parent, Flags.c_BadHeader);
return null;
return GetCompleteMessage();
}

break;
Expand Down Expand Up @@ -286,7 +286,7 @@ internal async Task<IncomingMessage> ProcessAsync(CancellationToken cancellation
// FIXME
// evaluate the purpose of this reply back to the NanoFramework device, the nanoCLR doesn't seem to have to handle this. In the end it looks like this does have any real purpose and will only be wasting CPU.
await IncomingMessage.ReplyBadPacketAsync(_parent, Flags.c_BadPayload, cancellationToken);
return null;
return GetCompleteMessage();
}

break;
Expand All @@ -302,7 +302,7 @@ internal async Task<IncomingMessage> ProcessAsync(CancellationToken cancellation
}

Debug.WriteLine("??????? leaving reassembler");
return null;
return GetCompleteMessage();
}

private int ValidMarker(byte[] marker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal async Task<IncomingMessage> PerformRequestAsync()

Debug.WriteLine($"Performing request exceeded attempts count...");

return null;
return reply;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<PackageId>nanoFramework.Tools.Debugger.UWP</PackageId>
<PackageVersion>0.4.0-preview016</PackageVersion>
<PackageVersion>0.4.0-preview017</PackageVersion>
<Description>This UWP library provides a debug client for nanoFramework devices using USB or Serial connection to a board.</Description>
<Authors>nanoFramework project contributors</Authors>
<Title>nanoFramework debug library for UWP</Title>
Expand Down

0 comments on commit 9a34e4e

Please sign in to comment.