cancel
Showing results for 
Search instead for 
Did you mean: 

What to do with HAL MSP initialization process?

Md Mubdiul Hasan
Associate III
Posted on December 26, 2016 at 02:08

Dear Sir,

According to user manual,UM1785, (Description of STM32F0xx HAL and Low-layer drivers)

stm32f0xx_hal_msp.c filecontains the MSP initialization and de-initialization (main routine and callbacks) of the peripheral used in the user application.

Its written that, ' It can be generated automatically by STM32CubeMX tool and further modified.'

I have generated my

stm32f0xx_hal_msp.c usingSTM32CubeMX as,

* Includes ------------------------------------------------------------------*/
#include 'stm32f0xx_hal.h'
extern void Error_Handler(void);
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
 * Initializes the Global MSP.
 */
void HAL_MspInit(void)
{
 /* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* System interrupt init*/
 /* SVC_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SVC_IRQn, 0, 0);
 /* PendSV_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0);
 /* SysTick_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
 but, the project I am going to work its stm32f0xx_hal_msp.c is different, 
#include 'stm32f0xx_hal.h'
extern void Error_Handler(void);
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
 * Initializes the Global MSP.
 */
void HAL_MspInit(void)
{
 /* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* System interrupt init*/
 /* SVC_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SVC_IRQn, 0, 0);
 /* PendSV_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0);
 /* SysTick_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
 if(hspi->Instance==SPI1)
 {
 /* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */
 /* Peripheral clock enable */
 __HAL_RCC_SPI1_CLK_ENABLE();
 
 /**SPI1 GPIO Configuration 
 PA7 ------> SPI1_MOSI
 PB3 ------> SPI1_SCK
 PB4 ------> SPI1_MISO 
 */
 GPIO_InitStruct.Pin = GPIO_PIN_7;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
 }
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{
if(hspi->Instance==SPI1)
 {
 /* USER CODE BEGIN SPI1_MspDeInit 0 */
/* USER CODE END SPI1_MspDeInit 0 */
 /* Peripheral clock disable */
 __HAL_RCC_SPI1_CLK_DISABLE();
 
 /**SPI1 GPIO Configuration 
 PA7 ------> SPI1_MOSI
 PB3 ------> SPI1_SCK
 PB4 ------> SPI1_MISO 
 */
 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3|GPIO_PIN_4);
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

I have number of question?

1. What difference you find comparing each other?

2. Second example is done forperipheral needs to be de-initialized during the program execution?

3. Let me know whyHal_MspInit() and MSP De-Initialization is done for SPI1 ?

4. Can not we add those things main.c for SPI1 and keep the first code example as it is?

Help me please.

#hal-drivers
3 REPLIES 3
Nesrine M_O
Lead II
Posted on December 26, 2016 at 09:48

Hi

Hasan.Md_Mubdiul

‌,

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.

Please refer toHAL MSP initialization process section in the

http://www.st.com/content/ccc/resource/technical/document/user_manual/2f/77/25/0f/5c/38/48/80/DM00122pdf/files/DM00122pdf/jcr:content/translations/en.DM00122pdf

To format source code, click on ''More >> Syntax Highlighter'':

0690X00000605yFQAQ.png

-Nesrine-

Posted on December 27, 2016 at 01:13

Hi Nesrine,

Thank you for your response here.

The MspInit callback performs the low level initialization related to the different additional hardware resources: RCC, GPIO, NVIC and DMA.

I understand while reading the manual. Did you read my question below number 3 and 4?

Kindly take a look carefully.

Posted on July 21, 2017 at 09:24

Hi Nesrine,

I have found something conflicting. So as you say in UM1785 the MspInit function performs the low level initialization etc. but in other places and in practice (as this is the code generated by cubeMX) I have noticed that Hal_MspInit is supposed to set the interrupt priorities and the clocks are configured with SystemClockConfig which is run after HAL_MspInit.

Also could you give an example of what would need to be initialized for the system in HAL_MspInit from RCC, GPIO, NVIC and DMA, since these will largely be initialized by the peripherals?

Thank you very much for your time.