Measuring the Precision of High-Precision Timers in Linux Driver Development

Introduction

  • Today, we will evaluate the high-precision timers in the Linux kernel, while also conducting cross-testing using a Tektronix oscilloscope and a DS100 Mini digital oscilloscope.
  • Due to project requirements for precise timing cycles, we need to assess its feasibility and verify whether the oscilloscope from Atomic Clock can support the embedded development process.

Overview of Linux High-Precision Timers

  • In fact, traditional low-resolution timers can no longer meet development needs as technology evolves. Moreover, with the continuous advancement of hardware, the precision of hardware timers has also increased, creating favorable conditions for high-precision timers.
  • Low-resolution timers can generally achieve O(1) time complexity, but when carry occurs, the unpredictable O(N) cascading migration time of the timer affects its precision.
  • Low-resolution timers are more suitable for timeout application scenarios, where the goal is to obtain correct results before the timeout occurs, making low-resolution timers quite appropriate.
  • To meet the evolving technology and precision requirements of timers, the Linux kernel has redesigned a software architecture for high-precision timers, which can provide us with nanosecond-level timer precision to meet our development needs. How to measure this precision will be revealed…

Writing Linux High-Precision Timer Drivers

  • To verify the resolution of the high-precision timer, we will write a simple kernel driver (function: reverse IO within a set cycle and measure precision using an oscilloscope).

  • Steps to use the high-precision timer:

  1. Initialize the timer working mode: hrtimer_init(&kthread_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2. Set the timer’s callback function: kthread_timer.function = hrtimer_cb_func;
  3. Start the timer: hrtimer_start(&kthread_timer, ktime_set(HRTIMER_TEST_CYCLE), HRTIMER_MODE_REL);
  4. In the timer callback function, increase the timer expiration time: hrtimer_forward(timer, timer->base->get_time(), ktime_set(HRTIMER_TEST_CYCLE));
  • Implementation of the kernel driver module code:
#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 "Register 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");
  • The main function of this driver module: periodically reverse GPIO in the timer callback function and then check its timer precision. The macro definition (HRTIMER_TEST_CYCLE) defines the timer cycle.

Evaluation of Linux High-Precision Timers

  1. Evaluation with a 1ms cycle:
  • Modify the macro definition: HRTIMER_TEST_CYCLE to set the cycle to 1ms. The modification is as follows:
#define HRTIMER_TEST_CYCLE   0, (1000000 / 2)
  • Measured with a Tektronix oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Measured with a DS100 Mini digital oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Conclusion: The data from the Tektronix oscilloscope and the DS100 Mini digital oscilloscope are consistent, and the waveforms are stable. The frequency and cycle statistics match the software settings.
  1. Evaluation with a 100us cycle:
  • Modify the macro definition: HRTIMER_TEST_CYCLE to set the cycle to 100us. The modification is as follows:
#define HRTIMER_TEST_CYCLE   0, (100000 / 2)
  • Measured with a Tektronix oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Measured with a DS100 Mini digital oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Conclusion: The data from the Tektronix oscilloscope and the DS100 Mini digital oscilloscope are consistent, and the waveforms are stable. The frequency and cycle statistics match the software settings.
  1. Evaluation with a 10us cycle:
  • Modify the macro definition: HRTIMER_TEST_CYCLE to set the cycle to 10us. The modification is as follows:
#define HRTIMER_TEST_CYCLE   0, (10000 / 2)
  • Measured with a Tektronix oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Measured with a DS100 Mini digital oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Conclusion: The data from both the Tektronix oscilloscope and the DS100 Mini digital oscilloscope could not be accurately measured, and the waveforms were unclear.
  1. Evaluation with a 1us cycle:
  • Modify the macro definition: HRTIMER_TEST_CYCLE to set the cycle to 1us. The modification is as follows:
#define HRTIMER_TEST_CYCLE   0, (1000 / 2)
  • Measured with a Tektronix oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Measured with a DS100 Mini digital oscilloscope:
Measuring the Precision of High-Precision Timers in Linux Driver Development
  • Conclusion: The data from both the Tektronix oscilloscope and the DS100 Mini digital oscilloscope could not be accurately measured, and the waveforms were unclear.

Conclusion

  • Summary of High-Precision Timers
  1. The high-precision timer provided by Linux can meet most of our needs. It is important to note that the timer callback function should not perform too many tasks and needs to execute quickly; otherwise, periodicity cannot be guaranteed. (The author believes that high-precision timers can be treated as an external interrupt concept for processing.)
  2. Through this evaluation, the high-precision timer provided by Linux can meet my project requirements, and it is recommended for use in scenarios requiring tens of nanoseconds; however, it is not suitable for requirements in the few nanoseconds range.
  • Summary of Oscilloscopes
  1. Based on the evaluation data, the DS100 Mini digital oscilloscope can replace a general desktop oscilloscope.
  2. The DS100 Mini digital oscilloscope can be used in most scenarios and can meet project requirements.

Measuring the Precision of High-Precision Timers in Linux Driver Development

END

Source:Rice Embedded

Copyright belongs to the original author. If there is any infringement, please contact for deletion..Recommended ReadingSupply Cut! Huawei no longer has Windows availableThe company using pirated AD software has received a lawyer’s letter…Step-by-step guide to writing embedded driver programs (based on timing diagrams)→ Follow for more updates ←

Leave a Comment