Skip to content

Commit 0b3808e

Browse files
authored
Merge pull request #375 from screamerbg/f/target-awareness
Target awareness, part 1
2 parents fbbc9dd + d41c7db commit 0b3808e

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Image: BUILD/K64F/GCC_ARM/mbed-os-program.bin
344344

345345
The arguments for *compile* are:
346346

347-
* `-m <MCU>` to select a target.
347+
* `-m <MCU>` to select a target. If 'detect' or 'auto' parameter is passed then mbed CLI will attempt to detect the connected target and compile against it.
348348
* `-t <TOOLCHAIN>` to select a toolchain (of those defined in `mbed_settings.py`, see above). The value can be either `ARM` (ARM Compiler 5), `GCC_ARM` (GNU ARM Embedded), or `IAR` (IAR Embedded Workbench for ARM).
349349
* `--source <SOURCE>` to select the source directory. The default is `.` (the current directorty). You can specify multiple source locations, even outside the program tree.
350350
* `--build <BUILD>` to select the build directory. Default: `BUILD/` inside your program.
@@ -443,6 +443,8 @@ $ mbed compile -t GCC_ARM -m K64F --profile mbed-os/tools/profiles/debug.json
443443

444444
Using `mbed target <target>` and `mbed toolchain <toolchain>` you can set the default target and toolchain for your program, meaning you won't have to specify these every time you compile or generate IDE project files.
445445

446+
You can also use `mbed target detect', which will attempt to detect the connected target board and use it as parameter every time you compile or export.
447+
446448

447449
## Exporting to desktop IDEs
448450

@@ -501,7 +503,7 @@ mbedgt: completed in 21.28 sec
501503
```
502504

503505
The arguments to `test` are:
504-
* `-m <MCU>` to select a target for the compilation.
506+
* `-m <MCU>` to select a target for the compilation. If 'detect' or 'auto' parameter is passed then mbed CLI will attempt to detect the connected target and compile against it.
505507
* `-t <TOOLCHAIN>` to select a toolchain (of those defined in `mbed_settings.py`, see above), where `toolchain` can be either `ARM` (ARM Compiler 5), `GCC_ARM` (GNU ARM Embedded), or `IAR` (IAR Embedded Workbench for ARM).
506508
* `--compile-list` to list all the tests that can be built
507509
* `--run-list` to list all the tests that can be ran (they must be built first)

mbed/mbed.py

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import shutil
2828
import stat
2929
import errno
30+
import ctypes
3031
from itertools import chain, izip, repeat
3132
from urlparse import urlparse
3233
import urllib2
@@ -1335,15 +1336,28 @@ def get_env(self):
13351336
def get_target(self, target=None):
13361337
target_cfg = self.get_cfg('TARGET')
13371338
target = target if target else target_cfg
1339+
1340+
if target and (target.lower() == 'detect' or target.lower() == 'auto'):
1341+
targets = self.get_detected_targets()
1342+
if targets == False:
1343+
error("The target detection requires that the 'mbed-ls' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-ls'.")
1344+
elif len(targets) > 1:
1345+
error("Multiple targets were detected.\nOnly 1 target board should be connected to your system when you use the '-m auto' switch.")
1346+
elif len(targets) == 0:
1347+
error("No targets were detected.\nPlease make sure a target board is connected to this system.")
1348+
else:
1349+
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1350+
target = targets[0]['name']
1351+
13381352
if target is None:
1339-
error('Please specify compile target using the -m switch or set default target using command "target"', 1)
1353+
error("Please specify target using the -m switch or set default target using command 'mbed target'", 1)
13401354
return target
13411355

13421356
def get_toolchain(self, toolchain=None):
13431357
toolchain_cfg = self.get_cfg('TOOLCHAIN')
13441358
tchain = toolchain if toolchain else toolchain_cfg
13451359
if tchain is None:
1346-
error('Please specify compile toolchain using the -t switch or set default toolchain using command "toolchain"', 1)
1360+
error("Please specify toolchain using the -t switch or set default toolchain using command 'mbed toolchain'", 1)
13471361
return tchain
13481362

13491363
def set_defaults(self, target=None, toolchain=None):
@@ -1370,6 +1384,28 @@ def ignore_build_dir(self):
13701384
except IOError:
13711385
error("Unable to write build ignore file in \"%s\"" % os.path.join(build_path, '.mbedignore'), 1)
13721386

1387+
def get_detected_targets(self):
1388+
targets = []
1389+
try:
1390+
import mbed_lstools
1391+
oldError = None
1392+
if os.name == 'nt':
1393+
oldError = ctypes.windll.kernel32.SetErrorMode(1) # Disable Windows error box temporarily. note that SEM_FAILCRITICALERRORS = 1
1394+
mbeds = mbed_lstools.create()
1395+
detect_muts_list = mbeds.list_mbeds()
1396+
if os.name == 'nt':
1397+
ctypes.windll.kernel32.SetErrorMode(oldError)
1398+
1399+
for mut in detect_muts_list:
1400+
targets.append({
1401+
'id': mut['target_id'], 'name': mut['platform_name'],
1402+
'mount': mut['mount_point'], 'serial': mut['serial_port']
1403+
})
1404+
except (IOError, ImportError, OSError):
1405+
return False
1406+
1407+
return targets
1408+
13731409

13741410
# Global class used for global config
13751411
class Global(object):
@@ -2270,18 +2306,25 @@ def detect():
22702306
# Gather remaining arguments
22712307
args = remainder
22722308
# Find the root of the program
2273-
program = Program(os.getcwd(), True)
2309+
program = Program(os.getcwd(), False)
22742310
program.check_requirements(True)
22752311
# Change directories to the program root to use mbed OS tools
22762312
with cd(program.path):
2277-
tools_dir = program.get_tools()
2313+
tools_dir = program.get_tools_dir()
22782314

2279-
# Prepare environment variables
2280-
env = program.get_env()
2315+
if tools_dir:
2316+
# Prepare environment variables
2317+
env = program.get_env()
22812318

2282-
popen(['python', '-u', os.path.join(tools_dir, 'detect_targets.py')]
2283-
+ args,
2284-
env=env)
2319+
popen(['python', '-u', os.path.join(tools_dir, 'detect_targets.py')]
2320+
+ args,
2321+
env=env)
2322+
else:
2323+
warning("The mbed tools were not found in \"%s\". \nLimited information will be shown about connected mbed targets/boards" % program.path)
2324+
targets = program.get_detected_targets()
2325+
if targets:
2326+
for target in targets:
2327+
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (target['name'], target['mount'], target['serial']))
22852328

22862329

22872330
# Generic config command

0 commit comments

Comments
 (0)