Answer the question
In order to leave comments, you need to log in
How to make an infinite time counter in linux driver?
How to make a method call once per second an infinite number of times in a linux driver? You need to call the function once every n milliseconds. The presented example can work out a finite number of times. How to do it indefinitely?
//структуры таймера
static ktime_t tout;
static struct kt_data {
struct hrtimer timer;
ktime_t period;
int numb;
} *data;
static enum hrtimer_restart ktfun( struct hrtimer *var ) {
ktime_t now = var->base->get_time(); // текущее время в типе ktime_t
printk( KERN_INFO "timer run #%d at jiffies=%ld\n", data->numb, jiffies );
hrtimer_forward( var, now, tout );
return data->numb-- > 0 ? HRTIMER_RESTART : HRTIMER_NORESTART;
}
//функция загрузки модуля
static int __init init(void) {
// Регистрируем драйвер
major_number = register_chrdev(0, DEVICE_NAME, &fops);
printk(KERN_ALERT "Hello, I am a pci express device driver! major = %d\n", major_number);
enum hrtimer_mode mode;
mode = HRTIMER_MODE_REL;
tout = ktime_set( 1, 0 ); /* 1 sec. + 0 nsec. */
data = kmalloc( sizeof(*data), GFP_KERNEL );
data->period = tout;
// используем системные часы CLOCK_REALTIME
hrtimer_init( &data->timer, CLOCK_REALTIME, mode );
data->timer.function = ktfun;
data->numb = CLOCK_MONOTONIC;
printk( KERN_INFO "timer start at jiffies=%ld\n", jiffies );
hrtimer_start( &data->timer, data->period, mode );
return 0;
}
//функция выгрузки модуля
static void __exit exit(void) {
//освобождаем выделенную память
hrtimer_cancel( &data->timer );
kfree( data );
// Освобождаем устройство
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "Goodbye!\n");
}
//регистрация функций загрузки и выгрузки модуля
module_init(init);
module_exit(exit);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question