Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Nios II Timer

Altera_Forum
Honored Contributor II
2,352 Views

I have the need for a 10 mS periodic timer interrupt for my system. I used the timer in the SOPC builder and selected a time base of 10 mS. To keep the size low, I opted for the simple periodic timer. After loading the new config file and writing a simple program to toggle an LED in the timer IRQ, every thing went well. I used the alarm function. My first attempt was to pass the alarm start function nticks per second so I can see the LED blink. Worked perfect! However, I tried to increase the speed, but there was no change in the IRQ frequency http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/huh.gif . No matter what I set the ntick parameter to, I always got a 1 second IRQ. Do I need to opt for the fully functional timer in the SOPC? Or is there something that I am doing wrong? Please advise... 

 

Rick
0 Kudos
11 Replies
Altera_Forum
Honored Contributor II
685 Views

You will need to enable the "writeable period" checkbox in the timer properties sheet. The simple periodic timer does not have this enabled, so you can&#39;t change the period. Secting the "full featured" version would do this by default. 

 

Steve.
Altera_Forum
Honored Contributor II
685 Views

Thanks for the tip! Just wondering why the IRQ is 1 second when I set the simple timer it to 10mS.

Altera_Forum
Honored Contributor II
685 Views

Hi rppolicy. 

 

Here are my thoughts... 

 

When you call the alarm_start function, the first call to alarm_callback will go occur in nticks. After the alarm_callback function gets called, the return value of the alarm_callback funtion becomes nticks.  

 

If you started with a sample timer, the alarm_callback function will look something like this: 

 

alt_u32 my_alarm_callback (void* context) {   /*   * This return value is used to determine when the call back function will   * be called again...  In this case, once every second   */  return alt_ticks_per_second();   } 

 

where the return value (100) is the number of ticks until 1 second expires, which then calls your alarm_callback funtion again (once every second). 

 

I believe that if you return 1, you will get an interrupt in "one tick" which should be your 10 ms. 

 

Hopefully this helps. 

 

tjd
Altera_Forum
Honored Contributor II
685 Views

Hello, 

 

In most of the example designs, included with Nios II, a 1ms timer is included for clock ticks. If you look at how this is implemented (hardware in SOPC Builder, software in the ./components/altera_avalon_timer). Focus on the ./components/altera_avalon_timer/altera_avalon_timer_sc.c file. 

 

This timer generates an interrupt every 1ms. At first glance, it looks like it&#39;s doing exactly what you want, only at 1ms intervals. 

 

Regards, 

 

- slacker
Altera_Forum
Honored Contributor II
685 Views

Thanks again for the help. Didn&#39;t realize the return value of the callback setup the next alarm. Got it working perfectly! 

 

Rick
Altera_Forum
Honored Contributor II
685 Views

hello, I have one question over here. 

 

Did the callback function called exactly after 10ms after the call of alt_start_alarm. 

 

I have faced a delay in calling the callback for the first time, after that it worked well. Do you have any idea about this ? 

 

configured delay is 4 sec. first time callback called after 5 sec, after that callback called in every 4 sec. what could be the problem ?
Altera_Forum
Honored Contributor II
685 Views

Do you use a cash for the Data-Master inside the NIOS?

Altera_Forum
Honored Contributor II
685 Views

No. we are not using cash for data master in nios. 

we are using nios II/e version
Altera_Forum
Honored Contributor II
685 Views

Personally, for an embedded system, I'd not use a timer interrupt but just read a 'time counter' register at appropriate times. 

It is likely that the timer interrupt just sets a flag that the main processing loop checks - so it is just as easy for the processing loop to check the time itself. 

Getting rid of the interrupt removes all the ISR entry/exit costs and all the places where the interrupt would have to be disabled.
Altera_Forum
Honored Contributor II
685 Views

but the problem what I faced is the delay is not accurate for the first time, after that it works fine. 

 

alt_start_alarm(for 4 sec) ...............5secs................ callback called .........4sec.........callback called.............4sec...........callback called 

 

why it take 5 sec for the first time. Is it because the function alt_start_alarm take 1 sec extra
Altera_Forum
Honored Contributor II
685 Views

That could just be because it ensures the interval is at least as long as the one requested. 

It is not uncommon for timers (especially those started in response to a 'tick') to run for one more 'tick' than you might expect - because otherwise the delay will be shorter than the requested interval. 

Having said that, I don't know the specifics of this code.
Reply