How to use HAL_UART_Receive_IT to receive uart data?
Hi all,
What is the correct flow to use HAL_UART_Receive_IT() to receive uart data?
The first package is lost first its first char,
Here is the flow used now,
From the gif, you can see "H" is missing for 1st package, and all normal for others.
The problem is that the we call "HAL_UART_Receive_IT(&huart3, &data, 1);" outside the endless while(1), which lost the first char "H"
Maybe it can solved by LL lib to solve it, I just want to know if it is possible to use all HAL function to implement a normal flow to receive data correctly,
Thanks,
E-John
typedef struct {
uint8_t buffer[RX_BUFFER_SIZE];
size_t head;
size_t tail;
} RingBuffer;
RingBuffer rx_buffer;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART3)
{
PC_Uart_RXCpltCallback(huart);
}
}
void PC_Uart_RXCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t rx_data;
HAL_StatusTypeDef status;
status = HAL_UART_Receive_IT(&huart3, &rx_data, 1);
if (status == HAL_OK) {
taskENTER_CRITICAL();
/* Add the data to the ring buffer */
rx_buffer.buffer[rx_buffer.head] = rx_data;
rx_buffer.head = (rx_buffer.head + 1) % RX_BUFFER_SIZE;
taskEXIT_CRITICAL();
}
if (rx_data == '\r') {
cmd_terminated = 1;
}
}
void PCUartTask(void *pvParameters)
{
uint8_t data;
uint16_t len = 0;
HAL_UART_Receive_IT(&huart3, &data, 1);
while (1) {
if (cmd_terminated == 1) {
taskENTER_CRITICAL();
len = rx_buffer.head;
if (len > 0) {
HAL_UART_Transmit_IT(&huart3, rx_buffer.buffer, len);
}
cmd_terminated = 0;
rx_buffer.head = 0;
taskEXIT_CRITICAL();
}
vTaskDelay(pdMS_TO_TICKS(100)); // Delay for 100ms
}
} 
