import struct Import("env") # noqa: F821 # Parse input and create UF2 file def create_uf2(source, target, env): # source_hex = target[0].get_abspath() source_hex = target[0].get_string(False) source_hex = ".\\" + source_hex print("#########################################################") print("Create UF2 from " + source_hex) print("#########################################################") # print("Source: " + source_hex) target = source_hex.replace(".hex", "") target = target + ".uf2" # print("Target: " + target) with open(source_hex, mode="rb") as f: inpbuf = f.read() outbuf = convert_from_hex_to_uf2(inpbuf.decode("utf-8")) write_file(target, outbuf) print("#########################################################") print(target + " is ready to flash to target device") print("#########################################################") # Add callback after .hex file was created env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", create_uf2) # noqa: F821 # UF2 creation taken from uf2conv.py UF2_MAGIC_START0 = 0x0A324655 # "UF2\n" UF2_MAGIC_START1 = 0x9E5D5157 # Randomly selected UF2_MAGIC_END = 0x0AB16F30 # Ditto familyid = 0xADA52840 class Block: def __init__(self, addr): self.addr = addr self.bytes = bytearray(256) def encode(self, blockno, numblocks): global familyid flags = 0x0 if familyid: flags |= 0x2000 hd = struct.pack( "