在kernel,有个线程每个十秒往上层系统上报battery及USB的状态信息。
加入直接sleep 10 秒的话,插入USB的时候不能及时更新状态,这个时候就可以使用 hrtimer + wait queue 来实现了
int bat_thread_kthread(void *x) { ktime_t ktime = ktime_set(BAT_TASK_PERIOD, 0); // 10s, 10* 1000 ms while (1) { wait_event(bat_thread_wq, (bat_thread_timeout == KAL_TRUE)); bat_thread_timeout = KAL_FALSE; // 重新启动定时器 hrtimer_start(&battery_kthread_timer, ktime, HRTIMER_MODE_REL); //更新状态 ........... } return 0; }//启动一个内核线程 kthread_run(bat_thread_kthread, NULL, "bat_thread_kthread");
void battery_kthread_hrtimer_init(void) { ktime_t ktime; // 设置定时器的时间为 10* 1000 ms ktime = ktime_set(10, 0); // 10* 1000 ms hrtimer_init(&battery_kthread_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); // 设置回调函数指针 battery_kthread_timer.function = battery_kthread_hrtimer_func; // 启动定时器 hrtimer_start(&battery_kthread_timer, ktime, HRTIMER_MODE_REL); }
enum hrtimer_restart battery_kthread_hrtimer_func(struct hrtimer *timer) { bat_thread_timeout = 1; // 唤醒线程去更新battery信息 wake_up(&bat_thread_wq); return HRTIMER_NORESTART; }