This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Circular buffer logical error?

I was going through the sample code i obtained from the keil website for LpC2148 USB .As far as i could make out, the code just echos the contents of a buffer onto the hyperterminal when a keypress is detected.

In the PopfromCIrBuf function, i have encountered a small problem. As i understand it, Bufsize is the amt of data to be read in the cir buf and size = size of location we are writing to. Logically , we should write when bufsize<= size but the code seems to be doing the opposite.
However this code runs correctly. upon modifying bufsize>= size to bufsize <= size and executing, it did not work. i would like to understand this anomaly. Thank you for your attention. the code is as follows .

#include <LPC214x.H>              /* LPC21xx definitions */
#include "type.h"
#include "vcom.h"
#include "usbapi.h"

#define USB_TEXT "\n\r Testing USB NGX's BlueBoard \n\r BlueBoard Revision : 1 \n\r Firmware Version: 1 \n\r For more information on BlueBoard visit ">www.ngxtechnologies.com"

/**
*******************************************************************
        Funtion Name : test_usb()

        Description  :

        Input        :

        Output       : None

        Note         :
*****************************************************************
*/
void test_usb(void)
{
  U8 buffer[] = USB_TEXT;

  while((VCOM_write(sizeof(buffer), buffer)) != sizeof(buffer));
}

int main (void)
{
        U8 ch = 0;

        SCS = 3;    //sets port 1 and 0 to fast mode (not compatable with previous LPCs)

        VCOM_init();

        while (1)
        {
            if(get_USB_enumeration_status())
            {
                        if(VCOM_read(sizeof(ch),&ch))
                        {
                                test_usb();
                        }
            }
        }
}


VCOM read is

S32 VCOM_read(U16 len,U8 *buf)
{
  S32 retval=0;


if(get_USB_enumeration_status()) { retval = popFromCirBuf(&in_circular_buf, buf, len);
if( (retval >0) && (retval < len)) { popFromCirBuf(&in_circular_buf, buf, retval); } }
return retval; }

and circlar buffer relevant code is

S32 getCirBufSize(CIR_BUFFER *circular_buf)
{
  U32 retval= 0 ;
  if ( circular_buf != NULL )
  {
    if ( (circular_buf->readPos <= circular_buf->writePos ) && circular_buf->overflow == 0)
    {
      retval = ( circular_buf->writePos ) - circular_buf->readPos;
    }
    else if ( ( circular_buf->writePos == circular_buf->readPos ) && circular_buf->overflow == 1 )
    {
      retval = circular_buf->size ;
    }
    else
    {
      retval = (circular_buf->size - circular_buf->readPos) + circular_buf->writePos ;
    }
  }

  return retval;
}


S32 popFromCirBuf(CIR_BUFFER *circular_buf, U8 *buffer, U32 size)
{
  S32 retval= 0;
  U32 i;
  U32 BufSize;

  if ( circular_buf != NULL )
  {
     BufSize = getCirBufSize (circular_buf);
     if ( BufSize >= size )
     {
        for(i=0;i<size;i++)
        {
          buffer[i] = circular_buf->Buffer [ circular_buf->readPos ];
          if ( circular_buf->readPos+1 == circular_buf->size )
          {
            circular_buf->readPos = 0;
            circular_buf->overflow = 0 ;
          }
          else
          {
            circular_buf->readPos ++ ;
          }
        }
        retval = size ;
    }
    else if(BufSize < size )
    {
      retval = BufSize;
    }
  }
  else
  {
    retval = -1;
  }

  return retval;
}

Parents Reply Children
  • As i understand it, Bufsize is the amt of data to be read in the cir buf

    No - it is the number of bytes stored in the buffer at the time of the call. Your statement refers to the parameter provided to the function that read data from the ring buffer.

  • yes that is logical. And yes i meant amount of data already present in buffer which is available to be popped. i guess i did not express that clearly. I think i get it now . please correct me if i am wrong

    So suppose bufsize = 8 and size =5 then it will write only 5 bytes to the buffer whose length =size and return the value 5?

    and the purpose of this comparison is to ensure that at least 'size' bytes of data is present in the circular buffer before popping