From 71b3ce3aceb25139d3f41af9441ade9d446498bb Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 2 Feb 2022 20:14:37 +1100 Subject: [PATCH] esp32: Create .uf2 binaries for S2 and S3 chips. The name of the filesystem partition is updated to support "ffat", as used by TinyUF2. Signed-off-by: Damien George --- ports/esp32/Makefile | 5 +++-- ports/esp32/makeimg.py | 31 +++++++++++++++++++++++++++++-- ports/esp32/modules/flashbdev.py | 3 +++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 403b698a32..c8ca9262c8 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -36,8 +36,9 @@ all: $(BUILD)/sdkconfig \ $(BUILD)/bootloader/bootloader.bin \ $(BUILD)/partition_table/partition-table.bin \ - $(BUILD)/micropython.bin \ - $(BUILD)/firmware.bin + $(BUILD)/micropython.bin \ + $(BUILD)/firmware.bin \ + $(BUILD)/micropython.uf2 $(BUILD)/bootloader/bootloader.bin $(BUILD)/partition_table/partition-table.bin $(BUILD)/micropython.bin: FORCE diff --git a/ports/esp32/makeimg.py b/ports/esp32/makeimg.py index 0646ce372c..5964e91d28 100644 --- a/ports/esp32/makeimg.py +++ b/ports/esp32/makeimg.py @@ -10,15 +10,29 @@ OFFSET_BOOTLOADER_DEFAULT = 0x1000 OFFSET_PARTITIONS_DEFAULT = 0x8000 -def load_sdkconfig_hex_value(filename, value, default): +def load_sdkconfig_value(filename, value, default): value = "CONFIG_" + value + "=" with open(filename, "r") as f: for line in f: if line.startswith(value): - return int(line.split("=", 1)[1], 16) + return line.split("=", 1)[1] return default +def load_sdkconfig_hex_value(filename, value, default): + value = load_sdkconfig_value(filename, value, None) + if value is None: + return default + return int(value, 16) + + +def load_sdkconfig_str_value(filename, value, default): + value = load_sdkconfig_value(filename, value, None) + if value is None: + return default + return value.strip().strip('"') + + def load_partition_table(filename): with open(filename, "rb") as f: return gen_esp32part.PartitionTable.from_binary(f.read()) @@ -30,8 +44,10 @@ arg_bootloader_bin = sys.argv[2] arg_partitions_bin = sys.argv[3] arg_application_bin = sys.argv[4] arg_output_bin = sys.argv[5] +arg_output_uf2 = sys.argv[6] # Load required sdkconfig values. +idf_target = load_sdkconfig_str_value(arg_sdkconfig, "IDF_TARGET", "").upper() offset_bootloader = load_sdkconfig_hex_value( arg_sdkconfig, "BOOTLOADER_OFFSET_IN_FLASH", OFFSET_BOOTLOADER_DEFAULT ) @@ -85,3 +101,14 @@ with open(file_out, "wb") as fout: ) sys.exit(1) print("%-22s% 8d" % ("total", cur_offset)) + +# Generate .uf2 file if the SoC has native USB. +if idf_target in ("ESP32S2", "ESP32S3"): + sys.path.append(os.path.join(os.path.dirname(__file__), "../../tools")) + import uf2conv + + families = uf2conv.load_families() + uf2conv.appstartaddr = 0 + uf2conv.familyid = families[idf_target] + with open(arg_application_bin, "rb") as fin, open(arg_output_uf2, "wb") as fout: + fout.write(uf2conv.convert_to_uf2(fin.read())) diff --git a/ports/esp32/modules/flashbdev.py b/ports/esp32/modules/flashbdev.py index bf4fec9f38..c3f75c7a7e 100644 --- a/ports/esp32/modules/flashbdev.py +++ b/ports/esp32/modules/flashbdev.py @@ -1,4 +1,7 @@ from esp32 import Partition +# MicroPython's partition table uses "vfs", TinyUF2 uses "ffat". bdev = Partition.find(Partition.TYPE_DATA, label="vfs") +if not bdev: + bdev = Partition.find(Partition.TYPE_DATA, label="ffat") bdev = bdev[0] if bdev else None