@@ -15,6 +15,11 @@ namespace nanoFramework.Tools.FirmwareFlasher
1515{
1616 internal class StmJtagDevice
1717 {
18+ /// <summary>
19+ /// Error message from ST CLI.
20+ /// </summary>
21+ private static string _stCLIErrorMessage ;
22+
1823 /// <summary>
1924 /// This property is <see langword="true"/> if a JTAG device is connected.
2025 /// </summary>
@@ -65,6 +70,16 @@ public StmJtagDevice(string jtagId = null)
6570
6671 if ( ! cliOutput . Contains ( "Connected via SWD." ) )
6772 {
73+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
74+ {
75+ // show error detail, if available
76+ Console . ForegroundColor = ConsoleColor . Red ;
77+
78+ Console . WriteLine ( _stCLIErrorMessage ) ;
79+
80+ Console . ForegroundColor = ConsoleColor . White ;
81+ }
82+
6883 throw new CantConnectToJtagDeviceException ( ) ;
6984 }
7085 }
@@ -91,6 +106,17 @@ public ExitCodes FlashHexFiles(IList<string> files)
91106 if ( cliOutput . Contains ( "Error:" ) )
92107 {
93108 Console . WriteLine ( "" ) ;
109+
110+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
111+ {
112+ // show error detail, if available
113+ Console . ForegroundColor = ConsoleColor . Red ;
114+
115+ Console . WriteLine ( _stCLIErrorMessage ) ;
116+
117+ Console . ForegroundColor = ConsoleColor . White ;
118+ }
119+
94120 return ExitCodes . E5002 ;
95121 }
96122
@@ -132,6 +158,16 @@ public ExitCodes FlashHexFiles(IList<string> files)
132158
133159 if ( ! cliOutput . Contains ( "File download complete" ) )
134160 {
161+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
162+ {
163+ // show error detail, if available
164+ Console . ForegroundColor = ConsoleColor . Red ;
165+
166+ Console . WriteLine ( _stCLIErrorMessage ) ;
167+
168+ Console . ForegroundColor = ConsoleColor . White ;
169+ }
170+
135171 return ExitCodes . E5006 ;
136172 }
137173 }
@@ -198,6 +234,18 @@ public ExitCodes FlashBinFiles(IList<string> files, IList<string> addresses)
198234
199235 if ( ! cliOutput . Contains ( "Connected via SWD." ) )
200236 {
237+ Console . WriteLine ( "" ) ;
238+
239+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
240+ {
241+ // show error detail, if available
242+ Console . ForegroundColor = ConsoleColor . Red ;
243+
244+ Console . WriteLine ( _stCLIErrorMessage ) ;
245+
246+ Console . ForegroundColor = ConsoleColor . White ;
247+ }
248+
201249 return ExitCodes . E5002 ;
202250 }
203251
@@ -240,6 +288,16 @@ public ExitCodes FlashBinFiles(IList<string> files, IList<string> addresses)
240288
241289 if ( ! cliOutput . Contains ( "Programming Complete." ) )
242290 {
291+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
292+ {
293+ // show error detail, if available
294+ Console . ForegroundColor = ConsoleColor . Red ;
295+
296+ Console . WriteLine ( _stCLIErrorMessage ) ;
297+
298+ Console . ForegroundColor = ConsoleColor . White ;
299+ }
300+
243301 return ExitCodes . E5006 ;
244302 }
245303 }
@@ -306,6 +364,18 @@ public ExitCodes ResetMcu()
306364
307365 if ( cliOutput . Contains ( "Error:" ) )
308366 {
367+ Console . WriteLine ( "" ) ;
368+
369+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
370+ {
371+ // show error detail, if available
372+ Console . ForegroundColor = ConsoleColor . Red ;
373+
374+ Console . WriteLine ( _stCLIErrorMessage ) ;
375+
376+ Console . ForegroundColor = ConsoleColor . White ;
377+ }
378+
309379 return ExitCodes . E5002 ;
310380 }
311381
@@ -349,8 +419,18 @@ public ExitCodes MassErase()
349419
350420 if ( ! cliOutput . Contains ( "Mass erase successfully achieved" ) )
351421 {
352- Console . ForegroundColor = ConsoleColor . Red ;
353- Console . WriteLine ( "ERROR" ) ;
422+ Console . WriteLine ( "" ) ;
423+
424+ if ( ! string . IsNullOrEmpty ( _stCLIErrorMessage ) )
425+ {
426+ // show error detail, if available
427+ Console . ForegroundColor = ConsoleColor . Red ;
428+
429+ Console . WriteLine ( _stCLIErrorMessage ) ;
430+
431+ Console . ForegroundColor = ConsoleColor . White ;
432+ }
433+
354434 return ExitCodes . E5005 ;
355435 }
356436
@@ -373,6 +453,9 @@ private static string RunSTM32ProgrammerCLI(string arguments)
373453 {
374454 try
375455 {
456+ // reset error message
457+ _stCLIErrorMessage = string . Empty ;
458+
376459 var stLinkCli = new Process
377460 {
378461 StartInfo = new ProcessStartInfo ( Path . Combine ( Program . ExecutingPath , "stlink" , "bin" , "STM32_Programmer_CLI.exe" ) ,
@@ -384,20 +467,40 @@ private static string RunSTM32ProgrammerCLI(string arguments)
384467 }
385468 } ;
386469
387-
388470 // start STM32 Programmer CLI and...
389471 stLinkCli . Start ( ) ;
390472
391473 // ... wait for exit
392474 stLinkCli . WaitForExit ( ) ;
393475
394476 // collect output messages
395- return stLinkCli . StandardOutput . ReadToEnd ( ) ;
477+ string cliOutput = stLinkCli . StandardOutput . ReadToEnd ( ) ;
478+
479+ // check and parse any error in the output
480+ _stCLIErrorMessage = GetErrorMessageFromSTM32CLI ( cliOutput ) ;
481+
482+ return cliOutput ;
396483 }
397484 catch ( Exception ex )
398485 {
399486 throw new StLinkCliExecutionException ( ex . Message ) ;
400487 }
401488 }
489+
490+ private static string GetErrorMessageFromSTM32CLI ( string cliOutput )
491+ {
492+ var regEx = new Regex ( @"Error: (?<error>.+)." , RegexOptions . IgnoreCase ) ;
493+
494+ var match = regEx . Match ( cliOutput ) ;
495+
496+ if ( match . Success )
497+ {
498+ return match . Groups [ "error" ] . Value ;
499+ }
500+ else
501+ {
502+ return "" ;
503+ }
504+ }
402505 }
403506}
0 commit comments