## Script/command to auto-calculate Flash and SRAM usage for you
## Option 1 (preferred) `size_mcu` script I wrote
Happy New Year 2024! Here's a gift: you can use this brand new script now: [`size_mcu`](https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/size_mcu.sh) from my [eRCaGuy_dotfiles](https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles) repo.
This should work for any `.elf` file, not just STM32 `.elf` files, including PIC32, AVR, STM32, other ARM-core, etc.
Installation instructions are [in the top of the file](https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/size_mcu.sh).
Example usage using STM32's `arm-none-eabi-size` executable:
```bash
# specify the `size` executable, .elf file, and 128 KiB max flash and
# 20 KiB max RAM
# The `--flash` and `--ram` arguments are optional. If not specified,
# percent remaining and bytes remaining values will not be calculated.
size_mcu arm-none-eabi-size "STM32F103RB_Nucleo.elf" --flash 131072 --ram 20480
```
Example output (with added line breaks for readability):
```bash
size_info = 'arm-none-eabi-size "STM32F103RB_Nucleo.elf"' =
text data bss dec hex filename
2896 12 1588 4496 1190 STM32F103RB_Nucleo.elf
FLASH used = 2908 bytes ( 2.219%).
Remaining is 128164 bytes ( 97.781%).
Max is 131072 bytes.
SRAM used by global variables = 1600 bytes ( 7.813%).
Remaining is 18880 bytes ( 92.187%) for local (stack) variables or RTOS
stack & heap.
Max is 20480 bytes.
```
## Option 2: 1-line bash script
As an extension of the other two answers, [including my own][1], here is a 1-line Linux bash script you can use to automatically calculate `FLASH` and `SRAM` used.
This assumes your command to view the Berkeley size information of your .elf file is `arm-none-eabi-size "STM32F103RB_Nucleo.elf"`. Update that path to your .elf file of interest. If looking at Linux .elf files or regular executable files instead of STM32 .elf files, use `size` instead of `arm-none-eabi-size`. Ex: `size path/to/my_program.elf`.
Anyway, here is the script:
```bash
size_info=$(arm-none-eabi-size "STM32F103RB_Nucleo.elf" | awk NR\>1); \
text=$(echo "$size_info" | awk '{print $1}'); \
data=$(echo "$size_info" | awk '{print $2}'); \
bss=$(echo "$size_info" | awk '{print $3}'); \
flash=$(($text + $data)); \
sram=$(($bss + $data)); \
echo "FLASH used = $flash bytes"; \
echo "SRAM used by global variables = $sram bytes"
```
Assuming that `arm-none-eabi-size "STM32F103RB_Nucleo.elf"` shows this:
> text data bss dec hex filename
> 2896 12 1588 4496 1190 STM32F103RB_Nucleo.elf
...here is a sample output of running the above script:
> FLASH used = 2908 bytes
> SRAM used by global variables = 1600 bytes
## Breakdown of Flash, SRAM, and the 3 Berkely sections
- Flash and SRAM:
- `FLASH = text + data`
- `SRAM = bss + data`
- The 3 Berkeley sections:
- `text` = program data + ISR vector + rodata (read-only data: `const` and `constexpr` static and global read-only variables)
- `data` = NON-zero-initialized (ie: initialized with a number other than zero) static and global variables, + .init_array + .fini_array
- `bss` = zero-initialized static and global variables + stack and heap space set aside by the linker script
For more details see my other, very detailed, answer here: https://electronics.stackexchange.com/questions/363931/how-do-i-find-out-at-compile-time-how-much-of-an-stm32s-flash-memory-and-dynami/523439#523439
## References:
1. the main answer: https://electronics.stackexchange.com/questions/363931/how-do-i-find-out-at-compile-time-how-much-of-an-stm32s-flash-memory-and-dynami/363932#363932
1. my other anwer: https://electronics.stackexchange.com/questions/363931/how-do-i-find-out-at-compile-time-how-much-of-an-stm32s-flash-memory-and-dynami/523439#523439
1. how to add numbers in bash: https://stackoverflow.com/questions/6348902/how-can-i-add-numbers-in-a-bash-script/6348941#6348941
1. how to use `awk` to remove the first line of a text blob: https://superuser.com/questions/284258/remove-first-line-in-bash/284270#284270
1. general `awk` example [from my own notes](https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/git%20%26%20Linux%20cmds%2C%20help%2C%20tips%20%26%20tricks%20-%20Gabriel.txt#L3279) to remind me how to use it: `du -h | tail -n 1 | awk '{print $1}'`
[1]: https://electronics.stackexchange.com/a/523439/26234