Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

stack problem (printf?)

I have some problems with a custom board and software.

It seams that the stack get's destroyed, when I use the printf function.

I don't know if printf is really the problem.

Always if i change something, it works or it works not !

I could not produce the error on the cyclone dev. board.

I could reduce my software to a small testprogram, which fills a testbuffer (srcbuff)

in global memory, copies it then onto the stack (dstbuff) , calls printf and

compares afterwards the two buffers.

some system properties:

- quartus 5.0 Nios 5.0

- NiosII/s (2k instruction cache)

- 1k onchip memory (exception address 0x00000020)

- external sram 0x800000-0x87FFFF

- reduced device drivers (jtag uart should use no interrupt)

- no os

the following testprogram works if

- dstbuff is global (not on stack)

or

- printf is used (but interrupts are disabled during call to printf)

or

- printf is replaced by iprintf

or

- printf No.2 is used instead of printf No.1

if the program is like below,

dstbuff is on stack,interrupts are enabled, then i get error e.g.

...

Hello from Nios II!

TESTCOUNT:00000

+++ ERROR dstbuff

first diff index :32764 at 0x00814008 srcbuff:60

first diff index :32764 at 0x0087FFE0 dstbuff:E4

...

# include <stdio.h># include <stdlib.h># include <string.h># include <time.h># include <alt_types.h># include <unistd.h># include "sys/alt_irq.h"# include"system.h"

# define BYTE unsigned char

# define BUFFSIZE 32768

BYTE srcbuff[BUFFSIZE];

// BYTE dstbuff[BUFFSIZE];

int fill_testbuffer(BYTE *buffer,int count);

alt_irq_context irqcontext;

int main()

{

BYTE dstbuff[BUFFSIZE];

int testcount=0;

int i;

printf("Hello from Nios II!\n");

// irqcontext=alt_irq_disable_all();

while(1) {

fill_testbuffer(srcbuff,BUFFSIZE);

memcpy(dstbuff,srcbuff,BUFFSIZE);

// irqcontext=alt_irq_disable_all();

printf("TESTCOUNT:%05d\r\n",testcount++); // PRINTF No.1

// printf("CLOCK:%06d TESTCOUNT:%05d\r\n",(int)clock(),testcount++); // PRINTF No.2

// alt_irq_enable_all(irqcontext);

if(memcmp(srcbuff,dstbuff,BUFFSIZE)) {

for(i=0;i<BUFFSIZE;i++) {

if(srcbuff!=dstbuff) {

printf("+++ ERROR dstbuff\r\n");

printf("first diff index :%d at 0x%08X srcbuff:%02X\r\n",i,(int)&srcbuff,srcbuff);

printf("first diff index :%d at 0x%08X dstbuff:%02X\r\n",i,(int)&dstbuff,dstbuff);

break;

}

}

while(1);

}

}

return 0;

}

int fill_testbuffer(BYTE *buffer,int count) {

int i;

unsigned int seed;

seed=clock();

srand(seed);

for(i=0;i<count;i++) {

buffer[i]=rand();

}

return 0;

}

any ideas ?

thanks
No RepliesBe the first to reply