search
HomeSystem TutorialLINUXLinux device driver timing and delay: a convenient method to implement time-related functions

Linux device driver timing and delay: a convenient method to implement time-related functions

Feb 13, 2024 pm 02:45 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

Have you ever wondered how to write drivers for your devices in Linux? Have you ever thought about how to enable your driver to implement some time-related functions in a Linux system, such as timing, delay, timeout, etc.? If you are interested in these issues, then this article will introduce you to an effective method to achieve these goals-Linux device driver timing and delay.

Linux device driver timing and delay: a convenient method to implement time-related functions

Linux generates timer interrupts at regular intervals (measured by HZ) through the system hardware timer. Each interrupt causes the value jiffies of a kernel counter to accumulate, so this jiffies records the time elapsed since the system started, and then the kernel Based on this, software timers and delays are implemented.

Demo for jiffies and HZ

    #include 

    unsigned long j, stamp_1, stamp_half, stamp_n;

    j = jiffies; /* read the current value */
    stamp_1 = j + HZ; /* 1 second in the future */
    stamp_half = j + HZ/2; /* half a second */
    stamp_n = j + n * HZ / 1000; /* n milliseconds */

Kernel timer

The hardware clock interrupt handler will raise the TIMER_SOFTIRQ soft interrupt and run all core timers that have expired on the current processor.

Timer definition/initialization

In the Linux kernel, an instance of the timer_list structure corresponds to a timer:

    /* 当expires指定的定时器到期时间期满后,将执行function(data) */
    struct timer_list {
        unsigned long expires;           /*定时器到期时间*/
        void (*function)(unsigned long); /* 定时器处理函数 */
        unsigned long data;              /* function的参数 */
        ...
    };

    /* 定义 */
    struct timer_list my_timer;

    /* 初始化函数 */
    void init_timer(struct timer_list * timer);
    /* 初始化宏 */
    TIMER_INITIALIZER(_function, _expires, _data)

    /* 定义并初始化宏 */
    DEFINE_TIMER(_name, _function, _expires, _data)

Timer Add/Remove

    /* 注册内核定时器,将定时器加入到内核动态定时器链表中 */
    void add_timer(struct timer_list * timer);

    /* del_timer_sync()是 del_timer()的同步版,在删除一个定时器时需等待其被处理完,
       因此该函数的调用不能发生在中断上下文 */
    void del_timer(struct timer_list * timer);
    void del_timer_sync(struct timer_list * timer);

Timing time modification

    int mod_timer(struct timer_list *timer, unsigned long expires);

Delay

Short delay

    void ndelay(unsigned long nsecs);
    void udelay(unsigned long usecs);
    void mdelay(unsigned long msecs);

When the kernel starts, it will run a delay test program (delay loop calibration) to calculate lpj (loops per jiffy). These functions are implemented based on lpj, which is busy waiting.

Long delay

  • A very intuitive way is to compare the current jiffies with the target jiffies:

    int time_after(unsigned long a, unsigned long b);    /* a after b, true */
    int time_before(unsigned long a, unsigned long b);   /* a before b */
    int time_after_eq(unsigned long a, unsigned long b); /* a after or equal b */
    int time_before_eq(unsigned long a, unsigned long b);/* a before or equal b */
    
  • Asleep delay

    void msleep(unsigned int millisecs);
    unsigned long msleep_interruptible(unsigned int millisecs);
    void ssleep(unsigned int seconds);
    

    Tip: msleep() and ssleep() cannot be interrupted.

Through this article, we learned about the application and role of timing and delay in Linux device drivers, and learned how to use various timers and delay functions. We found that timing and delay are a very suitable method for the development of embedded systems, which allows us to easily implement time-related functions. Of course, timing and delay also have some precautions and limitations, such as accuracy issues and performance impacts. Therefore, when using timing and delay, we need to have certain hardware knowledge and experience, as well as good programming habits and debugging skills. I hope this article can provide you with a simple and useful guide, giving you a preliminary understanding of timing and delay.

The above is the detailed content of Linux device driver timing and delay: a convenient method to implement time-related functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How to Use 'next' Command with Awk in Linux - Part 6How to Use 'next' Command with Awk in Linux - Part 6May 15, 2025 am 10:43 AM

In this sixth installment of our Awk series, we will explore the next command, which is instrumental in enhancing the efficiency of your script executions by skipping redundant processing steps.What is the next Command?The next command in awk instruc

How to Efficiently Transfer Files in LinuxHow to Efficiently Transfer Files in LinuxMay 15, 2025 am 10:42 AM

Transferring files in Linux systems is a common task that every system administrator should master, especially when it comes to network transmission between local or remote systems. Linux provides two commonly used tools to accomplish this task: SCP (Secure Replication) and Rsync. Both provide a safe and convenient way to transfer files between local or remote machines. This article will explain in detail how to use SCP and Rsync commands to transfer files, including local and remote file transfers. Understand the scp (Secure Copy Protocol) in Linux scp command is a command line program used to securely copy files and directories between two hosts via SSH (Secure Shell), which means that when files are transferred over the Internet, the number of

10 Most Popular Linux Desktop Environments of All Time10 Most Popular Linux Desktop Environments of All TimeMay 15, 2025 am 10:35 AM

One fascinating feature of Linux, in contrast to Windows and Mac OS X, is its support for a variety of desktop environments. This allows desktop users to select the most suitable and fitting desktop environment based on their computing requirements.A

How to Install LibreOffice 24.8 in Linux DesktopHow to Install LibreOffice 24.8 in Linux DesktopMay 15, 2025 am 10:15 AM

LibreOffice stands out as a robust and open-source office suite, tailored for Linux, Windows, and Mac platforms. It boasts an array of advanced features for handling word documents, spreadsheets, presentations, drawings, calculations, and mathematica

How to Work with PDF Files Using ONLYOFFICE Docs in LinuxHow to Work with PDF Files Using ONLYOFFICE Docs in LinuxMay 15, 2025 am 09:58 AM

Linux users who manage PDF files have a wide array of programs at their disposal. Specifically, there are numerous specialized PDF tools designed for various functions.For instance, you might opt to install a PDF viewer for reading files or a PDF edi

How to Filter Command Output Using Awk and STDINHow to Filter Command Output Using Awk and STDINMay 15, 2025 am 09:53 AM

In the earlier segments of the Awk command series, our focus was primarily on reading input from files. However, what if you need to read input from STDIN?In Part 7 of the Awk series, we will explore several examples where you can use the output of o

Clifm - Lightning-Fast Terminal File Manager for LinuxClifm - Lightning-Fast Terminal File Manager for LinuxMay 15, 2025 am 09:45 AM

Clifm stands out as a distinctive and incredibly swift command-line file manager, designed on the foundation of a shell-like interface. This means that users can engage with their file system using commands they are already familiar with.The choice o

How to Upgrade from Linux Mint 21.3 to Linux Mint 22How to Upgrade from Linux Mint 21.3 to Linux Mint 22May 15, 2025 am 09:44 AM

If you prefer not to perform a new installation of Linux Mint 22 Wilma, you have the option to upgrade from a previous version.In this guide, we will detail the process to upgrade from Linux Mint 21.3 (the most recent minor release of the 21.x series

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools