How to add a BSP on top of CubeMX-4.21 generated code

Document created by Eric Dampierre Moderator on May 11, 2017Last modified by Eric Dampierre Moderator on May 12, 2017
Version 5Show Document
  • View in full screen mode

Let’s take the example of L476G Discovery board on which we would like to integrate the BSP on top of the code generated by CubeMX-4.21 with EWARM tool chain and IAR IDE.

Here are the main steps that will be followed and described :

  • MCU configuration and code generation in CubeMX-4.21
  • Importation of BSP files
  • Project build with BSP example on top of CubeMX-4.21 with IAR.
  • Some tips to avoid building and linking issues.


MCU configuration and code generation in CubeMX-4.21

Open a new project in CubeMX and choose L476VGTx MCU. Then generate the code.

(If you start with a project where some IPs are already enabled, it might happen that some functions are defined twice. In this case, go directly to the paragraph "Some tips to avoid building and linking issues".)


Importation of BSP files

Copy/paste STM32Cube\Repository\STM32Cube_FW_L4_V1.8.0\Drivers\BSP\Components and BSP\STM32L476G-Discovery directories in the project path/Drivers/BSP.


Both directories (Drivers\BSP\Components and Drivers\BSP\STM32L476G-Discovery) must be taken as Board Support Package (BSP) is composed of 2 parts:

  • Component : (STM32Cube\Repository\STM32Cube_FW_L4_V1.8.0\Drivers\BSP\Components) :
    this is the driver related to the external device on the board and not related to the STM32. The component driver provides specific APIs to the BSP driver external components and
    can be ported to any board.
  • BSP driver : (STM32Cube\Repository\STM32Cube_FW_L4_V1.8.0\Drivers\BSP\STM32L476G-Discovery) :
    it permits to link the component driver to a specific board and provide a set of user-friendly APIs. The API naming rule is
    BSP_FUNCT_Action(): for instance: BSP_LET_Init(),BSP_LED_On().


Import BSP drivers in the tool chain used (IAR in this case) with the needed source files (right click on Drivers/Add Group -named BSP here and then, right click on BSP/Add Files and add in this case stm32l476g_discovery.c and stm32l476g_discovery_audio.c files) :


Add BSP folder in the include search path (right click on the project – Options\C/C++ compiler\Preprocessor\<click to add>) :


Then, add #include "stm32l476g_discovery.h“ in main.c :

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32l4xx_hal.h"
/* USER CODE BEGIN Includes */
#include "stm32l476g_discovery.h“
/* USER CODE END Includes */


Project build with BSP example on top of CubeMX-4.21 with IAR.

Then build the project in IAR. Some errors will arise as below :

=> These errors mean that DFSDM and SAI IPs need to be activated.


In CubeMX, activate one DFSDM (in this case, DFSDM1/Channel 0 is set to parallel input mode) and one SAI (SAI1/SAIA is set to master mode here) .


Each time another BSP component is activated (in IAR, add files in Drivers/BSP/), it is potentially needed to enable another component :

Source file addedComponent to enable
stm32l476g_discovery_glass_lcd.c*LCD (set to Static mode in this case)
stm32l476g_discovery_qspi.c*QuadSPI (set to Single/dual SPI line in this case)

(* added first
stm32l476g_discovery_compass.c then stm32l476g_discovery_glass_lcd.c then stm32l476g_discovery_gyroscope.c etc…)


Then generate the code again with CubeMX-4.21 and rebuild the project in IAR.



Some tips to avoid building and linking issues.

It can happen that some functions are defined both in examples and in code generated by CubeMX because those functions were defined in the Examples before their integration in the CubeMX generated source code.

In order to avoid some duplicate definitions, comment some parts of the example code:

In stm32l476g_discovery_audio.c :

  • HAL_SAI_MspInit(SAI_HandleTypeDeh *hsai)
  • HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai)
  • HAL_DFSDM_ChannelMspInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  • HAL_DFSDM_ChannelMspDeInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)

(as those functions are already defined in stm32l4xx_hal_msp.c).

Clean and re-build the project under IAR.

=> No compilation errors should arise and BSP is correctly added and compiled in CubeMX-4.21 generated source code.


Furthermore, some BSP examples can be found under STM32Cube\Repository\STM32Cube_FW_L4_V1.8.0\Projects\STM32L476G-Discovery\Examples\BSP\EWARM.

Here are some forum links that deal with this topic:

3 people found this helpful