Skip to content

Commit 93be66e

Browse files
authored
Merge pull request #13081 from ARMmbed/rw-post-build-hooks
Reinstate post build hooks for legacy targets
2 parents d19b23b + 6ee9b16 commit 93be66e

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

tools/targets/__init__.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ def lpc_patch(t_self, resources, elf, binf):
470470
t_self.notify.debug("LPC Patch: %s" % os.path.split(binf)[1])
471471
patch(binf)
472472

473+
473474
class MTSCode(object):
474475
"""Generic MTS code"""
475476
@staticmethod
@@ -499,11 +500,145 @@ def _combine_bins_helper(target_name, binf):
499500
os.remove(binf)
500501
os.rename(target, binf)
501502

503+
@staticmethod
504+
def combine_bins_mts_dot(t_self, resources, elf, binf):
505+
"""A hook for the MTS MDOT"""
506+
MTSCode._combine_bins_helper("MTS_MDOT_F411RE", binf)
507+
502508
@staticmethod
503509
def combine_bins_mts_dragonfly(t_self, resources, elf, binf):
504510
"""A hoof for the MTS Dragonfly"""
505511
MTSCode._combine_bins_helper("MTS_DRAGONFLY_F411RE", binf)
506512

513+
@staticmethod
514+
def combine_bins_mtb_mts_dragonfly(t_self, resources, elf, binf):
515+
"""A hook for the MTB MTS Dragonfly"""
516+
MTSCode._combine_bins_helper("MTB_MTS_DRAGONFLY", binf)
517+
518+
519+
class LPC4088Code(object):
520+
"""Code specific to the LPC4088"""
521+
@staticmethod
522+
def binary_hook(t_self, resources, elf, binf):
523+
"""Hook to be run after an elf file is built"""
524+
if not os.path.isdir(binf):
525+
# Regular binary file, nothing to do
526+
LPCTargetCode.lpc_patch(t_self, resources, elf, binf)
527+
return
528+
outbin = open(binf + ".temp", "wb")
529+
partf = open(os.path.join(binf, "ER_IROM1"), "rb")
530+
# Pad the fist part (internal flash) with 0xFF to 512k
531+
data = partf.read()
532+
outbin.write(data)
533+
outbin.write(b'\xFF' * (512*1024 - len(data)))
534+
partf.close()
535+
# Read and append the second part (external flash) in chunks of fixed
536+
# size
537+
chunksize = 128 * 1024
538+
partf = open(os.path.join(binf, "ER_IROM2"), "rb")
539+
while True:
540+
data = partf.read(chunksize)
541+
outbin.write(data)
542+
if len(data) < chunksize:
543+
break
544+
partf.close()
545+
outbin.close()
546+
# Remove the directory with the binary parts and rename the temporary
547+
# file to 'binf'
548+
shutil.rmtree(binf, True)
549+
os.rename(binf + '.temp', binf)
550+
t_self.notify.debug(
551+
"Generated custom binary file (internal flash + SPIFI)"
552+
)
553+
LPCTargetCode.lpc_patch(t_self, resources, elf, binf)
554+
555+
556+
class TEENSY3_1Code(object):
557+
"""Hooks for the TEENSY3.1"""
558+
@staticmethod
559+
def binary_hook(t_self, resources, elf, binf):
560+
"""Hook that is run after elf is generated"""
561+
# This function is referenced by old versions of targets.json and
562+
# should be kept for backwards compatibility.
563+
pass
564+
565+
566+
class MCU_NRF51Code(object):
567+
"""NRF51 Hooks"""
568+
@staticmethod
569+
def binary_hook(t_self, resources, _, binf):
570+
"""Hook that merges the soft device with the bin file"""
571+
# Scan to find the actual paths of soft device
572+
sdf = None
573+
sd_with_offsets = t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS
574+
for softdevice_and_offset_entry in sd_with_offsets:
575+
for hexf in resources.get_file_paths(FileType.HEX):
576+
if hexf.find(softdevice_and_offset_entry['name']) != -1:
577+
t_self.notify.debug("SoftDevice file found %s."
578+
% softdevice_and_offset_entry['name'])
579+
sdf = hexf
580+
581+
if sdf is not None:
582+
break
583+
if sdf is not None:
584+
break
585+
586+
if sdf is None:
587+
t_self.notify.debug("Hex file not found. Aborting.")
588+
return
589+
590+
# Look for bootloader file that matches this soft device or bootloader
591+
# override image
592+
blf = None
593+
if t_self.target.MERGE_BOOTLOADER is True:
594+
for hexf in resources.get_file_paths(FileType.HEX):
595+
if hexf.find(t_self.target.OVERRIDE_BOOTLOADER_FILENAME) != -1:
596+
t_self.notify.debug(
597+
"Bootloader file found %s."
598+
% t_self.target.OVERRIDE_BOOTLOADER_FILENAME
599+
)
600+
blf = hexf
601+
break
602+
elif hexf.find(softdevice_and_offset_entry['boot']) != -1:
603+
t_self.notify.debug("Bootloader file found %s."
604+
% softdevice_and_offset_entry['boot'])
605+
blf = hexf
606+
break
607+
608+
# Merge user code with softdevice
609+
from intelhex import IntelHex
610+
binh = IntelHex()
611+
_, ext = os.path.splitext(binf)
612+
if ext == ".hex":
613+
binh.loadhex(binf)
614+
elif ext == ".bin":
615+
binh.loadbin(binf, softdevice_and_offset_entry['offset'])
616+
617+
if t_self.target.MERGE_SOFT_DEVICE is True:
618+
t_self.notify.debug("Merge SoftDevice file %s"
619+
% softdevice_and_offset_entry['name'])
620+
sdh = IntelHex(sdf)
621+
sdh.start_addr = None
622+
binh.merge(sdh)
623+
624+
if t_self.target.MERGE_BOOTLOADER is True and blf is not None:
625+
t_self.notify.debug("Merge BootLoader file %s" % blf)
626+
blh = IntelHex(blf)
627+
blh.start_addr = None
628+
binh.merge(blh)
629+
630+
with open(binf.replace(".bin", ".hex"), "w") as fileout:
631+
binh.write_hex_file(fileout, write_start_addr=False)
632+
633+
634+
class NCS36510TargetCode(object):
635+
@staticmethod
636+
def ncs36510_addfib(t_self, resources, elf, binf):
637+
from tools.targets.NCS import add_fib_at_start
638+
print("binf ", binf)
639+
add_fib_at_start(binf[:-4])
640+
641+
507642
class RTL8195ACode(object):
508643
"""RTL8195A Hooks"""
509644
@staticmethod

0 commit comments

Comments
 (0)