AnsweredAssumed Answered

RingBuffer on S12X

Question asked by Markus Haedrich on May 12, 2009
Latest reply on May 13, 2009 by Markus Haedrich

hello everybody,

 

I'm new to µC programming. my first project includes working with the SCI interface. for this purpose I wanted to use a ringbuffer, to which my SCI-ISR write every received byte.

 

the ISR itself works fine but my ringbuffer seems to be corrupt. I've already slightly rewrote the code so I can test it on the PC where everything goes without errors. 

but when I execute the code on the µC (MC9S12XDT512), there are some pretty strange things going on. here is the code I wrote:

 

 


/* Includes */
#define TRUE  1
#define FALSE 0

/* HEADER HAPTIC */
#define BUFFERSIZE 10


struct ringBuffer
{
unsigned int elementCount;
unsigned int posRead;
unsigned int posWrite;
unsigned char stack[BUFFERSIZE];
};


struct knobDevice
{
struct ringBuffer *buffer;
};

struct knobDevice pr1000_SCI0;
struct ringBuffer pr1000_buffer;



/* FUNCTIONS HAPTIC */
void reset_buffer(struct ringBuffer * buffer){
buffer->posRead=0;
buffer->posWrite=0;
buffer->elementCount=0;
}

unsigned int increment_buffer(unsigned int i)
{
//this is used to "increment" the read&write position of a ringbuffer, when it touches BUFFERSIZE we reset to 0
return (i+1)==BUFFERSIZE ? 0 : i+1;
}

unsigned char get_buffer(struct ringBuffer * buffer)
{
//empty buffer has to be handled before function call!
unsigned int posRead;
posRead=buffer->posRead;
buffer->posRead=increment_buffer(buffer->posRead);
buffer->elementCount--;
return buffer->stack[posRead];
}

void put_buffer(struct ringBuffer * buffer, unsigned char storeThis)
{
//when buffer is full, we overwrite it from the beginning
buffer->stack[buffer->posWrite]=storeThis;
buffer->posWrite=increment_buffer(buffer->posWrite);
if(buffer->elementCount<BUFFERSIZE)
{
  //only increment elementcount if we didn't overwrite existing data
  buffer->elementCount++;
}else
{
  //buffer is overwritten, increment posRead so we don't get the just inserted data when reading next time
  buffer->posRead=increment_buffer(buffer->posRead);
}
}

unsigned char isempty_buffer(struct ringBuffer * buffer)
{
if(buffer->elementCount>0)
{

  return FALSE;
}else{
return TRUE;
}
}






void main(void){
unsigned char i;

pr1000_SCI0.buffer= &pr1000_buffer;
reset_buffer(pr1000_SCI0.buffer);

         while(1){
         for(i=0;i<5;i++){
          put_buffer(pr1000_SCI0.buffer, i);}
         }
}

 

 

 

 I use ICC for compiling and NoICE for debugging.

 

when I run the code and watch the buffer, right after doing the "reset_buffer", the buffer.elementCount=0xFF00 and the buffer's stack is 00,00,00,1F,00,00,00,D0,00,3F

 also I am only able to modify a few of my buffer variables from within NoICE... 

 

what am I doing wrong? I guess there are some memory issues so my variables are stored in a wrong area or something. unfortunately I have no idea how to verify my guess.

 

I'd really appreciate some hints here :smileywink:

 

thanks,

markus

Outcomes