Skip to content

Commit a694210

Browse files
authored
Merge pull request #36 from nanoframework/release-v1.13.0
Release release-v1.13.0
2 parents ee1263c + cb292f1 commit a694210

File tree

6 files changed

+113
-9
lines changed

6 files changed

+113
-9
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace nanoFramework.Tools.FirmwareFlasher
5+
{
6+
/// <summary>
7+
/// Verification of DFU write failed.
8+
/// </summary>
9+
[Serializable]
10+
internal class DfuVerificationFailedException : Exception
11+
{
12+
public DfuVerificationFailedException()
13+
{
14+
}
15+
16+
public DfuVerificationFailedException(string message) : base(message)
17+
{
18+
}
19+
20+
public DfuVerificationFailedException(string message, Exception innerException) : base(message, innerException)
21+
{
22+
}
23+
24+
protected DfuVerificationFailedException(SerializationInfo info, StreamingContext context) : base(info, context)
25+
{
26+
}
27+
}
28+
}

source/nanoFirmwareFlasher/FirmwarePackage.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
191191
Path.Combine(LocationPath, fwFileName),
192192
LocationPath);
193193

194-
if (Verbosity >= VerbosityLevel.Normal)
195-
{
196-
Console.WriteLine($"OK");
197-
}
198-
else
194+
if (Verbosity >= VerbosityLevel.Detailed)
199195
{
200196
Console.WriteLine("");
201197
}

source/nanoFirmwareFlasher/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ await parsedArguments
7777

7878
if (verbosityLevel > VerbosityLevel.Quiet)
7979
{
80-
OutputError(_exitCode, verbosityLevel >= VerbosityLevel.Normal, _extraMessage);
80+
OutputError(_exitCode, verbosityLevel > VerbosityLevel.Normal, _extraMessage);
8181
}
8282

8383
return (int)_exitCode;
@@ -771,14 +771,17 @@ private static void OutputError(ExitCodes errorCode, bool outputMessage, string
771771
{
772772
if (errorCode != ExitCodes.OK)
773773
{
774-
Console.Write($"error {errorCode.ToString()}");
774+
Console.Write($"Error {errorCode}");
775775
}
776776

777777
if (outputMessage)
778778
{
779779
var exitCodeDisplayName = errorCode.GetAttribute<DisplayAttribute>();
780780

781-
Console.Write($": { exitCodeDisplayName.Name }");
781+
if (!string.IsNullOrEmpty(exitCodeDisplayName.Name))
782+
{
783+
Console.Write($": { exitCodeDisplayName.Name }");
784+
}
782785

783786
if (string.IsNullOrEmpty(extraMessage))
784787
{

source/nanoFirmwareFlasher/StDfu.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ internal static extern uint STDFU_Open(
345345
[MarshalAs(UnmanagedType.LPStr)]string szDevicePath,
346346
out IntPtr hDevice);
347347

348+
[DllImport(@"stdfu\STDFU.dll", EntryPoint = "STDFU_Upload", CharSet = CharSet.Ansi)]
349+
internal static extern uint STDFU_Upload(
350+
ref IntPtr hDevice,
351+
[MarshalAs(UnmanagedType.LPArray)]byte[] pBuffer,
352+
uint nBytes,
353+
ushort nBlocks);
354+
355+
348356

349357
#endregion
350358

@@ -947,6 +955,36 @@ internal static void WriteBlock(
947955
}
948956
}
949957

958+
internal static void ReadBlock(
959+
IntPtr hDevice,
960+
uint address,
961+
byte[] data,
962+
uint blockNumber)
963+
{
964+
DfuStatus dfuStatus = new DfuStatus();
965+
966+
if (0 == blockNumber)
967+
{
968+
SetAddressPointer(hDevice, address);
969+
}
970+
971+
STDFU_GetStatus(ref hDevice, ref dfuStatus);
972+
while (dfuStatus.bState != STATE_DFU_IDLE)
973+
{
974+
STDFU_ClrStatus(ref hDevice);
975+
STDFU_GetStatus(ref hDevice, ref dfuStatus);
976+
}
977+
978+
STDFU_Upload(ref hDevice, data, (uint)data.Length, (ushort)(blockNumber + 2));
979+
980+
STDFU_GetStatus(ref hDevice, ref dfuStatus);
981+
while (dfuStatus.bState != STATE_DFU_UPLOAD_IDLE)
982+
{
983+
STDFU_ClrStatus(ref hDevice);
984+
STDFU_GetStatus(ref hDevice, ref dfuStatus);
985+
}
986+
}
987+
950988
private static void SetAddressPointer(
951989
IntPtr hDevice,
952990
uint address)

source/nanoFirmwareFlasher/StmDfuDevice.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public void FlashDfuFile(string filePath)
237237
Console.Write("Flashing device...");
238238
}
239239

240+
// write flash
240241
for (int elementIndex = 0; elementIndex < dfuTarget.DfuElements.Length; ++elementIndex)
241242
{
242243
StDfu.DfuElement dfuElement = dfuTarget.DfuElements[elementIndex];
@@ -256,6 +257,44 @@ public void FlashDfuFile(string filePath)
256257
Console.WriteLine(" OK");
257258
}
258259

260+
if (Verbosity >= VerbosityLevel.Normal)
261+
{
262+
Console.Write("Verifying flash...");
263+
}
264+
265+
// read back for confirmation
266+
for (int elementIndex = 0; elementIndex < dfuTarget.DfuElements.Length; ++elementIndex)
267+
{
268+
StDfu.DfuElement dfuElement = dfuTarget.DfuElements[elementIndex];
269+
270+
// read the data in MaxWriteBlockSize blocks
271+
for (uint blockNumber = 0; blockNumber <= (uint)dfuElement.Data.Length / _maxWriteBlockSize; blockNumber++)
272+
{
273+
byte[] readBuffer = new byte[_maxWriteBlockSize];
274+
StDfu.ReadBlock(_hDevice, dfuElement.Address, readBuffer, blockNumber);
275+
276+
// get data for the current block
277+
byte[] buffer = dfuElement.Data.Skip((int)(_maxWriteBlockSize * blockNumber)).Take(_maxWriteBlockSize).ToArray();
278+
279+
// exit condition has to be tied with the buffer length because the data for the last block
280+
// can be shorter than the block max size
281+
for(int index = 0; index < buffer.Length; index++)
282+
{
283+
if(readBuffer[index] != buffer[index])
284+
{
285+
Console.WriteLine("");
286+
Console.WriteLine($"Error verifying flash write. Check failed for block address 0x{dfuElement.Address + blockNumber * _maxWriteBlockSize:X8}.");
287+
throw new DfuVerificationFailedException();
288+
}
289+
}
290+
}
291+
}
292+
293+
if (Verbosity >= VerbosityLevel.Normal)
294+
{
295+
Console.WriteLine(" OK");
296+
}
297+
259298
if (Verbosity >= VerbosityLevel.Normal)
260299
{
261300
Console.Write("Launching nanoBooter...");

source/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.12.1",
3+
"version": "1.13.0",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)