The usage of UART callback function install

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

The usage of UART callback function install

跳至解决方案
9,430 次查看
ConstYu
NXP Employee
NXP Employee

I am running the uart_blocking_example in C:\Freescale\KSDK_1.2.0\examples\frdmk22f\driver_examples\uart\uart_blocking\iar,  it can runs well, after adding the UART_DRV_InstallTxCallback function like below code, it blocked the UART output. so i wonder the usage of UART callback function, is there any example?

 

    // Initialize the uart module with base address and config structure

    UART_DRV_Init(BOARD_DEBUG_UART_INSTANCE, &uartState, &uartConfig);

   UART_DRV_InstallTxCallback(uint32_t instance,  (uart_tx_callback_t) callback, NULL,  NULL);

    // Inform to start blocking example

    byteCountBuff = sizeof(buffStart);

    UART_DRV_SendDataBlocking(BOARD_DEBUG_UART_INSTANCE, buffStart, byteCountBuff, 1000u);

标签 (1)
0 项奖励
回复
1 解答
6,865 次查看
ConstYu
NXP Employee
NXP Employee

Hi Xiangjun,

Thanks for your reply, i test the code you send me, but found it always output "K" after install the callback function, the callback function should only output one char "K" at one time, i also only do count++ in the call back, but found it will not stop to call the callback, it never to stop. My another colleague also encounter this issue.

So i dive into the driver layer code carefully, found it would be one bug in driver lay, it needs to add the two line red code, as below is the code in TX interrupt in fsl_uart_driver.c, if i install the TX callback function by  UART_DRV_InstallTxCallback function, the code will never move the position of TXbuffer and transfer byte numbers, so it lead to never to call UART_DRV_CompleteSendData(instance);

Now after i add these two line, the code can work as expected. do you think so? (one remainder, don't forget to build the lib after you change the fsl_uart_driver.c file)

if((UART_BRD_C2_TIE(base)) && (UART_BRD_S1_TDRE(base)))

    {

        /* Check to see if there are any more bytes to send */

        if (uartState->txSize)

        {

            uint8_t emptyEntryCountInFifo;

#if FSL_FEATURE_UART_HAS_FIFO

            emptyEntryCountInFifo = uartState->txFifoEntryCount -

                                    UART_HAL_GetTxDatawordCountInFifo(base);

#else

            emptyEntryCountInFifo = uartState->txFifoEntryCount;

#endif

            while(emptyEntryCountInFifo--)

            {

                /* Transmit data and update tx size/buff */

                UART_HAL_Putchar(base, *(uartState->txBuff));

                /* Invoke callback if there is one */

                if (uartState->txCallback != NULL)

                {

                  /* The callback MUST set the txSize to 0 if the

                    * transmit is ended.*/

                  uartState->txCallback(instance, uartState);

                

                  ++uartState->txBuff;

                  --uartState->txSize;

                }

                else

                {

                    ++uartState->txBuff;

                    --uartState->txSize;

                }

                /* Check and see if this was the last byte */

                if (uartState->txSize == 0U)

                {

                    UART_DRV_CompleteSendData(instance);

                    break;

                }

            }

        }

在原帖中查看解决方案

0 项奖励
回复
4 回复数
6,284 次查看
Elieser
Contributor I

Thanks, you helped me a lot for this!!

 

0 项奖励
回复
6,864 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, NingNing,

I have developed an example based on the example the SDK provides:

D:\Freescale\KSDK_1.2.0\examples\frdmk22f\driver_examples\uart\uart_non_blocking, I have changed the main.c to test the call back function, the callback function works well.

I attach the main.c function. Hope it can help you.

BR

Xiangjun rong

0 项奖励
回复
6,866 次查看
ConstYu
NXP Employee
NXP Employee

Hi Xiangjun,

Thanks for your reply, i test the code you send me, but found it always output "K" after install the callback function, the callback function should only output one char "K" at one time, i also only do count++ in the call back, but found it will not stop to call the callback, it never to stop. My another colleague also encounter this issue.

So i dive into the driver layer code carefully, found it would be one bug in driver lay, it needs to add the two line red code, as below is the code in TX interrupt in fsl_uart_driver.c, if i install the TX callback function by  UART_DRV_InstallTxCallback function, the code will never move the position of TXbuffer and transfer byte numbers, so it lead to never to call UART_DRV_CompleteSendData(instance);

Now after i add these two line, the code can work as expected. do you think so? (one remainder, don't forget to build the lib after you change the fsl_uart_driver.c file)

if((UART_BRD_C2_TIE(base)) && (UART_BRD_S1_TDRE(base)))

    {

        /* Check to see if there are any more bytes to send */

        if (uartState->txSize)

        {

            uint8_t emptyEntryCountInFifo;

#if FSL_FEATURE_UART_HAS_FIFO

            emptyEntryCountInFifo = uartState->txFifoEntryCount -

                                    UART_HAL_GetTxDatawordCountInFifo(base);

#else

            emptyEntryCountInFifo = uartState->txFifoEntryCount;

#endif

            while(emptyEntryCountInFifo--)

            {

                /* Transmit data and update tx size/buff */

                UART_HAL_Putchar(base, *(uartState->txBuff));

                /* Invoke callback if there is one */

                if (uartState->txCallback != NULL)

                {

                  /* The callback MUST set the txSize to 0 if the

                    * transmit is ended.*/

                  uartState->txCallback(instance, uartState);

                

                  ++uartState->txBuff;

                  --uartState->txSize;

                }

                else

                {

                    ++uartState->txBuff;

                    --uartState->txSize;

                }

                /* Check and see if this was the last byte */

                if (uartState->txSize == 0U)

                {

                    UART_DRV_CompleteSendData(instance);

                    break;

                }

            }

        }

0 项奖励
回复
6,864 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, NingNing,

I think your modification is okay. whether the driver is okay or not is dependent on how to define the task of the callback function. Generally, the callback function is called in the interrupt service routine of the UART transmitter. After the UART module has completed to transfer a char, the interrupt service routine of the UART transmitter is executed once, of course, the callback function is executed once.

BR

xiangjun Rong

0 项奖励
回复