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.

Best way to read data from UART

Other Parts Discussed in Thread: Z-STACK

Hi TI,

I would like to receive 69 bytes from UART RX pin of my microcontroller. My question is, what's the best way to do it?

This is what I have in mind:

1.) Set the rx fifo level interrupt every UART_FIFO_RX1_8
2.) Read the RX 1/8 of the fifo inside the ISR callback function and save it to global buffer
3.) Set a global flag counter everytime step 2(above) happens
4.) Post a msg event inside the ISR if the global flag counter meets the predefined value = 69
5.) Process the uint8 buffer[69] in the msg event switch case

I'm quite confused on what is the best RX FIFO buffer len to be set during UART Initialization. I've looked into some references and I found out it should be power of 2.

  • Do you use Z-Stack? If yes, you just register a RX callback function and it would get called when there is data comes in.
  • Yes I use Z-Stack. Actually as I'm trying to understand the HAL Uart, there were some weird issues that happened.

    I did an experiment, where in, I disabled MT. I used usb to serial chip that will send 10 bytes to uart backchannel of smartrf06 board.

    I programmed the LCD to output the rx buffer len, # of times ISR callback function was called, & event.

    here's what LCD showed me BEFORE sending 10 bytes: (16 made sense that event = HAL_UART_TX_EMPTY)

    0

    0

    16

    here's what LCD showed me AFTER sending single 10 bytes: (6 here should be 10 right??)

    6

    1

    4

    My uart initialization:

    #define LEN_TOTAL_METER_BYTES 69
    
    static void v_SetupUART1( void );
    
    static void v_SetupUART1( void ) {
      halUARTCfg_t uartConfig;
      
      //Configure UART parameters
      uartConfig.configured = true;
      uartConfig.baudRate = HAL_UART_BR_19200;
      uartConfig.flowControl = HAL_UART_FLOW_OFF;
      uartConfig.flowControlThreshold = 0; //Set this to 0 for now
      uartConfig.rx.maxBufSize = LEN_TOTAL_METER_BYTES + 1;   //+1 to get the last byte correctly???
      uartConfig.tx.maxBufSize = 16;        //We don't need to transmit for now
      uartConfig.idleTimeout = 1;
      uartConfig.intEnable = true;
      uartConfig.callBackFunc = (halUARTCBack_t)v_uartCallback;
      
      //Open the port
      if (HalUARTOpen(HAL_UART_PORT_1, &uartConfig) == HAL_UART_SUCCESS) {
        HalLcdWriteString("Init OK", HAL_LCD_LINE_5);
      }
    
    }

  • UART RX callback notifies you there is UART message comes in. There is no guarantee that 10 bytes would come in at one time. You have to use a buffer to collect incoming data and parse it.
  • So how do I do it if it doesn't guarantee the # of bytes I need?
  • You have to use a RX buffer to collect incoming message until it gets enough number of bytes you need. Then, you start to process it.