STM32 32KBytes - Decrease overall code size after compiling

Hi, I’m running simple FOC on a STSPIN32F0 which has a limited flash size of 32KB.
The 6BLDC driver code works and I was able to verify PWM execution, but once I start adding encoder and FOCMotor objects to the code the size exceeds my 32KB.

Any recommendations on how to get below 32KB? I have been modifying several files in the source code and got it to go from 38KB to 34KB, but still running out of those 2 extra KB.

Note: For stspin32f0 I manually added support on the STM32duino core. Also ran successfully this with nucleo-F446RE (bigger flash) to make sure compatibility was ok.

Thanks

Very interesting point!
I have even looked the size the compiled code for the stm32.

It is interesting to see that the stm32 has larger compilation size that atmega328 (also 32kb) and esp32. They both have the compilation size around ~20kb.

I’ve restructured the minimal branch today and included the release v2.0.1.

Using this branch you can reduce the code a bit I hope. I would be in to dig into this deeper to avoid such a large size of the library for the stm32.

Here are some ideas from my head:

  • try to avoid using SimpleFOC.h - it includes everything at once. See the minimal branch.
  • try to change the _cos()and _sin() to cos() and sin() in the BLDCmotor. The _sin and _cos approximate the sine and cosine with table in the ram (it is faster but takes some memory).
  • try to remove all the code that is not needed in the stm32_mcu.cpp: keep only the functions that are necessary for the 6PWM mode.
  • try to change the optimization level of the compiler (for me the “Smallest -Os with LTO” ) somehow makes for the smallest size

Can you tell us what did you change and what did you already do, so we can advise you a bit more precisely!

Perhaps relevant, I am also experiencing the same issue. I need to add some features and already my code is about 95% of the STM32F103C8Tx target. Switching to LTO reduced the footprint to 72%, which is a drastic improvement. I am using everything out of the box, no library or code exclusion. I have not yet tried to run the code, I am still writing and building but I remember I had some issues with the LTO optimization before.

Of course, Arduino is not exactly known for small footprint to begin with.

I am writing it for my new board, and I want to include CAN Bus and some extra I/O features.

I wonder if there is a way to “cut out” some of the loaded code with directives instead of manually editing the library itself.

Edit: The LTO breaks the execution. The MCU STM32F103C8Tx target soft-bricks, so that’s not an option.

Bummer.

By soft-bricks I mean I need to use the STLINK via SWD to load the new code with the core ST bootloader (Boot0 low). The MCU does not respond to anything else

Hey @Valentine,

The best way to go at the moment is to build the minimal sketch, not including anything that you do not need.
If that is already too large then you’ll need to remove some code from the BLDCMotor.cpp, hardware specific functions that are not used can be removed. :confused:

@Antun_Skuric

Thank you.

I’ve never done this. I would need:

  1. Current sense
  2. SPI sensor
  3. UART - tx/rt
  4. CAN (that’s my own library)
  5. Motion control: Voltage, torque or current control, etc.

Does the minimal sketch allow for that (excluding CAN of course)?

Thanks,
Valentine

Hey @Valentine,

You can just follow the procedure in the minimal branch:

The branch itself is not up to date but the procedure is the same.

You just copy the newest (2.1.1 or dev if you wish) library src folder to your pc and following the procedure in the readme copy paste the parts that you need. :smiley:

I will definitely do that.

Currently I am trying to build everything “out of box” since whoever is going to use the board will do the same, not because I am afraid to experiment. I am trying to think like an end user at that point.

Worst case I’ll ask the HW engineer politely to put a bigger MCU. I already designed a small board with a big MCU, problem is these are more expensive.

As the move said, we just need a bigger boat.

Thanks,
Valentine

OK, the velocity control with the SPI sensor on the board ate up 95% but is running just fine, perhaps the CANBus since it’s really small will take up very little space.

The board is running very cool and the motor is running very smooth, so this is really encouraging.

Of course this won’t have been possible without @Antun_Skuric and everyone else helping, so a big Thanks! to all of the people that are making this possible.

Thanks!
Valentine

:smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

Commenting out the Commander part of the code

Commander command = Commander(Serial3);

freed up 25% of the size, which is really drastic. The STM32 went from nearly 99% to 75%.