且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Linux内核设备驱动程序中使用计时器?

更新时间:2022-02-21 22:01:46

看看以下文章有一个关于如何使用Linux内核计时器的小示例(为方便起见,在此处包括了注释,来自我本人,删除了printk消息)

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;
}