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.

TM4C123GH6PM: Flushing UART Transmit Buffer

Part Number: TM4C123GH6PM


Is there a way to clear the UART Transmit buffer before I send any data (to ensure it is indeed empty)? I'm using UART0 with the FIFO disabled.

Regards,

Ksawery

  • With the FIFO disabled you have a transmit shift register and an one byte holding register. Once a character transmission has started (data in the shift register) it will continue to transmit until complete even if the TXE (transmit enable) bit is cleared. That will, however, stop the character in the holding register from transferring to the shift register. 

    You can tell if the holding register is empty by checking the TXFE bit of the UART FLAG register. (If set, the holding register is empty.)

  • If I use UARTCharPutNonBlocking(), would that overwrite any byte stuck in the holding register?

  • No, that function checks to see that the FIFO full flag is not set before writing the character. When FIFO mode is disabled, the FIFO full flag represents the state of the holding register. The source for that function is in the file "C:\ti\TivaWare_C_Series-2.1.4.178\driverlib\uart.c". I have included a copy of the function below.

    This is one of the rare cases where what you want to do is not supported by the TivaWare library. You could write your own function that does the HWREG write without checking the FIFO full flag.

    //*****************************************************************************
    //
    //! Sends a character to the specified port.
    //!
    //! \param ui32Base is the base address of the UART port.
    //! \param ucData is the character to be transmitted.
    //!
    //! This function writes the character \e ucData to the transmit FIFO for the
    //! specified port.  This function does not block, so if there is no space
    //! available, then a \b false is returned and the application must retry the
    //! function later.
    //!
    //! \return Returns \b true if the character was successfully placed in the
    //! transmit FIFO or \b false if there was no space available in the transmit
    //! FIFO.
    //
    //*****************************************************************************
    bool
    UARTCharPutNonBlocking(uint32_t ui32Base, unsigned char ucData)
    {
        //
        // Check the arguments.
        //
        ASSERT(_UARTBaseValid(ui32Base));
    
        //
        // See if there is space in the transmit FIFO.
        //
        if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF))
        {
            //
            // Write this character to the transmit FIFO.
            //
            HWREG(ui32Base + UART_O_DR) = ucData;
    
            //
            // Success.
            //
            return(true);
        }
        else
        {
            //
            // There is no space in the transmit FIFO, so return a failure.
            //
            return(false);
        }
    }
    

  • Ok, so it does the exact opposite of what I expected :) I will need to use UARTCharPut instead.

    Regards,

    Ksawery