Reducing STM32 Firmware Size by 30% Using GCC Optimization Flags ‘-Os’ and ‘-ffunction-sections’
In embedded systems development, firmware size is often a critical factor due to limited memory resources. This tutorial will guide you through the process of optimizing your STM32 firmware size by utilizing GCC optimization flags, specifically ‘-Os’ and ‘-ffunction-sections’. By the end, you should be able to reduce your firmware size by up to 30%.
Prerequisites
- Basic knowledge of C programming.
- STM32 development board and setup.
- GCC toolchain installed on your development environment.
- Familiarity with Makefile or CMake for building projects.
Parts/Tools
- STM32 development board (e.g., STM32F4, STM32F1)
- GCC toolchain for ARM (e.g., arm-none-eabi-gcc)
- Code editor (e.g., Visual Studio Code, Eclipse)
- Debugger (optional, for testing the optimized firmware)
Steps
- Open your project configuration file.
- Locate the Makefile or CMakeLists.txt in your project directory.
- Open the file in your code editor.
- Modify the compiler flags.
- If you are using a Makefile, find the line that defines
CFLAGS
orCPPFLAGS
. - Add the following flags to optimize for size:
CFLAGS += -Os -ffunction-sections -fdata-sections
- If you are using a Makefile, find the line that defines
- If using CMake, add the flags in your
CMakeLists.txt
: - Linker flags adjustment.
- In your Makefile, locate the
LDFLAGS
section. - Add the following linker flag to remove unused sections:
LDFLAGS += -Wl,--gc-sections
- In your Makefile, locate the
- For CMake, update the linker flags as follows:
- Rebuild your project.
- Run the make command in your terminal:
make clean && make
- For CMake, run:
- Verify the firmware size reduction.
- Check the size of the generated firmware file (e.g.,
.bin
or.hex
). You can use the following command:
ls -lh your_firmware.bin
- Check the size of the generated firmware file (e.g.,
- Compare it with the previous size to see the reduction percentage.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -ffunction-sections -fdata-sections")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
cmake . && make
Troubleshooting
- Firmware fails to build:
- Ensure that you have the correct GCC toolchain installed and configured in your environment.
- Double-check for typos in the flags added to the Makefile or CMakeLists.txt.
- Unexpected behavior after optimization:
- Review your code for any assumptions about function inlining or unwanted optimizations.
- Test with different optimization flags if issues persist.
- Size reduction not as expected:
- Consider further refactoring your code to remove unused functions or libraries.
- Verify that all optimization flags are correctly applied during the build process.
Conclusion
By following the steps outlined in this tutorial, you should be able to effectively reduce your STM32 firmware size by leveraging GCC optimization flags. Remember to test your firmware thoroughly after applying optimizations to ensure functionality remains intact. Happy coding!