S32k uart RX interrupt problem

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

S32k uart RX interrupt problem

554 Views
alice_th
Contributor III

hello,

i see there is lpuart_echo_s32k144 demo, the rx using LPUART_DRV_InstallRxCallback(INST_LPUART_LPUART_1, rxCallback, NULL);

 why still in polling mode the receive the data in main loop?

how to modify it with rx only interrupt mode ? 

if remove below code

/* Receive and store data byte by byte until new line character is received,
* or the buffer becomes full (256 characters received)
*/
LPUART_DRV_ReceiveData(INST_LPUART_LPUART_1, buffer, 1U);
/* Wait for transfer to be completed */
while(LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1, &bytesRemaining) == STATUS_BUSY);

/* Check the status */
status = LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1, &bytesRemaining);

it doesn't work.

int main(void)
{
/* Write your local variable definition here */
status_t status;
/* Declare a buffer used to store the received data */
uint32_t bytesRemaining;

/* Write your code here */
/* For example: for(;;) { } */

/* Initialize and configure clocks
* - see clock manager component for details
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

/* Initialize pins
* - See PinSettings component for more info
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0, g_pin_mux_InitConfigArr0);

/* Initialize LPUART instance */
LPUART_DRV_Init(INST_LPUART_LPUART_1, &lpUartState1, &lpUartInitConfig1);
/* Install the callback for rx events */
LPUART_DRV_InstallRxCallback(INST_LPUART_LPUART_1, rxCallback, NULL);
/* Send a welcome message */
LPUART_DRV_SendDataBlocking(INST_LPUART_LPUART_1, (uint8_t *)welcomeMsg, strlen(welcomeMsg), TIMEOUT);

/* Infinite loop:
* - Receive data from user
* - Echo the received data back
*/
while (1)
{
/* Receive and store data byte by byte until new line character is received,
* or the buffer becomes full (256 characters received)
*/
LPUART_DRV_ReceiveData(INST_LPUART_LPUART_1, buffer, 1U);
/* Wait for transfer to be completed */
while(LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1, &bytesRemaining) == STATUS_BUSY);

/* Check the status */
status = LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1, &bytesRemaining);

if (status != STATUS_SUCCESS)
{
/* If an error occurred, send the error message and exit the loop */
LPUART_DRV_SendDataBlocking(INST_LPUART_LPUART_1, (uint8_t *)errorMsg, strlen(errorMsg), TIMEOUT);
break;
}

/* Append string terminator to the received data */
bufferIdx++;
buffer[bufferIdx] = 0U;

/* If the received string is "Hello Board", send back "Hello World" */
if(strcmp((char *)buffer, "Hello Board\n") == 0)
{
strcpy((char *)buffer, "Hello World\n");
}

/* Send the received data back */
LPUART_DRV_SendDataBlocking(INST_LPUART_LPUART_1, buffer, bufferIdx, TIMEOUT);
/* Reset the buffer index to start a new reception */
bufferIdx = 0U;
}

for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

0 Kudos
Reply
2 Replies

535 Views
Julián_AragónM
NXP TechSupport
NXP TechSupport

Hi @alice_th,

Even if you configure interrupts for the UART module, you still need to signal that a reception is going to happen.

You must call LPUART_DRV_ReceiveData(). And upon reception, either use LPUART_DRV_SetRxBuffer() to update the buffer and have a continuous reception or end the reception and call LPUART_DRV_ReceiveData() again to enable the Rx interrupt, all inside the UART callback function.

I've attached a modified main for the lpuart_echo example where the polling section in main is removed, and the callback handling I mentioned is implemented. The echo is only performed if bRxFlag is set, which happens in the UART_EVENT_RX_FULL event.

Hope this helps.

0 Kudos
Reply

551 Views
alice_th
Contributor III

