on
2021-11-30
01:17 AM
- edited on
2024-06-18
01:22 AM
by
Laurids_PETERSE
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.
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.
Open STM32CubeIDE
Create a new project using the STM32L496G-DISCO
Give a name to the project
For this article I will call my project “AddBSP”
Initialize all peripherals with their default settings:
To do this please click on Yes below:
This will create our template project.
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:
This will open a window that indicates where the project is located, in my case:
“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:
Now go to the “Drivers” directory.
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:
Now copy this directory and paste it into the Drivers folder of your Project in the other window:
Delete the rest of the folder and keep only these ones which are relative to the STM32L496-Discovery Kit.
Now go to the Components directory and keep only the following components:
“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.
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:
Copy and paste the folder into your Project directory like this:
Now go back to STM32CubeIDE. The project structure should be like this in the Project Explorer Window:
Now refresh the project by left clicking on the Project Name and selecting Refresh:
This will add the BSP folder and subfolders and all the source files to our project.
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:
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();):
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.
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
"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:
Good luck! :)
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.
But 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.
What do I do wrong?
Thanks,
Louis
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?
Hi Mattias,
I am afraid I can't follow you, more exactly Eclipse/CDT, but here is my project structure (Created by STM32CubeMX)
I 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
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.
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
Tagging @mattias norlander for the above question. :)
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
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?
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?
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
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.