@@ -1400,16 +1400,9 @@ def get_target(self, target=None):
1400
1400
target = target if target else target_cfg
1401
1401
1402
1402
if target and (target .lower () == 'detect' or target .lower () == 'auto' ):
1403
- targets = self .get_detected_targets ()
1404
- if targets == False :
1405
- error ("The target detection requires that the 'mbed-ls' python module is installed.\n You can install mbed-ls by running 'pip install mbed-ls'." )
1406
- elif len (targets ) > 1 :
1407
- error ("Multiple targets were detected.\n Only 1 target board should be connected to your system when you use the '-m auto' switch." )
1408
- elif len (targets ) == 0 :
1409
- error ("No targets were detected.\n Please make sure a target board is connected to this system." )
1410
- else :
1411
- action ("Detected \" %s\" connected to \" %s\" and using com port \" %s\" " % (targets [0 ]['name' ], targets [0 ]['mount' ], targets [0 ]['serial' ]))
1412
- target = targets [0 ]['name' ]
1403
+ detected = self .detect_target ()
1404
+ if detected :
1405
+ target = detected ['name' ]
1413
1406
1414
1407
if target is None :
1415
1408
error ("Please specify target using the -m switch or set default target using command 'mbed target'" , 1 )
@@ -1447,6 +1440,22 @@ def ignore_build_dir(self):
1447
1440
except IOError :
1448
1441
error ("Unable to write build ignore file in \" %s\" " % os .path .join (build_path , '.mbedignore' ), 1 )
1449
1442
1443
+ def detect_target (self , info = None ):
1444
+ targets = self .get_detected_targets ()
1445
+ if targets == False :
1446
+ error ("The target detection requires that the 'mbed-ls' python module is installed.\n You can install mbed-ls by running 'pip install mbed-ls'." )
1447
+ elif len (targets ) > 1 :
1448
+ error ("Multiple targets were detected.\n Only 1 target board should be connected to your system." )
1449
+ elif len (targets ) == 0 :
1450
+ error ("No targets were detected.\n Please make sure a target board is connected to this system." )
1451
+ else :
1452
+ action ("Detected \" %s\" connected to \" %s\" and using com port \" %s\" " % (targets [0 ]['name' ], targets [0 ]['mount' ], targets [0 ]['serial' ]))
1453
+ info = {'msd' : targets [0 ]['mount' ], 'port' : targets [0 ]['serial' ], 'name' : targets [0 ]['name' ]}
1454
+
1455
+ if info is None :
1456
+ error ("The detected target doesn't support Mass Storage Device capability (MSD)" , 1 )
1457
+ return info
1458
+
1450
1459
def get_detected_targets (self ):
1451
1460
targets = []
1452
1461
try :
@@ -2137,12 +2146,13 @@ def status_(ignore=False):
2137
2146
dict (name = '--source' , action = 'append' , help = 'Source directory. Default: . (current dir)' ),
2138
2147
dict (name = '--build' , help = 'Build directory. Default: build/' ),
2139
2148
dict (name = ['-c' , '--clean' ], action = 'store_true' , help = 'Clean the build directory before compiling' ),
2149
+ dict (name = ['-f' , '--flash' ], action = 'store_true' , help = 'Flash the built firmware onto a connected target.' ),
2140
2150
dict (name = ['-N' , '--artifact-name' ], help = 'Name of the built program or library' ),
2141
2151
dict (name = ['-S' , '--supported' ], dest = 'supported' , action = 'store_true' , help = 'Shows supported matrix of targets and toolchains' ),
2142
2152
dict (name = '--app-config' , dest = "app_config" , help = "Path of an app configuration file (Default is to look for 'mbed_app.json')" ),
2143
2153
help = 'Compile code using the mbed build tools' ,
2144
2154
description = ("Compile this program using the mbed build tools." ))
2145
- def compile_ (toolchain = None , target = None , profile = False , compile_library = False , compile_config = False , config_prefix = None , source = False , build = False , clean = False , artifact_name = None , supported = False , app_config = None ):
2155
+ def compile_ (toolchain = None , target = None , profile = False , compile_library = False , compile_config = False , config_prefix = None , source = False , build = False , clean = False , flash = False , artifact_name = None , supported = False , app_config = None ):
2146
2156
# Gather remaining arguments
2147
2157
args = remainder
2148
2158
# Find the root of the program
@@ -2221,6 +2231,23 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2221
2231
+ (['-v' ] if verbose else [])
2222
2232
+ args ,
2223
2233
env = env )
2234
+
2235
+ if flash :
2236
+ fw_name = artifact_name if artifact_name else program .name
2237
+ fw_fbase = os .path .join (build_path , fw_name )
2238
+ fw_file = fw_fbase + ('.hex' if os .path .exists (fw_fbase + '.hex' ) else '.bin' )
2239
+ if not fw_file :
2240
+ error ("Firmware file not found \" %s\" " % fw_file )
2241
+ detected = program .detect_target ()
2242
+
2243
+ try :
2244
+ from mbed_host_tests .host_tests_toolbox import flash_dev , reset_dev
2245
+ except (IOError , ImportError , OSError ):
2246
+ error ("The target programing requires that the 'mbed-greentea' python module is installed.\n You can install mbed-ls by running 'pip install mbed-greentea'." )
2247
+ return False
2248
+
2249
+ flash_dev (detected ['msd' ], fw_file , program_cycle_s = 0 )
2250
+ reset_dev (detected ['port' ])
2224
2251
2225
2252
program .set_defaults (target = target , toolchain = tchain )
2226
2253
0 commit comments