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.

AM5748: UART_readPolling is causing delays in the execution

Part Number: AM5748
Other Parts Discussed in Thread: SYSBIOS

Tool/software:

Hi,

I am working on UART communication with modem.As per my observation,If i use the UART_read API there is a data loss approximately 4-5 bytes of data is lost.But if i use the UART_readPolling API there is a huge delay seen though the entire packet is being read correctly.How to resolve this delay issue.

Thanks,

Anila

  • Hi Anila,

    As per my observation,If i use the UART_read API there is a data loss approximately 4-5 bytes of data is lost.But if i use the UART_readPolling API there is a huge delay seen though the entire packet is being read correctly.How to resolve this delay issue.

    Could you please tell me which SDK version you are using and whether you are using a custom board or TI EVM? if it is TI EVM , Please describe the modifications and procedures you took to identify the issue, and I'll try to reproduce it on my end.

    Regards,

    Karthik

  • Hi ,

    I am using processor_sdk_rtos_am57xx_08_01_00_09 and a custom board.
    I am trying to send a packet of length 71 for every 20 ms with the UART baud rate of 19200.By using the uart_read there is a data loss and by using the UART_readPolling there is a delay of 150 ms between the packets.

    The uart parameters that is set to 


    UART_Params_init(&uartParams);
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.readTimeout =0;
    uartParams.baudRate =19200;

  • Hi,

    I am using processor_sdk_rtos_am57xx_08_01_00_09 and a custom board.

    Could you be able to verify that you can read and write without any errors by testing the uart driver example on your custom board at 115200 baud rate?

    Regards,
    Karthik

  • Yes UART_write and UART_read is working fine for 115200,but the issue is with the delay caused because of UART_readPolling

  • Hi,

    I am trying to send a packet of length 71 for every 20 ms with the UART baud rate of 19200.By using the uart_read there is a data loss and by using the UART_readPolling there is a delay of 150 ms between the packets.

    If you're reading 71 bytes, it should take approximately 37 ms per packet at 19200 baud. However, if you're using a large timeout, the function may wait unnecessarily long before returning.Try adjusting the timeout to match the expected packet arrival rate more closely. For example, set the timeout to 40 ms to match the expected time to receive a packet.  and increasing the baud rate can help this kind of data loss.

    Regards,

    Karthik

  • One of the observation that i found with respect to UART_readPolling is


    [2024-10-04 15:00:19.180] before UART_readPolling
    [2024-10-04 15:00:19.180] after UART_readPolling
    [2024-10-04 15:00:19.180] before UART_readPolling
    [2024-10-04 15:00:19.180] after UART_readPolling
    [2024-10-04 15:00:19.180] before UART_readPolling
    [2024-10-04 15:00:19.202] after UART_readPolling
    [2024-10-04 15:00:19.202] before UART_readPolling
    [2024-10-04 15:00:19.202] after UART_readPolling

    Here for a certain amount of time only this UART_readPolling is executed though the function itself is not taking any time for its execution.Does it mean that the blocking nature of UART_readPolling is causing other tasks not to execute and creating an delay at overall.

  • Hi,

    Yes, In UART_readPolling, the CPU remains blocked, waiting for the entire data to be received. While it's waiting, other tasks are not allowed to execute, resulting in an overall delay in your system's responsiveness. This is expected behaviour in polling mode.

    Regards,

    Karthik

  • Hi,

    Since the polling method is not solving the delay issue,I am checking it with the interrupt mode.Following is my code

    void uartCallback(UART_Handle handle, void *rxBuf, size_t size) {
    ConsolePrint("UART callback triggered\n");

    if (size > 0) {
    ConsolePrint("Received %d bytes: ", size);
    for (size_t i = 0; i < size; i++) {
    ConsolePrint("0x%02x ", ((uint8_t*)rxBuf)[i]);
    }
    ConsolePrint("\n");

    } else {
    ConsolePrint("No data received\n");
    }

    UART_read(handle, rxBuf, 16);
    }

    void configure_uart8_irq(void) {
    CSL_xbarIrqConfigure(CSL_XBAR_IRQ_CPU_ID_MPU,CSL_XBAR_INST_MPU_IRQ_72, CSL_XBAR_UART8_IRQ);
    }

    void uartIrqEnable(uint16_t uartNo)
    {
    if(uartNo == 7)
    {
    CSL_xbarMpuIrqConfigure(CSL_XBAR_INST_MPU_IRQ_72,CSL_XBAR_UART8_IRQ);

    }

    }

    int UARTPinMuxing(uint16_t uartNo)
    {
    CSL_l4per_cm_core_componentRegs *l4PerCmReg = (CSL_l4per_cm_core_componentRegs *) CSL_MPU_L4PER_CM_CORE_REGS;


    if(uartNo == 4)
    {
    CSL_FINST(l4PerCmReg->CM_L4PER_UART4_CLKCTRL_REG, L4PER_CM_CORE_COMPONENT_CM_L4PER_UART4_CLKCTRL_REG_MODULEMODE, ENABLE);
    while (CSL_L4PER_CM_CORE_COMPONENT_CM_L4PER_UART4_CLKCTRL_REG_IDLEST_FUNC != CSL_FEXT(l4PerCmReg->CM_L4PER_UART4_CLKCTRL_REG, L4PER_CM_CORE_COMPONENT_CM_L4PER_UART4_CLKCTRL_REG_IDLEST));
    return;
    }
    /* enable UART8 clock */
    if(uartNo == 7)
    {
    CSL_FINST(l4PerCmReg->CM_L4PER2_UART8_CLKCTRL_REG, L4PER_CM_CORE_COMPONENT_CM_L4PER2_UART8_CLKCTRL_REG_MODULEMODE, ENABLE);
    while (CSL_L4PER_CM_CORE_COMPONENT_CM_L4PER2_UART8_CLKCTRL_REG_IDLEST_FUNC != CSL_FEXT(l4PerCmReg->CM_L4PER2_UART8_CLKCTRL_REG, L4PER_CM_CORE_COMPONENT_CM_L4PER2_UART8_CLKCTRL_REG_IDLEST));

    }
    HW_WR_REG32(0x4A0034F4 , 0x00050005); // rx //0101 0000 0000 0000 0101
    HW_WR_REG32(0x4A0034F8, 0x00010005); // tx //0001 0000 0000 0000 0101
    HW_WR_REG32(0x4A0034FC, 0x00050005); // cts //0101 0000 0000 0000 0101
    HW_WR_REG32(0x4A003500, 0x00010005); // rts //0001 0000 0000 0000 0101


    GPIOModuleEnable(CSL_MPU_GPIO8_REGS); //GPIO8 bank enable
    GPIOModuleEnable(CSL_MPU_GPIO3_REGS); //GPIO3 bank enable
    GPIOModuleEnable(CSL_MPU_GPIO7_REGS); //GPIO7 bank enable

    }


    void InitUART(uint16_t uartNo) {
    UART_Params uartParams;

    UARTPinMuxing(uartNo);
    UART_init();

    UART_Params_init(&uartParams);
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.readMode = UART_MODE_CALLBACK;
    uartParams.readCallback = uartCallback;
    uartParams.baudRate = 115200;
    UART_Handle uarthandle = UART_open(uartNo, &uartParams);
    if (uarthandle == NULL) {
    ConsolePrint("Error: Failed to open UART %d\n", uartNo);
    return;
    }

    uartIrqEnable(uartNo);
    configure_uart8_irq();
    UART_write(uarthandle,"abcdefghijklmnop",strlen("abcdefghijklmnop"));
    UART_read(uarthandle, rxBuf, 16);
    ConsolePrint("UART %d initialized successfully.\n", uartNo);
    }


    in the code the callback function is not getting triggered.UART_write works only if uartIrqEnable() function is called.is there any other configuration that I am missing out for the callback function to be triggered.

  • Hi Anila,

    in the code the callback function is not getting triggered.UART_write works only if uartIrqEnable() function is called.is there any other configuration that I am missing out for the callback function to be triggered

    Could you please refer this /pdk_am57xx_1_0_21/packages/ti/drv/uart/test/src/main_uart_test.c  file let us if this helps.

    Regards,

    Karthik

  • Hi,

    The read callback function is triggered only if UART_readCancel()Api is called after the initial read.The callback function is triggered only once and even if the UART_read is being in the callback function,it(callback function is not getting again and again).If the UART_readCancel() API is used in the calback also then only that function gets executed for sometime and the code crashes.Someone please help me with the changes that is to be done for the following,for the UART_read to work in the callback mode and the UART_write to work in the blocking mode.


    #include <stdio.h>
    #include <string.h>

    #ifdef USE_BIOS
    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/runtime/Error.h>
    #include <xdc/runtime/System.h>

    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #include <ti/board/board.h>
    #endif /* #ifdef USE_BIOS */

    /* CSL Header files */
    #include <ti/csl/csl_chip.h>
    #include <ti/csl/soc.h>
    #include <ti/csl/arch/a15/interrupt.h>

    /* OSAL Header files */
    #include <ti/osal/osal.h>

    /* UART Header files */
    #include <ti/drv/uart/UART.h>
    #include <ti/drv/uart/UART_stdio.h>

    #include <ti/drv/uart/src/UART_osal.h>
    #include <ti/drv/uart/soc/UART_soc.h>

    /*LWS GPIO RESET added*/
    #include <ti/drv/gpio/GPIO.h>
    #include <ti/drv/gpio/soc/GPIO_soc.h>
    #include <ti/drv/gpio/soc/GPIO_v1.h>

    /* GPIO CSL libraries */
    #include <ti/csl/src/ip/gpio/V1/gpio_v2.h>
    #include <ti/csl/soc/am574x/src/cslr_soc_mpu_baseaddress.h>

    #define UART_CMD_RESERVED 32
    #define UART_AM57XX_RX_FIFO_FLUSH UART_CMD_RESERVED + 2

    //#include "UART_board.h"

    //#define UART_INSTANCE1 2U
    uint32_t uartTestInstance = 7U;

    UART_PAR uartParity = UART_PAR_NONE;

    UART_Handle uart = NULL;
    UART_Params uartParams;

    char *receivedData;
    /* UART8 init and clock enable */
    bool Board_initUART(void)
    {
    Board_initCfg boardCfg;
    Board_STATUS boardStatus;
    CSL_l4per_cm_core_componentRegs *l4PerCmReg =
    (CSL_l4per_cm_core_componentRegs *) CSL_MPU_L4PER_CM_CORE_REGS;


    boardCfg = BOARD_INIT_PINMUX_CONFIG |
    BOARD_INIT_MODULE_CLOCK |
    BOARD_INIT_UART_STDIO;

    boardStatus = Board_init(boardCfg);
    if (boardStatus != BOARD_SOK)
    {
    return (false);
    }
    /* enable UART4 clock */
    CSL_FINST(l4PerCmReg->CM_L4PER_UART4_CLKCTRL_REG,
    L4PER_CM_CORE_COMPONENT_CM_L4PER_UART4_CLKCTRL_REG_MODULEMODE,
    ENABLE);

    while (CSL_L4PER_CM_CORE_COMPONENT_CM_L4PER_UART4_CLKCTRL_REG_IDLEST_FUNC
    != CSL_FEXT(
    l4PerCmReg->CM_L4PER_UART4_CLKCTRL_REG,
    L4PER_CM_CORE_COMPONENT_CM_L4PER_UART4_CLKCTRL_REG_IDLEST))
    ;

    CSL_FINST(l4PerCmReg->CM_L4PER2_UART8_CLKCTRL_REG,
    L4PER_CM_CORE_COMPONENT_CM_L4PER2_UART8_CLKCTRL_REG_MODULEMODE,
    ENABLE);

    while (CSL_L4PER_CM_CORE_COMPONENT_CM_L4PER2_UART8_CLKCTRL_REG_IDLEST_FUNC
    != CSL_FEXT(
    l4PerCmReg->CM_L4PER2_UART8_CLKCTRL_REG,
    L4PER_CM_CORE_COMPONENT_CM_L4PER2_UART8_CLKCTRL_REG_IDLEST))
    ;

    // HW_WR_REG32(0x4A0037E0, 0xD0000); // rx
    // HW_WR_REG32(0x4A0037E4, 0x90000); // tx
    // HW_WR_REG32(0x4A0037E8, 0x50000); // cts
    // HW_WR_REG32(0x4A0037EC, 0x10000); // rts

    /* UART 8 */
    // HW_WR_REG32(0x4A0034F4 , 0x50005); // rx
    // HW_WR_REG32(0x4A0034F8, 0x10005); // tx
    // HW_WR_REG32(0x4A0034FC, 0x50005); // cts
    // HW_WR_REG32(0x4A003500, 0x10005); // rts

    GPIOModuleEnable(CSL_MPU_GPIO8_REGS); //GPIO8 bank enable
    GPIOModuleEnable(CSL_MPU_GPIO3_REGS); //GPIO3 bank enable
    GPIOModuleEnable(CSL_MPU_GPIO7_REGS); //GPIO7 bank enable
    /* GPIO For MODEM reset*/

    HW_WR_REG32(0x4A003520,0x0004000E); // GPIO3_15
    HW_WR_REG32(0x4A0035E0,0x0004000E); // GPIO8_1
    HW_WR_REG32(0x4A003790,0x0004000E); // GPIO7_2
    }


    static void UART_initConfig(bool dmaMode)
    {
    UART_HwAttrs uart_cfg;

    /* Get the default UART init configurations */
    UART_socGetInitCfg(uartTestInstance, &uart_cfg);

    uart_cfg.edmaHandle = NULL;
    uart_cfg.dmaMode = dmaMode;
    uart_cfg.loopback = FALSE;

    /* Set the DMA enabled UART init configurations */
    UART_socSetInitCfg(uartTestInstance, &uart_cfg);
    }

    Void MODEM_reset(void)
    {
    GPIODirModeSet(CSL_MPU_GPIO3_REGS, 15, GPIO_DIR_OUTPUT);
    GPIODirModeSet(CSL_MPU_GPIO8_REGS, 1, GPIO_DIR_OUTPUT);
    GPIODirModeSet(CSL_MPU_GPIO7_REGS, 2, GPIO_DIR_OUTPUT);

    UART_printf("Resetting the modem\n");
    GPIOPinWrite(CSL_MPU_GPIO3_REGS, 15, 1);
    Task_sleep(1000);
    GPIOPinWrite(CSL_MPU_GPIO3_REGS, 15, 0);
    Task_sleep(1000);
    GPIOPinWrite(CSL_MPU_GPIO3_REGS, 15, 1);
    GPIOPinWrite(CSL_MPU_GPIO8_REGS, 1, 1);
    Task_sleep(500);
    UART_printf("Reset Done \n");
    }

    //Void modem_read_task_fxn(UArg a0, UArg a1)
    //{
    // unsigned char rx_buf[17];
    // int i ,count;
    // UART_printf("in the callback \n");
    // {
    // count = UART_read(uart,rx_buf,1);
    // if(rx_buf[0] == 'O' && rx_buf[1] == 'K')
    // {
    // UART_printf("MODEM read %s \n",rx_buf);
    // }
    // for(i = 0 ; i < 17; i++)
    // {
    // rx_buf[i] = 0x0;
    // }
    // UART_printf("MODEM read %s \n",rx_buf);
    //
    // }
    //}

    Void modem_read_task_fxn(UArg a0, UArg a1)
    {
    unsigned char rx_buf[17] = {0}; // Initialize the buffer to zero
    int count;

    UART_printf("In the callback \n");

    // Read data from the UART into the buffer
    count = UART_read(uart, rx_buf, sizeof(rx_buf) - 1); // Read up to 16 bytes

    rx_buf[count] = '\0'; // Null-terminate the received string

    if (count > 1 && rx_buf[0] == 'O' && rx_buf[1] == 'K') // Check for "OK"
    {
    UART_printf("MODEM read %s \n", rx_buf);
    }
    else
    {
    UART_printf("Received data: %s \n", rx_buf); // Print all received data
    }

    Task_sleep(10);

    }


    static unsigned char rx_buf[17]; // Buffer for receiving data

    Void modemUartOpen()
    {

    UART_init();

    UART_initConfig(false);

    // UARTModuleReset(CSL_MPU_UART8_REGS);

    /* Initialize the default configuration params. */
    UART_Params_init(&uartParams);
    uartParams.readCallback =modem_read_task_fxn;
    uartParams.readMode = UART_MODE_CALLBACK;
    uartParams.parityType = uartParity;
    uartParams.baudRate = 19200;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.writeDataMode = UART_DATA_BINARY;
    // uartParams.writeMode = UART_MODE_CALLBACK;

    uart = UART_open(uartTestInstance, &uartParams);

    CSL_xbarMpuIrqConfigure(CSL_XBAR_INST_MPU_IRQ_72,CSL_XBAR_UART8_IRQ);

    UART_read(uart,rx_buf, 1);
    // Osal_delay(10000);

    /* Cancel the read before the read transfer is completed */
    UART_readCancel(uart);
    }

    Void modem_cmd_task_fxn(UArg a0, UArg a1)
    {
    unsigned char atcmd1[4] = "AT\r\n"; // LWS
    unsigned char atcmd2[17] = "ATE0+FCLASS = 0\r\n"; // LWS
    unsigned char atcmd3[6] = "ATH1\r\n"; // LWS
    unsigned char atcmd4[6] = "ATH0\r\n"; // LWS
    int32_t count = 0;

    // char atcmd3[] = "AT+IPR = 4800"; //LWS

    MODEM_reset();

    count = UART_write(uart, &atcmd2[0], sizeof(atcmd2));
    UART_printf("Bytes written %d\n", count);

    while (1)
    {
    #if 1
    count = UART_write(uart, &atcmd3[0], sizeof(atcmd3));
    UART_printf("Bytes written %d ATH1\n", count);
    Task_sleep(1000);
    count = UART_write(uart, &atcmd4[0], sizeof(atcmd4));
    UART_printf("Bytes written %d ATH0\n", count);
    #else
    count = UART_write(uart, &atcmd1[0], sizeof(atcmd1));
    UART_printf("Bytes written %d\n", count);
    #endif
    Task_sleep(1000);

    }
    }

    /* MAIN */

    Int main()
    {
    Task_Handle task;
    Error_Block eb;
    Task_Params taskParams1 , taskParams2;

    if (Board_initUART() == false)
    {
    UART_printf("\nBoard_initUART failed!\n");
    return(0);
    }
    UART_printf("\nModem Init..\n");

    modemUartOpen();

    Error_init(&eb);

    /* Initialize the task params */
    Task_Params_init(&taskParams1);

    /* Set the task priority higher than the default priority (1) */
    taskParams1.priority = 2;
    taskParams1.stackSize = 0x2000;

    task = Task_create(modem_cmd_task_fxn, &taskParams1, &eb);
    if (task == NULL)
    {

    BIOS_exit(0);
    }


    BIOS_start(); /* does not return */

    return(0);
    }

     

  • Hi,

    Could you please test the UART_BasicExample_idkAM574x_armTestproject uart driver example on your custom board. It will help you determine whether your uart callback is proper.

    Please find the attached file:

    UART UT 3
    
     UART non-DMA read write test in callback mode
    
     enter the data of 16 character 
    
     Data entered is as follows 
    dsfsfdsdfsdfsdff
     enter the data of 16 character 
    
     Data entered is as follows 
    fffffffffffkkkkj
    
    UART UT 3 PASSED
    

    Regards,

    Karthik