STM32CubeIDE Code Generation Without Losing Your Code

In this tutorial, you’ll learn how to do STM32CubeIDE/CubeMX code generation without losing your source code. We’ll discuss how to place your manual application code within the auto-generated code inside an STM32CubeIDE project. Therefore, each time you change something in STM32CubeMX (.ioc) configurations and re-run the code generation, your manual application code will be preserved and won’t get deleted by mistake.

It’s so frustrating to make a tiny change in STM32CubeMX (.ioc) configurations like a pin change or something, then you’re surprised that the newly generated code has wiped out (deleted) your manual source code. If you’re using Git, you could reverse the damage, but you’ll still have to properly place your code within the comment tags provided by the STM32CubeIDE. And this is what we’re going to learn here in this tutorial.

Table of Contents

  1. STM32CubeIDE Code Generation
  2. STM32CubeIDE Manual Source Code Placement (Sections)
  3. Example Code Placement in STM32CubeIDE
  4. Wrap Up

STM32CubeIDE Code Generation

After completing your STM32 project’s configurations in CubeMX or in STM32CubeIDE, you can click on the generate code button or save your settings, and it’ll also prompt you to generate the source code that applies your desired configurations.

Below is an example main.c file after being generated in the STM32CubeIDE project. You can easily spot the “guard” comments that define where the user’s (your) code should be.


STM32CubeIDE Manual Source Code Placement (Sections)

The STM32CubeIDE code generation tool doesn’t preserve all the manual code you write anywhere inside the generated source files. It only preserves the code that you place between the USER CODE BEGIN and USER CODE END comment tags.

For example, this is a valid place to add your own header files.

And this is another valid place to add your own private variables.

Any code placed between those two guard comments will be kept by STM32CubeIDE when you re-generate the project code. On the other hand, any manual code written outside these user code sections can be overwritten or deleted by the code generation tool.

Here is a quick overview of the most commonly used user code sections inside the main.c file.

User Code SectionWhat To Place There
USER CODE BEGIN IncludesExtra header files
USER CODE BEGIN PTDPrivate typedefs, structs, enums
USER CODE BEGIN PDPrivate definitions and constants
USER CODE BEGIN PMPrivate macros
USER CODE BEGIN PVPrivate global/static variables
USER CODE BEGIN PFPPrivate function prototypes
USER CODE BEGIN 0Private functions or callback functions
USER CODE BEGIN 1Code at the beginning of main() before HAL initialization
USER CODE BEGIN InitExtra initialization after HAL_Init()
USER CODE BEGIN SysInitSystem-level initialization after clock configuration
USER CODE BEGIN 2Application initialization after peripheral initialization
USER CODE BEGIN 3Application code inside the infinite while loop

The most important sections you’ll use most of the time are Includes, PD, PV, PFP, 0, 2, and 3.

For example, if you want to add a variable, place it in the PV section. If you want to add a function prototype, place it in the PFP section. If you want to add your main application logic that runs continuously, place it inside the USER CODE BEGIN 3 section.

Note: You should also be careful not to rename, delete, or move the user code guard comments themselves. STM32CubeIDE depends on these exact comment tags to know which parts of the file should be preserved.


Example Code Placement in STM32CubeIDE

Let’s say we’ve configured one GPIO pin as an output pin for an LED, and another GPIO pin as an external interrupt input for a push button. We want to toggle the LED each time the button is pressed.

First, in STM32CubeMX, you can label the pins as LED_Pin and BTN_Pin. This will make the generated code more readable in main.h.

Now, the manual application code can be placed in the USER CODE BEGIN 0 section.

That’s it for this example. The while(1) loop can stay empty.

Now, whenever the button interrupt fires, the HAL library will call HAL_GPIO_EXTI_Callback(), and our manual code will toggle the LED output pin.

Since the callback function is written between the USER CODE BEGIN 0 and USER CODE END 0 tags, STM32CubeIDE will preserve it even if you change the .ioc file and regenerate the code later.


Wrap Up

In conclusion, STM32CubeIDE gives us dedicated user code sections to safely place our manual application code within the auto-generated project files. As long as your code is placed between the USER CODE BEGIN and USER CODE END comment tags, it’ll be preserved after each code generation.

This is extremely important when working with STM32CubeMX or STM32CubeIDE because project configurations usually change many times during development. So, always make sure to place your includes, variables, functions, initialization code, and main loop logic in the proper user code sections.

This will save you a lot of frustration and help keep your STM32 projects’ manual code clean, organized, and compatible with the STM32CubeIDE auto code generation process.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment