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.

Can only read or write upto 16 bytes in UART in TM4C129.



Is there any solution to read or write more than 16 bytes?

  • Yes,

    Flippantly the answer is just perform the read correctly. The usual problem is that there is no check made for available characters.

    Robert
  • Hello Anjana

    Code please. How do we know what process is being used to read the UART without knowing how the code has been structured

    Hello Robert,

    Seeing the number of thread on this issue, do you think it is worth doing a code for more than 16 bytes (of course end equipment or device needs to be defined)

    Regards
    Amit
  • Amit,

    Yep, probably a simple interrupt example to show the necessity of dealing with both receive interrupt sources.

    Robert
  • Amit Ashara said:
    Seeing the number of thread on this issue, do you think it is worth doing a code for more than 16 bytes

    But alas - do not the very, "Number of those existing threads" signal that many/most posters are imperfect or poorly motivated "investigators?"   Thus - if the data is, "Already here" - how beneficial would a new, "Code write up prove?"

    Instead - a formal, "Poster Guideline" which crisply directs posters to the top ten or so, "issue areas" appears more suitable.   Poster's who do not follow the, "Guidelines" must, "Fall to the rear of the queue" - so that they (in time) learn to follow direction & become properly resourceful.

    Encouraging little effort or failed methods may not prove best for even, "rushed, frustrated posters" - too busy or too entitled to do their own, "dirty work."

  • /******************************.FUNCTION_HEADER.******************************
    .Purpose : Initializes PC Handler
    .Returns : Error codes
    .Note : None
    ******************************************************************************/
    int8_t PC_Handler_Init(void)
    {

    UART_Params uartParams = {0};
    Task_Params taskParams = {0u};
    Task_Handle hPcTask;
    Error_Block error = {0u};
    int8_t s8_ret_err = PC_SUCCESS;

    UART_Params_init(&uartParams);
    uartParams.baudRate = 115200;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readTimeout = 1000;

    ghUart1 = UART_open(UART_PORT_NUM_PC, &uartParams);
    if( ghUart1 == NULL)
    {
    System_printf("Failed to open UART %d", UART_PORT_NUM_PC);
    s8_ret_err = PORT_ERROR;
    }
    else
    {
    Task_Params_init(&taskParams);
    Error_init(&error);
    taskParams.stackSize = (xdc_SizeT) PC_TASK_STACK_SIZE;
    taskParams.stack = (xdc_Ptr)u8_pc_handler_Stack;
    taskParams.priority = (xdc_Int) PC_TASK_PRIORITY;
    hPcTask = Task_create(&PC_read_thread, &taskParams, &error);
    // hPcTask = Task_create(PC_read_thread, &taskParams, NULL);
    if(NULL == hPcTask)
    {
    System_printf("Error: Failed to create PC Task\n");
    System_flush();
    s8_ret_err = TASK_ERROR;
    }
    else
    {
    System_printf("Success: Created PC Task\n");
    System_flush();
    }
    }
    return s8_ret_err;
    }
  • /******************************.FUNCTION_HEADER.******************************
    .Purpose : PC Task to read messages from PC
    .Returns : Error codes
    .Note : None
    ******************************************************************************/
    void PC_read_thread(UArg arg00, UArg arg01)
    {
    uint8_t u8_pc_cmd[100] = {0};
    uint8_t u8_ack = 1;
    uint16_t u16_bytes_ret, u16_length, u16_bytes_return, u16_bytes_return1, u16_bytes, u16_bytes_read = 0u;

    if( ghUart1 != NULL)
    {
    while(1)
    {
    MASK_SET_BIT(gu32_mask_reg, PC_BIT_POS);
    /* UART receives 2 bytes of data which is the length of the packet*/
    u16_bytes_ret = UART_read(ghUart1, u8_pc_cmd, 2u);
    if(u16_bytes_ret > 0u)
    {
    u16_length = (uint16_t)u8_pc_cmd[0];
    u16_length = u16_length << 8;
    u16_length |= (uint16_t)u8_pc_cmd[1];
    System_printf("u16_length = %d !\n",u16_length);
    System_flush();

    u16_bytes = UART_write(ghUart1, &u8_ack, 1);
    if(u16_bytes > 0)
    {
    System_printf("\nAck sent\r\n");
    System_flush();
    }
    else
    {
    System_printf("\nAck not sentr\n");
    System_flush();
    }

    while(u16_bytes_read <= u16_length)
    {
    u16_bytes_return = UART_read(ghUart1, &u8_pc_cmd[u16_bytes_read], 2);
    System_printf("u16_bytes_return = %d !\n",u16_bytes_return);
    System_flush();
    u16_bytes_read = u16_bytes_read + u16_bytes_return;
    if(u16_bytes_read == u16_length)
    {
    break;
    }
    }


    /* while(u16_length > 4)
    {
    u16_bytes_return = UART_read(ghUart1, u8_pc_cmd, 4);
    System_printf("u16_bytes_return = %d !\n",u16_bytes_return);
    System_flush();
    u16_length = u16_length - 1;//u16_bytes_return;
    }*/


    /* u16_bytes_return1 = UART_read(ghUart1, u8_pc_cmd, u16_length);
    System_printf("u16_bytes_return1 = %d !\n",u16_bytes_return1);
    System_flush();
    */

    if(u16_bytes_return > 0u)
    {
    if(gu8_pc_reg_done)
    {
    pc_get_msg(u8_pc_cmd, sizeof(u8_pc_cmd));
    }
    }
    else
    {
    System_printf("Error reading UART!\n");
    System_flush();
    }

    }
    else
    {
    System_printf("Error reading UART!\n");
    System_flush();
    }
    MASK_CLEAR_BIT(gu32_mask_reg, PC_BIT_POS);
    // Watchdog_clear(ghWatchdog_handle);
    STATUS_SET_BIT(gu32_status_reg, PC_BIT_POS);
    //Task_sleep((xdc_UInt32)10);
    }
    }
    }
  • This is the full code:-

    /*********************************.FILE_HEADER.*******************************
    <Copyright Notice>
    .File : PC_Handler.c
    .Summary : This file handles the communication between the comms micro and PC
    .Note :

    Author Date Description
    ------------------------------------------------------------------------------
    Anjana Rajam 21-10-2015 created

    ******************************************************************************/
    #include "SYSTEM_MANAGER/System_Manager.h"
    #include "PC_Handler.h"

    /*variable declarations*/
    UART_Handle ghUart1;
    uint8_t gu8_pc_reg_done = 0u;

    /*Function pointer declaration*/
    uint8_t (*pc_get_msg)(uint8_t *u8_pc_get_buf, uint32_t u32_size_buf);
    uint8_t u8_pc_handler_Stack[PC_TASK_STACK_SIZE];


    /******************************.FUNCTION_HEADER.******************************
    .Purpose : Initializes PC Handler
    .Returns : Error codes
    .Note : None
    ******************************************************************************/
    int8_t PC_Handler_Init(void)
    {

    UART_Params uartParams = {0};
    Task_Params taskParams = {0u};
    Task_Handle hPcTask;
    Error_Block error = {0u};
    int8_t s8_ret_err = PC_SUCCESS;

    UART_Params_init(&uartParams);
    uartParams.baudRate = 115200;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readTimeout = 1000;

    ghUart1 = UART_open(UART_PORT_NUM_PC, &uartParams);
    if( ghUart1 == NULL)
    {
    System_printf("Failed to open UART %d", UART_PORT_NUM_PC);
    s8_ret_err = PORT_ERROR;
    }
    else
    {
    Task_Params_init(&taskParams);
    Error_init(&error);
    taskParams.stackSize = (xdc_SizeT) PC_TASK_STACK_SIZE;
    taskParams.stack = (xdc_Ptr)u8_pc_handler_Stack;
    taskParams.priority = (xdc_Int) PC_TASK_PRIORITY;
    hPcTask = Task_create(&PC_read_thread, &taskParams, &error);
    // hPcTask = Task_create(PC_read_thread, &taskParams, NULL);
    if(NULL == hPcTask)
    {
    System_printf("Error: Failed to create PC Task\n");
    System_flush();
    s8_ret_err = TASK_ERROR;
    }
    else
    {
    System_printf("Success: Created PC Task\n");
    System_flush();
    }
    }
    return s8_ret_err;
    }

    /******************************.FUNCTION_HEADER.******************************
    .Purpose : PC Task to read messages from PC
    .Returns : Error codes
    .Note : None
    ******************************************************************************/
    void PC_read_thread(UArg arg00, UArg arg01)
    {
    uint8_t u8_pc_cmd[100] = {0};
    uint8_t u8_ack = 1;
    uint16_t u16_bytes_ret, u16_length, u16_bytes_return, u16_bytes_return1, u16_bytes, u16_bytes_read = 0u;

    if( ghUart1 != NULL)
    {
    while(1)
    {
    //MASK_SET_BIT(gu32_mask_reg, PC_BIT_POS);
    /* UART receives 2 bytes of data which is the length of the packet*/
    u16_bytes_ret = UART_read(ghUart1, u8_pc_cmd, 2u);
    if(u16_bytes_ret > 0u)
    {
    u16_length = (uint16_t)u8_pc_cmd[0];
    u16_length = u16_length << 8;
    u16_length |= (uint16_t)u8_pc_cmd[1];
    System_printf("u16_length = %d !\n",u16_length);
    System_flush();

    u16_bytes = UART_write(ghUart1, &u8_ack, 1);
    if(u16_bytes > 0)
    {
    System_printf("\nAck sent\r\n");
    System_flush();
    }
    else
    {
    System_printf("\nAck not sentr\n");
    System_flush();
    }

    while(u16_bytes_read <= u16_length)
    {
    u16_bytes_return = UART_read(ghUart1, &u8_pc_cmd[u16_bytes_read], 2);
    System_printf("u16_bytes_return = %d !\n",u16_bytes_return);
    System_flush();
    u16_bytes_read = u16_bytes_read + u16_bytes_return;
    if(u16_bytes_read == u16_length)
    {
    break;
    }
    }

    if(u16_bytes_return > 0u)
    {
    if(gu8_pc_reg_done)
    {
    pc_get_msg(u8_pc_cmd, sizeof(u8_pc_cmd));
    }
    }
    else
    {
    System_printf("Error reading UART!\n");
    System_flush();
    }

    }
    else
    {
    System_printf("Error reading UART!\n");
    System_flush();
    }
    MASK_CLEAR_BIT(gu32_mask_reg, PC_BIT_POS);
    // Watchdog_clear(ghWatchdog_handle);
    STATUS_SET_BIT(gu32_status_reg, PC_BIT_POS);
    //Task_sleep((xdc_UInt32)10);
    }
    }
    }
    /******************************.FUNCTION_HEADER.******************************
    .Purpose : Registered call back function to callback functions from application
    .Returns : Error codes
    .Note : None
    ******************************************************************************/

    int8_t PC_get_msg_callback(uint8_t(*pc_recv_cmd)(uint8_t *u8_pc_msg_buf, uint32_t u32_buf_size))
    {

    int8_t s8_ret_error = PC_SUCCESS;

    if (pc_recv_cmd == NULL)
    {
    System_printf("Invalid callback function\r\n");
    System_flush();
    s8_ret_error = PC_INVALID_CB;
    }
    else
    {
    pc_get_msg = pc_recv_cmd;
    gu8_pc_reg_done = 1u;
    }

    return s8_ret_error;
    }

    /******************************.FUNCTION_HEADER.******************************
    .Purpose : This function sends messages to PC through debug UART
    .Returns : Error codes
    .Note : None
    ******************************************************************************/
    int8_t PC_send_msg (const uint8_t u8_pc_msg[], uint32_t u32_size_msg )
    {

    int32_t u32_ret;
    int8_t s8_ret_error = PC_SUCCESS;

    if((NULL == u8_pc_msg) || (0u == u32_size_msg))
    {
    System_printf("Message invalid\r\n");
    System_flush();
    s8_ret_error = INAVLID_ARGS;
    }
    else
    {
    u32_ret = UART_write(ghUart1, u8_pc_msg, u32_size_msg);
    if(u32_ret == UART_ERROR)
    {
    s8_ret_error = UART_WRITE_ERROR;
    }

    }

    return s8_ret_error;
    }
  • Hard to read. Pleas use paste code.

    Where's the definition of UART_read?

    Robert
  • Hello Robert,

    I think this is coming from the RTOS and it is the implementation of the same that needs to be checked.

    Hello Anjana,

    Did you check if the UART Status register shows that there are more bytes to read? Also check for the interrupt status in MIS and RIS and the IM bit to see if the interrupt sources are active.

    Regards
    Amit