How to use S32K344 MCAL to implement Serial port interruption

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use S32K344 MCAL to implement Serial port interruption

1,292 Views
syfchao
Contributor II

hello,

i want receive a byte by Serial port,but i can not  receive a byte in Lpuart_Uart_Ip_Handler,

the demo is as follows(but it is not i want ,i need reveive a byte in Serial port interruption function)

can you help me?


Std_ReturnType Send_Data(uint8 transChannel, uint8 recvChannel, const uint8* pBuffer, uint32 length)
{
Std_ReturnType T_Uart_Status;
Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
uint32 T_bytesRemaining;
uint32 T_timeout = 0xFFFFFF;
uint8 Rx_Buffer[MSG_LEN];

/* Uart_AsyncReceive transmit data */
T_Uart_Status = Uart_AsyncReceive(recvChannel, Rx_Buffer, length);
if (E_OK != T_Uart_Status)
{
return E_NOT_OK;
}
/* Uart_AsyncSend transmit data */
T_Uart_Status = Uart_AsyncSend(transChannel, pBuffer, length);

 if (E_OK != T_Uart_Status)
 {
 return E_NOT_OK;
 }
/* Check for no on-going transmission */
 do
{
Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
} while (UART_STATUS_NO_ERROR != Uart_TransmitStatus && 0 < T_timeout--);

T_timeout = 0xFFFFFF;

do
{
Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &T_bytesRemaining, UART_RECEIVE);
} while (UART_STATUS_NO_ERROR != Uart_ReceiveStatus && 0 < T_timeout--);

if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus) || (UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
{
return E_NOT_OK;
}

return E_OK;
}


/*==================================================================================================
* GLOBAL FUNCTIONS
==================================================================================================*/


/**
* @brief Main function of the example
* @details Initializez the used drivers and uses the Icu
* and Dio drivers to toggle a LED on a push button
*/
int main(void)
{
volatile Std_ReturnType T_Uart_Status1;
volatile Std_ReturnType T_Uart_Status2;

/* Initialize the Mcu driver */
Mcu_Init(NULL_PTR);

Mcu_InitClock(McuClockSettingConfig_0);
#if (MCU_NO_PLL == STD_OFF)
while ( MCU_PLL_LOCKED != Mcu_GetPllStatus() )
{
/* Busy wait until the System PLL is locked */
}

Mcu_DistributePllClock();
#endif
Mcu_SetMode(McuModeSettingConf_0);

/* Initialize Mcl module */
Mcl_Init(NULL_PTR);

/* Initialize all pins using the Port driver */
Port_Init(NULL_PTR);

/* Initialize IRQs */
Platform_Init(NULL_PTR);

/* Initializes an UART driver*/
Uart_Init(NULL_PTR);


T_Uart_Status2 = Send_Data(UART_LPUART_INTERNAL_CHANNEL, UART_LPUART_INTERNAL_CHANNEL, (const uint8 *)WELCOME_MSG_2, strlen(WELCOME_MSG_2));

 Uart_Deinit();

 Mcl_DeInit();

Exit_Example((T_Uart_Status1 == E_OK) && (T_Uart_Status2 == E_OK));

return (0U);
}

0 Kudos
5 Replies

1,237 Views
syfchao
Contributor II

Hi:

 Thank you for your reply, i have config this callback,  when i receive a Byte, i can find the callback function is called by , but it is only called by once, i can not receive another Byte.it means some flag can not clear, but i do not know which flag or api to use. 

this is my function,

#define MSG_LEN 1U
uint8 Uart3_Buf_Rev[MSG_LEN];

Uart_SetBuffer(UART_LPUART_INTERNAL_CHANNEL, Uart3_Buf_Rev, MSG_LEN, UART_RECEIVE );

void Uart_Callback(uint8 HwInstance, Uart_EventType Event)
{
//IP_LPUART_3.
//Lpuart_Uart_Ip_StateStructureType* UartState;
/* uint8 data;

if(HwInstance == UART_LPUART_INTERNAL_CHANNEL)
{
if(Event == UART_EVENT_RX_FULL)
{
 //UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[HwInstance];
// if(UartState->ReceiveStatus == LPUART_UART_IP_STATUS_SUCCESS)
//{
         data = Uart3_Buf_Rev[0];

        //*UartState->RxBuff;

//}
}

}

thank you again.

 

0 Kudos

1,226 Views
cuongnguyenphu
NXP Employee
NXP Employee

Hi @syfchao 

This interrupt will be trigger in Rx when the Rx buffer is full or complete of reception. That's why it only call once.
When this callback is called, the Rx_Buffer already received all data from Tx. This buffer is setup previous by Uart_AsyncReceive.

What is the purpose of your callback function ?

0 Kudos

1,201 Views
syfchao
Contributor II

Thank you, i known when the buffer is full,the callback function is valid, but i have to use Uart_SetBuffer(),config the buffer again, it can call Uart_Callback again.

like this:

void Uart_Callback(uint8 HwInstance, Uart_EventType Event)
{
   uint8 data;

  if(HwInstance == UART_LPUART_INTERNAL_CHANNEL)
 {
    if(Event == UART_EVENT_RX_FULL)
   {

        data = Uart3_Buf_Rev[0];
       Uart_SetBuffer(UART_LPUART_INTERNAL_CHANNEL, Uart3_Buf_Rev, 1,     UART_RECEIVE );
   }

 }

}

RX data length is random,so i have to processing data by one byte,

am right to implement Uart_Callback ?

Tags (1)
0 Kudos

1,196 Views
cuongnguyenphu
NXP Employee
NXP Employee

Yes, I see you implemented the callback correctly as your use case.
It's also mentioned in the User Manual:

cuongnguyenphu_0-1664268498295.png

 

0 Kudos

1,262 Views
cuongnguyenphu
NXP Employee
NXP Employee

Hi @syfchao ,
In your demo, the function Uart_AsyncReceive only setup the Rx_Buffer to receive data. And the return value OK if there isn't error in the setting.
The actual receive process will handle in Uart interrupt handler, let's check the function Lpuart_Uart_Ip_IrqHandler for more detail. This interrupt will be trigger in Rx when the Rx buffer is full or complete of reception
You can define and use the Uart Callback Capability to handle your own purpose after enter interrupt:

cuongnguyenphu_0-1663665661628.png