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
#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.
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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Generate .bin .hex .txt files on CCS and burn them to MSP430 to make the LCD light up
- Digital video image elimination based on FPGADSP...
- Award-winning live broadcast: Registration is open for TI desktop DLP 3D printing, 3D scanning and industrial display applications based on DLP Pico technology~
- Is there any pure hardware?
- "Pingtou Ge Jianchi CDK Quick Start Guide"
- [AT32F421 review] + Display driver for digital tube module
- The difference between C language character array and ordinary array
- May I ask, [Environmental humidity: 5%~85%], what does the [5%~85%] here refer to?
- MSP430 Simulation & Programming
- [Atria Development Board AT32F421 Review] - Getting Started Tutorial