hr timers -- high resolution timed interrupts in the picomod

  • I am writing a device driver to monitor mains inputs (50 or 60Hz a.c) . Ideally I need
    an interrupt every 2mS.


    The Clock tick on the standard timer wheel in picomod is 200HZ or 5mS.


    This is just a bit too slow.


    I have been trying to use hrtimers, a relatively new feature in the kernel.


    I have a callback function which works when returning HR_NORESTART for the first
    call. IF I return HR_RESTART from the callback, the linux system simply hangs.


    I placed a guard variable on this and allowed it to return HR_RESTART a few times,
    and then to send back HR_NORESTART.
    It did this, but the first interrupt took the designated time.
    The next ones all happened very very quickly.


    Obviously the linux lock up was due to the fact that the kernel was in a cyclic interrupt loop.


    Does anyone know how to retrigger the hrtimers while in the call back function ?
    Do they need to be re-triggered ?

  • OK I found this


    <!-- m --><a class="postlink" href="http://lwn.net/Articles/167897/">http://lwn.net/Articles/167897/</a><!-- m -->


    and it has a method for re-triggering, but the API has changed.


    From LINUX web Site above:

    In the restart case, the callback must set a new expiration time before returning. Usually, restarting timers are used by kernel subsystems which need a callback at a regular interval. The hrtimer code provides a function for advancing the expiration time to the next such interval:


    unsigned long hrtimer_forward(struct hrtimer *timer, ktime_t interval);


    This function will advance the timer's expiration time by the given interval. If necessary, the interval will be added more than once to yield an expiration time in the future. Generally, the need to add the interval more than once means that the system has overrun its timer period, perhaps as a result of high system load. The return value from hrtimer_forward() is the number of missed intervals, allowing code which cares to detect and respond to the situation.


    In pico/linux-2.6.28.6-picomod6/include/linux/hrtimer.h
    the function is now:



    /* Forward a hrtimer so it expires after now: */
    extern u64
    hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);


    I can call hrtimer_forward (without getting a crash) but do not know what to put in the `now' structure.

  • OK got it !


    This re-triggers the hrtimer !