cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l4xx_hal_msp.c vs main.c?

SWenn.1
Senior III

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Khouloud ZEMMELI
ST Employee

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

View solution in original post

1 REPLY 1
Khouloud ZEMMELI
ST Employee

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