Skip to content

Commit 6a52780

Browse files
authored
Fix execution of STM32 mass erase as standalone operation (#66)
***NO_CI***
1 parent 3785e4f commit 6a52780

File tree

3 files changed

+89
-48
lines changed

3 files changed

+89
-48
lines changed

nanoFirmwareFlasher/Program.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,28 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
781781
o.JtagDeviceId,
782782
_verbosityLevel);
783783

784-
if (_exitCode != ExitCodes.OK)
785-
{
786-
// done here
787-
return;
788-
}
784+
// done here
785+
return;
789786
}
790787
}
788+
else if(o.MassErase)
789+
{
790+
_exitCode = Stm32Operations.MassErase(
791+
o.JtagDeviceId,
792+
_verbosityLevel);
793+
794+
// done here
795+
return;
796+
}
797+
else if (o.ResetMcu)
798+
{
799+
_exitCode = Stm32Operations.ResetMcu(
800+
o.JtagDeviceId,
801+
_verbosityLevel);
802+
803+
// done here
804+
return;
805+
}
791806
}
792807

793808
#endregion

nanoFirmwareFlasher/Stm32Operations.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,33 @@ internal static ExitCodes ResetMcu(
247247
return jtagDevice.ResetMcu();
248248
}
249249

250+
internal static ExitCodes MassErase(
251+
string jtagId,
252+
VerbosityLevel verbosity)
253+
{
254+
// JATG device
255+
StmJtagDevice jtagDevice = new StmJtagDevice(jtagId);
256+
257+
if (!jtagDevice.DevicePresent)
258+
{
259+
// no JTAG device found
260+
261+
// done here, this command has no further processing
262+
return ExitCodes.E5001;
263+
}
264+
265+
if (verbosity >= VerbosityLevel.Normal)
266+
{
267+
Console.WriteLine($"Connected to JTAG device with ID { jtagDevice.DeviceId }");
268+
}
269+
270+
// set verbosity
271+
jtagDevice.Verbosity = verbosity;
272+
273+
// perform erase operation
274+
return jtagDevice.MassErase();
275+
}
276+
250277
internal static ExitCodes InstallDfuDrivers(VerbosityLevel verbosityLevel)
251278
{
252279
try

nanoFirmwareFlasher/StmJtagDevice.cs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,13 @@ public ExitCodes FlashHexFiles(IList<string> files)
9696
// erase flash
9797
if (DoMassErase)
9898
{
99-
if (Verbosity >= VerbosityLevel.Normal)
100-
{
101-
Console.ForegroundColor = ConsoleColor.White;
102-
Console.Write("Mass erase device...");
103-
}
99+
var eraseResult = MassErase();
104100

105-
cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=UR -e all");
106-
107-
if (!cliOutput.Contains("Flash memory erased."))
101+
if (eraseResult != ExitCodes.OK)
108102
{
109-
return ExitCodes.E5005;
103+
return eraseResult;
110104
}
111105

112-
if (Verbosity >= VerbosityLevel.Normal)
113-
{
114-
Console.ForegroundColor = ConsoleColor.Green;
115-
Console.WriteLine(" OK");
116-
}
117-
else
118-
{
119-
Console.ForegroundColor = ConsoleColor.White;
120-
Console.WriteLine("");
121-
}
122-
Console.ForegroundColor = ConsoleColor.White;
123-
124106
// toggle mass erase so it's only performed before the first file is flashed
125107
DoMassErase = false;
126108
}
@@ -221,32 +203,13 @@ public ExitCodes FlashBinFiles(IList<string> files, IList<string> addresses)
221203
// erase flash
222204
if (DoMassErase)
223205
{
224-
if (Verbosity >= VerbosityLevel.Normal)
225-
{
226-
Console.ForegroundColor = ConsoleColor.White;
227-
Console.Write("Mass erase device...");
228-
}
229-
230-
cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=UR -e all");
206+
var eraseResult = MassErase();
231207

232-
if (!cliOutput.Contains("Flash memory erased."))
208+
if (eraseResult != ExitCodes.OK)
233209
{
234-
Console.ForegroundColor = ConsoleColor.Red;
235-
Console.WriteLine("ERROR");
236-
return ExitCodes.E5005;
210+
return eraseResult;
237211
}
238212

239-
if (Verbosity >= VerbosityLevel.Normal)
240-
{
241-
Console.ForegroundColor = ConsoleColor.Green;
242-
Console.WriteLine(" OK");
243-
}
244-
else
245-
{
246-
Console.WriteLine("");
247-
}
248-
Console.ForegroundColor = ConsoleColor.White;
249-
250213
// toggle mass erase so it's only performed before the first file is flashed
251214
DoMassErase = false;
252215
}
@@ -369,6 +332,42 @@ public ExitCodes ResetMcu()
369332
return ExitCodes.OK;
370333
}
371334

335+
336+
/// <summary>
337+
/// Reset MCU of connected JTAG device.
338+
/// </summary>
339+
public ExitCodes MassErase()
340+
{
341+
if (Verbosity >= VerbosityLevel.Normal)
342+
{
343+
Console.ForegroundColor = ConsoleColor.White;
344+
Console.Write("Mass erase device...");
345+
}
346+
347+
var cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=UR -e all");
348+
349+
if (!cliOutput.Contains("Mass erase successfully achieved"))
350+
{
351+
Console.ForegroundColor = ConsoleColor.Red;
352+
Console.WriteLine("ERROR");
353+
return ExitCodes.E5005;
354+
}
355+
356+
if (Verbosity >= VerbosityLevel.Normal)
357+
{
358+
Console.ForegroundColor = ConsoleColor.Green;
359+
Console.WriteLine(" OK");
360+
}
361+
else
362+
{
363+
Console.WriteLine("");
364+
}
365+
366+
Console.ForegroundColor = ConsoleColor.White;
367+
368+
return ExitCodes.OK;
369+
}
370+
372371
private static string RunSTM32ProgrammerCLI(string arguments)
373372
{
374373
try

0 commit comments

Comments
 (0)