如何建设一个国际化的网站北京到安阳的高铁
web/
2025/10/9 2:12:26/
文章来源:
如何建设一个国际化的网站,北京到安阳的高铁,随手app广告怎么关闭,湛江市seo网站设计哪里好在Linux UWB Stack的内核模块实现中#xff0c;较多的使用了内核定时器#xff0c;本文基于fake MCPS实现中的应用为背景#xff0c;介绍了内核定时器的使用。 1. 内核定时器 Linux内核用来控制在未来某个时间点【基于jiffies(节拍总数)】调度执行某个函数的一种机制#x… 在Linux UWB Stack的内核模块实现中较多的使用了内核定时器本文基于fake MCPS实现中的应用为背景介绍了内核定时器的使用。 1. 内核定时器 Linux内核用来控制在未来某个时间点【基于jiffies(节拍总数)】调度执行某个函数的一种机制相关函数位于 linux/timer.h 和 kernel/timer.c 文件中。 当内核定时器定时时间到达时会进入用户指定的函数相当于软中断。内核定时器注册开启后运行一次就不会再运行(相当于自动注销)我们可以重新设置定时器的超时时间让定时器重复运行。 每当时钟tick中断发生时全局变量jiffies(一个32位的unsigned long 变量)就加1因此jiffies记录了linux系统启动后时钟中断发生的次数驱动程序常利用jiffies来计算不同事件间的时间间隔。内核每秒钟将jiffies变量增加HZ次。因此对于HZ值为100的系统jiffy1等于隔了10ms而对于HZ为1000的系统jiffy1仅为1ms。
内核定时器使用
首先需要创建一个struct timer_list实例并通过timer_setup设置定时器的超时处理函数并启动。
static struct timer_list g_timer;void timer_setup(struct timer_list *timer, void (*callback)(struct timer_list *), unsigned int flags);
timer_setup函数的实现相对比较简单
void timer_setup(struct timer_list *timer, void (*callback)(struct timer_list *), unsigned int flags)
{memset(timer, 0, sizeof(*timer));INIT_LIST_HEAD(timer-entry);timer-function callback;timer-flags flags;
}2定时器设置好之后配置超时时间启动定时器
// 设置定时器的超时时间在全局jiffies变量基础上增加jiffies与系统设置的HZ相关
void mod_timer(struct timer_list *timer, unsigned long expires);这样定时器在设置的超时时间到之后运行用户设定的函数callback若要保证周期性的运行则可以在周期函数中重复设置定时器的超时时间。
void del_timer(struct timer_list * timer);del_timer 函数用于删除一个已经激活的定时器。其中timer 是一个指向定时器结构体的指针。如果定时器已经被取消或者还未被激活则不会有任何操作。在删除定时器之后定时器的定时操作也会被取消。
关于内核定时器时间转换
//将输入的微秒时间转换为jiffies单位
unsigned long usecs_to_jiffies(const unsigned int m);
//将输入的毫秒时间转换为jiffies单位
unsigned long msecs_to_jiffies(const unsigned int m);相反的也可以将jiffies单位的时间转换为struct timespec的时间单位。
void jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)struct timespec {time_t tv_sec; // 秒数long tv_nsec; // 纳秒数
};2. 内核定时器应用——fake MCPS实现
在uwb stack的mcps802154_fake.c的fake MCPS实现代码如下
static struct timer_list g_timer;
// 1s 1000 ms
static const int g_time_interval 1000;// 周期任务在每次任务中重新设定定时器的超时时间
static void periodic_task(struct timer_list *unused)
{// .../*Restarting the timer...*/mod_timer(g_timer, jiffies msecs_to_jiffies(g_time_interval));if (rx_enabled) {rx_enabled false;mcps802154_rx_frame(driver_llhw);}if (tx_queued) {tx_queued false;mcps802154_tx_done(driver_llhw);}
}static int __init fake_init(void)
{int r;pr_info(fake_mcps: init\n);// 分配底层硬件驱动driver_llhw mcps802154_alloc_llhw(0, fake_ops);if (driver_llhw NULL) {return -ENOMEM;}// ...// mcps802154注册底层硬件驱动r mcps802154_register_llhw(driver_llhw);if (r) {mcps802154_free_llhw(driver_llhw);driver_llhw NULL;return r;}// 设置定时器启动周期定时器timer_setup(g_timer, periodic_task, 0);mod_timer(g_timer, jiffies msecs_to_jiffies(g_time_interval));return 0;
}static void __exit fake_exit(void)
{pr_info(fake_mcps: Exit\n);// 注销及释放底层硬件驱动mcps802154_unregister_llhw(driver_llhw);mcps802154_free_llhw(driver_llhw);driver_llhw NULL;/*Deleting the timer aka the periodic task.*/stop_timer true;del_timer(g_timer);
}module_init(fake_init);
module_exit(fake_exit);
通过fake_init完成必要的mcps802154相关的注册同时设置内核定时器周期性的启动模拟的收发操作。 模块退出时通过fake_exit函数进行注销删除已经激活的定时器。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/89386.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!