Electronic – STM32: Difference between HAL_xxx_Init() and HAL_xxx_MspInit()

hal-librarymicrocontrollerprogrammingstm32stm32f3

When using the HAL (Hardware Abstraction Library) for any of the STM32 micro controllers:

For a given peripheral, why do you have to call an HAL_…init() , as well as a HAL…_MspInit() – why are several 'inits' separated?

Best Answer

The ST User Manual UM1725 - Description of STM32F4 HAL and LL drivers, see pages 61 - 63, gives an explanation:

The peripheral initialization is done through HAL_PPP_Init() while
the hardware resources initialization used by a peripheral (PPP) is performed during this initialization by calling MSP callback function HAL_PPP_MspInit().
The MspInit callback performs the low-level initialization related to the different additional hardware resources: RCC, GPIO, NVIC and DMA.

(MSP: MCU Support Package)

and

2.12.2 HAL global initialization:

HAL_Init(): this function must be called at application startup to
- initialize data/instruction cache and pre-fetch queue
- set SysTick timer to generate an interrupt each 1ms (based on HSI clock) with the lowest priority
- set priority grouping to 4 preemption bits
- call HAL_MspInit() user callback function to perform system level initializations (Clock, GPIOs, DMA, interrupts). HAL_MspInit() is defined as “weak” empty function in the HAL drivers.