cancel
Showing results for 
Search instead for 
Did you mean: 

UART_WaitOnFlagUntilTimeout

Kai_Satone
Associate III

i was debugging code HAL_uart_Receive ();

 sprintf(command, "AT+CMGL=\"ALL\"\r\n");//read all sms
    		    HAL_UART_Transmit(&huart1, (uint8_t *)command, strlen(command), 10);
//    		    HAL_Delay(100);
    		    HAL_UART_Receive(&huart1, RXbuffer, sizeof(RXbuffer), HAL_MAX_DELAY);

 i jump suddenly in here

/**
  * @brief  This function handles UART Communication Timeout. It waits
  *         until a flag is no longer in the specified status.
  * @PAram  huart  Pointer to a UART_HandleTypeDef structure that contains
  *                the configuration information for the specified UART module.
  * @PAram  Flag specifies the UART flag to check.
  * @PAram  Status The actual Flag status (SET or RESET).
  * @PAram  Tickstart Tick start value
  * @PAram  Timeout Timeout duration
  * @retval HAL status
  */
static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
                                                     uint32_t Tickstart, uint32_t Timeout)
{
  /* Wait until flag is set */
  while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  {
    /* Check for the Timeout */
    if (Timeout != HAL_MAX_DELAY)
    {
      if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
      {

        return HAL_TIMEOUT;
      }

      if ((READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U) && (Flag != UART_FLAG_TXE) && (Flag != UART_FLAG_TC))
      {
        if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) == SET)
        {
          /* Clear Overrun Error flag*/
          __HAL_UART_CLEAR_OREFLAG(huart);

          /* Blocking error : transfer is aborted
          Set the UART state ready to be able to start again the process,
          Disable Rx Interrupts if ongoing */
          UART_EndRxTransfer(huart);

          huart->ErrorCode = HAL_UART_ERROR_ORE;

          /* Process Unlocked */
          __HAL_UNLOCK(huart);

          return HAL_ERROR;
        }
      }
    }
  }
  return HAL_OK;
}


how to solve this problem?

5 REPLIES 5
Karl Yamashita
Lead II

That's not a problem because that's what you've instructed the HAL driver to do.

You've used HAL_MAX_DELAY which is waiting for ~49 days before it returns from the timeout or you've received the correct amount of bytes.

Replace HAL_MAX_DELAY to something like 100, which will return after 100ms timeout. 

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.

Still same problem

It's supposed to go to that function. So you need to explain in more detail what YOU think is the problem.

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.

while running 

  sprintf(command, "ATE1\r\n");
    HAL_UART_Transmit(&huart1, (uint8_t *)command, strlen(command), 10);
    HAL_UART_Receive(&huart1, buffer, sizeof(buffer), 100);

    memset(command, 0, sizeof(command));

    sprintf(command, "AT+CMGF=1\r\n");//text mode
    HAL_UART_Transmit(&huart1, (uint8_t *)command, strlen(command), 10);
    HAL_UART_Receive(&huart1, buffer, sizeof(buffer), 200);

    memset(command, 0, sizeof(command));

    sprintf(command, "AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
    HAL_UART_Transmit(&huart1, (uint8_t *)command, strlen(command), 10);
    HAL_UART_Receive(&huart1, buffer, sizeof(buffer), 100);

    sscanf(buffer, "%*[^:]:%d", &num_sms);
    //	MAX_SENTENCES = num_sms;
     if (num_sms == 0)
    {
        return;
    }

    memset(command, 0, sizeof(command));

code 

it struck into below code

/**
  * @brief  This function handles UART Communication Timeout. It waits
  *         until a flag is no longer in the specified status.
  * @PAram  huart  Pointer to a UART_HandleTypeDef structure that contains
  *                the configuration information for the specified UART module.
  * @PAram  Flag specifies the UART flag to check.
  * @PAram  Status The actual Flag status (SET or RESET).
  * @PAram  Tickstart Tick start value
  * @PAram  Timeout Timeout duration
  * @retval HAL status
  */
static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
                                                     uint32_t Tickstart, uint32_t Timeout)
{
  /* Wait until flag is set */
  while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  {
    /* Check for the Timeout */
    if (Timeout != HAL_MAX_DELAY)
    {
      if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
      {

        return HAL_TIMEOUT;
      }

      if ((READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U) && (Flag != UART_FLAG_TXE) && (Flag != UART_FLAG_TC))
      {
        if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) == SET)
        {
          /* Clear Overrun Error flag*/
          __HAL_UART_CLEAR_OREFLAG(huart);

          /* Blocking error : transfer is aborted
          Set the UART state ready to be able to start again the process,
          Disable Rx Interrupts if ongoing */
          UART_EndRxTransfer(huart);

          huart->ErrorCode = HAL_UART_ERROR_ORE;

          /* Process Unlocked */
          __HAL_UNLOCK(huart);

          return HAL_ERROR;
        }
      }
    }
  }
  return HAL_OK;
}

 

Ok, so now we know the problem. You should have said you're stuck in that function the first place, instead of only saying "i jump suddenly in here".

 

Sounds like the System Tick is not working. 

Make sure you've have it selected

KarlYamashita_1-1726308609356.png

And that the NVIC is correct

KarlYamashita_2-1726308966878.png

 

 

If you do have it selected, then try debugging. In the stm32f4xx_it.c file, place a breakpoint on the HAL_IncTick and run in debug mode. 

KarlYamashita_0-1726308372505.png

 

If it doesn't break on you breakpoint, then maybe it's a clock issue? 

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.