static struct hrtimer timer;
ktime_t kt;
static unsigned int interval=1000; /* unit: 1ms */

static enum hrtimer_restart hrtimer_handler(struct hrtimer *timer)
{
nuc977_pin_ctrl();

hrtimer_forward(timer, timer->base->get_time(), kt);

return HRTIMER_RESTART;
}

static void init_htimer( void )
{
kt = ktime_set( interval/1000000, (interval%1000000)*1000 );
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = hrtimer_handler;
hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
}

static void exit_htimer(void)
{
int ret;

ret = hrtimer_cancel(&timer);
if (ret)
printk("nuc977_scuart:exit_htimer() hTimer was still in use...\n");
printk("nuc977_scuart: exit_htimer().\n");
}

 

一个串口驱动使用到了高精度定时器,运行了几年,一直没有问题,最近在做停上电实验,驱动在动态加载之后导致内核崩溃...最终查找到问题是由于使用高精度内核定时器的流程问题

    kt = ktime_set( interval/1000000, (interval%1000000)*1000 );  
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);

//启动高精度定时器
hrtimer_start(&timer, kt, HRTIMER_MODE_REL);

//绑定高精度的回调函数
timer.function = hrtimer_handler;

然而就是在启动高精度定时器和绑定高精度回调函数之前出现了内核资源调度的切换、抢占,导致内核崩溃!