|
| 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