Skip to main content
Amewp.1
Associate
January 4, 2022
Solved

HAL Librarys Return Values

  • January 4, 2022
  • 4 replies
  • 3008 views

Hello Everyone. I'm a student and learning ST nowadays.

Forgive me for my childish question but I really don't get it.

HAL library's functions are usually returns a value of result of the process.

For example, HAL_Init func returns HAL_StatusTypeDef value. When I generate a project from CubeMX or CubeIDE, default codes doesn't use HAL_StatusTypeDef.

The program just inits and moves on. Never check if it if busy or gets error.

I don't know could I tell myself clearly

Thanks for reply, have a good day.

This topic has been closed for replies.
Best answer by TDK

Yes, but the success code is HAL_OK. This is done many places in the CubeMX generated code. For example:

 if (HAL_UART_Init(&huart3) != HAL_OK)
 {
 Error_Handler();
 }

4 replies

Peter BENSCH
ST Technical Moderator
January 4, 2022

Welcome, @Amewp.1​, to the community!

Well, it is up to the user to evaluate in his program the return values of the HAL calls and to react accordingly. In the source codes generated by STM32CubeMX/CubeIDE you can find examples like:

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
 Error_Handler();
}

Of course this Error_Handler, which is typically located in main.c, must also be filled with actions, by default it is indeed empty. But it is also mentioned in the comments:

/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state */
 
 /* USER CODE END Error_Handler_Debug */
}

So you can insert any actions there: a reset, switching on a LED and subsequent while(1), output via a terminal, etc etc. You are completely free to do whatever you want.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Amewp.1
Amewp.1Author
Associate
January 4, 2022
uint8_t errCode;
 
errCode = HAL_Init();
 
if (errCode != SUCCESS)
{
 // do something
}

Ok I got the concept but can I use this kind of? Does it makes sense?

TDK
TDKBest answer
January 4, 2022

Yes, but the success code is HAL_OK. This is done many places in the CubeMX generated code. For example:

 if (HAL_UART_Init(&huart3) != HAL_OK)
 {
 Error_Handler();
 }

"If you feel a post has answered your question, please click ""Accept as Solution""."
Amewp.1
Amewp.1Author
Associate
January 4, 2022

OK thak you for all perfect answers. Have a good day