problem with large buffers in ram

Hi,
MCU: LPC2138
I am writing a code that takes data from serial port (interrupt driven) and stores it in queues.
Here's an overview:

//////////////
struct queue{
   char *buffer;
   int wp;
   in rp;
 };

char buffer1[6000];
char buffer2[1000];
char buffer3[600];
char uartBuffer[100];
struct queue myQueue;

void initQueue(char* bufferQueue, int size) {
    myQueue.buffer = bufferQueue;
    myQueue.wp = 0;
    myQueue.rp = 0;
}

void enqueue(char *data) {
  q = &myQueues;
  while(data[i] != '\0') {
        q->buffer[q->writePointer++] = data[i++];
        if (q->writePointer >= q->size)
                q->writePointer = 0;
        }
        q->buffer[q->writePointer++] = 0;
        if (q->writePointer >= q->size)
                q->writePointer = 0;
}

int main () {
  char str[50];
  UART0_init(9600);
  UART0_interrupt_init();
  initQueue(buffer3, 600);

  while(1) {
     sprintf(str, "wp: %d    rp: %d", myQueues.wp, myQueues.rp);
     UART0_sendstring(str);
  }
}

void UART0Isr() {
     UARTbuffer[i] = UART0_getbyte(); // get data until sequence completed
     if(/*sequence complete condition*/)
           enqueue(UARTbuffer);
}

Now, the problem is that the controller hangs after "enqueuing" 8 sequences(all sequences are same). and the number 8 decreases if i increase the size of sequence.

if i reduce the buffer3 size to 500 or less, code works perfectly fine.
for buffer3 size >= 500 ----> code hangs

if i interchange the declaration sequence of global variables like:

char buffer3[600];
char buffer1[6000];
char buffer2[1000];

it gets ok. Atleast for 1000 sequences.

if i disable all interrupts and use enqueue in main in a while loop, its runs fine.

void main() {
while(1) {
  enqueue(sequence);    //works fine here
}
}

Thanks for reading and advance thanks for helpful replies.

Regards,
Salman.

Parents
  • It hurts to see your code.

    Lots of use of variables we can't see the data type of. Are they global?

    Your serial ISR is using an array to store the temporary byte received before enqueue - why?

    enqueue() takes a pointer to a char as parameter. How would it know how many characters there are in the array? Does your UART ISR even pick up multiple characters - you using receive FIFO?

    Where do you reset your variable "i"?

    Why does your enqueue also insert a zero? You don't expect to be able to print the queue buffer directly as a C string? Remember that it's a ring buffer...

    How can you have a queue with a write pointer named "wp" but code that plays with a write pointer named "writePointer"?

    For some reason, I can not for the life of me understand the meaning of posting random typed text and ask for help. What was wrong with posting the real code? That the real code could possibly make sense? What you typed is just nonsense since it does not compute. It does not compile. That makes it basically just a sequence of more or less random characters that we can't relate to.

Reply
  • It hurts to see your code.

    Lots of use of variables we can't see the data type of. Are they global?

    Your serial ISR is using an array to store the temporary byte received before enqueue - why?

    enqueue() takes a pointer to a char as parameter. How would it know how many characters there are in the array? Does your UART ISR even pick up multiple characters - you using receive FIFO?

    Where do you reset your variable "i"?

    Why does your enqueue also insert a zero? You don't expect to be able to print the queue buffer directly as a C string? Remember that it's a ring buffer...

    How can you have a queue with a write pointer named "wp" but code that plays with a write pointer named "writePointer"?

    For some reason, I can not for the life of me understand the meaning of posting random typed text and ask for help. What was wrong with posting the real code? That the real code could possibly make sense? What you typed is just nonsense since it does not compute. It does not compile. That makes it basically just a sequence of more or less random characters that we can't relate to.

Children
More questions in this forum