【linux】驱动-15-定时器
前言
15. 定时器
本章内容为驱动基石之一。
驱动只提供功能,不提供策略。
原文:https://www.cnblogs.com/lizhuming/p/14922233.html
参考源码路径:include\linux\timer.h
参考例子就明白如何使用了。
15.1 内核函数汇总
KERNEL 使用。
说明:以下只是给出部分 API 而已。为 Linux5.12.8 版本。还有其它 API 可以实现内核定时器功能,具体参考内核文档。
setup_timer(): 设置定时器。add_timer(): 向内核添加定时器。mod_timer(): 修改定时器超时时间。del_timer(): 删除定时器。
15.2 内核滴答
获取 CONFIG_HZ:
- 在 Linux 内核源码根目录上找到 .config 配置文件。打开该文件找到宏
CONFIG_HZ,该宏定义了内核滴答(tick)频率。 - 命令行:进入 /boot,查找文件 config-5.8.0-53-geberic(各个系统,文件名不一样,看前缀即可) 内容。
- 如命令:
grep ^CONFIG_HZ /boot/config-$(uname -r)。
- APP:
sysconf(_SC_CLK_TCK)。
内核 tick:
每跳动一次,内核全局值 jiffies 就会累加 1。
定时器的时间值就是基于 jiffies 的。
所以,其实也可以比较设定的 滴答值 和 jiffies 来判断是否超时。由此,内核推荐使用以下 4 个 API:
/*
* These inlines deal with timer wrapping correctly. You are
* strongly encouraged to use them
* 1. Because people otherwise forget
* 2. Because if the timer wrap changes in future you won't have to
* alter your driver code.
*
* time_after(a,b) returns true if the time a is after time b.
*
* Do this with "<0" and ">=0" to only test the sign of the result. A
* good compiler would generate better code (and a really good compiler
* wouldn't care). Gcc is currently neither.
*/
#define time_after(a,b) \
(typecheck(unsigned long, a) && \
typecheck(unsigned long, b) && \
((long)((b) - (a)) < 0))
#define time_before(a,b) time_after(b,a)
#define time_after_eq(a,b) \
(typecheck(unsigned long, a) && \
typecheck(unsigned long, b) && \
((long)((a) - (b)) >= 0))
#define time_before_eq(a,b) time_after_eq(b,a)
15.3 相关结构体
timer_list:
struct timer_list {
/*
* All fields that change during normal runtime grouped to the
* same cacheline
*/
struct hlist_node entry;
unsigned long expires; // 超时时间
void (*function)(struct timer_list *); // 回调函数
u32 flags; // 标志
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
};
15.4 setup_timer() 设置定时器
功能:设置定时器。初始化 timer_list 结构体。
函数原型:
/**
* timer_setup - prepare a timer for first use
* @timer: the timer in question
* @callback: the function to call when timer expires
* @flags: any TIMER_* flags
*
* Regular timer initialization should use either DEFINE_TIMER() above,
* or timer_setup(). For timers on the stack, timer_setup_on_stack() must
* be used and must be balanced with a call to destroy_timer_on_stack().
*/
#define timer_setup(timer, callback, flags) \
__init_timer((timer), (callback), (flags))
15.5 add_timer() 向内核添加定时器
功能:向内核添加定时器。
函数原型:
/**
* add_timer - start a timer
* @timer: the timer to be added
*
* The kernel will do a ->function(@timer) callback from the
* timer interrupt at the ->expires point in the future. The
* current time is 'jiffies'.
*
* The timer's ->expires, ->function fields must be set prior calling this
* function.
*
* Timers with an ->expires field in the past will be executed in the next
* timer tick.
*/
void add_timer(struct timer_list *timer)
{
BUG_ON(timer_pending(timer));
__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
}
EXPORT_SYMBOL(add_timer);
15.6 mod_timer() 修改定时器超时时间
功能:修改定时器超时时间。
注:注释说明中,修改超时时间相当于删除定时器再重新添加到内核。说明,在添加到内核后就不能简单的使用 imer->expires = expires; 来修改超时时间啦。
函数原型:
/**
* mod_timer - modify a timer's timeout
* @timer: the timer to be modified
* @expires: new timeout in jiffies
*
* mod_timer() is a more efficient way to update the expire field of an
* active timer (if the timer is inactive it will be activated)
*
* mod_timer(timer, expires) is equivalent to:
*
* del_timer(timer); timer->expires = expires; add_timer(timer);
*
* Note that if there are multiple unserialized concurrent users of the
* same timer, then mod_timer() is the only safe way to modify the timeout,
* since add_timer() cannot modify an already running timer.
*
* The function returns whether it has modified a pending timer or not.
* (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
* active timer returns 1.)
*/
int mod_timer(struct timer_list *timer, unsigned long expires)
{
return __mod_timer(timer, expires, 0);
}
EXPORT_SYMBOL(mod_timer);
15.7 del_timer 删除定时器
功能:删除定时器。
函数原型:
/**
* del_timer - deactivate a timer.
* @timer: the timer to be deactivated
*
* del_timer() deactivates a timer - this works on both active and inactive
* timers.
*
* The function returns whether it has deactivated a pending timer or not.
* (ie. del_timer() of an inactive timer returns 0, del_timer() of an
* active timer returns 1.)
*/
int del_timer(struct timer_list *timer)
{
struct timer_base *base;
unsigned long flags;
int ret = 0;
debug_assert_init(timer);
if (timer_pending(timer)) {
base = lock_timer_base(timer, &flags);
ret = detach_if_pending(timer, base, true);
raw_spin_unlock_irqrestore(&base->lock, flags);
}
return ret;
}
EXPORT_SYMBOL(del_timer);
【linux】驱动-15-定时器的更多相关文章
- Linux驱动之定时器在按键去抖中的应用
机械按键在按下的过程中会出现抖动的情况,如下图,这样就会导致本来按下一次按键的过程会出现多次中断,导致判断出错.在按键驱动程序中我们可以这么做: 在按键驱动程序中我们可以这么做来取消按键抖动的影响:当 ...
- linux驱动之定时器的介绍和内核时间的学习
本文章摘自下面的网友: http://blog.sina.com.cn/s/blog_6e5b342e0100m87d.html 一.内核中如何记录时间 任何程序都需要时间控制,其主要目的是: 测量时 ...
- linux驱动之定时器的使用
被文章摘自一下几位网友.非常感谢他们. http://blog.sina.com.cn/s/blog_57330c3401011cq3.html Linux的内核中定义了一个定时器的结构: #incl ...
- Linux驱动设计—— 中断与时钟
中断和时钟技术可以提升驱动程序的效率 中断 中断在Linux中的实现 通常情况下,一个驱动程序只需要申请中断,并添加中断处理函数就可以了,中断的到达和中断函数的调用都是内核实现框架完成的.所以程序员只 ...
- Linux驱动之触摸屏程序编写
本篇博客分以下几部分讲解 1.介绍电阻式触摸屏的原理 2.介绍触摸屏驱动的框架(输入子系统) 3.介绍程序用到的结构体 4.介绍程序用到的函数 5.编写程序 6.测试程序 1.介绍电阻式触摸屏的原理 ...
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- linux驱动工程面试必问知识点
linux内核原理面试必问(由易到难) 简单型 1:linux中内核空间及用户空间的区别?用户空间与内核通信方式有哪些? 2:linux中内存划分及如何使用?虚拟地址及物理地址的概念及彼此之间的转化, ...
- linux驱动面试题整理
1.字符型驱动设备你是怎么创建设备文件的,就是/dev/下面的设备文件,供上层应用程序打开使用的文件? 答:mknod命令结合设备的主设备号和次设备号,可创建一个设备文件. 评:这只是其中一种方式,也 ...
- Linux驱动面试题
1. Linux设备中字符设备与块设备有什么主要的区别?请分别列举一些实际的设备说出它们是属于哪一类设备. 字符设备:字符设备是个能够像字节流(类似文件)一样被访问的设备,由字符设备驱动程序来实现这种 ...
- 编写linux驱动所用到的头文件(转)
转自:http://blog.csdn.net/lufeiop02/article/details/6448497 关于linux驱动(应用)程序头文件使用 收藏 驱动程序: #include < ...
随机推荐
- Hive企业级性能优化
Hive作为大数据平台举足轻重的框架,以其稳定性和简单易用性也成为当前构建企业级数据仓库时使用最多的框架之一. 但是如果我们只局限于会使用Hive,而不考虑性能问题,就难搭建出一个完美的数仓,所以Hi ...
- Linux Limit相关内容设置大全(值得收藏)
目录 一. /etc/security/limits.conf 详解 /etc/security/limits.conf 配置解析 /etc/security/limits.d/目录 二. ulimi ...
- 优雅地使用命令行:Tmux 终端复用
转自:http://harttle.com/2015/11/06/tmux-startup.html 你是否曾经开过一大堆的Terminal?有没有把它们都保存下来的冲动?Tmux 的Session就 ...
- Python 送你一棵圣诞树
Python 送你一棵圣诞树 2019-01-02阅读 8800 今天是圣诞节,先祝大家圣诞快乐!??? 有人要说了,圣诞节是耶稣诞生的日子,我又不信基督教,有啥好庆祝的.这你就有所不知了,Pyt ...
- Linux_进程管理的基本概述
一.进程的基本概述 1️⃣:进程是已启动的可执行程序的运行中实例 2️⃣:/proc目录下以数字为名的目录,每一个目录代表一个进程,保存着进程的属性信息 3️⃣:每一个进程的PID是唯一的,就算进程退 ...
- 7.7-9 chage、chpasswd、su
7.7 chage:修改用户密码有效期 chage命令用于查看或修改用户密码的有效期,有些参数和passwd的功能相同. -d 设置上一次密码更改的日期 -E 账号过期的日期.日期格式 ...
- 2017-11-20 崂应工作总结,含LTC3780模块分析,含运放原理
学习了运算放大器的分类 运放的单点输入 差动模式 共模抑制输入模式 反相位比例运放 正相比例运放 电压跟随器 运放的放大比例计算 LTC3780模块的原理 因为: R19 这个电阻不确定他的接法 暂 ...
- ifconfig显示的网卡信息和我的配置文件名不符
比如我的配置文件, cd /etc/sysconfig/network-scripts/ifcfg-Auto_eth0是这个名称,但是我使用ifconfig显示的信息却是 eth6 Link en ...
- (xxx) is not defined at HTMLInputElement.onblur(Day_27)
错误: 这个报错我当时是卡了很久,方法是肯定没有问题的,但js所有的事件都失效了. 解决方案: 1.检查js命名是否有误,若外部引用js文件,尽量使用全小写命名,遵守js命名规范. 2.若还不行,请将 ...
- Django使用Ace实现在线编辑器
前言:最近自己开发SQL工单功能,期间接触到了Ace在线编辑器,折腾一下,感觉功能挺多,特意去了解学习一下分享跟大家. ACE 是一个功能非常强大的编辑器,实现语法高亮.代码补全功能,还有很多主题,支 ...