Search code examples
themesstm32stm32cubeidestm32cubemx

Is that possible the generate without comments main.c in stm32Cubeide


Well i am newone at stm32, i have been working on arduino there is only setup and loop there is only one comments. But with stm32cubeide if i generate my main.c from cubemx. Ohh my god. Where should i start to write my code i am confused. Are there any settings or like thema for less comments line? Also comments begin and end numbers very strange starting and ending places. Such as "user code begin 3" start at the end of infinite while loop, ending can not reachable point.

/* USER CODE BEGIN 2 */
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
   /* USER CODE END WHILE */
 
   /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */

Solution

  • A Better way to use STM32CubeIDE

    Personally I hate STM32CubeIDE's generated "excessive comments" hell. The best way is not to use it. This is how I do it.

    1. Open STM32CubeIDE, select your MCU and generate a new Project. The CubeMX will generate the main.c that populated with all the excessive comments.

    2. Add a forward declared function prototype app_main() in main.c in

    /* Private function prototypes -----------------------------------------------*/
    
    /* USER CODE BEGIN PFP */
    void app_main(void);     // add this line
    /* USER CODE END PFP */
    
    1. Call app_main() after all the CubeMX generated configuration function calls but prior the while(1) loop in main()
      /* USER CODE BEGIN 2 */
      app_main();
      /* USER CODE END 2 */
    
    1. Create a new folder within the Project by right-click on the Project -> New -> Source Folder.

    2. Create a new Source File app_main.c by right-click on the newly crated app folder, select New -> Source File.

    The IDE will generated the file which only consists of a few line of comment, add the app_main() function template:

    /*
     * app_main.c
     *
     *  Created: Aug 19, 2023
     *  Author:  Henry Cheung
     */
    #include "main.h"
    
    void app_main() {
        // your app setup code here
    
        // your 'loop()' here
        while(1) {
    
        }
    }
    

    This will be where you write your code with and the while(1) loop ensure that it will never return to the main.c.

    1. In order for the STM32CubeIDE to correctly include the newly created app folder and the code in app_main(), right click on the Project, select Properties. Navigate to C/C++ General -> Paths and Symbols, click on Source Location tab and click Add Folder to add a folder for <your_project>/app, click on Apply and Close to save it.

    The screen capture show the project folder structure and the configuration described in step 6. With this configuration, all user code will be within the app folder (if you have your own driver.c, driver.h, etc.), with occasional exceptions where you might need to to open the main() to add a few line of configuration there.

    enter image description here

    It takes away all the distractive/excessive comments and avoid the chances where you accidentally put your code in the wrong place in the main(). I hope ST Microelectronics one day would use my approach to configure STM32CubeIDE!