嵌入式开发记录 day33 linux内核定时器
1、这种的定时器应用在定时精度不高的情况下:
1、延时某一段时间后,执行某一个动作;
2、定时的查询某一个状态;
2、定时的基础概念
1、Hz:系统定时器时钟通过CONFIG_HZ设置,范围是100-1000,因此会决定中断发生频率;
2、jiffies: 内核中的全局变量,记录内核自启动以来产生的中断次数,那么,jiffies/HZ就是Linux内核启动的秒数;
3、定时器有关的接口:
1、结构体:timer_list
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_STATS int start_pid; void *start_site; char start_comm[16]; #endif #ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map; #endif };
2、setup_timer()、add_timer()、del_timer()、mod_timer()
3、 双向链表
platform_driver_register→driver_register
→bus_add_driver→struct bus_type *bus
→struct subsys_private *p
→struct kset subsys
→struct list_head list;
mod_timer = del_timer(timer);timer->expires = expires; add_timer(timer);
4、内核定时器应用
#include "linux/module.h" #include "linux/timer.h" #include "linux/jiffies.h" // 定时器列表 struct timer_list demo_timer; // 是一个双向链表 // 定时溢出中断函数 static void time_func(unsigned long data) { printk("%s ,secs = %ld!\n",(char *)data,jiffies/HZ); mod_timer(&demo_timer,jiffies + 5*HZ); // 设置定时器器的超时时间 } static int __init mytimer_init(void) { printk("mytimer_init!\n"); // 配置定时器 setup_timer(&demo_timer,time_func,(unsigned long) "demo_timer!"); demo_timer.expires = jiffies + 1*HZ; // 设置定时时间 动作执行时间设置1s add_timer(&demo_timer); // 定时器列表上添加定时器 return 0; } static void __exit mytimer_exit(void) { printk("mytimer_exit!\n"); del_timer(&demo_timer); } module_init(mytimer_init); module_exit(mytimer_exit); MODULE_LICENSE("Dual BSD/GPL");

浙公网安备 33010602011771号