定时器
2025-11-20
7
0
jiffies是系统起动时的定时计数,其时长由内核CONFIG_HZ决定,一般为100HZ.
#include <linux/jiffies.h>
#include <linux/timer.h>
struct timer_list timer;
void function(unsigned long arg)
{
static bool ison = false;
ison = !ison;
if(ison)
{
led_switch(LEDON);
}
else
{
led_switch(LEDOFF);
}
/*
* 定时器处理代码
*/
/* 如果需要定时器周期性运行的话就使用 mod_timer
* 函数重新设置超时值并且启动定时器。 */
mod_timer(&timer, jiffies + msecs_to_jiffies(100));
}
void init(void)
{
init_timer(&timer); /* 初始化定时器 */
timer.function = function; /* 设置定时处理函数 */
timer.expires= jiffies + msecs_to_jiffies(2000);/* 超时时间 2 秒 */
timer.data = (unsigned long)&newchrled; /* 将设备结构体作为参数 */
//不调用,不启用定时器
add_timer(&timer); /* 启动定时器 */
printk("stat timer\r\n");
}
/* 退出函数 */
void exit(void)
{
del_timer(&timer); /* 删除定时器 */
/* 或者使用 */
del_timer_sync(&timer);//会等待其他处理器使用完定时器再删除
}
- void ndelay(unsigned long nsecs) 纳秒、
- void udelay(unsigned long usecs)微秒
- void mdelay(unsigned long mseces)毫秒延时函数。
| 函数 | 描述 |
|---|---|
int jiffies_to_msecs(const unsigned long j) |
将 jiffies 类型的参数 j 转换为对应的毫秒 |
int jiffies_to_usecs(const unsigned long j) |
将 jiffies 类型的参数 j 转换为对应的微秒 |
u64 jiffies_to_nsecs(const unsigned long j) |
将 jiffies 类型的参数 j 转换为对应的纳秒 |
long msecs_to_jiffies(const unsigned int m) |
将毫秒值转换为 jiffies 类型 |
long usecs_to_jiffies(const unsigned int u) |
将微秒值转换为 jiffies 类型 |
unsigned long nsecs_to_jiffies(u64 n) |
将纳秒值转换为 jiffies 类型 |
ARM&Linux基础





