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:
Hi @fengba_360,
When I receive data in the serial port interrupt, should I receive byte by byte or multiple bytes at a time?
This is based on your implementation. You can either wait for a multiple byte reception or receive byte by byte.
Is the serial port receiving interrupt triggered when the FIFO receives 1 byte or when the FIFO is full? How should I design my software buffer?
The uart callback is entered whenever the FIFO receives 1 byte, but you can wait until the FIFO is full with the events (LPUART_UART_IP_EVENT_RX_FULL). When the buffer is full, you can use Lpuart_Uart_Ip_SetRxBuffer() to update the buffer and keep receiving.
Please look at the attached example. It configures UART6 to receive 1 byte at a time and waits for the end line character ('\n') or until the buffer is full to echo the received characters to the terminal.
When 1 byte is received, which event is triggered to invoke the callback?
Hi @fengba_360
I apologize; I got confused. The callback is only entered when the buffer is full. I configured the buffer for 1 byte and thus entered the callback on 1 byte only.
You can see the events declared in the Lpuart_Uart_Ip_Types.h file:
Best regards,
Julián
Where is this 1-byte buffer configured? Is it in this function: Lpuart_Uart_Ip_AsyncReceive(UART_LPUART_INTERNAL_CHANNEL, buffer, 1U)? For example, if I configure it as 2 bytes here, will receiving 2 bytes trigger the callback due to the PUART_UART_IP_EVENT_RX_FULL event? Or does it involve the FIFO exceeding 4 bytes? I'm a bit confused about this.
Hi @fengba_360.,
You are correct, it is configured inside the AsyncReceive function with the 1U parameter.
if I configure it as 2 bytes here, will receiving 2 bytes trigger the callback due to the PUART_UART_IP_EVENT_RX_FULL event?
Yes. You can see in the project I've shared, when configured for 1 byte, the callback is entered with event RX_FULL when 1 byte is received.
Best regards,
Julián
When I debugged, the system enters a HardFault error upon executing this specific line of code.
/* Clear the error/interrupt flags */
Base->STAT = LPUART_FEATURE_STAT_REG_FLAGS_MASK;
Hi @fengba_360,
I can see you have the POWER module included in your project. Do you have the LPUART4 peripheral clock enabled for the mode you are using?
Best regards,
Julián
When I initialize using the following code, the MCU enters a HardFault. Could this be caused by an incorrect initialization method?
void lpuart_Init(void)
{
Lpuart_Uart_Ip_Init(UART_LPUART_INTERNAL_CHANNEL_4, &Lpuart_Uart_Ip_xHwConfigPB_4);
IntCtrl_Ip_EnableIrq(LPUART4_IRQn);
IntCtrl_Ip_InstallHandler(LPUART4_IRQn, LPUART_UART_IP_4_IRQHandler, NULL_PTR);
Lpuart_Uart_Ip_AsyncReceive(UART_LPUART_INTERNAL_CHANNEL_4, (uint8_t*)rx_data_uart, 1u);
}