I'm learning to use STM32 microcontrollers using CubeMX to generate a project. I've noticed that CubeMX generates two basic stages of initialization for nearly every peripheral:
(A) Within the stm32fxxxx_hap_msp.c
file, it defines a number of callbacks of the form HAL_XXX_MspInit()
.
(B) Within main.c
, it defines a number of functions of the form MX_XXX_Init()
.
Sometimes a given peripheral's HAL_XYZ_Init()
function is called from within (A) as it is with the ADC and RTC. Other times a given peripheral's HAL_XYZ_Init()
function is called from within (B) as it is done with GPIO.
Both sets of functions call __HAL_RCC_xxxx
macros extensively.
I can't make heads or tails of the usage pattern and neither the HAL guide nor reference manual give much direction here.
So, questions:
I suspect that the callback format provided by (A) has something to do with the uC waking from low-power modes, but I am not positive about this. Is this correct, or what is the purpose of the
HAL_XXX_MspInit
callback format?I noticed that (A) contains more user-definition block comments indicating room for my code to cohabit the functions generated by CubeMX. But why, for instance, would it be wrong to move code from (B) into the space of (A) in order to keep my initialization sequence clear and easy to read?