In a project, I am currently working on. I am trying to use LPUART for three UART ports. Most of the code are based from UART interrupt example project provided in the MCUXPRESSO IDE. The goal was to implement three UART port for three different tasks. Trouble understanding what is the best way to get data received from the interrupt. Currently, this is what I have done so far for one of the task. I removed all the other codes that are within the task, but this is the layout I am trying to use to make UART work. Currently, the issue is that the message received from UART does not get copied properly to the recv_buffer. Below the code there is a our debug console message. I used teraterm to send messages to UART. Thank you for the help!
/**********************CODE*************************************/
uint8_t UART1RingBuffer[UART1_RING_BUFFER_SIZE];
volatile uint16_t txIndex; /* Index of the data to send out. */
volatile uint16_t rxIndex; /* Index of the memory to save new arrived data. */
uint8_t g_tipString[] =
"UART 1 initialized \r\n";
void UART1_IRQHandler(void)
{
uint8_t data;
uint16_t tmprxIndex = rxIndex;
uint16_t tmptxIndex = txIndex;
/* If new data arrived. */
if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(LPUART_1))
{
data = LPUART_ReadByte(LPUART_1);
/* If ring buffer is not full, add data to ring buffer. */
if (((tmprxIndex + 1) % UART1_RING_BUFFER_SIZE) != tmptxIndex)
{
UART1RingBuffer[rxIndex] = data;
rxIndex++;
rxIndex %= UART1_RING_BUFFER_SIZE;
}
}
SDK_ISR_EXIT_BARRIER;
// Printing out received data:
PRINTF("Printing out received data:");
for (int i = 0; i < UART1_RING_BUFFER_SIZE; i++)
{
PRINTF("UART RING BUFFER ITEM: %u\n:", UART1RingBuffer[i]);
}
}
void com_task(void *pvParameters)
{
/*initiate UART 1*/
lpuart_config_t config;
uint16_t tmprxIndex = rxIndex;
uint16_t tmptxIndex = txIndex;
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;
LPUART_Init(LPUART_1, &config, LPUART1_CLK_FREQ);
/* Send g_tipString out. */
if(kStatus_Success == LPUART_WriteBlocking(LPUART_1, g_tipString, sizeof(g_tipString) / sizeof(g_tipString[0]))) {
PRINTF("UART1 succeed write blocking\r\n");
} else {
PRINTF("UART1 failed write blocking\r\n");
}
/* Enable RX interrupt. */
LPUART_EnableInterrupts(LPUART_1, kLPUART_RxDataRegFullInterruptEnable);
EnableIRQ(UART1_IRQn);
/*UART 1 initialization done */
uint8_t recv_buffer[UART1_RING_BUFFER_SIZE];
static QueueHandle_t uartRxQueue; //handle by which the queue can be referenced
uartRxQueue = xQueueCreate(sizeof(g_tipString)/sizeof(char), sizeof(char));
configASSERT(uartRxQueue);
LPUART_DisableInterrupts(LPUART_1, kLPUART_RxDataRegFullInterruptEnable);
for(;;)
{
xLastWakeTime = xTaskGetTickCount();
LPUART_EnableInterrupts(LPUART_1, kLPUART_RxDataRegFullInterruptEnable);
xQueueSendToBack(uartRxQueue, &UART1RingBuffer, ( TickType_t ) 1000);
xQueueReceive(uartRxQueue, &recv_buffer, ( TickType_t ) 1000); //instead of uart_receive
PRINTF("UART Receive: %c\n", recv_buffer);
for(int i = 0; i < UART1_RING_BUFFER_SIZE; i++) {
PRINTF("%c", recv_buffer[i]);
}
PRINTF("\n");
LPUART_DisableInterrupts(LPUART_1, kLPUART_RxDataRegFullInterruptEnable);
vTaskDelayUntil(&xLastWakeTime, xDelayms);
}
}
/**********************CODE*************************************/
/**********************DEBUG CONSOLE****************************/
UART1 succeed write blocking
UART Receive: ,
Printing out received data:UART RING BUFFER ITEM: 97
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:Printing out received data:UART RING BUFFER ITEM: 97
:UART RING BUFFER ITEM: 115
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:Printing out received data:UART RING BUFFER ITEM: 97
:UART RING BUFFER ITEM: 115
:UART RING BUFFER ITEM: 100
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:Printing out received data:UART RING BUFFER ITEM: 97
:UART RING BUFFER ITEM: 115
:UART RING BUFFER ITEM: 100
:UART RING BUFFER ITEM: 102
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART RING BUFFER ITEM: 0
:UART Receive: ,
a¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