2025-09-08 11:17 PM - last edited on 2025-09-09 8:26 AM by Andrew Neil
Hi,
I am using USART4 in STM32L562E-DK eval board. with given configuration.
In which I am able to transmit the data correctly on TX based on which UART slave device is sending back the 4 bytes ACK and 16 bytes RESPONSE.
Using following code
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == UART4) // check correct UART
{
// Store received byte
//UART_Print("bytercv\r\n");
if (rxIndex < RX_BUFFER_SIZE_4)
{
rxBuffer4[rxIndex++] = rxData;
}
// Check if done
if (rxIndex >= rxTargetLen)
{
rxComplete = 1; // Mark reception complete
}
else
{
// Keep receiving next byte
HAL_UART_Receive_IT(&huart4, &rxData, 1);
}
}
}
I am able to receive the ACK but after some time when I read the RESPONSE there is not data available.
If I am reading only 4bytes of data what happens to remaining data is it loss?
Or I have to use FIFO based UART implementation.
Regards,
Aman
Edited to apply source code formatting - please see How to insert source code for future reference.
2025-09-09 6:27 AM
> If I am reading only 4bytes of data what happens to remaining data is it loss?
If you don't read out a byte before a new byte comes in, an overrun occurs and the new data will be lost. The peripheral will need to have the OVR flag cleared before it will receive new data.
2025-09-09 8:59 PM
Hi,
On probing the RX signal,
I am receiving 4 bytes and after approx. 500us I am receiving other 15 bytes of data.
On reading 4 bytes I am getting the desired data after processing; I am reading next 15 bytes of data during which HAL_BUSY error is received.
Will I need to clear OVR flag manually after reading 4 bytes.
Also, UART1 interface used for user input receiving data without clearing OVR flag, using same interrupt based HAL_UART_Recived_IT function.
Regards,
Aman
2025-09-10 7:19 AM
> Will I need to clear OVR flag manually after reading 4 bytes.
Are more bytes coming in which you aren't ready for?
HAL_BUSY indicates the peripheral is already waiting for data. You shouldn't be calling HAL_UART_Receive_IT more than once until it finishes.
2025-09-10 11:29 AM
If the slave is sending in packets, then use HAL_UARTEx_ReceiveToIdle_DMA and HAL_UARTEx_RxEventCallback for idle event.
On each packet sent, there will be an idle state which will interrupt. By using a queue, you can separate each packet in it's own queue for easing processing. If you don't want to use DMA, you can use HAL_UARTEx_ReceiveToIdle_IT without DMA.
See this example project https://github.com/karlyamashita/Nucleo-G071RB_UART_DMA_Idle_Circular/wiki
2025-09-17 12:51 AM
Can i see the size of rxbuffer4? and if you clear the index? if index more than size , will it receive(handle the data) the data read, so i think you can have a fifo, as soon as data come, you just read it to fifo, and have a process in main while and deal those data in fifo, in this case you will not omission any data.