Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Valued Contributor III
1,127 Views

Nios II delay calculation

hi i am a beginner in nios ii. i just wanted to know how to create a delay of 5microseconds using c code for nios ii processor. i intend to assert particular values on PIO for specific time intervals which would be captured by other FPGAs. Please if anybody could help, i would be grateful.

0 Kudos
2 Replies
Highlighted
Valued Contributor III
17 Views

I do something like this: 

 

// ****************************************************************************** // This is to allow very short delays for things like bit-banged timing // Assembler code is used to allow shorter loops // and to bypass compiler optimizations changing the timing of C code // This should generally not be called directly. A macro is included with fractional microsecond resolution void _short_delay(volatile int count) { // 'asm' notes: // Lines end with ';' // Embedded 'C' comments allowed (like /* comment here */, no double slashes) // Labels are the first non-white space, followed by a ':' // The compiler takes care of registers used when returning to standard C code asm(" ; ldw r2,0(sp) ; movi r3,-1 ; loop: ; add r2,r2,r3 ; bne r2,r3,loop ; "); } // a macro is defined in the header file for translating // delay based on count to one based on fractional micro-seconds i.e. //# define INSTRUCTIONS_PER_LOOP (3) //# define _uS(t) _short_delay((int)((t-0.3)*(ALT_CPU_FREQ/1000000/INSTRUCTIONS_PER_LOOP))  

 

Plain C can get optimized differently at different times. Using inline asm gets by this. The macro takes values from the BSP and adjusts for different clock frequencies. 

 

There is a partial adjustment for the call/return times, but the latency of SRAM is not included which can increase the loop time. Cache memory can lower this, but it often still affects the first loop until the instructions are in the cache. If you need to be exact, put this code it in closely coupled memory.
0 Kudos
Highlighted
Valued Contributor III
17 Views

thanks fow this answer

0 Kudos