Skip to content

Add build_memory_info for esp32s2 #4763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ports/esp32s2/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
build*
build*/
sdkconfig.old
4 changes: 2 additions & 2 deletions ports/esp32s2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
$(BUILD)/firmware.elf: $(OBJ) | esp-idf-stamp
$(STEPECHO) "LINK $@"
$(Q)$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(ESP_IDF_COMPONENTS_EXPANDED) $(BINARY_BLOBS) $(MBEDTLS_COMPONENTS_LINK_EXPANDED) build-$(BOARD)/esp-idf/esp-idf/newlib/libnewlib.a -Wl,--end-group -u newlib_include_pthread_impl -Wl,--start-group $(LIBS) -Wl,--end-group build-$(BOARD)/esp-idf/esp-idf/pthread/libpthread.a -u __cxx_fatal_exception
# $(Q)$(SIZE) $@ | $(PYTHON3) $(TOP)/tools/build_memory_info.py $(BUILD)/esp-idf/esp-idf/esp32s2/esp32s2_out.ld

$(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf
$(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf | tools/build_memory_info.py
$(STEPECHO) "Create $@"
$(Q)esptool.py --chip esp32s2 elf2image $(FLASH_FLAGS) --elf-sha256-offset 0xb0 -o $@ $^
$(Q)$(PYTHON3) tools/build_memory_info.py $< $(BUILD)/esp-idf/sdkconfig $@

$(BUILD)/firmware.bin: $(BUILD)/circuitpython-firmware.bin | esp-idf-stamp
$(Q)$(PYTHON) ../../tools/join_bins.py $@ 0x1000 $(BUILD)/esp-idf/bootloader/bootloader.bin 0x8000 $(BUILD)/esp-idf/partition_table/partition-table.bin 0x10000 $(BUILD)/circuitpython-firmware.bin
Expand Down
84 changes: 84 additions & 0 deletions ports/esp32s2/tools/build_memory_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3

# SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
#
# SPDX-License-Identifier: MIT

import os
import re
import sys

from elftools.elf.elffile import ELFFile

print()

internal_memory = [
# Name, Start, Length
("RTC Fast Memory", (0x3FF9_E000, 0x4007_0000), 8 * 1024),
("RTC Slow Memory", (0x5000_0000,), 8 * 1024),
("Internal SRAM 0", (0x3FFB_0000, 0x4002_0000), 32 * 1024),
("Internal SRAM 1", (0x3FFB_8000, 0x4002_8000), 288 * 1024),
]


def partition_size(arg):
if "4MB" in arg:
return 1408 * 1024
else:
return 2048 * 1024


def align(n, m):
return m * ((n + m - 1) // m)


regions = dict((name, 0) for name, _, _ in internal_memory)

# This file is the elf
with open(sys.argv[1], "rb") as stream:
elffile = ELFFile(stream)
for section in elffile.iter_sections():
start = section["sh_addr"]
size = section["sh_size"]
offset = section["sh_offset"]
if not size or not start:
continue
for name, starts, length in internal_memory:
for mem_start in starts:
mem_end = mem_start + length
if start >= mem_start and start < mem_end:
regions[name] = max(regions.get(name, 0), size)
# print("# putting %s in %s (start=0x%x, size=%d)" % (section.name, name, start, size))

# This file is the sdkconfig
with open(sys.argv[2], "r") as f:
for line in f:
line = line.strip()
if line.startswith("CONFIG_PARTITION_TABLE_FILENAME"):
firmware_region = int(partition_size(line.split("=")[-1]))

# This file is the bin
used_flash = os.stat(sys.argv[3]).st_size

free_flash = firmware_region - used_flash
print(
"{:7} bytes used, {:7} bytes free in flash firmware space out of {} bytes ({}kB).".format(
used_flash, free_flash, firmware_region, firmware_region / 1024
)
)
for name, mem_start, length in internal_memory:
if name in regions:
print(
"{:7} bytes used, {:7} bytes free in '{}' out of {} bytes ({}kB).".format(
regions[name], length - regions[name], name, length, length / 1024
)
)
print()

# Check that we have free flash space. GCC doesn't fail when the text + data
# sections don't fit in FLASH. It only counts data in RAM.
if free_flash < 0:
print("Too little flash!!!")
print()
sys.exit(-1)