Skip to main content
SWenn.1
Senior III
Mar 21, 2021 11:58 PM UTC
Solved

stm32l4xx_hal_msp.c vs main.c?

Can someone explain to me why CubeMX creates handles for objects within main but the GPIO configuration for that object (example an A/D or USART using DMA) is configured under the stm32l4xx_hal_msp.c instead of right within main as part of the object?

This topic has been closed for replies.
Khouloud ZEMMELI
Khouloud ZEMMELIBest answer
ST Employee
Mar 24, 2021 2:59 PM UTC

Hello @SWenn.1​ 

Thank you for your post,

Actually, it's a kind of organization decided to be used for CubeMX code generation.

In fact, the stm32l4xx_hal_msp.c defines all initialization functions to configure the peripheral instances according to the user configuration such as the pin allocation, enabling of clock, use of DMA and Interrupts.

For the main.c, it is in charge of configuring and initializing the system clock and GPIOs that are not used by peripherals, in addition to that, the main.c define and call, for each configured peripheral, a peripheral initialization function that defines a handle structure that will be passed to the corresponding peripheral HAL init function which in turn will call the peripheral HAL MSP initialization function.

It's possible to gather the configuration generated in the two files by selecting the "Generate the peripheral Init as pair of '.c/.h'.." option as mentioned in the image below.

0693W000008xnwEQAQ.png 

Hope my answer helped you :) when your question is answered, please close this topic by choosing Select as Best, it will help other user find that answer faster.

Thanks, Khouloud

1 reply

Khouloud ZEMMELI
Khouloud ZEMMELIBest answer
ST Employee
Mar 24, 2021 2:59 PM UTC

Hello @SWenn.1​ 

Thank you for your post,

Actually, it's a kind of organization decided to be used for CubeMX code generation.

In fact, the stm32l4xx_hal_msp.c defines all initialization functions to configure the peripheral instances according to the user configuration such as the pin allocation, enabling of clock, use of DMA and Interrupts.

For the main.c, it is in charge of configuring and initializing the system clock and GPIOs that are not used by peripherals, in addition to that, the main.c define and call, for each configured peripheral, a peripheral initialization function that defines a handle structure that will be passed to the corresponding peripheral HAL init function which in turn will call the peripheral HAL MSP initialization function.

It's possible to gather the configuration generated in the two files by selecting the "Generate the peripheral Init as pair of '.c/.h'.." option as mentioned in the image below.

0693W000008xnwEQAQ.png 

Hope my answer helped you :) when your question is answered, please close this topic by choosing Select as Best, it will help other user find that answer faster.

Thanks, Khouloud