Skip to content

Commit 756fc38

Browse files
committed
Reintroduce NRF51 post-build hook.
The online compiler relies on this post build hook for NRF51 targets.
1 parent a28e809 commit 756fc38

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tools/targets/__init__.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,75 @@ def combine_bins_mts_dragonfly(t_self, resources, elf, binf):
504504
"""A hoof for the MTS Dragonfly"""
505505
MTSCode._combine_bins_helper("MTS_DRAGONFLY_F411RE", binf)
506506

507+
508+
class MCU_NRF51Code(object):
509+
"""NRF51 Hooks"""
510+
@staticmethod
511+
def binary_hook(t_self, resources, _, binf):
512+
"""Hook that merges the soft device with the bin file"""
513+
# Scan to find the actual paths of soft device
514+
sdf = None
515+
sd_with_offsets = t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS
516+
for softdevice_and_offset_entry in sd_with_offsets:
517+
for hexf in resources.get_file_paths(FileType.HEX):
518+
if hexf.find(softdevice_and_offset_entry['name']) != -1:
519+
t_self.notify.debug("SoftDevice file found %s."
520+
% softdevice_and_offset_entry['name'])
521+
sdf = hexf
522+
523+
if sdf is not None:
524+
break
525+
if sdf is not None:
526+
break
527+
528+
if sdf is None:
529+
t_self.notify.debug("Hex file not found. Aborting.")
530+
return
531+
532+
# Look for bootloader file that matches this soft device or bootloader
533+
# override image
534+
blf = None
535+
if t_self.target.MERGE_BOOTLOADER is True:
536+
for hexf in resources.get_file_paths(FileType.HEX):
537+
if hexf.find(t_self.target.OVERRIDE_BOOTLOADER_FILENAME) != -1:
538+
t_self.notify.debug(
539+
"Bootloader file found %s."
540+
% t_self.target.OVERRIDE_BOOTLOADER_FILENAME
541+
)
542+
blf = hexf
543+
break
544+
elif hexf.find(softdevice_and_offset_entry['boot']) != -1:
545+
t_self.notify.debug("Bootloader file found %s."
546+
% softdevice_and_offset_entry['boot'])
547+
blf = hexf
548+
break
549+
550+
# Merge user code with softdevice
551+
from intelhex import IntelHex
552+
binh = IntelHex()
553+
_, ext = os.path.splitext(binf)
554+
if ext == ".hex":
555+
binh.loadhex(binf)
556+
elif ext == ".bin":
557+
binh.loadbin(binf, softdevice_and_offset_entry['offset'])
558+
559+
if t_self.target.MERGE_SOFT_DEVICE is True:
560+
t_self.notify.debug("Merge SoftDevice file %s"
561+
% softdevice_and_offset_entry['name'])
562+
sdh = IntelHex(sdf)
563+
sdh.start_addr = None
564+
binh.merge(sdh)
565+
566+
if t_self.target.MERGE_BOOTLOADER is True and blf is not None:
567+
t_self.notify.debug("Merge BootLoader file %s" % blf)
568+
blh = IntelHex(blf)
569+
blh.start_addr = None
570+
binh.merge(blh)
571+
572+
with open(binf.replace(".bin", ".hex"), "w") as fileout:
573+
binh.write_hex_file(fileout, write_start_addr=False)
574+
575+
507576
class RTL8195ACode(object):
508577
"""RTL8195A Hooks"""
509578
@staticmethod

0 commit comments

Comments
 (0)