Skip to main content
Visitor II
November 15, 2017
Question

HAL_RCC_OscConfig function takes too much space

  • November 15, 2017
  • 4 replies
  • 4434 views
Posted on November 15, 2017 at 11:09

Hello 

After I face some fitting to flash problem I saw my largest function is HAL_RCC_OscConfig.

I thouht a function that writes static variables to registers should be smaller. Am I doing something wrong or is there a setting for it?

I am using GCC 6_2017q2 with System workbench

#program-size #hal #flash #cube-hal
    This topic has been closed for replies.

    4 replies

    Super User
    November 15, 2017
    Posted on November 15, 2017 at 11:15

    Cube is aimed at being versatile, which is quite the opposite of optimal. That routine includes setting every oscillator type, even if you don't need it.

    Write your own if you want it slimmer.

    JW

    Visitor II
    November 15, 2017
    Posted on November 15, 2017 at 14:11

    I enabled 'place the function in their own section '

    and 'place the data in their own section ' and it make some difference in code size.

    If I face flash overlow again I think I am gonna write my own clock setup code. 

    I want to try using Low Layer library also.

    Visitor II
    November 15, 2017
    Posted on November 15, 2017 at 12:09

    Or, you could try to step up compiler optimization levels.

    Levels greater than 0 use to drop code that never gets executed.

    But that might bring up other issues ...

    Super User
    November 15, 2017
    Posted on November 15, 2017 at 13:10

    It's in a different file than from where it is called, so only link-time optimization could remove unused code.

    JW

    Visitor II
    November 15, 2017
    Posted on November 15, 2017 at 14:08

    Not sure what you mean.

    The library stuff is supposedly C source, and built with application (not a link library). Or not ?

    So, a decent optimizing compiler might throw out unused code.

    Not sure how those Cube library sources behave with high optimization settings, removing unwanted code seems safer to me.

    However, the HAL_RCC_OscConfig just consumes the largest chunk of the functions shown.

    In absolute terms, it seems not that outstanding to me.

    I suggest a broader view of allocated ressources.

    HAL_RCC_OscConfig clearly consumes more than necessary, but if you are already having trouble right now, there is much more to come.

    Projects tend to grow, rather than shrink ...

    Visitor II
    August 10, 2023

    waclawek.jan mentioned thed that the compiler can't optimise away the contents of HAL_RCC_OscConfig because it's in a separate file than where it is called, so the compiler is unaware of what it can optimise away because it only considers one file at a time.

    Therefore I tried copying the function into a separate c file and having another function in that file call it with known arguments. That increased the total size of flash by 8 bytes, so it doesn't seem to have resulted in any optimisation.

    Then I went back to original, and then tried hardcoding the struct at the top of the function call itself in stm32g0xx_hal_rcc.c. This increases the program size by 280 (quite strange). No simple luck yet

     

    Visitor II
    May 3, 2024

    I recently faced the same issue of HAL_RCC_OscConfig() taking too much space in flash. I solved the issue with a very simple trick. 

    Just change HAL to LL for RCC driver selection in CubeMX code generator in Advanced setting.

    Magically, It reduced Code size by almost 40%. 

     

    Screenshot 2024-05-03 105326.png

     

    Hope this helps!