s32k312 uart

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

s32k312 uart

2,625 次查看
fengba_360
Contributor III

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;
            }
}
 
}
}
0 项奖励
回复
11 回复数

2,587 次查看
fengba_360
Contributor III
When 1 byte is received, which event is triggered to invoke the callback?
0 项奖励
回复

2,596 次查看
Julián_AragónM
NXP TechSupport
NXP TechSupport

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.

0 项奖励
回复

2,582 次查看
fengba_360
Contributor III

When 1 byte is received, which event is triggered to invoke the callback?

0 项奖励
回复

2,560 次查看
Julián_AragónM
NXP TechSupport
NXP TechSupport

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: 

  • LPUART_UART_IP_EVENT_RX_FULL = 0x00U, /**< @brief Rx buffer is full */
  • LPUART_UART_IP_EVENT_TX_EMPTY = 0x01U, /**< @brief Tx buffer is empty */
  • LPUART_UART_IP_EVENT_END_TRANSFER = 0x02U, /**< @brief The current transfer is ending */
  • LPUART_UART_IP_EVENT_ERROR = 0x03U, /**< @brief An error occured during transfer */

Best regards,
Julián

0 项奖励
回复

2,547 次查看
fengba_360
Contributor III

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.

0 项奖励
回复

2,522 次查看
Julián_AragónM
NXP TechSupport
NXP TechSupport

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

0 项奖励
回复

2,443 次查看
fengba_360
Contributor III

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;

 

0 项奖励
回复

2,390 次查看
Julián_AragónM
NXP TechSupport
NXP TechSupport

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? 

Julin_AragnM_0-1745877820293.png

Julin_AragnM_1-1745877835100.png

Best regards,
Julián

0 项奖励
回复

2,336 次查看
fengba_360
Contributor III

You're absolutely right—it's indeed not enabled here. My apologies, I’ve been tied up with the project over the past few days and didn’t reply in a timely manner.

2,312 次查看
Julián_AragónM
NXP TechSupport
NXP TechSupport

Hi @fengba_360,

No worries! I'm glad the issue was resolved.

Best regards,
Julián

0 项奖励
回复

2,445 次查看
fengba_360
Contributor III

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);
}微信图片_20250427171343.png微信图片_20250427171303.png

0 项奖励
回复