Skip to main content
wbarkley
Associate III
September 27, 2012
Question

Checking for USART Buffer Overflows

  • September 27, 2012
  • 5 replies
  • 968 views
Posted on September 27, 2012 at 15:27

Should I be checking for buffer overflows?

Do I just clear the bit if the interrupt occurs?

What is the best way to handle buffer overflows?
    This topic has been closed for replies.

    5 replies

    Tesla DeLorean
    Guru
    September 27, 2012
    Posted on September 27, 2012 at 15:41

    You should check for ALL the error status bits, and read the DR to clear them, otherwise the receiver will stop working.

    How you deal with them will depend on your system/protocols. For things like XMODEM-1K you could ignore them and let the higher level protocol detect the transmission failure, and retry. Generally in systems where you want tolerance/robustness, you packetize or otherwise validate data blocks with CRC's or checksums.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    wbarkley
    wbarkleyAuthor
    Associate III
    September 27, 2012
    Posted on September 27, 2012 at 16:52

    You should check for ALL the error status bits

    I assume you mean turning on the USART_IT_ERR and possibly the USART_IT_PE interrupts.  My protocol is simple - transmit buffer size of 3 worth of data, receive buffer size of 3 worth of data.  Specifically, the RxBuffer[0] = 170 unless I ignore data and reset RxCount, else move on the the 2 remaining receives;

    So if I were to get a frame error, noise error, or overflow error, what would I do besides reset the bits that caused the interrupt to happen? Possibly clear out USART buffer when any error occurs, clear certain flags, etc.  I mean there is no way to capture lost (overflowed) data or is there?

    Tesla DeLorean
    Guru
    September 27, 2012
    Posted on September 27, 2012 at 17:05

    I mean

    #define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)

    Ignore the ''interrupt'' reflections of the bits in the actual USART status register.

    Yes, once you're missed the byte(s) they are gone. You could work around that by using error correction methods.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    wbarkley
    wbarkleyAuthor
    Associate III
    September 27, 2012
    Posted on September 27, 2012 at 18:43

    So basically,

    if (USART_FLAG_ERRORS > 0){

         USART_ClearFlag(Off whatever bit triggered error);

         errors++;

         //whatever else is usually done to handle errors

    }

    I guess I am wondering about the //whatever else is usually done to handle errors part besides clearing the bits that triggered the error and keeping track of the number of errors.

    Tesla DeLorean
    Guru
    September 27, 2012
    Posted on September 27, 2012 at 22:34

    I guess I am wondering about the //whatever else is usually done to handle errors part besides clearing the bits that triggered the error and keeping track of the number of errors.

     

    I tend to clear the error (Reading DR), and count the errors, and send my data in a manner that it can resynchronize the stream in the parsing routines. So you might need to add some sync/preamble bytes at the front, and some frame check bytes at the back.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..