Skip to main content
Visitor II
February 5, 2024
Solved

HAL_StatusTypeDef function

  • February 5, 2024
  • 4 replies
  • 2810 views

Hi, i come across a H7 code that use HAL_StatusTypeDef , could anyone kindly advise what does the HAL_StatusTypeDef do, whats HAL_OK means? what does ret != HAL_OK mean? and what does return HAL_OK means?

HAL_StatusTypeDef spi_write(reg, *pData)

{

  HAL_StatusTypeDef ret;

  uint8_t sendData[2] = {reg, *pData};

 

  HAL_GPIO_WritePin(...SPI1_CS_Pin, 0);  // CS pull low

  ret = HAL_SPI_Transmit(&hspi1, sendData, 2, 20);

  if  (ret != HAL_OK)

  {

              HAL_GPIO_WritePin(.., SPI1_CS_Pin, 1);  // CS pull high

              return ret;

  }

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    ret != HAL_OK means something went wrong with HAL_SPI_Transmit(&hspi1, sendData, 2, 20); and it could be one of the errors:

    HAL_ERROR
    HAL_BUSY
    HAL_TIMEOUT

    In any case you need to debug why an error were occurred.

    When all went good,  HAL_OK is returned.

     

    4 replies

    Graduate
    February 5, 2024

    With CubeIDE, you may press Ctrl + click on a name to see its definition, so simply do it and see for yourself.

    Super User
    February 5, 2024

    @gbm wrote:

    With CubeIDE, you may press Ctrl + click on a name to see its definition


    @StanCosgrove  You can also use F3, or right-click and choose from the pop-up menu:

    AndrewNeil_2-1707142778477.png

     

    As the name suggests, HAL_OK is returned when everything went OK; so checking for not OK catches the case where something went wrong, and allows recovery action to be taken

     if( ret != HAL_OK )
     {
     // The return value is not OK - handle the error
     :
     :

     

    Technical Moderator
    February 5, 2024

    Hello @StanCosgrove 

    You can check user manual of HAL driver for H7 section 4.9 HAL common resources or simply you can go to definition in your toolchain.

    FBelaid_0-1707141191960.png

     

    mƎALLEmAnswer
    Technical Moderator
    February 5, 2024

    Hello,

    ret != HAL_OK means something went wrong with HAL_SPI_Transmit(&hspi1, sendData, 2, 20); and it could be one of the errors:

    HAL_ERROR
    HAL_BUSY
    HAL_TIMEOUT

    In any case you need to debug why an error were occurred.

    When all went good,  HAL_OK is returned.

     

    Super User
    February 5, 2024

    Use this button to properly post source code:

    AndrewNeil_0-1707142419058.png

     

    To get that extra row of icons, press this button:

    AndrewNeil_1-1707142419078.png