Skip to content

Commit f0dc2b6

Browse files
authored
Improve error handling and output for ST targets (#73)
***NO_CI***
1 parent da44039 commit f0dc2b6

File tree

1 file changed

+40
-71
lines changed

1 file changed

+40
-71
lines changed

nanoFirmwareFlasher/StmJtagDevice.cs

Lines changed: 40 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,7 @@ public StmJtagDevice(string jtagId = null)
7171

7272
if (!cliOutput.Contains("Connected via SWD."))
7373
{
74-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
75-
{
76-
// show error detail, if available
77-
Console.ForegroundColor = ConsoleColor.Red;
78-
79-
Console.WriteLine(_stCLIErrorMessage);
80-
81-
Console.ForegroundColor = ConsoleColor.White;
82-
}
74+
ShowCLIOutput(cliOutput);
8375

8476
throw new CantConnectToJtagDeviceException();
8577
}
@@ -101,25 +93,14 @@ public ExitCodes FlashHexFiles(IList<string> files)
10193
return ExitCodes.E5003;
10294
}
10395

104-
// need a couple of seconds before issuing next command
105-
Thread.Sleep(2000);
106-
10796
// try to connect to device with RESET
108-
var cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=NORMAL");
97+
var cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=UR");
10998

110-
if (cliOutput.Contains("Error:"))
99+
if (cliOutput.Contains("Error"))
111100
{
112101
Console.WriteLine("");
113-
114-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
115-
{
116-
// show error detail, if available
117-
Console.ForegroundColor = ConsoleColor.Red;
118-
119-
Console.WriteLine(_stCLIErrorMessage);
120102

121-
Console.ForegroundColor = ConsoleColor.White;
122-
}
103+
ShowCLIOutput(cliOutput);
123104

124105
return ExitCodes.E5002;
125106
}
@@ -162,15 +143,7 @@ public ExitCodes FlashHexFiles(IList<string> files)
162143

163144
if (!cliOutput.Contains("File download complete"))
164145
{
165-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
166-
{
167-
// show error detail, if available
168-
Console.ForegroundColor = ConsoleColor.Red;
169-
170-
Console.WriteLine(_stCLIErrorMessage);
171-
172-
Console.ForegroundColor = ConsoleColor.White;
173-
}
146+
ShowCLIOutput(cliOutput);
174147

175148
return ExitCodes.E5006;
176149
}
@@ -237,21 +210,13 @@ public ExitCodes FlashBinFiles(IList<string> files, IList<string> addresses)
237210
Thread.Sleep(2000);
238211

239212
// try to connect to device with RESET
240-
var cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=NORMAL");
213+
var cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=UR");
241214

242215
if (!cliOutput.Contains("Connected via SWD."))
243216
{
244217
Console.WriteLine("");
245218

246-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
247-
{
248-
// show error detail, if available
249-
Console.ForegroundColor = ConsoleColor.Red;
250-
251-
Console.WriteLine(_stCLIErrorMessage);
252-
253-
Console.ForegroundColor = ConsoleColor.White;
254-
}
219+
ShowCLIOutput(cliOutput);
255220

256221
return ExitCodes.E5002;
257222
}
@@ -295,15 +260,7 @@ public ExitCodes FlashBinFiles(IList<string> files, IList<string> addresses)
295260

296261
if (!cliOutput.Contains("Programming Complete."))
297262
{
298-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
299-
{
300-
// show error detail, if available
301-
Console.ForegroundColor = ConsoleColor.Red;
302-
303-
Console.WriteLine(_stCLIErrorMessage);
304-
305-
Console.ForegroundColor = ConsoleColor.White;
306-
}
263+
ShowCLIOutput(cliOutput);
307264

308265
return ExitCodes.E5006;
309266
}
@@ -369,19 +326,11 @@ public ExitCodes ResetMcu()
369326
// try to connect to device with RESET
370327
var cliOutput = RunSTM32ProgrammerCLI($"-c port=SWD sn={DeviceId} mode=UR -rst");
371328

372-
if (cliOutput.Contains("Error:"))
329+
if (cliOutput.Contains("Error"))
373330
{
374331
Console.WriteLine("");
375332

376-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
377-
{
378-
// show error detail, if available
379-
Console.ForegroundColor = ConsoleColor.Red;
380-
381-
Console.WriteLine(_stCLIErrorMessage);
382-
383-
Console.ForegroundColor = ConsoleColor.White;
384-
}
333+
ShowCLIOutput(cliOutput);
385334

386335
return ExitCodes.E5002;
387336
}
@@ -428,15 +377,7 @@ public ExitCodes MassErase()
428377
{
429378
Console.WriteLine("");
430379

431-
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
432-
{
433-
// show error detail, if available
434-
Console.ForegroundColor = ConsoleColor.Red;
435-
436-
Console.WriteLine(_stCLIErrorMessage);
437-
438-
Console.ForegroundColor = ConsoleColor.White;
439-
}
380+
ShowCLIOutput(cliOutput);
440381

441382
return ExitCodes.E5005;
442383
}
@@ -456,6 +397,28 @@ public ExitCodes MassErase()
456397
return ExitCodes.OK;
457398
}
458399

400+
private void ShowCLIOutput(string cliOutput)
401+
{
402+
// show CLI output, if verbosity is diagnostic
403+
if (Verbosity == VerbosityLevel.Diagnostic)
404+
{
405+
Console.WriteLine(">>>>>>>>");
406+
Console.WriteLine($"{cliOutput}");
407+
Console.WriteLine(">>>>>>>>");
408+
}
409+
410+
// show error message from CLI, if there is one
411+
if (!string.IsNullOrEmpty(_stCLIErrorMessage))
412+
{
413+
// show error detail, if available
414+
Console.ForegroundColor = ConsoleColor.Red;
415+
416+
Console.WriteLine(_stCLIErrorMessage);
417+
418+
Console.ForegroundColor = ConsoleColor.White;
419+
}
420+
}
421+
459422
private static string RunSTM32ProgrammerCLI(string arguments)
460423
{
461424
try
@@ -506,8 +469,14 @@ private static string GetErrorMessageFromSTM32CLI(string cliOutput)
506469
}
507470
else
508471
{
509-
return "";
472+
// look for DEV_USB_COMM_ERR
473+
if(cliOutput.Contains("DEV_USB_COMM_ERR"))
474+
{
475+
return "USB communication error. Please unplug and plug again the ST device.";
476+
}
510477
}
478+
479+
return "";
511480
}
512481
}
513482
}

0 commit comments

Comments
 (0)