Hi all,

I have implemented a sort of circular buffer in C for embedded system the code is something like this

#define BUFFER_LENGTH   25
UINT8_T array[BUFFER_LENGTH];
UINT8_T increment_interrupt;
void interrupt(void)
{
 increment_interrupt++;
 array[increment_interrupt] = data;
 if(increment_interrupt == 25)
    increment_interrupt = 0;
}

UINT8_T g_old_Index_u8=0;
UINT8_T g_new_Index_u8=0;

void main()
{
g_old_Index_u8 = g_new_Index_u8;
g_new_Index_u8 = increment_interrupt;

  if(g_new_Index_u8 >  g_old_Index_u8) 
  {
    fl_origianlcopy_u8 = g_new_Index_u8 - g_old_Index_u8;
    fl_local_counter_u8 =  fl_origianlcopy_u8;
  } 
  else if(g_new_Index_u8 < g_old_Index_u8)
  {
    fl_origianlcopy_u8 =  BUFFER_LENGTH + g_new_Index_u8 -  g_old_Index_u8;      
    fl_local_counter_u8 = fl_origianlcopy_u8;
  } 
  else 
  {

  }

  while(fl_local_counter_u8)
  {
      fl_sum_index = (g_old_Index_u8 + fl_index_three_u8) % (25);
      fl_local_data_uA[fl_index_three_u8]=array[fl_sum_index];
      fl_local_counter_u8--;
      fl_index_three_u8++;
  }

  if(fl_origianlcopy_u8 > 0)
  {
    /* Do some procesing*/
  }
}

The interrupt function and the main function are parallel processing. I could get the originalcopy_u8 correct for some test cases. But for some test cases it is failing. Please someone help me to correct the mistake. I have not exactly copy pasted the code here but the logic.

But for some test cases it is failing

What are the failing test cases? That's important information.

A few things I noticed on a quick inspection:

BUFFER_LENGTH is defined at the top, but you appear to have missed a few places where you should be using it instead of a literal 25.

And in this section:

void interrupt(void)
{
 increment_interrupt++;
 array[increment_interrupt] = data;
 if(increment_interrupt == 25)
    increment_interrupt = 0;
}

What happens when increment_interrupt is 24? Take a closer look at where you're incrementing that variable.

Thankyou for the mail. I corrected the mistakes. I could not replace 25 with the macro as my compiler was giving errors, may be i will try it again. Regarding the test cases where it is failing i am not sure, i was trying to read the number of messages through the interrupt and the main routine starts processing these received messages and transmit to other channel. If the received messages are say 10 in interrupts the main function transmits only some 4 and stops transmitting then i was doubting the above formula, but actual reason i could not trace out as the communication is stopping in between and i cannot put any breakpoint. Any suggestions on how to debug such issues.

I corrected the mistakes.

Post corrected code please?

I could not replace 25 with the macro as my compiler was giving errors, may be i will try it again

That is strange. What are the errors? This should work; you've got it working here:

UINT8_T array[BUFFER_LENGTH];

If the received messages are say 10 in interrupts the main function transmits only some 4 and stops transmitting

What's the "embedded system" that this runs in? Do you have a virtual device to test with?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.