Skip to main content
Visitor II
July 15, 2024
Solved

STM32 HAL Peripherals Using too Much FLASH Memory

  • July 15, 2024
  • 3 replies
  • 2895 views

Hi Folks, 

I am using a STM32F070F6P6 microcontroller for a very basic/simple project to help learn the STM32 platform, etc.

I am getting an error when I attempt to build my project that my code overflows FLASH memory by quite a bit.  I did a test on a new project, and even without any personal code of my own, if I try to compile the exact same build, the USB middleware overflows my FLASH memory by a ton.  I am only using I2C and USB peripheral in my project.

Does this seem abnormal? Am I doing something wrong?  It doesn't seem right to me that these add-ins themselves should be enough to overflow the FLASH memory size when I haven't even written a single line of code.  

Are there any steps you usually take to optimize the code?

rbenfichera_1-1721052980296.png

 

 

rbenfichera_0-1721052958071.png

 

    This topic has been closed for replies.
    Best answer by AScha.3

    >Why is 32KB not a clever decision for USB?

    F07x can have 128K + 16K :

    AScha3_0-1721066876611.png

    This is ok for using USB , but still a "small" cpu for such use .

    I just tried using a F103C8 , 64K flash + 20K ram. (I would say: minimum for simple USB , like CDC device.)

    It needs (with -O2 ) : (all ram i use is about 8 byte, for test CDC , send: "check ! \n " )

    AScha3_1-1721067197653.png

    If you wanna play the "can i get USB device with 6K ram running" game  - ok, maybe using other USB stack, it could work.

    see or try: https://github.com/hathach/tinyusb

     

    >Why would STM32 offer it if it were a bad idea?

    Maybe for an USB charger this 90ct version is a cheap solution (= good idea) - but for more, need the bigger 128/16K version.

    AScha3_2-1721067687796.png

     

    3 replies

    Super User
    July 15, 2024

    Hi,

    try : set optimizer -O2  , in IDE -> project-> properties

    see:

    AScha3_0-1721054831061.png

    For F103 with USB->CDC device i get 23KB flash used.

    btw  just a 32KB cpu is NOT clever decision for anything like USB ...

    Visitor II
    July 15, 2024

    Thank you for the reply.  Why is 32KB not a clever decision for USB?  Why would STM32 offer it if it were a bad idea?

    AScha.3Answer
    Super User
    July 15, 2024

    >Why is 32KB not a clever decision for USB?

    F07x can have 128K + 16K :

    AScha3_0-1721066876611.png

    This is ok for using USB , but still a "small" cpu for such use .

    I just tried using a F103C8 , 64K flash + 20K ram. (I would say: minimum for simple USB , like CDC device.)

    It needs (with -O2 ) : (all ram i use is about 8 byte, for test CDC , send: "check ! \n " )

    AScha3_1-1721067197653.png

    If you wanna play the "can i get USB device with 6K ram running" game  - ok, maybe using other USB stack, it could work.

    see or try: https://github.com/hathach/tinyusb

     

    >Why would STM32 offer it if it were a bad idea?

    Maybe for an USB charger this 90ct version is a cheap solution (= good idea) - but for more, need the bigger 128/16K version.

    AScha3_2-1721067687796.png

     

    Graduate
    July 15, 2024

    ST offers microcontrollers with wide ranges of memory sizes and peripherals.  Certainly some people apply devices like STM32F07 but don't use USB and therefore don't need memory for a USB stack.

    Graduate II
    July 15, 2024

    Watch also if anything pulls in the floating point libraries, these too can have quite a large up-front cost.

    You might want to try building with the Keil MDK, there's a free license for STM CM0(+) parts and you might be able to play games with optimization, dead code removal, and link time optimization. LTO is a multi-pass compile/link, which again tries to identify dead code, and the minimal working set.

    I've used some L011 (16KB) parts, but honestly it too tight to do much of anything, and I'm not attempting to pull in the USB stack. We liked the 20-pin square part, but this an exact fit C0 or G0 with more memory which I'd look to move too.