Skip to main content
Associate III
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;

  }

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

gbm
Principal
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.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Andrew Neil
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
 :
 :

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
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

 

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.Best regards,FBL
mƎALLEm
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.

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Andrew Neil
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

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.