This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP432P4111: Issue with UART receive callback function

Part Number: MSP432P4111


Hi,

I'm using a UART communication with another device. The incoming string is terminated with "\r\n". All is working well but sometimes it lost some incoming chars. The place where the chars are missed is not at the beginning or end, it is between the whole string. That the incoming answer string is ok I have checked with another serial monitor.
I think that the way I'm using this UART implementation must be the problem.

I use FreeRTOS with a 1ms tick. This one task is responsible for the uart read. With the callback function the incoming data are received and send away with a queue. After this I "reactivat" the UART_read().

char lineBuffer[256];

SemaphoreHandle_t xSemaphoreReadLine = NULL;

void uartRxFinished(UART_Handle handle, void *buf, size_t count)
{
    static BaseType_t xHigherPriorityTaskWoken = 0;

    // check if a not only \r\n is sent
    if(count>1)
    {
        memcpy(lineBuffer, buf, count);
        lineBuffer[count] = 0;

        if( xQueueModemRx != 0 )
        {
            BaseType_t xHigherPriorityTaskWoken;

           xHigherPriorityTaskWoken = pdFALSE;
           if(count>1){
               xQueueSendFromISR( xQueueModemRx, lineBuffer, &xHigherPriorityTaskWoken );
           }

           if( xHigherPriorityTaskWoken ){
               portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
           }
        }
    }

    if (xSemaphoreReadLine != NULL)
        xSemaphoreGiveFromISR(xSemaphoreReadLine, &xHigherPriorityTaskWoken);

    // force rescheduling if the signaled semaphore woke up a higher priorized thread
    if (xHigherPriorityTaskWoken){
        taskYIELD();
    }
}

void uartTxFinished(UART_Handle handle, void *buf, size_t count)
{
}

void *modemTask(void *arg0)
{
    UART_Params uartParams;
    UART_init();

    /* Create a UART with data processing off. */
    UART_Params_init(&uartParams);
    uartParams.readMode = UART_MODE_CALLBACK;
    uartParams.writeMode = UART_MODE_CALLBACK;
    uartParams.readDataMode = UART_DATA_TEXT;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readCallback = uartRxFinished;
    uartParams.writeCallback = uartTxFinished;
    uartParams.readReturnMode = UART_RETURN_NEWLINE;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 115200;
    uartParams.dataLength = UART_LEN_8;
    uartParams.parityType = UART_PAR_NONE;
    uartParams.stopBits = UART_STOP_ONE;

    uart = UART_open(Board_UART1, &uartParams);
    if (uart == NULL) {
        /* UART_open() failed */
        while (1);
    }

    xSemaphoreReadLine = xSemaphoreCreateBinary();
    xSemaphoreGive(xSemaphoreReadLine);
    xSemaphoreTake(xSemaphoreReadLine, portMAX_DELAY);

    UART_read(uart, RxBuf, sizeof(RxBuf));


    while (1) {
        if( xSemaphoreTake( xSemaphoreReadLine, portMAX_DELAY ) == pdTRUE )
        {
            /* reactivate receiving data */
            UART_read(uart, RxBuf, sizeof(RxBuf));
        }
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}


I also tried it with task priority=1 but it doesn't matter.

I'm not really sure if the using of uart_read() function is ok?

  • user5841294 said:
    All is working well but sometimes it lost some incoming chars.

    How are you determining that chars are missing? Are you checking in the task that reads the Queue "xQueueModemRx"? If so, can you confirm if that task is not being starved.

    user5841294 said:
    vTaskDelay(10 / portTICK_PERIOD_MS);

    Why is this line needed, since the xSemaphoreTake is waiting for ever? This line adds 10ms delay after the UART_read is called. Is that desired?

    Thanks,

    Sai

  • Hi,

    Sai Reddy said:
    How are you determining that chars are missing?

    Yes, I'm checking the task that reads the Queue. Since the Queue is big enough, no chars should be lost. And because of the fact that chars between a receive line are missing (mostly 1 or two chars) I would say that the queue works well.

    Sai Reddy said:
    Why is this line needed, since the xSemaphoreTake is waiting for ever?

    Why should the  if( xSemaphoreTake(xSemaphoreReadLine, portMAX_DELAY ) == pdTRUE) be waiting forever?

  • user5841294 said:
    Why should the  if( xSemaphoreTake(xSemaphoreReadLine, portMAX_DELAY ) == pdTRUE) be waiting forever?

    Please refer the FreeRTOS documentation: 

    Thanks,

    Sai

  • Yes, you are right. I also tested it without any delay in the while loop and the missing chars are a bit rarer.

    What else could be a reason for this?

**Attention** This is a public forum