Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: Circular buffers

2004-03-04 by embeddedjanitor

--- In lpc2000@yahoogroups.com, "Curt Powell" <curt.powell@s...> 
wrote:
> Can someone point me to some circular buffer routines?
There are various ways to implement circular buffers. The easiest 
way, and most robust way, IMHO is to use a counter to track how many 
items are in the list rather than comparing head and tail.

example
typedef struct{
  U8 fifo[uart_FIFO_SIZE];
  U32 head;
  U32 tail;
  U32 holding;
} uart_Buffer;
      // put a byte into the list
      if(s->rx.holding >= uart_FIFO_SIZE)
      {
        //Full, so got to throw away the oldest byte
        s->rx.holding = uart_FIFO_SIZE - 1;
        s->rx.tail++;
        if(s->rx.tail >= uart_FIFO_SIZE)
        {
          s->rx.tail = 0;
        }
      }
      s->rx.fifo[s->rx.head] = b;
      s->rx.head++;
      if(s->rx.head >= uart_FIFO_SIZE)
      {
        s->rx.head = 0;
      }
      s->rx.holding++;


  // pull a byte out of the list
  if(s->rx.holding > 0)
  {
    *pData = s->rx.fifo[s->rx.tail];
    s->rx.tail++;
    if(s->rx.tail  >= uart_FIFO_SIZE)
    {
      s->rx.tail = 0;
    }
    s->rx.holding--;
    retval = 1;
  }

  // initialise
  s->rx.tail = s->rx.head = s->rx.holding = 0;

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.