The usage of UART callback function install

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

The usage of UART callback function install

ソリューションへジャンプ
9,401件の閲覧回数
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,836件の閲覧回数
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,255件の閲覧回数
Elieser
Contributor I

Thanks, you helped me a lot for this!!

 

0 件の賞賛
返信
6,835件の閲覧回数
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,837件の閲覧回数
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,835件の閲覧回数
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 件の賞賛
返信