chaoxi cai

two suggestion for UART driver of KDSK2

Discussion created by chaoxi cai on Jan 31, 2017
Latest reply on Jun 21, 2017 by hector gastaminza

1. UART_RTOS_Receive() in fsl_uart_freertos.c ,

 

int UART_RTOS_Receive(uart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received)
{   ...
    /* Non-blocking call */
    UART_TransferReceiveNonBlocking(handle->base, handle->t_state, &handle->rxTransfer, &n);

    ev = xEventGroupWaitBits(handle->rxEvent,
                             RTOS_UART_COMPLETE | RTOS_UART_RING_BUFFER_OVERRUN | RTOS_UART_HARDWARE_BUFFER_OVERRUN,
                             pdTRUE, pdFALSE, portMAX_DELAY);
    ...
}

   If a program act as master, it would first send something to the slave, and wait slave respond. but if the packet come from slave occure any error, master can not receive enough data that it want to, the RTOS_UART_COMPLETE event will never be fired, the program will blocked in xEventGroupWaitBits() forever. because the parameter uxBitsToWaitFor is set portMAX_DELAY(it's means to never timeout). I suggest add a timeout parameter for UART_RTOS_Receive() to give a chance for user, to handle if any error occure from incoming data issue.

 

 

2. UART_TransferHandleIRQ()  in fsl_uart.c, do not need to call UART_TransferIsRxRingBufferFull() twice every time. I have make some change as follow.

void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle)
{
    ...
        /* If use RX ring buffer, receive data to ring buffer. */
        if (handle->rxRingBuffer)
        {
            while (count--)
            {
                /* If RX ring buffer is full, trigger callback to notify over run. */
                if (UART_TransferIsRxRingBufferFull(handle))
                {
                    if (handle->callback)
                    {
                        handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData);

                    }
                    /* Move to here */
                          /* If ring buffer is still full after callback function, the oldest data is overrided. */
                          if (UART_TransferIsRxRingBufferFull(handle))
                          {
                              /* Increase handle->rxRingBufferTail to make room for new data. */
                              if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
                              {
                                  handle->rxRingBufferTail = 0U;
                              }
                              else
                              {
                                  handle->rxRingBufferTail++;
                              }
                          }                   
                }

             #if 0  // don't need to call twice every time.
                /* If ring buffer is still full after callback function, the oldest data is overrided. */
                if (UART_TransferIsRxRingBufferFull(handle))
                {
                    /* Increase handle->rxRingBufferTail to make room for new data. */
                    if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
                    {
                        handle->rxRingBufferTail = 0U;
                    }
                    else
                    {
                        handle->rxRingBufferTail++;
                    }
                }
              #endif

                /* Read data. */
                handle->rxRingBuffer[handle->rxRingBufferHead] = base->D;
    ...
}

Outcomes