Linux内核驱动定时微秒级别实现
#include <linux/module.h> #include <linux/kthread.h> #define TIMEOUT_HR 1000000 /* 1us */ static struct hrtimer etx_hr_timer; ktime_t ktime; enum hrtimer_restart hrtimer_callback(struct hrtimer *timer) { static int count; printk(KERN_INFO "hrtimer callback is running count:%d\n", count++); hrtimer_forward_now(timer, ktime_set(0, TIMEOUT_HR)); return HRTIMER_RESTART; } static int __init lkm_init(void) { printk(KERN_INFO "init lkm module.\n"); /* 设置高精度时钟 */ ktime = ktime_set(0, TIMEOUT_HR); hrtimer_init(&etx_hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); etx_hr_timer.function = &hrtimer_callback; hrtimer_start(&etx_hr_timer, ktime, HRTIMER_MODE_REL); return 0; } static void __exit lkm_exit(void) { hrtimer_cancel(&etx_hr_timer); printk(KERN_INFO "exit lkm module.\n"); } module_init(lkm_init); module_exit(lkm_exit); MODULE_VERSION("0.0"); MODULE_DESCRIPTION("sample kernel module"); MODULE_LICENSE("GPL"); MODULE_AUTHOR("liweijie");
优质博客:
https://blog.csdn.net/qq_37858386/article/details/85784994