diff --git a/workspace_tools/build_api.py b/workspace_tools/build_api.py index 6a34ed47de..4595d35101 100644 --- a/workspace_tools/build_api.py +++ b/workspace_tools/build_api.py @@ -175,7 +175,7 @@ def build_mbed_libs(target, toolchain_name, options=None, verbose=False, clean=F # Target specific sources HAL_SRC = join(MBED_TARGETS_PATH, "hal") hal_implementation = toolchain.scan_resources(HAL_SRC) - toolchain.copy_files(hal_implementation.headers, BUILD_TARGET) + toolchain.copy_files(hal_implementation.headers + hal_implementation.hex_files, BUILD_TARGET) objects = toolchain.compile_sources(hal_implementation, TMP_PATH, [MBED_LIBRARIES, BUILD_TARGET]) # Common Sources diff --git a/workspace_tools/make.py b/workspace_tools/make.py index 5a6fe135f1..6bdc825f07 100644 --- a/workspace_tools/make.py +++ b/workspace_tools/make.py @@ -75,7 +75,8 @@ if __name__ == '__main__': default=None, help="The mbed serial port") parser.add_option("-b", "--baud", type="int", dest="baud", default=None, help="The mbed serial baud rate") - parser.add_option("--nrfjprog",dest="nrfjprog",default=None,help="Program nRF Chip via J-Link") + parser.add_option("--nrfjprog", dest="nrfjprog", action="store_true", + default=None, help="Program nRF Chip via J-Link") # Ideally, all the tests with a single "main" thread can be run with, or # without the rtos @@ -168,8 +169,7 @@ if __name__ == '__main__': copy(bin, options.disk) if options.nrfjprog: #Convert bin to Hex and Program nrf chip via jlink - call(["c:\\msdos\\msdos.exe","c:\\BIN2HEX.EXE",bin]) - call(["nrfjprog","-e","--program",bin.replace(".bin",".hex"),"--verify","-p"]) + call(["nrfjprog","-e","--program",bin.replace(".bin", ".hex"),"--verify","-p"]) if options.serial: # Import pyserial: https://pypi.python.org/pypi/pyserial from serial import Serial diff --git a/workspace_tools/targets.py b/workspace_tools/targets.py index 27f766827c..ce2386649e 100644 --- a/workspace_tools/targets.py +++ b/workspace_tools/targets.py @@ -176,7 +176,7 @@ class LPC4088(Target): hook.hook_add_binary("post", self.binary_hook) @staticmethod - def binary_hook(t_self, elf, binf): + def binary_hook(t_self, resources, elf, binf): if not os.path.isdir(binf): # Regular binary file, nothing to do return @@ -304,6 +304,9 @@ class LPC11U35_401(Target): class nRF51822(Target): + + EXPECTED_SOFTDEVICE = 's110_nrf51822_6.0.0_softdevice.hex' + def __init__(self): Target.__init__(self) @@ -313,6 +316,26 @@ class nRF51822(Target): self.supported_toolchains = ["ARM"] + def init_hooks(self, hook, toolchain_name): + if toolchain_name in ['ARM_STD', 'ARM_MICRO']: + hook.hook_add_binary("post", self.binary_hook) + + @staticmethod + def binary_hook(t_self, resources, elf, binf): + for hexf in resources.hex_files: + if hexf.find(nRF51822.EXPECTED_SOFTDEVICE) != -1: + break + else: + return + from intelhex import IntelHex + binh = IntelHex() + binh.loadbin(binf, offset = 0x14000) + sdh = IntelHex(hexf) + sdh.merge(binh) + outname = binf.replace(".bin", ".hex") + with open(outname, "w") as f: + sdh.tofile(f, format = 'hex') + t_self.debug("Generated SoftDevice-enabled image in '%s'" % outname) # Get a single instance for each target TARGETS = [ diff --git a/workspace_tools/toolchains/__init__.py b/workspace_tools/toolchains/__init__.py index 307f702a17..90f4903082 100644 --- a/workspace_tools/toolchains/__init__.py +++ b/workspace_tools/toolchains/__init__.py @@ -65,6 +65,9 @@ class Resources: self.repo_files = [] self.linker_script = None + + # Other files + self.hex_files = [] def add(self, resources): self.inc_dirs += resources.inc_dirs @@ -86,11 +89,13 @@ class Resources: if resources.linker_script is not None: self.linker_script = resources.linker_script - + + self.hex_files += resources.hex_files + def relative_to(self, base, dot=False): for field in ['inc_dirs', 'headers', 's_sources', 'c_sources', 'cpp_sources', 'lib_dirs', 'objects', 'libraries', - 'lib_builds', 'lib_refs', 'repo_dirs', 'repo_files']: + 'lib_builds', 'lib_refs', 'repo_dirs', 'repo_files', 'hex_files']: v = [rel_path(f, base, dot) for f in getattr(self, field)] setattr(self, field, v) if self.linker_script is not None: @@ -99,7 +104,7 @@ class Resources: def win_to_unix(self): for field in ['inc_dirs', 'headers', 's_sources', 'c_sources', 'cpp_sources', 'lib_dirs', 'objects', 'libraries', - 'lib_builds', 'lib_refs', 'repo_dirs', 'repo_files']: + 'lib_builds', 'lib_refs', 'repo_dirs', 'repo_files', 'hex_files']: v = [f.replace('\\', '/') for f in getattr(self, field)] setattr(self, field, v) if self.linker_script is not None: @@ -118,7 +123,9 @@ class Resources: ('Library directories', self.lib_dirs), ('Objects', self.objects), - ('Libraries', self.libraries) + ('Libraries', self.libraries), + + ('Hex files', self.hex_files), ): if resources: s.append('%s:\n ' % label + '\n '.join(resources)) @@ -296,14 +303,19 @@ class mbedToolchain: elif ext == self.LINKER_EXT: resources.linker_script = file_path - + elif ext == '.lib': resources.lib_refs.append(file_path) + elif ext == '.bld': resources.lib_builds.append(file_path) + elif file == '.hgignore': resources.repo_files.append(file_path) - + + elif ext == '.hex': + resources.hex_files.append(file_path) + return resources def scan_repository(self, path): @@ -432,7 +444,7 @@ class mbedToolchain: if self.need_update(bin, [elf]): self.progress("elf2bin", name) - self.binary(elf, bin) + self.binary(r, elf, bin) if self.target.name.startswith('LPC'): self.debug("LPC Patch %s" % (name + '.bin')) diff --git a/workspace_tools/toolchains/arm.py b/workspace_tools/toolchains/arm.py index 4a3e714eb7..2ee611f649 100644 --- a/workspace_tools/toolchains/arm.py +++ b/workspace_tools/toolchains/arm.py @@ -121,7 +121,7 @@ class ARM(mbedToolchain): self.default_cmd(self.ld + args + objects + libraries + self.sys_libs) @hook_tool - def binary(self, elf, bin): + def binary(self, resources, elf, bin): self.default_cmd([self.elf2bin, '--bin', '-o', bin, elf]) diff --git a/workspace_tools/toolchains/gcc.py b/workspace_tools/toolchains/gcc.py index ebeaa35a44..64cab533b5 100644 --- a/workspace_tools/toolchains/gcc.py +++ b/workspace_tools/toolchains/gcc.py @@ -156,7 +156,7 @@ class GCC(mbedToolchain): self.default_cmd(self.ld + ["-T%s" % mem_map, "-o", output] + objects + ["-L%s" % L for L in lib_dirs] + libs) - def binary(self, elf, bin): + def binary(self, resources, elf, bin): self.default_cmd([self.elf2bin, "-O", "binary", elf, bin]) diff --git a/workspace_tools/toolchains/iar.py b/workspace_tools/toolchains/iar.py index 87fcbe9781..8e022aa82b 100644 --- a/workspace_tools/toolchains/iar.py +++ b/workspace_tools/toolchains/iar.py @@ -102,5 +102,5 @@ class IAR(mbedToolchain): args = [self.ld, "-o", output, "--config", mem_map] self.default_cmd(args + objects + libraries) - def binary(self, elf, bin): + def binary(self, resources, elf, bin): self.default_cmd([self.elf2bin, '--bin', elf, bin])