为了验证高精度定时器的分辨率,我们写一个简单的内核驱动(功能:在设定的周期内反转IO,然后通过示波器测量精度)。
高精度定时器使用步骤:
#include "hrtimer_test.h"
#define HRTIMER_TEST_PIN 7
#define HRTIMER_TEST_CYCLE 0, (100000 / 2)
#define DEVICE_NAME "HRTIMER_TEST"
#define CLASS_NAME "HRTIMER_TEST"
int major_number;
struct device *device;
struct class *class;
static struct hrtimer kthread_timer;
int value = 0;
enum hrtimer_restart hrtimer_cb_func(struct hrtimer *timer) {
ChipBspGpio_ExportSet(ULTRASONIC_TEST_PIN, value);
value = !value;
hrtimer_forward(timer, timer->base->get_time(), ktime_set(HRTIMER_TEST_CYCLE));
return HRTIMER_RESTART;
}
void kthread_hrtimer_init(void) {
hrtimer_init(&kthread_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
kthread_timer.function = hrtimer_cb_func;
hrtimer_start(&kthread_timer, ktime_set(HRTIMER_TEST_CYCLE), HRTIMER_MODE_REL);
}
static int __init hrtimer_test_init(void) {
printk(KERN_ALERT "hrtimer_test : Init !!\n");
major_number = register_chrdev(0, DEVICE_NAME, NULL);
if (major_number < 0) {
printk(KERN_ALERT "hrtimer_test: Register fail!\n");
return major_number;
}
printk(KERN_ALERT "Registe success, major number is %d\n", major_number);
class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(class)) {
unregister_chrdev(major_number, DEVICE_NAME);
return PTR_ERR(class);
}
device = device_create(class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
if (IS_ERR(device)) {
class_destroy(class);
unregister_chrdev(major_number, DEVICE_NAME);
return PTR_ERR(device);
}
printk(KERN_ALERT "hrtimer_test: init success!!\n");
kthread_hrtimer_init();
return 0;
}
static void __exit hrtimer_test_exit(void) {
hrtimer_cancel(&kthread_timer);
device_destroy(class, MKDEV(major_number, 0));
class_unregister(class);
class_destroy(class);
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "hrtimer_test: exit success!!\n");
}
module_init(hrtimer_test_init);
module_exit(hrtimer_test_exit);
MODULE_AUTHOR("RieChen");
MODULE_LICENSE("GPL");
#define HRTIMER_TEST_CYCLE 0, (1000000 / 2)
#define HRTIMER_TEST_CYCLE 0, (100000 / 2)
#define HRTIMER_TEST_CYCLE 0, (10000 / 2)
#define HRTIMER_TEST_CYCLE 0, (1000 / 2)
审核编辑 黄昊宇
全部0条评论
快来发表一下你的评论吧 !