Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use timers in Linux kernel device drivers?

I want to implement a counter in Linux device drivers which increments after every fixed interval of time. I want to do this with the help of timers. A sample code snippet would be very useful.

like image 913
user1395806 Avatar asked May 30 '12 08:05

user1395806


1 Answers

Have a look at following article IBM Developerworks: Timers and Lists

There is a small example of how to use Linux kernel timers (included it here for convenience, comments are from myself, removed printk messages)

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>

MODULE_LICENSE("GPL");

static struct timer_list my_timer;

void my_timer_callback( unsigned long data )
{
     /* do your timer stuff here */
}

int init_module(void)
{
  /* setup your timer to call my_timer_callback */
  setup_timer(&my_timer, my_timer_callback, 0);
  /* setup timer interval to 200 msecs */
  mod_timer(&my_timer, jiffies + msecs_to_jiffies(200));
  return 0;
}

void cleanup_module(void)
{
  /* remove kernel timer when unloading module */
  del_timer(&my_timer);
  return;
}
like image 88
dwalter Avatar answered Oct 24 '22 05:10

dwalter