Skip to main content
Visitor II
January 4, 2022
Solved

HAL Librarys Return Values

  • January 4, 2022
  • 3 replies
  • 2961 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();
     }

    3 replies

    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

    Amewp.1Author
    Visitor II
    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?

    TDKAnswer
    Super User
    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();
     }

    Amewp.1Author
    Visitor II
    January 4, 2022

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