2020-10-16 05:38 AM
Dear ST Hello,
what is the best way to use CubeMX without any deleting functions in CubeIDE?
For code added to the main file, it's clear to use /**User */ comment guidelines but, for functions, should i create another file or module for my functions?
Thank you in advance,
S.Tairk,
Solved! Go to Solution.
2024-05-28 10:20 PM
I wouldn't rely on using the USER CODE section.
I had spent a couple of hours adding code to the USER CODE section and STM32CubeIDE (Cube MX built in) crashed during code generation. The main.c file was corrupted and STM32CubeIDE could not open the file nor could any text editor. Lesson learned, so I never use the USER CODE section anymore.
So now I create a PollingRoutine.c/h files. In it i have two functions, PollingInit() and PollingRoutine().
#include "main.h"
void PollingInit(void)
{
}
void PollingRoutine(void)
{
}
In main.c I just write those two calls, one before the while loop and one within like so.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
PollingInit();
while (1)
{
PollingRoutine();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
So anything i want to initialize, i write in PollingInt() and my while loop is now PollingRoutine(). Then you can write other functions within PollingRoutine.c without having to find a USER CODE section to write new functions like you would in main.c. Or you can create other files pertaining to certain peripherals like UART_Handler.c or I2C_Handler.c, etc.
If the main.c ever gets corrupted again, the only thing i lost was writing the two function calls.
Another advantage is that main.c already has a lot of generated code so instead of scrolling through all that mess to find your added functions. you can scroll through PollingRoutine.c that has only your functions.
2024-08-23 07:36 AM
I just experience this nonsense. Kept my code under main but deleted my code under the while(1) loop. Like seriouly WTF?