Search This Blog

10 July, 2009

Using Timers in Linux kernel module

Linux kernel timer tutorial
This is a tutorial for using timer in linux kernel module. we all need one function to be called periodically say 500ms,1s to do so some task periodically(like GPIO polling, sending some message to user space etc)

As any system/OS, we need to initialize the timer module before we can use it. We need to give a name for the timer(for us to configure and restart the timer), timer function handler(ie.when timer expires this function will be called). One important thing to be noted is after timer expires it wont restart the timer automatically, it has to be restarted manually to get the next timer expiry.

The timer implementation resides in and kernel/timer.c

The timer study program is as shown below. In the module init we are initializing the timer with the the timer expiry routine(timer1_routine), data to be passed when the routing is called(in my example 1) , the timer expiry in jiffies. these are the minimum initialization we need to do with the timer struct. After calling the add_timer function the timer starts and the expiray routine will be called after the specifies time, in our case 1 second Dont forget to stop the timer del_timer_sync(&timer1); in the module unload. If we want to execute the timer1_routine every 1 second we need to restart the timer with mod_timer(&timer1, jiffies + HZ);

Any feedback welcome ! You can read the below book

Linux Device Drivers, 3rd Edition


/********************************** timerStudy.c *************************************************
#include
#include
#include

MODULE_LICENSE("Dual BSD/GPL");

struct timer_list timer1;

int i;

void timer1_routine(unsigned long data)
{

printk(KERN_ALERT"Inside Timer Routine count-> %d data passed %ld\n",i++,data);
mod_timer(&timer1, jiffies + HZ); /* restarting timer */
}


static int timer_module_init(void)
{


init_timer(&timer1);

timer1.function = timer1_routine;
timer1.data = 1;
timer1.expires = jiffies + HZ; /* 1 second */
add_timer(&timer1); /* Starting the timer */

printk(KERN_ALERT"Timer Module loaded\n");
return 0;
}

static void timer_module_exit(void)
{
del_timer_sync(&timer1); /* Deleting the timer */

printk(KERN_ALERT "Timer module unloaded \n");
}

module_init(timer_module_init);
module_exit(timer_module_exit);

********************************** timerStudy.c *************************************************/



Make file to compile the above timer module.

/********************************** Makefile *************************************************



obj-m +=timerStudy.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

********************************** Makefile *************************************************/

Dino Joseph Mycle

18 June, 2007

Monitor flickering problem - Linux (Redhat 9)

Monitor flickering problem in Linux (Redhat 9)

I installed Redhat 9, i was having intel chipset (865). when the window screen comes(X window)the monitor was flickering . i tried changing the horizontal scan freq. in the X11/XF86Config file, but no use. Then at last only i got the idea of why don't i try installing the graphics driver for the board. Then i went through intel website and downloaded the graphics driver for Linux. After installing the graphics driver the monitor flickering problem is solved.
i think this will work for all flickering problem in Linux better try first installing the graphics driver and then try for scanning freq.

04 October, 2006

Making the VxWorks Tornado project independent of absolute path

Please Follow the steps below
Assuming the project name is trnado_prj and it is stored at D:\src_trnado_prj\src

1) Close Tornado Application
2) Open the trnado_prj.wpj in NOTEPAD
3) Replace D:/src_trnado_prj/src with $(PRJ_DIR)/../../../../../.. using the notepad replace all the occurences in the trnado_prj.wpj file
4) Save and close it
5) After doing this delete the makefile in the then compile it

Note: Only once you have to do it. After that if the source code is the directory which contains the above mentioned trnado_prj.wpj file, and if you are renaming it again the base folder name, just delete the make file and Build it

03 October, 2006

Technical Tips ---> K 750i

To take picture without making sound for Sony Ericsson K750i, press teh '#' for 2-3 seconds and change the profile to silent mode. After that if u take picture sound wont be there

tmux enabling mouse interaction

Add the below line to the ~/.tmux.conf setw -g mouse on save the file and exit from the tmux shell, you should be able to use your mouse to ...