modm API documentation
|
void | modm::delay (std::chrono::nanoseconds ns) |
void | modm::delay_ns (uint32_t ns) |
void | modm::delay (std::chrono::microseconds us) |
void | modm::delay_us (uint32_t us) |
void | modm::delay (std::chrono::milliseconds ms) |
Spin for milliseconds. | |
void | modm::delay_ms (uint32_t ms) |
lbuild module: modm:architecture:delay
These functions allow you to spin for a short time using only the CPU.
In general it is recommended to use the std::chrono
duration types to allow the compiler to choose the optimal implementation for you:
In your code you can also use the std::chrono
literals:
In order to not require wild casting around of values, there are also three overloads for (unsigned) integer values. These are particularly useful for when you do not want to or cannot use chrono literals.
Note that these delay functions work at any CPU clock speed, even if changed dynamically at runtime and are available very early in the startup process at hardware-init time.
The main limitations are accuracy and length of delay. The only guarantee given to you is to delay for at least the specified time. Note that invocation of interrupts during spinning may add delay too.
modm::delay(ns)
guarantees at most 1'000'000ns = 1ms delay.modm::delay(us)
guarantees at most 1'000'000µs = 1s delay.modm::delay(ms)
is limited to 49 days.Please note that the delay these functions provide is defined as the time from invocation to the time of execution return. Obviously no delay beyond that is considered, which may require you to use shorter delays to compensate for the overhead of your code:
You should always prefer Software Timers (see modm:processing:timer
) over these blocking delay functions. However, when modm::Clock
is not set up yet, or when you need very small delays (for example to bit-bang a protocol), you need to use these delay functions.
For the technical details on the delay implementations you can read "Accurate Micro- and Nanosecond Delay in modm".
void modm::delay | ( | std::chrono::microseconds | us | ) |
Spin for microseconds.
void modm::delay | ( | std::chrono::nanoseconds | ns | ) |
Spin for nanoseconds.