Why udelay and ndelay is not accurate in linux kernel?

前端 未结 3 1246
萌比男神i
萌比男神i 2021-01-15 05:57

I make a function like this

trace_printk(\"111111\");
udelay(4000);
trace_printk(\"222222\");

and the log shows it\'s 4.01 ms , it\'OK

3条回答
  •  攒了一身酷
    2021-01-15 06:31

    You can use ktime_get_ns() to get high precision time since boot. So you can use it not only as high precision delay but also as high precision timer. There is example:

    u64 t;
    t = ktime_get_ns(); // Get current nanoseconds since boot
    for (i = 0; i < 24; i++) // Send 24 1200ns-1300ns pulses via GPIO
    {
        gpio_set_value(pin, 1); // Drive GPIO or do something else
        t += 1200; // Now we have absolute time of the next step
        while (ktime_get_ns() < t); // Wait for it
        gpio_set_value(pin, 0); // Do something, again
        t += 1300; // Now we have time of the next step, again
        while (ktime_get_ns() < t);  // Wait for it, again
    }
    

提交回复
热议问题