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.

RTOS/CC1310: Some details of the UART

Part Number: CC1310

Tool/software: TI-RTOS

Hi Team,

I am using the UART driver, but I have not figured out a lot of details.

1.The manual says that the UART has a double buffer interface, one is a 32-byte hardware buffer and the other is a ringbuffer buffer, right?

2.Hardware buffering is not enabled by default and it has only one byte depth, right?

3.The UART contains a Receive time-out interrupt. Is it equivalent to an idle interrupt? Can it be generated if the FIFO is not enabled? Can it be used to distinguish packets?

4.Can the UART driver count the received data length and provide it to the user?

5.I see in other posts that UART drivers can't use DMA?

  • Hi X,

    1) What manual exactly are you referring to here? The UART hardware has 32 packets depth TX/RX FIFOs while the UART driver has an additional RX ringbuffer.

    2) The UART drivers uses the FIFO per default, it is not turned of. Turning of the FIFO equals to having a depth of one. You can configure the watermark levels for the FIFO in the board file.

    3) The Receive time.outwill only trigger if the lines have been idle for a 32-bit period WHILE there is data in the input FIFO. This is the reason the UART driver never reads out all the data from the FIFO if it expects to receive more. It should be generated even in the case where FIFOs is disabled as long as there is unprocessed data (which might not be convenient in the case where you don't use the FIFO).

    4) The UART drivers will return to the user with the amount of data requested when calling UART_read(). If "partial return" is enabled, it will return with an arbitrary amount of data between 1-N_REQUESTED if there is an reception time-out.

    5) The UART driver do not support DMA today. The UART hardware however does.
  • Hi M-W,
    I am most concerned about the use of ringbuffer. If it is received by interrupt: UART_read(uart, &input, 1); The size of the ringbuffer is 32 bytes. When I receive 30 bytes of data at a time and receive 64 bytes of data at a time, the data in both cases is how to store and read in the ringbuffer?
  • In the uartecho example I encountered a strange problem with the following code:

    #define MAX_NUM_RX_BYTES    1000   // Maximum RX bytes to receive in one go
    #define MAX_NUM_TX_BYTES    1000   // Maximum TX bytes to send in one go
    uint32_t wantedRxBytes;            // Number of bytes received so far
    uint8_t rxBuf[MAX_NUM_RX_BYTES];   // Receive buffer
    uint8_t txBuf[MAX_NUM_TX_BYTES];   // Transmit buffer
    // Callback function
    static void readCallback(UART_Handle handle, void *rxBuf, size_t size)
    {
        // Copy bytes from RX buffer to TX buffer
        for(size_t i = 0; i < size; i++)
            txBuf[i] = ((uint8_t*)rxBuf)[i];
        // Echo the bytes received back to transmitter
        UART_write(handle, txBuf, size);
        // Start another read, with size the same as it was during first call to
        // UART_read()
        UART_read(handle, rxBuf, wantedRxBytes);
    }
    
    
    void *mainThread(void *arg0)
    {
      /* Call driver init functions */
        GPIO_init();
        UART_init();
        
        UART_Handle handle;
        UART_Params params;
        // Init UART and specify non-default parameters
        UART_Params_init(&params);
        params.baudRate      = 9600;
        params.writeDataMode = UART_DATA_BINARY;
        params.readMode      = UART_MODE_CALLBACK;
        params.readDataMode  = UART_DATA_BINARY;
        params.readCallback  = readCallback;
        // Open the UART and initiate the first read
        handle = UART_open(Board_UART0, &params);
        //UART_write(handle, "start", 5);
        wantedRxBytes = 1;
        int rxBytes = UART_read(handle, rxBuf, wantedRxBytes);
        while(true); // Wait forever
    }
    
    When params.baudRate = 9600; I send a lot of bytes (such as twenty) at a time, the serial port can't return data and it is stuck.

      When params.baudRate = 115200; I send a two-byte serial port and it will be stuck.

      What is the reason for this? Is there a bug in the UART driver?

  • Your issue is that you are doing blocking calls from inside the callback. As with any TI-RTOS driver, you can not perform any blocking calls from ISR (Hwi, Swi) context. 

    As for the size of the ring buffer, it assumes you are emptying the buffer in an appropriate rate. You are free to size the buffer as you want inside the board file, 32 bytes is just the default. 

  • What do you mean by using the UART_write function in the receive callback to block the interrupt? Unless sent using an interrupt?
  • Callbacks is invoked in ISR context. This means that you can not perform a blocking call (such as doing a semaphore_pend) inside the callback as this will lock the system (you may not block inside ISRs).

    As you have not setup the UART driver to do writes in callback mode, the UART_write will be blocking (pending on a semaphore) and lock your application.
  • I used to think that when UART_write is executed in the ISR, it will automatically exit. Can I solve this problem if I use the writetimeout parameter? But the efficiency will lower than the interruption method?
  • It will not. It is not an problem/issue, it is the expected behavior in most RTOS situations. I would recommend you to simply move to using callback mode for both write and read.
  • Following your advice, I changed both read and write to callback mode. But there is still a problem, the returned data is much less than the received data and it is confusing. I give my code, you can test it.

    0310.uartecho.c
    /*
     * Copyright (c) 2015-2017, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *  ======== uartecho.c ========
     */
    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    #define MAX_NUM_RX_BYTES    1000   // Maximum RX bytes to receive in one go
    #define MAX_NUM_TX_BYTES    1000   // Maximum TX bytes to send in one go
    uint32_t wantedRxBytes;            // Number of bytes received so far
    uint8_t rxBuf[MAX_NUM_RX_BYTES];   // Receive buffer
    uint8_t txBuf[MAX_NUM_TX_BYTES];   // Transmit buffer
    
    // Callback function
    void Uart_ReadCallback(UART_Handle handle, void *rxBuf, size_t size)
    {  
       for(size_t i = 0; i < size; i++)
       txBuf[i] = ((uint8_t*)rxBuf)[i];
        // Echo the bytes received back to transmitter
       UART_write(handle, txBuf, size);
       UART_read(handle, rxBuf, 1);
    }
    
    void Uart_WriteCallback(UART_Handle handle, void *txBuf, size_t size)
    {
       // Start another read, with size the same as it was during first call to UART_read()
       UART_read(handle, rxBuf, 1);
    }
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        //char        input;
        const char  echoPrompt[] = "Echoing characters:\r\n";
        UART_Handle uart;
        UART_Params uartParams;
    
        /* Call driver init functions */
        GPIO_init();
        UART_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Turn on user LED */
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
    
        /* Create a UART with data processing off. */
        UART_Params_init(&uartParams);
        uartParams.readMode = UART_MODE_CALLBACK;
        uartParams.readCallback = Uart_ReadCallback;
        uartParams.writeCallback = Uart_WriteCallback;
        uartParams.writeMode = UART_MODE_CALLBACK;
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_FULL;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.baudRate = 9600;
    
        uart = UART_open(Board_UART0, &uartParams);
    
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
        UART_read(uart, rxBuf, 1);
        //UART_write(uart, echoPrompt, sizeof(echoPrompt));
    
        /* Loop forever echoing */
        while (1) {
    //        UART_read(uart, &input, 1);
    //        UART_write(uart, &input, 1);
        }
    }
    

  • Hi X,

    I'm not sure what you mean by less then expected. How much are you expecting? You are reading data out 1 byte at a time so I would expect you to get exactly one byte, no more no less.

    You also seem to be doing multiple read calls from different locations. For example, your read callback performs a read and the write callback performs a read, there is no reason for the later as a write always implies a read anyway in your case.
  • Hi M-W,
    My program is the echo function. You can use the serial port tool to send 20 bytes at a time, and then check if the data is returned as it is.My test result is that returning will lose a lot of data.Please give it a try.
  • In addition, I am using GPTimer to do the T35 receive timeout function (refer to Modbus serial frame split), but it seems that there is no API to clear the timer, which is very inconvenient. How to clear timer A in UART callback?
  • Hi X,

    Could you share what you did with the GPTimer, I'm not sure I fully understand the purpose of this, UART_read do feature a receive timeout that you can use if you operate it in blocking mode.
  • Task()

      GPTimerCC26XX_Value rxTimeoutVal;
      GPTimerCC26XX_Params tparams;
      tparams.width = GPT_CONFIG_32BIT;
      tparams.mode = GPT_MODE_ONESHOT_UP;
      tparams.debugStallMode = GPTimerCC26XX_DEBUG_STALL_OFF;
      hTimer = GPTimerCC26XX_open(Board_GPTIMER0A, &tparams);
      if(hTimer == NULL)
      {
       while(1);
      }

      GPTimerCC26XX_setLoadValue(hTimer, 167999); //3.5ms
      /* Register the GPTimer interrupt */
      GPTimerCC26XX_registerInterrupt(hTimer, rxTimeoutCb, GPT_INT_TIMEOUT);

    }

    void Uart_ReadCallback(UART_Handle handle, void *rxBuf, size_t size)

    {

       If it is the first byte

       GPTimerCC26XX_start(hTimer);

      else  Timer clear

       HWREG(hTimer->hwAttrs->baseAddr  + GPT_O_TAV) = 0;

    void rxTimeoutCb(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask)

    {

       GPTimerCC26XX_stop(hTimer);

    }

    This is my code logic. My goal is to time out if the serial port does not receive data within 3.5ms.

  • Hi X,

    Assuming you get the timer interrupt, I would maybe structure it something like this (pseudo code):

    task {
    
      // Setup Timer
    
      startTimer()
      UART_read()
    }
    
    readCb() {
    
      // if another read
      re-start_timer();
      UART_read()
    }
    
    timerCb() {
      stopTimer()
      UART_readCancel()
    }

    As you noted earlier, there is no API to clear/reset the timer, it is a bit inconvenient I admit.

  • Hi

    Can i peek the number of bytes available in uart fifo...without having the need to call uart read???..this capability i wish if available will solve a problem i have been facing for months now...so much so that i am willing to interface an external UART with my CC1310 if i have to...i just want to peek my buffer and decide on my own if i want to read those available bytes or not...this capability is very common in contemporay systems...unfortunately i have not been able to get it done on CC1310...please help

    Best Regards

    Ali

  • Hi Ali,

    I would advice you to use the "Ask related question" button to ask related questions in the future as this ensures that we are able to pick up and answer your questions (adding on to existing threads is a lot harder to track).

    As for peaking into the FIFO, this is no way to peak into the FIFO or get the "buffered" number of bytes from the driver today.