linux ps le hrtime,Linux 高精度定时器hrtimers简单介绍和应用场景
hrtimer:high-resolution kernel timers:
hrtimers的诞生是由于内核开发者在使用过程中发现,原始的定时器kernel/timers.c,已经可以满足所有场景的,但是在实际的大量测试中发现还是无法满足所有场景,所以hrtimers就诞生了。这里简单介绍下关键结构体和一个应用场景,具体远离下篇博客再来分析。
1.结构体简单介绍
1.struct hrtimer
这个是高精度对象的成员变量,内核注释已经很详细了,具体细节下篇来说,这里重点关注的是function,这个是我们需要实现的方法。
/**
* struct hrtimer - the basic hrtimer structure
* @node: timerqueue node, which also manages node.expires,
* the absolute expiry time in the hrtimers internal
* representation. The time is related to the clock on
* which the timer is based. Is setup by adding
* slack to the _softexpires value. For non range timers
* identical to _softexpires.
* @_softexpires: the absolute earliest expiry time of the hrtimer.
* The time which was given as expiry time when the timer
* was armed.
* @function: timer expiry callback function
* @base: pointer to the timer base (per cpu and per clock)
* @state: state information (See bit values above)
* @start_pid: timer statistics field to store the pid of the task which
* started the timer
* @start_site: timer statistics field to store the site where the timer
* was started
* @start_comm: timer statistics field to store the name of the process which
* started the timer
*
* The hrtimer structure must be initialized by hrtimer_init()
*/
struct hrtimer {
struct timerqueue_node node;
ktime_t _softexpires;
enum hrtimer_restart (*function)(struct hrtimer *);
struct hrtimer_clock_base *base;
unsigned long state;
#ifdef CONFIG_TIMER_STATS
int start_pid;
void *start_site;
char start_comm[16];
#endif
};
2.ktime_t
这个用来表示时间,单位为纳米。在启动定时器之前,需要设定一个时间来决定何时调用回调函数。
typedef union ktime ktime_t;
/*
* ktime_t:
*
* A single 64-bit variable is used to store the hrtimers
* internal representation of time values in scalar nanoseconds. The
* design plays out best on 64-bit CPUs, where most conversions are
* NOPs and most arithmetic ktime_t operations are plain arithmetic
* operations.
*
*/
union ktime {
s64 tv64;
};
2.hrtimers应用场景
1).创建定时器对象&设置首次触发时间
最近在做一个Camera的flash驱动,该camera需要pwm来控制亮度,而硬件上该GPIO口,没有pwm功能,所以只能来模拟pwm了。
本地flash控制对象中嵌套的有hrtimer对象:
struct flash_gpio_ctrl {
struct hrtimer *flash_timer;
ktime_t kt;
int flash_level;
int IsFlash_stop;
int loop_count;
uint32_t gpio_enf;
uint32_t gpio_enm; //pwm
};
创建定时器以及启动定时器:
static void flash_pwm_start(void)
{
hrtimer_init(flash_gpio_ctrl.flash_timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
flash_gpio_ctrl.flash_timer->function = hrtimer_handler;
flash_gpio_ctrl.kt = ktime_set(0, 10000);
hrtimer_start(flash_gpio_ctrl.flash_timer,flash_gpio_ctrl.kt,HRTIMER_MODE_REL);
pr_err("timer_start");
}
1.hrtimer_init:这个是用来创建timer定时器对象,由内核封装好了,具体细节先不用关系,只需要把定时器对象嵌套进我们定义的结构体中就行了。
2.function:这个就是回调函数指针,这里注册回调函数。
3.ktime_set(0, 10000):设置首次触发时间,这里我设置了10ms。
4.hrtimer_start();这里传入定时器对象,和触发时间,就开启定时器了。
2).回调函数预览
static enum hrtimer_restart hrtimer_handler(struct hrtimer *timer)
{
flash_flag = !flash_flag;
gpio_direction_output(flash_gpio_ctrl.gpio_enm, 0);
if(flash_gpio_ctrl.loop_count != 1){
if (flash_flag) {
gpio_direction_output(flash_gpio_ctrl.gpio_enm, 1);
flash_gpio_ctrl.kt = ktime_set(0, BASE_TIME * (flash_gpio_ctrl.flash_level) * 1000);
hrtimer_forward_now(flash_gpio_ctrl.flash_timer, flash_gpio_ctrl.kt);
//pr_err("pulse low level:%d",flash_gpio_ctrl.flash_level);
} else {
gpio_direction_output(flash_gpio_ctrl.gpio_enm, 0);
flash_gpio_ctrl.kt = ktime_set(0, 50000 - (BASE_TIME * (flash_gpio_ctrl.flash_level) * 1000));
hrtimer_forward_now(flash_gpio_ctrl.flash_timer, flash_gpio_ctrl.kt);
//pr_err("pulse high levle");
}
flash_gpio_ctrl.loop_count--;
pr_err("flash_timer out\n");
return HRTIMER_RESTART;
}else{
flash_gpio_ctrl.loop_count--;
return HRTIMER_NORESTART;
}
}
1.loop_count:这里我设置的是一个循环触发的定时器,所以为了不让定时器无休止的进行下去,设置一个触发次数。次数减为0,则推出定时器。
2.HRTIMER_RESTART:这个返回值告诉定时器核心,这里是定时器会重新触发,而且上面又重新设置了触发的时间。
3.hrtimer_forward_now:重新设置定时器触发的时间。
至此定时器就已经运行起来了,每割一定时间都会触发回调函数。而且产生的pwm能使flash亮起来,但是由于占空比不是很准确,所以flash亮度有些不稳定。(此外还有一种方法是启动一个线程,来同步产生控制信号)
linux ps le hrtime,Linux 高精度定时器hrtimers简单介绍和应用场景相关推荐
- linux ps le hrtime,前端Tips#4 - 用 process.hrtime 获取纳秒级的计时精度
视频讲解 文字讲解 如果去测试代码运行的时长,你会选择哪个时间函数? 一般第一时间想到的函数是 Date.now 或 Date.getTime. 1.先讲结论 之所以这么选,是基于 精度 和 时钟同步 ...
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...
- linux ps查看进程,Linux新手入门:PS命令查看正在运行的进程
Linux作为开源系统,里面有着大量命令需要了解和使用,同样的命令在不同系统中的使用方法各不相同,例如本次要介绍的PS命令,那么什么是PS命令?要如何使用PS命令?下面小编就跟大家详细讲解Linux ...
- linux ps命令大全,Linux ps命令例子汇总
Linux ps命令主要用于查看系统运行的进程,确定进程运行的状态机是否占用过多资源等?下面学习啦小编通过实例来给大家详细介绍下Linux的ps命令,一起来了解下吧. Linux提供了当前进程的同时, ...
- Linux ps命令、Linux top命令
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. Linux ps命令用于显示当前进程 (process) 的状态. 语法 ps [options] ...
- linux ps结果解析,Linux笔记-ps -aux的结果解析
ps 的参数说明 ps 提供了很多的选项参数,常用的有以下几个: l 长格式输出: u 按用户名和启动时间的顺序来显示进程: j 用任务格式来显示进程: f 用树形格式来显示进程: a 显示所有用户的 ...
- linux ps 进程组,linux进程管理(2)---进程的组织结构
一.目的 linux为了不同的进程管理目的,使用了不同的方法组织进程之间的关系,为了体现父子关系,使用了"树形"图:为了对同一信号量统一处理,使用了进程组:为了快速查找某个进程,使 ...
- linux ps 源代码,【linux】ps(示例代码)
Linux下PS命令详解 要对系统中进程进行监测控制,查看状态,内存,CPU的使用情况,使用命令:/bin/ps (1)ps :是显示瞬间进程的状态,并不动态连续: (2)top:如果想对进程运行时间 ...
- linux ps vsz malloc,Linux proc 内存
ps:html USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 4238 ...
最新文章
- 了解1D和3D卷积神经网络|Keras
- 解决VC++ Error spawning cl.exe 问题·
- Linux ubuntu终端sh、bash、shell的联系与区别
- linux没有jre文件夹,linux上配置jdk时,java命令提示没有此文件或文件夹的解决方法...
- Java执行程序服务类型
- Python数模笔记-NetworkX(4)最小生成树
- 基于PostgreSQL流复制的容灾库架构设想及实现
- 联想回文字符串的编程题
- centos7安装python3.6独立的virtualenv环境
- C++制作植物大战僵尸
- ios模拟器 安装ipa_用iOS模拟器安装App的方法
- Android开源库——xUtils框架
- Firefox书签同步工具Xmarks
- 如何启用计算机的休眠,win7休眠-win7如何启用休眠,我已经google过了,没用,请大家帮忙我? 爱问知识人...
- 删除IE浏览器input框自带的删除叉
- p40华为鸿蒙系统gms服务,华为P40确定:告别鸿蒙系统,告别谷歌GMS,转用使用HMS...
- Scala中下划线“_“的应用场景
- excel之单元格格式/设置/
- yolo结合多目标跟踪算法实现检测和跟踪效果
- 认识高通8155(开发板介绍)