cancel
Showing results for 
Search instead for 
Did you mean: 

How does HAL_ok command help in programming

Hjeet
Associate
 
1 REPLY 1
Bob S
Principal

"HAL_ok" is not a command. "HAL_OK" is one of the HAL library return status codes (see HAL_StatusTypeDef). So I'm not sure what your question really is?

Are you asking "how does checking the HAL status for HAL_OK help in programming"? If so, most every HAL function returns a status. Some functions always return HAL_OK, so you don't need to check the return from those functions (look at the HAL source code to know for sure). Other functions can return a variety of error codes and it is best to check to make sure the function succeeded. For example, there have been many (!!!) code samples posted on this forum that looks something like this, followed by "why doesn't this work":

void myFunction( .... )
{
   // do some stuff
 
  HAL_UART_Transmit_IT( .... );
  //  No checking of return status, call may have failed to start sending data
}

Better would be something like this:

void myFunction( .... )
{
  HAL_StatusTypeDef sts;
 
   // do some stuff
 
  sts = HAL_UART_Transmit_IT( .... );
  if ( sts != HAL_OK ) {
    // Report the error, or otherwise do something here
    // Exactly WHAT this looks like depends on your program, maybe something like this:
    DebugPrint( "UART error, sts=%d, UART error %d\n", sts, HAL_UART_GetError(huart) );
  }
}

Whatever you do to handle the error depends on your program and hardware environment. Perhaps output an error message on a debug UART, or on a screen if your system has one (as hinted at above).