diff --git a/boards/xtensa/esp32s2/common/Kconfig b/boards/xtensa/esp32s2/common/Kconfig
index 18c7905aed..67d129236b 100644
--- a/boards/xtensa/esp32s2/common/Kconfig
+++ b/boards/xtensa/esp32s2/common/Kconfig
@@ -3,3 +3,11 @@
# see the file kconfig-language.txt in the NuttX tools repository.
#
+config ESP32S2_MERGE_BINS
+ bool "Merge raw binary files into a single file"
+ default n
+ ---help---
+ Merge the raw binary files into a single file for flashing to the
+ device.
+ This is only useful when the path to binary files (e.g. bootloader)
+ is provided via the ESPTOOL_BINDIR variable.
diff --git a/tools/esp32s2/Config.mk b/tools/esp32s2/Config.mk
index 54f9d7670b..bd61b102c0 100644
--- a/tools/esp32s2/Config.mk
+++ b/tools/esp32s2/Config.mk
@@ -61,18 +61,45 @@ else
endif
ifdef ESPTOOL_BINDIR
- BL_OFFSET=0x1000
- PT_OFFSET=0x8000
- BOOTLOADER=$(ESPTOOL_BINDIR)/bootloader-esp32s2.bin
- PARTITION_TABLE=$(ESPTOOL_BINDIR)/partition-table-esp32s2.bin
- FLASH_BL=$(BL_OFFSET) $(BOOTLOADER)
- FLASH_PT=$(PT_OFFSET) $(PARTITION_TABLE)
+ BL_OFFSET := 0x1000
+ PT_OFFSET := 0x8000
+ BOOTLOADER := $(ESPTOOL_BINDIR)/bootloader-esp32s2.bin
+ PARTITION_TABLE := $(ESPTOOL_BINDIR)/partition-table-esp32s2.bin
+ FLASH_BL := $(BL_OFFSET) $(BOOTLOADER)
+ FLASH_PT := $(PT_OFFSET) $(PARTITION_TABLE)
+ ESPTOOL_BINS := $(FLASH_BL) $(FLASH_PT)
+endif
+
+ESPTOOL_BINS += 0x10000 nuttx.bin
+
+# MERGEBIN -- Merge raw binary files into a single file
+
+ifeq ($(CONFIG_ESP32S2_MERGE_BINS),y)
+define MERGEBIN
+ $(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
+ echo "MERGEBIN error: Missing argument for binary files directory."; \
+ echo "USAGE: make ESPTOOL_BINDIR=
"; \
+ exit 1; \
+ fi
+ $(Q) if [ -z $(FLASH_SIZE) ]; then \
+ echo "Missing Flash memory size configuration for the ESP32-S2 chip."; \
+ exit 1; \
+ fi
+ $(eval ESPTOOL_MERGEBIN_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff $(FLASH_FREQ))
+ esptool.py -c esp32s2 merge_bin --output nuttx.merged.bin $(ESPTOOL_MERGEBIN_OPTS) $(ESPTOOL_BINS)
+ $(Q) echo nuttx.merged.bin >> nuttx.manifest
+ $(Q) echo "Generated: nuttx.merged.bin"
+endef
+else
+define MERGEBIN
+
+endef
endif
# POSTBUILD -- Perform post build operations
define POSTBUILD
- $(Q) echo "MKIMAGE: ESP32S2 binary"
+ $(Q) echo "MKIMAGE: ESP32-S2 binary"
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
echo ""; \
echo "esptool.py not found. Please run: \"pip install esptool\""; \
@@ -81,11 +108,12 @@ define POSTBUILD
exit 1; \
fi
$(Q) if [ -z $(FLASH_SIZE) ]; then \
- echo "Missing Flash memory size configuration for the ESP32S2 chip."; \
+ echo "Missing Flash memory size configuration for the ESP32-S2 chip."; \
exit 1; \
fi
- esptool.py -c esp32-s2 elf2image $(ESPTOOL_ELF2IMG_OPTS) -o nuttx.bin nuttx
- $(Q) echo "Generated: nuttx.bin (ESP32S2 compatible)"
+ esptool.py -c esp32s2 elf2image $(ESPTOOL_ELF2IMG_OPTS) -o nuttx.bin nuttx
+ $(Q) echo "Generated: nuttx.bin (ESP32-S2 compatible)"
+ $(call MERGEBIN)
endef
# ESPTOOL_BAUD -- Serial port baud rate used when flashing/reading via esptool.py
@@ -95,13 +123,10 @@ ESPTOOL_BAUD ?= 921600
# DOWNLOAD -- Download binary image via esptool.py
define DOWNLOAD
-
- $(eval ESPTOOL_BINS := $(FLASH_BL) $(FLASH_PT) 0x10000 $(1).bin)
-
$(Q) if [ -z $(ESPTOOL_PORT) ]; then \
echo "DOWNLOAD error: Missing serial port device argument."; \
echo "USAGE: make download ESPTOOL_PORT= [ ESPTOOL_BAUD= ] [ ESPTOOL_BINDIR= ]"; \
exit 1; \
fi
- esptool.py -c esp32-s2 -p $(ESPTOOL_PORT) -b $(ESPTOOL_BAUD) write_flash $(ESPTOOL_WRITEFLASH_OPTS) $(ESPTOOL_BINS)
+ esptool.py -c esp32s2 -p $(ESPTOOL_PORT) -b $(ESPTOOL_BAUD) write_flash $(ESPTOOL_WRITEFLASH_OPTS) $(ESPTOOL_BINS)
endef