7

Good evening, i'm asking your help, to understand how to use a timer in kernel module to achieve periodic task with period less than a microsecond. I was searching on the web and i've found that exists a Timer (ARM side) that is capable of such timing. Has somebody used it and could give me a little hint?

2 Answers 2

5

Done it! This is the include section

#include <linux/hrtimer.h>
#include <linux/sched.h>

This is the variables block

/****************************************************************************/
/* Timer variables block                                                    */
/****************************************************************************/
static enum hrtimer_restart function_timer(struct hrtimer *);
static struct hrtimer htimer;
static ktime_t kt_periode;

This is the initialization

kt_periode = ktime_set(0, 100); //seconds,nanoseconds
hrtimer_init (& htimer, CLOCK_REALTIME, HRTIMER_MODE_REL);
htimer.function = function_timer;
hrtimer_start(& htimer, kt_periode, HRTIMER_MODE_REL);

This is the procedure to cancel execution of timer

hrtimer_cancel(& htimer);

and this is the callback

static enum hrtimer_restart function_timer(struct hrtimer * unused)
{
        if (gpio_current_state==0){
            gpio_set_value(GPIO_OUTPUT,1);
            gpio_current_state=1;
        }
        else{
            gpio_set_value(GPIO_OUTPUT,0);
            gpio_current_state=0;
        }
        hrtimer_forward_now(& htimer, kt_periode);
        return HRTIMER_RESTART;
}
3

I assume you are looking for timing with greater accuracy than 1 jiffy?

Have you checked out hrtimers?

This section may be of use to you

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.