Xilinx's microblaze hangs when a timer interrupt occurs after a "rand()" instruction.

Feb 27, 2008 6 Replies

Hi,



I have implemented a Microblaze v7.00.b with EDK 9.2 on a Virtex 4 fx12. A timer (xps_timer v1.00.a) generates an interrupt which is relayed by an interrupt controller (xps_intc v1.00.a) to the interrupt pin of the Microblaze. The code uses the standalone library.



The piece of code that handles this interrupt only increment a variable and display it using "xil_printf" instruction.



If i put an infinite loop "while(1)", at the end of the initialisation of the timer and the interrupt controller, the interrupt is normaly handled each time it occurs unless there is a rand() or srand() instruction in the code just before the "while(1)". When a rand() or srand() instruction is used, the interrupt that occurs just after is not handled, the microblaze is then frozen and a reset does not allow to re-run the programm as if it has been corrupted.


The code of the handler:



void TimerCounterHandler(void *CallBackRef, Xuint8 TmrCtrNumber) { Counter +=3D 1; xil_printf("\r\ntime: %d s",Counter); }


The setup of the timer and the interrupt controller:



XStatus Status;



//Initialize the timer counter Status =3D XTmrCtr_Initialize(&my_timer, TIMER_DEVICE_ID);



//Initialize the interrupt controller driver Status =3D XIntc_Initialize(&my_int_controller, INTC_DEVICE_ID);



//Connect the interrupt controller to the timer Status =3D XIntc_Connect( &my_int_controller, TIMER_INTERRUPT_ID, (XInterruptHandler)XTmrCtr_InterruptHandler, (void *)&my_timer);



//Start the interrupt controller in real mode Status =3D XIntc_Start(&my_int_controller, XIN_REAL_MODE);



//Enable the interrupt for the timer XIntc_Enable(&my_int_controller, INTC_DEVICE_ID);



//Enable the interrupts on the Microblaze microblaze_enable_interrupts();



//Set the handler to call when the timer is expired XTmrCtr_SetHandler( &my_timer, TimerCounterHandler, &my_timer);



//set the timer to autoreload mode and decreasing mode (increase is the default mode) XTmrCtr_SetOptions( &my_timer, TIMER_NBR_0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION );



XTmrCtr_SetResetValue( &my_timer, TIMER_NBR_0, RESET_VALUE);



//Start the timer counter XTmrCtr_Start( &my_timer,TIMER_NBR_0);


/**********************************



*
*
this one works
***********************************/ //int i; //i =3D rand()%1000; while(1) { // the interrupt occurs and is handled.


}



/**********************************



*
*
this one does not work
***********************************/ int i; i =3D rand()%1000; while(1) { // freeze of the microblaze when the interrupt occurs.
}

Regards,



R=E9my


Does Answer Record #29784

formatting link

29784.htm) sound like the problem you're having?

If so, looks like updating EDK to Service Pack 2 should solve the problem.

-Jake

The Service Pack 2 is already installed and i use the Microblaze

7.00.b

R=E9my

Hi,

Could you try the latest patch for MicroBlaze

formatting link
? It includes fixes which SP2 don't have.

Göran Bilski

7.00.b

Rémy

Hi,

I just try the patch

formatting link
and the bug steal occurs if I have a "rand" or "srand" function in my code. When i stop the CPU under XMD it returns me the same adress:

0x820036a4

if i do "mb-objdump -x -D -S -t executable.elf > dump.out" to output a dump file of my *.elf to see what there is at this address:

=2Esection .text =2Ealign 2 =2Eent _hw_exception_handler _hw_exception_handler: bri 0;

820036a4: b8000000 bri 0 // 820036a4

so apparently the code is crashed because of an exeption like the problem in "Answer Record #29784

formatting link
answers/

29784.htm". Although i work on the last version of tools with the SP2 and the last patch....

I have tried my code on a PowerPC architecture with the same IP on the same Hardware and I don't have the bug.

R=E9my

Hi Rémy,

Can you provide me with the MicroBlaze configuration settings (or the whole .mhs file)? It looks like you get a hardware exception but you don't have a exception handler. What hardware exceptions have you enabled on MicroBlaze?

Göran

I just try the patch

formatting link
and the bug steal occurs if I have a "rand" or "srand" function in my code. When i stop the CPU under XMD it returns me the same adress:

0x820036a4

if i do "mb-objdump -x -D -S -t executable.elf > dump.out" to output a dump file of my *.elf to see what there is at this address:

.section .text .align 2 .ent _hw_exception_handler _hw_exception_handler: bri 0;

820036a4: b8000000 bri 0 // 820036a4

so apparently the code is crashed because of an exeption like the problem in "Answer Record #29784

formatting link
answers/

29784.htm". Although i work on the last version of tools with the SP2 and the last patch....

I have tried my code on a PowerPC architecture with the same IP on the same Hardware and I don't have the bug.

Rémy

Hi,

We think we have found the problem. Newlib(libc) functions which is reentrant uses the heap. Your design didn't have any heap allocated at all so when these function calls malloc it will grab memory which holds the interrupt vector. These addresses will be overwritten and the program gets corrupted.

A very small heap size will fix the issue.

Göran

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required