Forum Discussion

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

altera timer hal overflow question ?

do altera hal timers work correct if the end of the system tick

variable range is reached ? Please check the following and tell me

if I'm wrong.

see files alt_tick.c, alt_alarm_start.c,

/*

* "_alt_nticks" is the number of system clock ticks that have elapsed since

* reset.

*/

volatile alt_u32 _alt_nticks = 0;

--> this will result in an overflow after about 49,71.. days

if the system clock timer is set to 1ms,

which is ok for a pc (normally shut off before 49 days),

but an embedded system will normally run longer.

what happens with the timers when an overflow condition exist ???

e.g. to simplify we use a range of 255 ticks and an periodic timer of

10 ticks.

callbacks are run if (alarm->time <= _alt_nticks) is true

so timer will run

ticks

000 system starts

...

010 (10<=10)-> first callback

(timer is rescheduled with alarmtime=alarmtime+10=20)

...

020 (20<=20)-> second callback

(timer is rescheduled with alarmtime=alarmtime+10=30)

...

...

...

250 (250<=250)-->x&#39; callback

(timer is rescheduled with alarmtime=alarmtime+10

overflow will result in alarmtime=4)

251 (4<=251)-->callback

(timer is rescheduled with alarmtime=alarmtime+10

overflow will result in alarmtime=14)

252 (14<=252)-->callback

(timer is rescheduled with alarmtime=alarmtime+10

overflow will result in alarmtime=24)

253 (24<=253)-->callback

(timer is rescheduled with alarmtime=alarmtime+10

overflow will result in alarmtime=34)

254 (34<=254)-->callback

(timer is rescheduled with alarmtime=alarmtime+10

overflow will result in alarmtime=44)

255 (44<=255)--> callback

(timer is rescheduled with alarmtime=alarmtime+10

overflow will result in alarmtime=54)

000 (54<=000) is false

...

054 (54<=54) -> callback

in the range of (tick_range - alarmtime) callbacks are executed

every tick instead of alarmtime ticks, on alt_tick overflow, timer

will be suspended for (alarmtime*alarmtime ticks).

e.g. a periodic timer with 1000ms and a system timer tick of 1ms

after 49,71 days the following will happen

the timer will run 1000 times every 1ms just before _alt_nticks overruns,

then it will need (1000*1000) ms = 1000sec = 16,66 minutes

until the timer will start again.

is this correct or I&#39;m wrong ???

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi Fischer,

    Which Nios II version are you looking at? I currently have Nios II 5.1, and in Nios II 5.1 there is a roll-over flag -for each alarm- that handles the wraparound of the timer.

    Although it has not the wraparound problem, as a general comment, the alarm implementation used by the HAL could be optimized: now it has an overall O(t*n) complexity, where t is the mean number of ticks of each alarm, and n is the number of alarms... If the alarm ticks are stored in an incremental way (like we do in the OSEK alarms implemented in ERIKA Enterprise), you can save some time when the numbre of alarms grows up...

    bye

    Paolo
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The system and time functions from newlibc don’t work as expected. Due to the limited range of _alt_nticks (32bit) the function clock() returns zero after 49 days.

    Why is _alt_nticks not implemented as 64bit integer?

    Does nios support 64bit integer (long long)?

    Thanks for all information...