Skip to content

Commit c931e5d

Browse files
committed
Add build_memory_info for esp32s2
This detects an overflowed flash partition, such as ``` 1452105 bytes used, -10313 bytes free in flash firmware space out of 1441792 bytes (1408.0kB). 444428 bytes used, 1652724 bytes free in ram for stack and heap out of 2097152 bytes (2048.0kB). ``` on a metro esp32-s2 built with debugging.
1 parent 35ee4ad commit c931e5d

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

ports/esp32s2/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
build*
1+
build*/
22
sdkconfig.old

ports/esp32s2/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
316316
$(BUILD)/firmware.elf: $(OBJ) | esp-idf-stamp
317317
$(STEPECHO) "LINK $@"
318318
$(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
319-
# $(Q)$(SIZE) $@ | $(PYTHON3) $(TOP)/tools/build_memory_info.py $(BUILD)/esp-idf/esp-idf/esp32s2/esp32s2_out.ld
319+
$(Q)$(SIZE) $@ | $(PYTHON3) tools/build_memory_info.py $(BUILD)/esp-idf/sdkconfig
320320

321321
$(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf
322322
$(STEPECHO) "Create $@"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
3+
# SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
4+
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
5+
#
6+
# SPDX-License-Identifier: MIT
7+
8+
import re
9+
import sys
10+
11+
# Handle size constants with K or M suffixes (allowed in .ld but not in Python).
12+
K_PATTERN = re.compile(r"([0-9]+)[kK]")
13+
K_REPLACE = r"(\1*1024)"
14+
15+
M_PATTERN = re.compile(r"([0-9]+)[mM]")
16+
M_REPLACE = r"(\1*1024*1024)"
17+
18+
print()
19+
20+
text = 0
21+
data = 0
22+
bss = 0
23+
# stdin is the linker output.
24+
for line in sys.stdin:
25+
# Uncomment to see linker output.
26+
# print(line)
27+
line = line.strip()
28+
if not line.startswith("text"):
29+
text, data, bss = map(int, line.split()[:3])
30+
31+
32+
def partition_size(arg):
33+
if "4MB" in arg:
34+
return "1408K"
35+
else:
36+
return "2048K"
37+
38+
39+
regions = {}
40+
# This file is the linker script.
41+
with open(sys.argv[1], "r") as f:
42+
for line in f:
43+
line = line.strip()
44+
if line.startswith("CONFIG_SPIRAM_SIZE"):
45+
regions["RAM"] = line.split("=")[-1]
46+
if line.startswith("CONFIG_PARTITION_TABLE_FILENAME"):
47+
regions["FLASH_FIRMWARE"] = partition_size(line.split("=")[-1])
48+
49+
for region in regions:
50+
space = regions[region]
51+
if "/*" in space:
52+
space = space.split("/*")[0]
53+
space = K_PATTERN.sub(K_REPLACE, space)
54+
space = M_PATTERN.sub(M_REPLACE, space)
55+
regions[region] = int(eval(space))
56+
57+
firmware_region = regions["FLASH_FIRMWARE"]
58+
ram_region = regions["RAM"]
59+
60+
used_flash = data + text
61+
free_flash = firmware_region - used_flash
62+
used_ram = data + bss
63+
free_ram = ram_region - used_ram
64+
print(
65+
"{} bytes used, {} bytes free in flash firmware space out of {} bytes ({}kB).".format(
66+
used_flash, free_flash, firmware_region, firmware_region / 1024
67+
)
68+
)
69+
print(
70+
"{} bytes used, {} bytes free in ram for stack and heap out of {} bytes ({}kB).".format(
71+
used_ram, free_ram, ram_region, ram_region / 1024
72+
)
73+
)
74+
print()
75+
76+
# Check that we have free flash space. GCC doesn't fail when the text + data
77+
# sections don't fit in FLASH. It only counts data in RAM.
78+
if free_flash < 0:
79+
print("Too little flash!!!")
80+
print()
81+
sys.exit(-1)

0 commit comments

Comments
 (0)