This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Get maximal free flash size at runtime

Is it possible to determine the maximal free/usable flash size at runtime? Assuming a memory layout as shown here, is there a register that keeps the end boundary of the application? something like UICR.BOOTLOADERADDR.

  • The linker script will define a load of symbols for the start and end of sections, one of those will tell you what the end of the last piece of flash which was used. In my case the last thing put into flash is .rodata and the symbol at the end of that is rodata_end (it's bound to be different in whatever compiler you're using but if you look at the map output or linker script it should be possible to find it).

    That's a defined linker symbol so you can use it in code, just remember to take its address

    void *last_used_flash = &__rodata_end__;    // works for me - will likely be different for you
    
  • Thanks for pointing me into the right direction.

    In Keil you can use these linker symbols to get location and length of the image.

    extern uint32_t Load$$LR$$LR_IROM1$$Base; // Address of the load region.
    extern uint32_t Load$$LR$$LR_IROM1$$Length; // Length of the load region in bytes.
    extern uint32_t Load$$LR$$LR_IROM1$$Limit; // Address of the byte beyond the end of the load region.
    
    uint32_t LROBase = (uint32_t)&Load$$LR$$LR_IROM1$$Base;
    uint32_t LROLength = (uint32_t)&Load$$LR$$LR_IROM1$$Length;
    uint32_t LROLimit = (uint32_t)&Load$$LR$$LR_IROM1$$Limit;
    
Related