27
27
import shutil
28
28
import stat
29
29
import errno
30
+ import ctypes
30
31
from itertools import chain , izip , repeat
31
32
from urlparse import urlparse
32
33
import urllib2
@@ -1322,17 +1323,29 @@ def get_env(self):
1322
1323
return env
1323
1324
1324
1325
def get_target (self , target = None ):
1325
- target_cfg = self .get_cfg ('TARGET' )
1326
- target = target if target else target_cfg
1326
+ if target :
1327
+ if target .lower () == 'detect' or target .lower () == 'auto' :
1328
+ targets = self .get_detected_targets ()
1329
+ if targets == False :
1330
+ error ("The target detection requires that the 'mbed-ls' python module is installed." )
1331
+ elif len (targets ) > 1 :
1332
+ error ("Multiple targets were detected.\n Only 1 target board should be connected to your system when you use the '-m auto' switch." )
1333
+ elif len (targets ) == 0 :
1334
+ error ("No targets were detected.\n Please make sure a target board to this system." )
1335
+ else :
1336
+ action ("Detected \" %s\" connected to \" %s\" and using com port \" %s\" " % (targets [0 ]['name' ], targets [0 ]['mount' ], targets [0 ]['serial' ]))
1337
+ target = targets [0 ]['name' ]
1338
+ else :
1339
+ target = self .get_cfg ('TARGET' )
1327
1340
if target is None :
1328
- error (' Please specify compile target using the -m switch or set default target using command " target"' , 1 )
1341
+ error (" Please specify target using the -m switch or set default target using command 'mbed target'" , 1 )
1329
1342
return target
1330
1343
1331
1344
def get_toolchain (self , toolchain = None ):
1332
1345
toolchain_cfg = self .get_cfg ('TOOLCHAIN' )
1333
1346
tchain = toolchain if toolchain else toolchain_cfg
1334
1347
if tchain is None :
1335
- error (' Please specify compile toolchain using the -t switch or set default toolchain using command " toolchain"' , 1 )
1348
+ error (" Please specify toolchain using the -t switch or set default toolchain using command 'mbed toolchain'" , 1 )
1336
1349
return tchain
1337
1350
1338
1351
def set_defaults (self , target = None , toolchain = None ):
@@ -1359,6 +1372,28 @@ def ignore_build_dir(self):
1359
1372
except IOError :
1360
1373
error ("Unable to write build ignore file in \" %s\" " % os .path .join (build_path , '.mbedignore' ), 1 )
1361
1374
1375
+ def get_detected_targets (self ):
1376
+ targets = []
1377
+ try :
1378
+ import mbed_lstools
1379
+ oldError = None
1380
+ if os .name == 'nt' :
1381
+ oldError = ctypes .windll .kernel32 .SetErrorMode (1 ) # Disable Windows error box temporarily. note that SEM_FAILCRITICALERRORS = 1
1382
+ mbeds = mbed_lstools .create ()
1383
+ detect_muts_list = mbeds .list_mbeds ()
1384
+ if os .name == 'nt' :
1385
+ ctypes .windll .kernel32 .SetErrorMode (oldError )
1386
+
1387
+ for mut in detect_muts_list :
1388
+ targets .append ({
1389
+ 'id' : mut ['target_id' ], 'name' : mut ['platform_name' ],
1390
+ 'mount' : mut ['mount_point' ], 'serial' : mut ['serial_port' ]
1391
+ })
1392
+ except (IOError , ImportError , OSError ):
1393
+ return False
1394
+
1395
+ return targets
1396
+
1362
1397
1363
1398
# Global class used for global config
1364
1399
class Global (object ):
@@ -2261,18 +2296,25 @@ def detect():
2261
2296
# Gather remaining arguments
2262
2297
args = remainder
2263
2298
# Find the root of the program
2264
- program = Program (os .getcwd (), True )
2299
+ program = Program (os .getcwd (), False )
2265
2300
program .check_requirements (True )
2266
2301
# Change directories to the program root to use mbed OS tools
2267
2302
with cd (program .path ):
2268
- tools_dir = program .get_tools ()
2303
+ tools_dir = program .get_tools_dir ()
2269
2304
2270
- # Prepare environment variables
2271
- env = program .get_env ()
2305
+ if tools_dir :
2306
+ # Prepare environment variables
2307
+ env = program .get_env ()
2272
2308
2273
- popen (['python' , '-u' , os .path .join (tools_dir , 'detect_targets.py' )]
2274
- + args ,
2275
- env = env )
2309
+ popen (['python' , '-u' , os .path .join (tools_dir , 'detect_targets.py' )]
2310
+ + args ,
2311
+ env = env )
2312
+ else :
2313
+ warning ("The mbed tools were not found in \" %s\" . \n Limited information will be shown about connected mbed targets/boards" % program .path )
2314
+ targets = program .get_detected_targets ()
2315
+ if targets :
2316
+ for target in targets :
2317
+ action ("Detected \" %s\" connected to \" %s\" and using com port \" %s\" " % (target ['name' ], target ['mount' ], target ['serial' ]))
2276
2318
2277
2319
2278
2320
# Generic config command
0 commit comments