UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Visitor heaooo
Visitor
8,771 Views
Registered: ‎12-26-2009

Who knows how to delay several milliseconds in MicroBlaze ?

Jump to solution

I need  the executing program in MicroBlaze to  delay  several milliseconds (say 2 ms).  I use a C language while loop to implement it.

the C codes is like this:

counter=0;

while(counter<1000)

{ ++counter;   }

 

However, it's difficult to calculate the accurate time it postponed. The obstacles are

1) How  the C source codes  are complied to  machine instructions  is depended on compliers.

    Different compliers may produce  different  results. 

2) For most instructions, it takes  a single  machine clock  to finish in a pipeline architecture, but some instructions, such     as memory access,  need  multiple machine clock to complete.

3)Specially, branch instructions will cause the pipeline to flush  instructions in fetch stage and decode stage, thus   increasing the latency.

 

If I want to delay an accurate time by means of  a while loop, rather than by a combination of timer and interrupt, is there a good way to achieve the goal, and does the Xinlinx EDK provide a utility  to evaluate the execution time of   a segment of C codes ?

 

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Contributor
Contributor
11,948 Views
Registered: ‎06-12-2009

Re: Who knows how to delay several milliseconds in MicroBlaze ?

Jump to solution

I just looked for sleep/wait/delay functions in Xilinx libs like libxil but did not see anything helpful.

 

What I have done for these kinds of delays is to use the XPS Timer/Counter core and poll the TCR register until it has incremented by the desired amount.  If the CPU does not need to do anything except wait, this works, and avoids using interrupts.  

 

Another approach would be to make a delay loop using inline assembly.  This would be predictable and should not depend on optimization, altho would probably be affected by caching.  For example:

 

   asm("LOOP1:");

   asm("addi r20, r20, -1");     // 1 clock

   asm("bnei r20, LOOP1");   // 1 clock if branch not taken, 3 clocks if taken

 

Jim

 

 

3 Replies
Highlighted
Contributor
Contributor
11,949 Views
Registered: ‎06-12-2009

Re: Who knows how to delay several milliseconds in MicroBlaze ?

Jump to solution

I just looked for sleep/wait/delay functions in Xilinx libs like libxil but did not see anything helpful.

 

What I have done for these kinds of delays is to use the XPS Timer/Counter core and poll the TCR register until it has incremented by the desired amount.  If the CPU does not need to do anything except wait, this works, and avoids using interrupts.  

 

Another approach would be to make a delay loop using inline assembly.  This would be predictable and should not depend on optimization, altho would probably be affected by caching.  For example:

 

   asm("LOOP1:");

   asm("addi r20, r20, -1");     // 1 clock

   asm("bnei r20, LOOP1");   // 1 clock if branch not taken, 3 clocks if taken

 

Jim

 

 

Visitor heaooo
Visitor
8,734 Views
Registered: ‎12-26-2009

Re: Who knows how to delay several milliseconds in MicroBlaze ?

Jump to solution

Jim, thanks , I  appreciate  your reply!

There seem  not to  be  a perfect solution to the problem , unless  resorting to  Timer.

0 Kudos
Historian
Historian
8,729 Views
Registered: ‎02-25-2008

Re: Who knows how to delay several milliseconds in MicroBlaze ?

Jump to solution
You really need to use a timer if you want any sort of accuracy.
----------------------------Yes, I do this for a living.
0 Kudos