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.

How to handle Buffer overrun error in UART



Hi all,
In uart receive if buffer overrun error occured means how do we handle that error? Because the buffer Overrun error bit is always set, so i can't able to proceed further receiving operation. what i have done in my progarm  as follows "if buffer overrun error occurs i will be coming out from the receive operation, also i am not read the UCA0RXBUF also that tim, after that again i am trying to read through uart that time i can't able to read"
while( size > 0 )
    {
        if((UCA0STAT & 0x20))
        {
            retVal = UART_ErrorBufOverRun;
            goto exit;
        }

        if((UCA0STAT & 0x40) )
        {
            retVal = UART_ErrorFraming;
            goto exit;
        }

        if((UCA0STAT & 0x10))
        {
            retVal = UART_ErrorParity;
            goto exit;
        }
            while(!(uartRegister_ptr->intrFlag & 0x01))
            {
                ++timeOutCounter;
                if( timeOutCounter >= UART_READTIMEOUTVALUE )
                {
                    timeOutCounter = 0;
                    retVal = Uart_ErrorTimeOut;
                    goto exit;
                }
            }

        timeOutCounter = 0;
        *txrxbuf_ptr = UCA0RXBUF;
        size--;
        if( size > 0)
        {
            txrxbuf_ptr++;
        }
    }
exit:
    return retVal;
can anyone tell me whether i am doing correctly.

  • A buffer overrun means you didn’t read the last byte before the next arrived. Handling it is simple: be faster. Read the incoming byte in time.
    Since UART communication is asynchronous, you cannot stop the other side from sending. And the UART has no own buffer. If a new byte arrives, the previous is lost unless you did read and store it in time. The overrun bit only tells you that you're too late and lost data. (that's why you have interrupts: to handle events when they are due, not when your main code finds the time to handle them)
  • Thanks Jens,
    But if one time the overrun error handle, still the bit is not at all cleared untill ireset the system, meanwhile i can't able to do the receive untill reset the system. For that reason only i have implemented the code as i posted before. Is that correct way of implementation?
  • Prakash Balagangatharan said:
    still the bit is not at all cleared untill ireset the system,

    Reading the User's Guide description for that register bit, it says: "UCOE is cleared automatically when UCxRXBUF is read, and must not be cleared by software."

    Your code exits and never does another read from RXBUF.

  • Why don’t you use the defined symbols instead of numerical bit values? BIT4 is more telling than 0x10, and UCPE is much more telling than BIT4. :)
    From the users guide: “UCOE is cleared automatically when UCxRXBUF is read, and must not be cleared by software. Otherwise, it will not function correctly.“
    So when byte2 arrived before you have read byte1 form RXBUF, then UCOE is set and RXBUF contains byte2. When you read byte2, UCOE clears. That’s the way it works.
    Don’t forget that the world doesn’t stop turning when you hit a breakpoint in the debugger. Or while the debugger updates the register view. While you are manually stepping over a single instruction, the USCI will perhaps receive hundreds of bytes, which means hundreds of further overflows that instantly set UCOE again.
  • I think you have to check the FIFO overflow bit. It seems that your FIFO getting overflow. Use FIFO you wll get better result.
  • ashutosh bhatt said:
    Use FIFO you wll get better result.

    The MSP430 UARTs don't have a FIFO.

  • Hi
    MSP430 doesn't have FIFO, it only have a 1 byte buffer
  • Yes, that's right - but you can implement a FIFO (ring-buffer) in software, collecting all incoming bytes from the UART in the ISR and handle the data in the main. Processing data in the ISR can be done, but this will only work for slow transmissions and small actions you want to to with your data. Don't start calculating things in an ISR.

    Dennis
  • Indeed, using a ring buffer (if you can afford the RAM) is a good thing. Easiest and fastest is a 256-byte ringbuffer, as the index can be implemented as unsigned char and you don't need to check for the size. But any size will do.
    The tricky thing is that when reading data from FIFO, you need to disable interrupts, so your access to the FIFO pointers won't collide with the ISR's access. This is especially important for the transmit FIFO. Here I use an approach where the ISR clears the TXIE bit when the buffer is empty (leaving TXIFG set), and the putchar function (with interrupts disabled) always sets it when it has something written into the buffer.
    This way, I have four UARTs, each with 115200Bd and full duplex, running at the same time and without any problems.

**Attention** This is a public forum