Dear NXP engineer, I have a few questions about using the serial port on the S32K312 microcontroller. In my scenario, the host computer continuously sends multiple bytes of varying lengths. When I receive data in the serial port interrupt, should I receive byte by byte or multiple bytes at a time? Is the serial port receive interrupt triggered when the FIFO receives 1 byte or when the FIFO is full? How should I design my software buffer? Please help guide me. Below is my code within the interrupt:
void UART_event_cbk(const uint8 HwInstance, const Lpuart_Uart_Ip_EventType Event, void *UserData)
{
(void) UserData;
uint32_t remainingBytes;
uint8_t status;
if(HwInstance == UART_LPUART_INTERNAL_CHANNEL_4)
{
if(Lpuart_Uart_Ip_GetReceiveStatus(HwInstance, &remainingBytes)!= LPUART_UART_IP_STATUS_BUSY )
{
status = Lpuart_Uart_Ip_GetReceiveStatus(HwInstance, &remainingBytes); //串口数据接收成功
if(status == LPUART_UART_IP_STATUS_SUCCESS)
{
/* 为接收到的字符串添加终止符 */
bufferIdx++;
uart_triger = 1;
rx_data_uart[bufferIdx] = 0U;
/* 将接收缓存文件复制到rxdata数组 */
memcpy(rxdata,rx_data_uart,bufferIdx);
memset(rx_data_uart,0,sizeof(rx_data_uart));
/* Reset the buffer index to start a new reception */
bufferIdx = 0U;
/*完成操作后清除中断标志位并重新开始接收串口数据*/
Lpuart_Uart_Ip_AsyncReceive(HwInstance, (uint8_t*)rx_data_uart, 1u);
}
else
{
//error handle
Lpuart_Uart_Ip_AsyncReceive(HwInstance, (uint8_t*)rx_data_uart, 1u);
bufferIdx = 0;
}
}
/*串口接收缓存满事件*/
if(Event==LPUART_UART_IP_EVENT_RX_FULL)
{
// 检查是否为换行符且缓冲区未满
if (rx_data_uart[bufferIdx] == '\n')
{
// 触发立即处理
uart_triger = 1;
}
else if (bufferIdx < (sizeof(rx_data_uart) - 2)) // 预留空间给终止符
{
bufferIdx++;
Lpuart_Uart_Ip_SetRxBuffer(HwInstance, &rx_data_uart[bufferIdx], 1);
}
else
{
// 缓冲区满但未收到换行符,处理或重置
uart_triger = 1; // 强制处理
bufferIdx = 0;
}
}
}
}