Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions tools/targets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ def lpc_patch(t_self, resources, elf, binf):
t_self.notify.debug("LPC Patch: %s" % os.path.split(binf)[1])
patch(binf)


class MTSCode(object):
"""Generic MTS code"""
@staticmethod
Expand Down Expand Up @@ -499,11 +500,145 @@ def _combine_bins_helper(target_name, binf):
os.remove(binf)
os.rename(target, binf)

@staticmethod
def combine_bins_mts_dot(t_self, resources, elf, binf):
"""A hook for the MTS MDOT"""
MTSCode._combine_bins_helper("MTS_MDOT_F411RE", binf)

@staticmethod
def combine_bins_mts_dragonfly(t_self, resources, elf, binf):
"""A hoof for the MTS Dragonfly"""
MTSCode._combine_bins_helper("MTS_DRAGONFLY_F411RE", binf)

@staticmethod
def combine_bins_mtb_mts_dragonfly(t_self, resources, elf, binf):
"""A hook for the MTB MTS Dragonfly"""
MTSCode._combine_bins_helper("MTB_MTS_DRAGONFLY", binf)


class LPC4088Code(object):
"""Code specific to the LPC4088"""
@staticmethod
def binary_hook(t_self, resources, elf, binf):
"""Hook to be run after an elf file is built"""
if not os.path.isdir(binf):
# Regular binary file, nothing to do
LPCTargetCode.lpc_patch(t_self, resources, elf, binf)
return
outbin = open(binf + ".temp", "wb")
partf = open(os.path.join(binf, "ER_IROM1"), "rb")
# Pad the fist part (internal flash) with 0xFF to 512k
data = partf.read()
outbin.write(data)
outbin.write(b'\xFF' * (512*1024 - len(data)))
partf.close()
# Read and append the second part (external flash) in chunks of fixed
# size
chunksize = 128 * 1024
partf = open(os.path.join(binf, "ER_IROM2"), "rb")
while True:
data = partf.read(chunksize)
outbin.write(data)
if len(data) < chunksize:
break
partf.close()
outbin.close()
# Remove the directory with the binary parts and rename the temporary
# file to 'binf'
shutil.rmtree(binf, True)
os.rename(binf + '.temp', binf)
t_self.notify.debug(
"Generated custom binary file (internal flash + SPIFI)"
)
LPCTargetCode.lpc_patch(t_self, resources, elf, binf)


class TEENSY3_1Code(object):
"""Hooks for the TEENSY3.1"""
@staticmethod
def binary_hook(t_self, resources, elf, binf):
"""Hook that is run after elf is generated"""
# This function is referenced by old versions of targets.json and
# should be kept for backwards compatibility.
pass


class MCU_NRF51Code(object):
"""NRF51 Hooks"""
@staticmethod
def binary_hook(t_self, resources, _, binf):
"""Hook that merges the soft device with the bin file"""
# Scan to find the actual paths of soft device
sdf = None
sd_with_offsets = t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS
for softdevice_and_offset_entry in sd_with_offsets:
for hexf in resources.get_file_paths(FileType.HEX):
if hexf.find(softdevice_and_offset_entry['name']) != -1:
t_self.notify.debug("SoftDevice file found %s."
% softdevice_and_offset_entry['name'])
sdf = hexf

if sdf is not None:
break
if sdf is not None:
break

if sdf is None:
t_self.notify.debug("Hex file not found. Aborting.")
return

# Look for bootloader file that matches this soft device or bootloader
# override image
blf = None
if t_self.target.MERGE_BOOTLOADER is True:
for hexf in resources.get_file_paths(FileType.HEX):
if hexf.find(t_self.target.OVERRIDE_BOOTLOADER_FILENAME) != -1:
t_self.notify.debug(
"Bootloader file found %s."
% t_self.target.OVERRIDE_BOOTLOADER_FILENAME
)
blf = hexf
break
elif hexf.find(softdevice_and_offset_entry['boot']) != -1:
t_self.notify.debug("Bootloader file found %s."
% softdevice_and_offset_entry['boot'])
blf = hexf
break

# Merge user code with softdevice
from intelhex import IntelHex
binh = IntelHex()
_, ext = os.path.splitext(binf)
if ext == ".hex":
binh.loadhex(binf)
elif ext == ".bin":
binh.loadbin(binf, softdevice_and_offset_entry['offset'])

if t_self.target.MERGE_SOFT_DEVICE is True:
t_self.notify.debug("Merge SoftDevice file %s"
% softdevice_and_offset_entry['name'])
sdh = IntelHex(sdf)
sdh.start_addr = None
binh.merge(sdh)

if t_self.target.MERGE_BOOTLOADER is True and blf is not None:
t_self.notify.debug("Merge BootLoader file %s" % blf)
blh = IntelHex(blf)
blh.start_addr = None
binh.merge(blh)

with open(binf.replace(".bin", ".hex"), "w") as fileout:
binh.write_hex_file(fileout, write_start_addr=False)


class NCS36510TargetCode(object):
@staticmethod
def ncs36510_addfib(t_self, resources, elf, binf):
from tools.targets.NCS import add_fib_at_start
print("binf ", binf)
add_fib_at_start(binf[:-4])


class RTL8195ACode(object):
"""RTL8195A Hooks"""
@staticmethod
Expand Down