Use of Linux 2.4.18 kernel timer

Publisher:breakthrough2Latest update time:2024-07-24 Source: elecfansKeywords:linux2  kernel  timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

My kernel is 2.4.18 . The Linux kernel defines a timer structure:

#include

struct timer_list

{

struct list_head list;

unsigned long expires; // Timer expiration time

unsigned long data; // passed as a parameter to the timer processing function

void (*function)(unsigned long);

};

Using this structure, we can easily use the timer in the driver.

1: Timer API function :

Initialize the timer:

void init_timer(struct timer_list * timer);

Add a timer:

void add_timer(struct timer_list * timer);

To delete a timer:

int del_timer(struct timer_list * timer);

Modify the expiration of the timer :

int mod_timer(struct timer_list *timer, unsigned long expires);

2: The general process of using the timer is:

( 1 ) Create a timer and write a timeout timer processing function ;

( 2 ) Assign values ​​to the timer 's expires , data , and function ;

( 3 ) Call add_timer to add the timer to the list;

( 4 ) When the timer expires, the function is executed;

( 5 ) In the program where timer control is involved, call del_timer or mod_timer appropriately to delete the timer or modify the timer 's expiration date .

Three: A simple example (timer.c) :

/******************************************

* Author : Cai Xiaofei *

* Date : November 27, 2008 *

* Name: timer.c *

* Description : This program is used to learn the Linux kernel *

* Basic use of timer *

*****************************************/

#include

#include

#include

#include //jiffies is defined in this header file

#include

#include

struct timer_list mytimer; // define a timer

void mytimer_ok(unsigned long arg)

{

printk("Mytimer is okn");

printk("receive data from timer: %dn",arg);

}

static int __init hello_init (void)

{

printk("hello,worldn");

init_timer(&mytimer); // Initialize the timer

mytimer.expires = jiffies+100; // Set the timeout period, 100 represents 1 second

mytimer.data = 250; // The value passed to the timer timeout function

mytimer.function = mytimer_ok; // Set timer timeout function

add_timer(&mytimer); // Add timer, timer starts to take effect

return 0;

}

static void __exit hello_exit (void)

{

del_timer(&mytimer); // Delete the timer when uninstalling the module

printk("Hello module exitn");

}

module_init(hello_init);

module_exit(hello_exit);

MODULE_AUTHOR("CXF");

MODULE_LICENSE("Dual BSD/GPL");

4: After cross-compilation, put it on the development board:

#insmod timer.o

It can be found that after one second, the timer expiration function is executed, the information is printed, and 250 is also passed correctly.

, haha!

#rmmod timer

The experiment is complete.

Five: Further understanding of the timer:

In the above timer timeout function mytimer_ok(unsigned long arg) , add the following

Code:

mytimer.expires = jiffies+100; // Set the timeout period, 100 represents 1 second

mytimer.function = mytimer_ok; // Set timer timeout function

add_timer(&mytimer); // Add timer, timer starts to take effect

After cross-compilation, put it on the development board

#insmod timer.o

It is found that the mytimer_ok function is executed once every second . This is because every time the timer expires,

It sets a new timeout again, and the new timeout function points to itself, forming a hand-off.

So it will continue to execute.

#rmmod timer

You can uninstall the module, and of course the printing will end. Note that the function keeps printing information because the timer times out.

, which will cause the timer timeout function to keep printing information when you enter the above command. Don't worry about it.

Patiently enter the above command to successfully uninstall.


Keywords:linux2  kernel  timer Reference address:Use of Linux 2.4.18 kernel timer

Previous article:Kernel transplantation and file system production (4): Summary of UBIFS root file system production
Next article:FL2440 driver added (4) LED driver added

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号