A bettery way to setup STM32CubeIDE

A better way to setup STM32CubeIDE

I have been using STM32CubeIDE for my STM32 development for past a couple of years, and there are things that I like about the IDE but there are also things that I don't like about it. In this blog I'd like to discuss how I setup the IDE to overcome some of the things that I don't like about the IDE.

By the way, this is not a tutorial on how to install STM32CubeIDE or how to use STM32CubeIDE, for that, please see the first part of How to install STM32CubeIDE about the tricks and steps of installing the IDE.

Things that I like about the STM32CubeIDE

There is no suprise that the STM32CubeMX (which is the code generator part of the STM32CubeIDE) that as part of the STM32CubeIDE makes it really easy to configure the hardware peripherials, modern MCU has became more and more complicated that it will take quite a while to manually configure the device peripherials and it could be an error-prone process without the tool like STM32CubeMX. This is one of the main reason and one of the key things that I like about STM32CubeIDe.

Another reason that I like about the STM32CubeIDE is the ability to click on a function, or right-click on a function to bring up the function definition in the soruce code for a quick peek on how the function is implemented. This capability might just part of the Eclipse functionality (I never use Eclipse IDE prior using STM32CubeIDE) but nevertheless this is a really helpful feature for software development as compare to coding using an code editor.

Things that I don't like about the STM32CubeIDE

The STM32CubeMx auto code generator is one the the reasons that not just me but many developers like about the IDE, but the STM32CubeMX auto code generator generates all codes in the main.c, and to make this worst is that it add a lot of comment lines with the restrictions to where you should put your variables, declarations, and codes within the main.c file, this is not only clutter the file, making it not only difficult to read, but also difficult for code sharing and distribution. A few line of code could easily filled with lines of boiler plate code and comments. If you are not puttuing the code in the designed section of the file, next time when you re-generate the code using the STM32CubeMX, your code might be wiped out when the IDE regenerate the code. This happens to me a few times in the past.

ST Microelectronics offers 3 libraries within the IDEs that you could use, CMSIS (technically, it is not a library, but a list of defintions and symbols that mapping with the ARM Core), a Low-Layer Library (LL) and Hardware Abstraction Library (HAL). Both LL and HAL utilizes part of CMSIS implementations, interestingly HAL library is not build on top of the LL library but it should, this might be because of the historical reason as the entire ST Microelectronics development platform is the result of a serious of acquisitions and in-hour developments. Personally, I don't quite like the HAL library for both performance and the way it is structured, I often feel that the things that I need to go through to setup a structure for using certain functions, I must well just uses the LL library instead. For this reason, I most of the time uses LL library.

Using LL Library

The STM32CubeMX code generator by default would generates the code based on HAL lirbary. In order to uses LL Library, it need to tell the code generator to generate code based on LL library.

Here are the steps for setting up STM32CubeIDE to use the LL Library in a project. Open STM32CubeIDE, create a project and generate the code as usual.

  1. double-click your-project.ioc to bring up the STM32CubeMX code generator from the side menu;

  2. Click on Project Manager tab;

  3. Click on Advaned Settings;

  4. On the Driver Selector section, click on each driver and change the "HAL" to "LL";

  5. Click on the Device Configuation Tool Code Generator icon to generate the code.

Setup to use LL Library
Setup to use LL Library

That is, now I can use the LL library for the project.

A Better Way to Setup STM32CubeIDE

Instead of using the default generated main.c, I usually create a separate folder called app with a file app_main.c so that in the case when I need to share my project as an open source project or back up it to github, I don't have to distribute the entire project with the main.c that is full of boiler plate code created by auto code generator, I only need to share the app folder.

In orde to do what I want, first create a project as usual, either use the LL lirbary as mentioend before or use the HAL library.

Then following the follow steps:

Step 1:

Right-click on the project folder on the left sidebar to create an app folder. I also create a lib folder within the app folder for getting the local library files.

Step 2:

  1. Right-clock on the project on the left sidebar, and select properties;

  2. Click C/C++ General and then Paths and Symbols;

  3. Select the Source Location tab;

  4. click on Add Folder... to add the app folder that we just created.

Add the app folder to Source Location
Add the app folder to Source Location

Step 3:

  1. With the Paths and Symbols menu still open, click on Includes tab;

  2. Add app as the directory;

  3. Select all the three options for Add to all configurations, Add to all languages, is a workspace path even though not all those options matter but no harm to select all;

  4. Click on Workspace... ;

  5. and make sure to select the correct app/inc folder in this case. We can then go ahead to re-generate the code.

Add project lib folder to Include tab
Add project lib folder to Include tab

Step 4:

Now it needs to go back to main.c to add "hooks" to use the newly created directory and the app_main.c that we are going to create soon. Add the following function declaration void app_main(void); and the call to app_main() function within the while loop.

    :
/* USER CODE BEGIN PFP */
void app_main(void);
/* USER CODE END PFP */
    :
int main(void)
{
    :
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    app_main();
    /* USER CODE END WHILE */
    :

With this configuration, we most of time will no longer need to touch the main.c other occasionally we might still need to add our custom configuration into the auto-generated function.

Step 5:

Create a file name app_main.c in the app folder that we created, for testing purpose, I add a couple of codes in the app_main.c function as I had pin PC6 configured as LED_Pin, but for compilation test, it is okay to leave it empty. Remember the app_main.c is running with the super loop of main.c should it will never go out of the loop, so in a way the app_main() function behave very much like the loop() in the Arduino framework.


#include "main.h"

void app_main() {

    LL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    LL_mDelay(500);

}

This is much cleaner and I no longer need to worry that my code would get wiped off due to the code generator as the all the code in the main.c seldom change through out the project development. It is also make it easier to setup github for code sharing and version control.

Until STM32CubeIDE improves its way of generating code in the main.c, this is the best way that I could think of to workaround the things that I don't like about the STM32CubeIDE!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.