2024-02-05 06:51 AM
Hello everyone,
I'm configuring a STM32L0 core board to connect to a module using AT command
All the commands work fine but when I get to a given command (AT+MSG="123456"\r\n), they work only the first time and the second time it only sends AT
for example this code would only work on the first iteration:
while(true){
char cmd[20];
sprintf(cmd, "AT+MSG=\"123456\"\r\n");
HAL_UART_Transmit(&huart4,cmd,strlen(cmd), 10000 );
HAL_Delay(2000);
}
I have seen that in the third more an error appears in the UART_WaitOnFlagUntilTimeout function in the line 33.
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)
{
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) == SET)
{
/* Clear Overrun Error flag*/
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
/* 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;
}
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
{
/* Clear Receiver Timeout flag*/
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
/* 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_RTO;
/* Process Unlocked */
__HAL_UNLOCK(huart);
return HAL_TIMEOUT;
}
}
}
}
return HAL_OK;
}
The curious thing is that it only appears with this command and when I call it for the second time
What do you think it could be due to?
Thanks
Solved! Go to Solution.
2024-02-05 07:19 AM
If ORE is set, it means characters came in that you didn't handle in time, and data was lost.
When the device sends data, you need to be ready to receive that data, typically in the background. This can be done with HAL_UART_Receive_IT or HAL_UART_Receive_DMA or HAL_UARTEx_ReceiveToIdle_DMA.
If you don't care about what was received, just clear the ORE flag.
2024-02-05 07:19 AM
If ORE is set, it means characters came in that you didn't handle in time, and data was lost.
When the device sends data, you need to be ready to receive that data, typically in the background. This can be done with HAL_UART_Receive_IT or HAL_UART_Receive_DMA or HAL_UARTEx_ReceiveToIdle_DMA.
If you don't care about what was received, just clear the ORE flag.
2024-09-12 04:46 AM
i got same problem
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);
directly jump 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;
}