Skip to content

Instantly share code, notes, and snippets.

@ajgappmark
Forked from itrobotics/my_timer.c
Created February 12, 2023 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajgappmark/0f3ab0a5043d92a130dbfaa9cbff9217 to your computer and use it in GitHub Desktop.
Save ajgappmark/0f3ab0a5043d92a130dbfaa9cbff9217 to your computer and use it in GitHub Desktop.
a simple timer example of Linux module
/*******************************************************************************
* Copyright (c) 2015 Song Yang @ ittraining
*
* All rights reserved.
* This program is free to use, but the ban on selling behavior.
* Modify the program must keep all the original text description.
*
* 保留所有權利。
* 本程式可任意使用,但是禁止販售行為。
* 修改程式時必須保留所有原有文字說明。
*
* Email: onionys@ittraining.com.tw
* Blog : http://blog.ittraining.com.tw
*******************************************************************************/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/timer.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ITtraining.com.tw");
MODULE_DESCRIPTION("A timer example.");
struct timer_list my_timer;
/*
* TIMER FUNCTION
* */
static void timer_function(unsigned long data){
printk("Time up");
// modify the timer for next time
mod_timer(&my_timer, jiffies + HZ / 2);
}
/*
* INIT MODULE
* */
int init_module(void)
{
printk("Hello My Timer\n");
// -- initialize the timer
init_timer(&my_timer);
my_timer.expires = jiffies + HZ ;
my_timer.function = timer_function;
my_timer.data = NULL;
// -- TIMER START
add_timer(&my_timer);
printk("END: init_module() \n");
return 0;
}
/*
* CLEANUP MODULE
* */
void cleanup_module(void)
{
del_timer(&my_timer);
printk("Goodbye\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment