概述
hrtime,高精度定时器,这里有篇文章,介绍hrtime相关的知识。点击打开链接
这里,主要介绍如何利用hrtime来模拟PWM的方波。
1 相关的方法:
<span style="font-size:14px;">/**
* hrtimer_start - (re)start an hrtimer on the current CPU
* @timer: the timer to be added
* @tim: expiry time
* @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
*
* Returns:
* 0 on success
* 1 when the timer was active
*/
int
hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)</span>
/**
* ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
* @secs: seconds to set
* @nsecs: nanoseconds to set
*
* Return the ktime_t representation of the value
*/
static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
/**
* hrtimer_init - initialize a timer to the given clock
* @timer: the timer to be initialized
* @clock_id: the clock to be used
* @mode: timer mode abs/rel
*/
void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
enum hrtimer_mode mode)
2
调用的方法
2.1 定义变量,在probe 里面初始化
<span style="font-size:14px;">static struct hrtimer pwm_timer;
static int gpio_status =0;
static int count=0;
static int delaytime=0;
hrtimer_init(&pwm_timer, CLOCK_MONOTONIC,HRTIMER_MODE_REL);
pwm_timer.function = pwm_timer_func;</span>
2
.2 模拟pwm方波
static enum hrtimer_restart pwm_timer_func(struct hrtimer *timer)
{
gpio_set_value(gpio_num,gpio_status); //set gpio 1/0
gpio_status = !gpio_status;
count--;<span style="white-space:pre"> </span> //count control the time of constant
if(0==count )
{
return HRTIMER_NORESTART;
}
else
{
hrtimer_start(&pwm_timer,ktime_set(0, delaytime),HRTIMER_MODE_REL); //cycle, duty cycle = 50%
}
return HRTIMER_NORESTART;
}
void set_pwm(int hz,int ms)
{
int hzTonas=0;
hzTonas = 2*1000*1000*1000/hz; //s to ns
count = ms*hz/1000;
delaytime=hzTonas;
hrtimer_start(&pwm_timer,ktime_set(0, hzTonas),HRTIMER_MODE_REL);
}
3 这里只是实现占空比固定为50%的波形,需要动态设置占空比,需要修改代码。
最后
以上就是贪玩热狗为你收集整理的初入android驱动开发之定时器hrtime的全部内容,希望文章能够帮你解决初入android驱动开发之定时器hrtime所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复