Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux kernel timer timer_list

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Linux kernel timer timer_list how to use, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Briefly introduce the timer timer_list:

1. Header file: linux/timer.h

2. Structure:

Struct timer_list {/ * All fields that change during normal runtime grouped to the * same cacheline * / struct list_head entry; unsigned long expires;struct tvec_base * base;void (* function) (unsigned long); unsigned long data;int slack;#ifdef CONFIG_TIMER_STATSint start_pid;void * start_site;char start_comm [16]; # endif#ifdef CONFIG_LOCKDEPstruct lockdep_map lockdep_map;#endif}

3. Introduction of the main members:

List is used when implementing, regardless of timer function.

Expires is the number of ticks timed by the timer (the current tick is jiffies)

Void (* function) (unsigned long) timer timeout handler

The argument passed by data to the timeout handler is mainly the difference between which timer timeout is when multiple timers are used at the same time.

4. The API API provided:

A, init_timer (struct timer_list*): timer initialization function

B, add_timer (struct timer_list*): add a timer to the system

C. Mod_timer (struct timer_list *, unsigned long jiffier_timerout): the timeout for modifying the timer is jiffies_timerout.

D, timer_pending (struct timer_list *): timer status query, returns 1 if it is in the timer list of the system, 0 otherwise

E, del_timer (struct timer_list*): delete timer.

5. Usage:

A. When creating a timer, you need to define struct timer_list my_timer first

B. Initialize timer init_timer (& my_timer) in the open function specified by file_operation

C. Reload timer time mod_timer (& my_timer,HZ) at the end of the timeout handling function

D. If there is an interrupt in the driver written by yourself, you need to del_timer (& my_timer) at the entrance of the interrupt, and reload the timer time mod_timer (& my_timer,HZ) at the entrance.

Second, an example demonstration:

1. Driver code:

# include # include / * delay*/#include # include # include / * kmalloc*/#include / * vmalloc*/#include / * ssize_t*/#include / * file_operaiotns*/#include # include # Include # include / * hardware related * / # include / * ordinary IO*/#define LED IMX_GPIO_NR (1Magne15) / * SD2_DAT0*/ / * allocate memory space * / # define WRITE_MALLOC_SIZE 4096pm * primary and secondary device numbers * * / # define DEVICE _ MAJOR 102#define DEVICE_MINOR 0Universe * cache pointer Point to the memory area * / static char * led_spvm / * create a class in the / sys directory * / static struct class * led_class; / * under this class, create a device node * / static struct cdev * led_class_dev;/* definition timer structure * / static struct timer_list timer; typedef unsigned short int unit; / * 2 bytes, 16-bit timeout void function declaration * / void mytimeout (void) / * timer initialization function * / void mytimer_init (void) {init_timer (& timer); / * initialize timer * / timer.expires = jiffies + HZ; / * set the timeout to 1S*/ timer.function = mytimeout / * interrupt service subroutine entry after timeout * / add_timer (& timer); / * start timer * /} / * implementation of open function * / static int led_open (struct inode * inode, struct file * file) {/ * timer initialization * / mytimer_init (); return 0 } / * implementation of release function * / static int led_close (struct inode * inode, struct file * file) {/ * release occupied resources * / gpio_free (LED); / * delete timer * / del_timer (& timer); / * print prompt exit message * / printk (KERN_ALERT "LED is closed!\ n"); return 0 } / * specific set of file operations * / static const struct file_operations led_fops = {/ * this is the owner * / .owner = THIS_MODULE, .open = led_open, .release = led_close,}; / * timeout interrupt service subfunction * / void mytimeout (void) {/ * LED flicker * / _ gpio_set_value (LED,1); mdelay (1000); _ gpio_set_value (LED,0) / * reset the timing time to 1s*/mod_timer (& timer,jiffies + HZ);} / * driver initialization function * / static int led_init (void) {/ * device initialization * / int devno,error;/* device number application, create * / devno = MKDEV (DEVICE_MAJOR,DEVICE_MINOR); / * assign the address space of the device structure * / led_spvm = (char *) vmalloc (WRITE_MALLOC_SIZE) Led_class_dev = cdev_alloc (); / * character device initialization, bind related operations to device * / cdev_init (led_class_dev,&led_fops); / * device owner * / led_class_dev- > owner = THIS_MODULE;/* add device to kernel * / cdev_add (led_class_dev,devno,1); / * statically apply for device number * / register_chrdev (DEVICE_MAJOR, "led", & led_fops) / * create a device class to automatically create a device file * / led_class = class_create (THIS_MODULE, "led"); / * create a device * / device_create (led_class,NULL,MKDEV (DEVICE_MAJOR,DEVICE_MINOR), NULL, "led") based on the previously created device class; / * apply for gpio*/error = gpio_request (LED, "gpio") If (error < 0) {printk (KERN_ALERT "failed to request GPIO LED\ n"); goto fail1;} / * set the IO direction to output * / error = gpio_direction_output (LED,0); if (error < 0) {printk (KERN_ALERT "failed to configure direction for LED\ n"); goto fail2;} return 0Tract1Fail1Rue error;fail2: gpio_free (LED) } / * exit function * / static void led_exit (void) {/ * timer uninstall * / del_timer (& timer); / * device uninstall * / unregister_chrdev (DEVICE_MAJOR, "led"); device_destroy (led_class,MKDEV (DEVICE_MAJOR,DEVICE_MINOR)); class_destroy (led_class) } / * LICENSE information * / MODULE_LICENSE ("GPL"); / * uninstall and load * / module_init (led_init); module_exit (led_exit)

2. Test program code:

# include # include int main (int argc,char * * argv) {int fd;/* corresponds to the loaded device name: dev/led*/fd = open ("/ dev/led", O_RDWR); / * if there is an error in opening the device, print the message * / if (fd < 0) {printf ("can`t open fd_write!\ n");} while (1) {sleep (1000) } / * when exiting, turn off the device * / close (fd);} after reading the above, have you mastered how to use the Linux kernel timer timer_list? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report