cancel
Showing results for 
Search instead for 
Did you mean: 

How to add a BSP to an STM32CubeIDE project

ST AME Support NF
ST Employee

1. Introduction


STMicroelectronics offers BSPs (Board Support Packages) for all STM32 Evaluation and Nucleo boards as well as Discovery Kits. In this article we will see how to add a BSP to your STM32CubeIDE project.
 

2. Prerequisites

 

  • Hardware:
  • Mini USB cable to power and program the board
  • STM32L496G-DISCO (rev B)

1712.png
 

  • Software: STM32CubeIDE

3. Theory

All ST’s STM32 boards include the microcontroller as well as basic components from button switches and LEDs to more advanced devices that include sensors, displays, etc. (check the documentation of the specific board of interest for the details). To help ease the use of the additional components, ST offers specific drivers in the BSPs for each board. These BSPs are included in each STM32 Cube Firmware Library.
In this article we will learn how to add a BSP to an STM32CubeIDE project. We will use the STM32L496G-DISCO kit and will see how to add the BSP, specifically the drivers for the display that is on the board.
We will then develop a short application that will initialize the LCD and display a message on it.


4. Steps 

  1. Open STM32CubeIDE  

  2. Create a new project using the STM32L496G-DISCO


1714.png
 

  1. Give a name to the project

For this article I will call my project “AddBSP”
1717.png

  1. Initialize all peripherals with their default settings:

To do this please click on Yes below:
1718.png
This will create our template project.

  1. Add BSP

During this step we will add the BSPs drivers we need for the STM32L496G-Discovery Kit to be able to have an application using the display in our case. 
First open the directory where the project has been created. To find out where the project was created go to File -> Open Projects from File System:
1719.png
This will open a window that indicates where the project is located, in my case:
1720.png
“xxxxx” indicates the username in my case.
Now with an explorer open the directory where the project is located. The structure of the project should look like this:
1721.png
Now go to the “Drivers” directory.
1722.png
This is where we will be adding the BSP files.
In a second explorer window open the STM32CubeL4 FW Library folder that should be in the following location on your disk:
C:\Users\xxxxxxxxxx\STM32Cube\Repository\STM32Cube_FW_L4_Vx.y.z
Where “xxxxxxxxxx” is your username if you are using Windows and “Vx.y.z” is the version of the STM32Cube Library, in my case I am using the version 1.17.0.
In the STM32CubeL4 directory go to the Drivers directory to find the BSP:
1723.png
Now copy this directory and paste it into the Drivers folder of your Project in the other window:
1724.png
Delete the rest of the folder and keep only these ones which are relative to the STM32L496-Discovery Kit.
1725.png
Now go to the Components directory and keep only the following components:
1726.png
“Common” includes all the components that are available for any evaluation board. The folder, “mfxstm32l152”, is a companion of the STM32L4 which is required to be included due to dependencies in the drivers and “st7789h2” contains the components for the display on the STM32L496G-Discovery.
In the STM32L496G-Discovery we will keep only these files as we won't use all of the features of the board, we will use the display for this example.
1727.png
Listed above are the absolute minimum files to have to be able to use the display drivers from the BSP.
We will also add the Utilities folder from the STM3Cube Library to our project because the BSP uses some of the files form this folder. You will find the Utilities folder of the at the root directory of the STM32L4Cube FW Library as shown below:
1728.png
Copy and paste the folder into your Project directory like this:
1729.png
Now go back to STM32CubeIDE. The project structure should be like this in the Project Explorer Window:
1730.png
Now refresh the project by left clicking on the Project Name and selecting Refresh:
1731.png
This will add the BSP folder and subfolders and all the source files to our project.
1732.png

  1. Project modification

Because of some drivers’ dependencies we need to add the Timer HAL drivers from the HAL Configuration file. Open stm32l4xx_hal_conf.h file located Under Core -> Inc from the Project Explorer and define HAL_TIME_MODULE_ENABLED to include the Timer drivers used by the BSP:
1733.png
 

  1. Adding code

