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;
}

0