Audio Processing #1: Basic Terminologies

Low Power Design #4, Better understanding of compiled code and it's optimization.

Let's break down the output of the arm-none-eabi-size command for your punchTrackerV1.0.elf file:

   text    data     bss     dec     hex    filename
  22068    100     2348    24516   5fc4   punchTrackerV1.0.elf

Explanation of Each Column

  1. text (22068 bytes):

    • This is the size of the executable code in your program, stored in the .text segment of flash memory.

    • It includes the compiled machine instructions for your functions, such as main, func, and any other functions in your code.

    • This segment is read-only and resides in flash memory.

  2. data (100 bytes):

    • This is the size of initialized global and static variables, stored in the .data segment.

    • These variables are stored in RAM, but their initial values are stored in flash memory and copied to RAM at startup.

    • For example, if you have a global variable like int x = 10;, the value 10 is stored in flash, and the variable x itself is stored in RAM.

  3. bss (2348 bytes):

    • This is the size of uninitialized global and static variables, stored in the .bss segment.

    • These variables are allocated in RAM but are not initialized (they are zeroed out at startup).

    • For example, a global variable like int y; would be stored in the .bss segment.

  4. dec (24516 bytes):

    • This is the total size of your program in decimal, calculated as:

      dec = text + data + bss

      In this case:

      22068 (text) + 100 (data) + 2348 (bss) = 24516 (dec)
  5. hex (5fc4):

    • This is the total size of your program in hexadecimal, which is just the decimal value 24516 converted to hex (0x5FC4).

  6. filename (punchTrackerV1.0.elf):

    • This is the name of the ELF (Executable and Linkable Format) file being analyzed.


Memory Layout Breakdown

Based on the output, here's how your program's memory is laid out:

  1. Flash Memory (Program Memory):

    • .text segment: 22068 bytes (executable code).

    • .data segment (initial values): 100 bytes (initial values for initialized variables).

    • Total Flash Usage: text + data = 22068 + 100 = 22168 bytes.

  2. RAM Usage:

    • .data segment: 100 bytes (initialized variables in RAM).

    • .bss segment: 2348 bytes (uninitialized variables in RAM).

    • Total RAM Usage: data + bss = 100 + 2348 = 2448 bytes.


Practical Implications

  1. Flash Memory Usage:

    • Your program uses 22168 bytes of flash memory for code and initialized data.

    • If your STM32 microcontroller has 64 KB of flash (common for smaller STM32s), you have plenty of space left:

      64 KB = 65536 bytes
      Free Flash = 65536 - 22168 = 43368 bytes
  2. RAM Usage:

    • Your program uses 2448 bytes of RAM.

    • If your STM32 has 20 KB of RAM (common for smaller STM32s), you have:

      20 KB = 20480 bytes
      Free RAM = 20480 - 2448 = 18032 bytes
  3. Optimization Considerations:

    • If you need to reduce flash usage, focus on optimizing the .text segment (e.g., remove unused functions, enable compiler optimizations like -Os for size).

    • If you need to reduce RAM usage, focus on reducing the size of the .bss and .data segments (e.g., use smaller data types, avoid large global arrays).

CONST KEYWORD

  • Local const variable:

    • If its address is not taken, it may be optimized away (no memory usage).

    • If its address is taken, it will likely be stored in .rodata (flash memory).

  • static const variable:

    • Always stored in .rodata (flash memory), regardless of whether its address is taken.


Comments