We are going to add some functions provided by the BSP to use the on-board display. We will initialize the display and then use some of the BSP function to display a sentence on the display.

 

/* USER CODE BEGIN Includes */
#include "../../../Drivers/BSP/STM32L496G-Discovery/stm32l496g_discovery.h"
#include "../../../Drivers/BSP/STM32L496G-Discovery/stm32l496g_discovery_lcd.h"
/* USER CODE END Includes */
…
  /* USER CODE BEGIN 2 */

  /* LCD Init */
  if (BSP_LCD_InitEx(LCD_ORIENTATION_PORTRAIT) == LCD_ERROR)
  {
    /* Initialization Error */
    Error_Handler();
  }

  BSP_LCD_DisplayStringAt(0, 240 - 65, (uint8_t *)"BSP Example", CENTER_MODE);

  /* USER CODE END 2 */


Note: if you don’t have an SD card inserted in the SD card slot of the Discovery Kit you will need to comment the Init of the SDMMC peripheral (//MX_SDMMC1_SD_Init();):
1734.png
 

  1. Now build the code, load it, and reset the board to start execution.

After the LCD is initialized, the text, “BSP Example”, will be displayed.


5. Related links

Comments
Louie88
Associate III

Hi,

Nice article. I am not sure how I can use it for STM32H747I-DISCO board. Can you tell me the differences?

Also, I do not understand why do you import the same AddBSP project what have just created?

Thanks for your help.

Louis

mattias norlander
ST Employee

"I do not understand why do you import the same AddBSP project what have just created?"

He is not importing a project. He opens the import wizard simply to be able to copy the path where the project exist on the file system. He opens a file explorer on that path, and a second window inside the CubeFW directories. Then he manually copies the BSP related sources from CubeFW folder to his project folder. Since he copied the files on the file system. CubeIDE will be unaware about these new files. Consequently you need to hit refresh inside CubeIDE on the project to detect the new files.

I am not sure how I can use it for STM32H747I-DISCO board. Can you tell me the differences?

The H747I is a dual-core M7/M4 device.

Start by copying this folder:

  1. C:\Users\MYUSERNAME\STM32Cube\Repository\STM32Cube_FW_H7_V1.9.1\Drivers\BSP\STM32H747I-DISCO into you project.
  2. Add include paths to the BSP folder (and Component folders)
  3. Add any required defines at project level, have a look at stm32h747i_discovery.h. It depends on which part you want to use of the BSP...Potentially step 4-5 will be easier if you disable any feature you don't need.
  4. Then try to build the project. This should fail because you have to:
    1. Open the stm32h7xx_hal_conf.f and enable the defines for the modules that are required to compile the BSP.
    2. Some BSP drivers will #include some compents. You have to find the required components and copy them into your project.
  5. Perform step 3 in iterations until you have cleaned out all build errors. There should be plenty of build errors initially!

Good luck! 🙂

Louie88
Associate III

Hi Mattias,

Thanks for the ideas. Currently I need the LCD driver on STM32H747I-DISCO board. Once I can add it to a project then I am sure I can add any other BSP feature.

I had a version in which I had only one error: BSP_LCD_Init(0, LCD_ORIENTATION_LANDSCAPE); not defined.


_legacyfs_online_stmicro_images_0693W00000JPCmDQAX.pngBut it is. If I select BSP_LCD_INIT and press F3 then it opens the stm32h747i_discovery_lcd.h file where the BSP_LCD_Init() is defined.


_legacyfs_online_stmicro_images_0693W00000JPCmIQAX.png 

What do I do wrong?

Thanks,

Louis

mattias norlander
ST Employee

Did you place the stm32X_discovery.c file in a "source code folder? There is a difference between a "folder" and a "source code folder" in CubeIDE (Eclipse/CDT feature).

Have a look at the project structure in the project explorer view. The Source folder are indicated by the purple/white "c" icon decoration. The build system will only look for source files in this source folders.

You can turn a normal folder into a source folder by right-click on it > Resource configuration > Exclude from build > Deselect All build configurations.

Could this be the root-cause?

Louie88
Associate III

Hi Mattias,

I am afraid I can't follow you, more exactly Eclipse/CDT, but here is my project structure (Created by STM32CubeMX)


_legacyfs_online_stmicro_images_0693W00000JPK8LQAX.pngI am sure the I am doing something wrong and yes, you question is a good question: The stm32h747i_discovery.c is int the host project's Driver folder which is not source folder.

Thanks for your valuable help!

Louis

mattias norlander
ST Employee

Aha! The STM32H747I-DISCO folder is inside the "root-project".

Everything is a bit more complex when moving from single-core to multi-core world. Our appologese! But multi-core is more complicated, sharing resources and other stuff...

In order for the stm32h747i_discovery.c to be built in the CM4 and/or the CM7 projects, it must exist in these projects.

In your screenshot it exists only in the "root-project" (AddBSPTest). It does not exist inside the AddBSPTest_CM4 project?

You have two options:

Either move the whole STM32H747I-DISCO folder inside the CM4 or the CM7 project and make sure it is seen as a "source folder".

Or, keep it in the parent folder as shared code. If so, you have to create links from the shared folder to to the CM4 and the CM7 project to make it visible to compiler.

If the information is insufficient (Eclipse/CDT is not always easy), instead drop me an e-mail with invite to screen share, and I can solve it quickly.

My e-mail address in my profile.

Legacy member
Not applicable

Is there a way to add BSP components (e.g., the LCD driver for the STM32H745I-DISCO) from inside CubeMX, or does it need to always be done manually from files like this?

Thanks,

P

Legacy member
Not applicable

Tagging @mattias norlander​ for the above question. 🙂

mattias norlander
ST Employee

Hi Peter,

Thanks for the tagging - would never have caught this question without it.

Unfortunately MX does not support adding BSP components into your project.

I know that there is an internal ticket reflecting your request inside CubeMX, but it is not at all scheduled for development today. Internal ticket reference: 55664

Sorry about that.

Kind regards, Mattias

SFent.3
Associate II

This process is very messy and prone to error. Having to hand copy files into a project :\ It would be great if all this was handled by the IDE as you created the project. Workflow would be much improved. 

New STM32 Project -> Select Board -> make chenges to .ioc -> Auto Code Gen pulls in the BSP that it needs depending on your choices. Possible?

Sen_Koc
Associate

Hello @mattias norlander / Hello community members,

I am facing similar issue while adding the BSP to my project. Below is the screenshot of my workspace. I am using STM32h747I-Eval board.

What I did was I copied the BSP folder from "LCD_DSI_CmdMode_DoubleBuffer" example and pasted it on my project's 'drivers' folder. I get too many errors, I added paths to my BSP folder. Still the errors persists. What am I missing?

 

Sen_Koc_0-1706289056933.png

 

mattias norlander
ST Employee

Sorry @Sen_Koc ,

Looking at your screenshot the error output is cropped. I can only see :

error : 

You need either to upload the or paste the build-log, or you if you can enlarge the build console and maybe use line wrapping...

My first guess would be that the defines are not updated in the stm32.._hal_conf.h to be in-sync with the requirements from the BSP related code. For example is this symbol defined:

#define HAL_SAI_MODULE_ENABLED 1

 

SFent.3
Associate II

Hi,

I'm in the process of creating a small series of walkthrough vidoes (using the STM32F769-DISCO as a platform) that might be of use to you. I import the BSPs and also show how to copy an existing project (amongst other things).

The first  in the series can be found here https://youtu.be/1xtwVx41KUs?feature=shared

I will be adding more as I'm guiding some under graduates through the process. If you subscribe, you will get the new ones as i post them.

Regards,

Steve.

Version history
Last update:
‎2024-06-18 01:22 AM
Updated by:
Contributors