i only need rx as interrupt and tx use as printf no need interrupt

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2144936%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ES32k%20uart%20RX%20interrupt%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2144936%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3Ehello%2C%3C%2FP%3E%3CP%3Ei%20see%20there%20is%26nbsp%3Blpuart_echo_s32k144%20demo%2C%20the%20rx%20using%26nbsp%3BLPUART_DRV_InstallRxCallback(INST_LPUART_LPUART_1%2C%20rxCallback%2C%20NULL)%3B%3C%2FP%3E%3CP%3E%26nbsp%3Bwhy%20still%20in%20polling%20mode%20the%20receive%20the%20data%20in%20main%20loop%3F%3C%2FP%3E%3CP%3Ehow%20to%20modify%20it%20with%20rx%20only%20interrupt%20mode%20%3F%26nbsp%3B%3C%2FP%3E%3CP%3Eif%20remove%20below%20code%3C%2FP%3E%3CP%3E%2F*%20Receive%20and%20store%20data%20byte%20by%20byte%20until%20new%20line%20character%20is%20received%2C%3CBR%20%2F%3E*%20or%20the%20buffer%20becomes%20full%20(256%20characters%20received)%3CBR%20%2F%3E*%2F%3CBR%20%2F%3ELPUART_DRV_ReceiveData(INST_LPUART_LPUART_1%2C%20buffer%2C%201U)%3B%3CBR%20%2F%3E%2F*%20Wait%20for%20transfer%20to%20be%20completed%20*%2F%3CBR%20%2F%3Ewhile(LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1%2C%20%26amp%3BbytesRemaining)%20%3D%3D%20STATUS_BUSY)%3B%3C%2FP%3E%3CP%3E%2F*%20Check%20the%20status%20*%2F%3CBR%20%2F%3Estatus%20%3D%20LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1%2C%20%26amp%3BbytesRemaining)%3B%3C%2FP%3E%3CP%3Eit%20doesn't%20work.%3C%2FP%3E%3CP%3Eint%20main(void)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%2F*%20Write%20your%20local%20variable%20definition%20here%20*%2F%3CBR%20%2F%3Estatus_t%20status%3B%3CBR%20%2F%3E%2F*%20Declare%20a%20buffer%20used%20to%20store%20the%20received%20data%20*%2F%3CBR%20%2F%3Euint32_t%20bytesRemaining%3B%3C%2FP%3E%3CP%3E%2F*%20Write%20your%20code%20here%20*%2F%3CBR%20%2F%3E%2F*%20For%20example%3A%20for(%3B%3B)%20%7B%20%7D%20*%2F%3C%2FP%3E%3CP%3E%2F*%20Initialize%20and%20configure%20clocks%3CBR%20%2F%3E*%20-%20see%20clock%20manager%20component%20for%20details%3CBR%20%2F%3E*%2F%3CBR%20%2F%3ECLOCK_SYS_Init(g_clockManConfigsArr%2C%20CLOCK_MANAGER_CONFIG_CNT%2C%3CBR%20%2F%3Eg_clockManCallbacksArr%2C%20CLOCK_MANAGER_CALLBACK_CNT)%3B%3CBR%20%2F%3ECLOCK_SYS_UpdateConfiguration(0U%2C%20CLOCK_MANAGER_POLICY_AGREEMENT)%3B%3C%2FP%3E%3CP%3E%2F*%20Initialize%20pins%3CBR%20%2F%3E*%20-%20See%20PinSettings%20component%20for%20more%20info%3CBR%20%2F%3E*%2F%3CBR%20%2F%3EPINS_DRV_Init(NUM_OF_CONFIGURED_PINS0%2C%20g_pin_mux_InitConfigArr0)%3B%3C%2FP%3E%3CP%3E%2F*%20Initialize%20LPUART%20instance%20*%2F%3CBR%20%2F%3ELPUART_DRV_Init(INST_LPUART_LPUART_1%2C%20%26amp%3BlpUartState1%2C%20%26amp%3BlpUartInitConfig1)%3B%3CBR%20%2F%3E%2F*%20Install%20the%20callback%20for%20rx%20events%20*%2F%3CBR%20%2F%3ELPUART_DRV_InstallRxCallback(INST_LPUART_LPUART_1%2C%20rxCallback%2C%20NULL)%3B%3CBR%20%2F%3E%2F*%20Send%20a%20welcome%20message%20*%2F%3CBR%20%2F%3ELPUART_DRV_SendDataBlocking(INST_LPUART_LPUART_1%2C%20(uint8_t%20*)welcomeMsg%2C%20strlen(welcomeMsg)%2C%20TIMEOUT)%3B%3C%2FP%3E%3CP%3E%2F*%20Infinite%20loop%3A%3CBR%20%2F%3E*%20-%20Receive%20data%20from%20user%3CBR%20%2F%3E*%20-%20Echo%20the%20received%20data%20back%3CBR%20%2F%3E*%2F%3CBR%20%2F%3Ewhile%20(1)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%3CSTRONG%3E%2F*%20Receive%20and%20store%20data%20byte%20by%20byte%20until%20new%20line%20character%20is%20received%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E*%20or%20the%20buffer%20becomes%20full%20(256%20characters%20received)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E*%2F%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ELPUART_DRV_ReceiveData(INST_LPUART_LPUART_1%2C%20buffer%2C%201U)%3B%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%2F*%20Wait%20for%20transfer%20to%20be%20completed%20*%2F%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ewhile(LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1%2C%20%26amp%3BbytesRemaining)%20%3D%3D%20STATUS_BUSY)%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%2F*%20Check%20the%20status%20*%2F%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Estatus%20%3D%20LPUART_DRV_GetReceiveStatus(INST_LPUART_LPUART_1%2C%20%26amp%3BbytesRemaining)%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3Eif%20(status%20!%3D%20STATUS_SUCCESS)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%2F*%20If%20an%20error%20occurred%2C%20send%20the%20error%20message%20and%20exit%20the%20loop%20*%2F%3CBR%20%2F%3ELPUART_DRV_SendDataBlocking(INST_LPUART_LPUART_1%2C%20(uint8_t%20*)errorMsg%2C%20strlen(errorMsg)%2C%20TIMEOUT)%3B%3CBR%20%2F%3Ebreak%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%2F*%20Append%20string%20terminator%20to%20the%20received%20data%20*%2F%3CBR%20%2F%3EbufferIdx%2B%2B%3B%3CBR%20%2F%3Ebuffer%5BbufferIdx%5D%20%3D%200U%3B%3C%2FP%3E%3CP%3E%2F*%20If%20the%20received%20string%20is%20%22Hello%20Board%22%2C%20send%20back%20%22Hello%20World%22%20*%2F%3CBR%20%2F%3Eif(strcmp((char%20*)buffer%2C%20%22Hello%20Board%5Cn%22)%20%3D%3D%200)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Estrcpy((char%20*)buffer%2C%20%22Hello%20World%5Cn%22)%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%2F*%20Send%20the%20received%20data%20back%20*%2F%3CBR%20%2F%3ELPUART_DRV_SendDataBlocking(INST_LPUART_LPUART_1%2C%20buffer%2C%20bufferIdx%2C%20TIMEOUT)%3B%3CBR%20%2F%3E%2F*%20Reset%20the%20buffer%20index%20to%20start%20a%20new%20reception%20*%2F%3CBR%20%2F%3EbufferIdx%20%3D%200U%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Efor(%3B%3B)%20%7B%3CBR%20%2F%3Eif(exit_code%20!%3D%200)%20%7B%3CBR%20%2F%3Ebreak%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ereturn%20exit_code%3B%3CBR%20%2F%3E%7D%20%2F***%20End%20of%20main%20routine.%20DO%20NOT%20MODIFY%20THIS%20TEXT!!!%20***%2F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2145411%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20S32k%20uart%20RX%20interrupt%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2145411%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F218113%22%20target%3D%22_blank%22%3E%40alice_th%3C%2FA%3E%2C%3C%2FP%3E%0A%3CP%3EEven%20if%20you%20configure%20interrupts%20for%20the%20UART%20module%2C%20you%20still%20need%20to%20signal%20that%20a%20reception%20is%20going%20to%20happen.%3C%2FP%3E%0A%3CP%3EYou%20must%20call%26nbsp%3B%3CSTRONG%3ELPUART_DRV_ReceiveData%3C%2FSTRONG%3E().%20And%20upon%20reception%2C%20either%20use%26nbsp%3B%3CSTRONG%3ELPUART_DRV_SetRxBuffer%3C%2FSTRONG%3E()%20to%20update%20the%20buffer%20and%20have%20a%20continuous%20reception%20or%20end%20the%20reception%20and%20call%26nbsp%3B%3CSTRONG%3ELPUART_DRV_ReceiveData%3C%2FSTRONG%3E()%20again%20to%20enable%20the%20Rx%20interrupt%2C%20all%20inside%20the%20UART%20callback%20function.%3C%2FP%3E%0A%3CP%3EI've%20attached%20a%20modified%20main%20for%20the%20lpuart_echo%20example%20where%20the%20polling%20section%20in%20main%20is%20removed%2C%20and%20the%20callback%20handling%20I%20mentioned%20is%20implemented.%20The%20echo%20is%20only%20performed%20if%26nbsp%3B%3CSTRONG%3EbRxFlag%3C%2FSTRONG%3E%20is%20set%2C%20which%20happens%20in%20the%26nbsp%3B%3CSTRONG%3EUART_EVENT_RX_FULL%3C%2FSTRONG%3E%20event.%3C%2FP%3E%0A%3CP%3EHope%20this%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2144940%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20S32k%20uart%20RX%20interrupt%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2144940%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3Ei%20only%20need%20rx%20as%20interrupt%20and%20tx%20use%20as%20printf%20no%20need%20interrupt%3C%2FP%3E%3C%2FLINGO-BODY%3E