1010cmd_jlink = os .path .join (tempfile .gettempdir (), 'cmd.jlink' )
1111console_out = os .path .join (tempfile .gettempdir (), 'console.output' )
1212
13- # Suppress traceback for Arduino IDE
14- # !! comment out if you want to see the traceback for debug/ developement !!
15- sys .tracebacklimit = 0
16-
1713def check_python_version ():
1814 major = sys .version_info .major
1915 minor = sys .version_info .minor
@@ -88,6 +84,16 @@ def remove_console_output_file(console_output_file):
8884 os .remove (console_output_file )
8985
9086def jlink_commander (device , serial_num , cmd_file , console_output = False ):
87+ """
88+ Executes J-Link Commander with the specified device, serial number, command file, and console output option.
89+ Args:
90+ device (str): The device name.
91+ serial_num (str): The serial number of the J-Link device.
92+ cmd_file (str): The path to the command file.
93+ console_output (bool, optional): Specifies whether to enable console output. Defaults to False (print the log rather than store in a file).
94+ Raises:
95+ Exception: If there is an error with J-Link.
96+ """
9197 jlink_cmd = [jlinkexe , '-autoconnect' , '1' ,'-exitonerror' , '1' , '-nogui' , '1' , '-device' , device , '-selectemubysn' , serial_num , '-if' , 'swd' , '-speed' , '4000' , '-commandfile' , cmd_file ]
9298
9399 #if console_output is True:
@@ -117,7 +123,7 @@ def process_console_output(string):
117123
118124def check_serial_number (serial_num ):
119125 if serial_num == None :
120- raise Exception ("Device not found! Please check the COM port" )
126+ raise Exception ("Device not found! Please check the serial port" )
121127
122128def get_mem_contents (addr , bytes , device , port ):
123129 try :
@@ -204,10 +210,20 @@ def check_mem(device, port):
204210 raise Exception ("Memory size of device connected does not match that of the selected device to flash" )
205211
206212
207- def upload (device , port , binfile ):
213+ def upload (device , port , binfile , enable_jlink_log ):
214+ """
215+ Uploads a binary file to a device using XMC flasher.
216+ Args:
217+ device (str): The device name or ID.
218+ port (str): The port to connect to the device.
219+ binfile (str): The path to the binary file to upload.
220+ enable_jlink_log (bool): Whether to enable J-Link logging.
221+ Returns:
222+ None
223+ """
208224 serial_num = get_device_serial_number (port )
209225 jlink_cmd_file = create_jlink_loadbin_command_file (binfile )
210- jlink_commander (device , serial_num , jlink_cmd_file )
226+ jlink_commander (device , serial_num , jlink_cmd_file , console_output = not enable_jlink_log )
211227 remove_jlink_command_file (jlink_cmd_file )
212228
213229def erase (device , port ):
@@ -227,7 +243,21 @@ def main_parser_func(args):
227243 def parser_upload_func (args ):
228244 check_device (args .device , args .port )
229245 check_mem (args .device , args .port )
230- upload (args .device , args .port , args .binfile )
246+ upload (args .device , args .port , args .binfile , args .verbose )
247+ # remove console output file if verbose is not enabled
248+ if not args .verbose :
249+ # check if upload was successful by parsing the console output
250+ with open (console_out , 'r' ) as f :
251+ found_loadbin = False
252+ for line in f :
253+ if "J-Link>loadbin" in line :
254+ found_loadbin = True
255+ elif found_loadbin and "O.K." in line :
256+ print ("Upload successful" )
257+ break
258+ else :
259+ print ("Upload failed" )
260+ remove_console_output_file (console_out )
231261
232262 def parser_erase_func (args ):
233263 erase (args .device , args .port )
@@ -252,9 +282,10 @@ def __call__(self, parser, namespace, values, option_string, **kwargs):
252282 required_upload .add_argument ('-d' ,'--device' , type = str , help = 'jlink device name' , required = True )
253283 required_upload .add_argument ('-p' ,'--port' , type = str , help = 'serial port' , required = True )
254284 required_upload .add_argument ('-f' ,'--binfile' , type = str , help = 'binary file to upload' , required = True )
285+ required_upload .add_argument ('--verbose' , action = 'store_true' , help = 'Enable verbose logging' )
255286 parser_upload .set_defaults (func = parser_upload_func )
256287
257- # Debug parser
288+ # Erase parser
258289 parser_erase = subparser .add_parser ('erase' , description = 'erase command' )
259290 required_erase = parser_erase .add_argument_group ('required arguments' )
260291 required_erase .add_argument ('-d' ,'--device' , type = str , help = 'jlink device name' , required = True )
@@ -264,8 +295,14 @@ def __call__(self, parser, namespace, values, option_string, **kwargs):
264295 # debug_parser.
265296 # TBD in future
266297
267- # Parser call
298+ # Parse arguments
268299 args = parser .parse_args ()
300+ # Set traceback limit based on the --verbose argument
301+ if args .verbose :
302+ sys .tracebacklimit = None # Enable full traceback
303+ else :
304+ sys .tracebacklimit = 0 # Disable traceback
305+ # Parser call
269306 args .func (args )
270307
271308if __name__ == "__main__" :
0 commit comments