|
PebbleOS
|
Waiting for a minimum amount of time, sleeping or spinning. More...
Macros | |
| #define | PBL_POLL_BUSY_US(cond, delay_us, timeout_us) |
| Spin until a condition holds or a timeout expires. | |
| #define | PBL_POLL_SLEEP_MS(cond, sleep_ms, timeout_ms) |
| Sleep until a condition holds or a timeout expires. | |
Functions | |
| void | pbl_busy_wait_us (uint32_t us) |
| Spin for at least a number of microseconds. | |
| void | pbl_sleep_ms (uint32_t ms) |
| Sleep for at least a number of milliseconds. | |
| void | pbl_sleep_us (uint32_t us) |
| Sleep for at least a number of microseconds. | |
Waiting for a minimum amount of time, sleeping or spinning.
Every call waits at least the requested time. Sleeping blocks the calling thread with tick resolution, so other threads run and the CPU may enter low power; it is only allowed from a thread, with the scheduler running and unlocked. Busy waiting spins with sub-microsecond resolution and works anywhere, including ISRs and before the scheduler starts, but burns power and starves lower priority threads: keep it to a few hundred microseconds.
| Need | Use |
|---|---|
| A few microseconds, or no thread | pbl_busy_wait_us() |
| Microseconds, being late is fine | pbl_sleep_us() |
| Milliseconds and longer | pbl_sleep_ms() |
| A timeout or a deadline | pbl_thread_sleep() |
| A condition, spinning | PBL_POLL_BUSY_US() |
| A condition, sleeping | PBL_POLL_SLEEP_MS() |
| #define PBL_POLL_BUSY_US | ( | cond, | |
| delay_us, | |||
| timeout_us | |||
| ) |
Spin until a condition holds or a timeout expires.
Evaluates cond, then every delay_us of busy waiting, timing out on the cycle counter. cond is evaluated once more after the timeout expires, so a slow evaluation or an interrupt cannot cause a false timeout. Usable wherever pbl_busy_wait_us() is.
| cond | Condition, evaluated repeatedly. |
| delay_us | Microseconds to spin between evaluations, 0 to re-evaluate immediately. |
| timeout_us | Microseconds to give up after; at most one second. |
cond holds, -ETIMEDOUT otherwise. | #define PBL_POLL_SLEEP_MS | ( | cond, | |
| sleep_ms, | |||
| timeout_ms | |||
| ) |
Sleep until a condition holds or a timeout expires.
Evaluates cond, then after every pbl_sleep_ms() of sleep_ms, timing out on the uptime. cond is evaluated once more after the timeout expires. Thread context only.
| cond | Condition, evaluated repeatedly. |
| sleep_ms | Milliseconds to sleep between evaluations, 0 to yield. |
| timeout_ms | Milliseconds to give up after. |
cond holds, -ETIMEDOUT otherwise. | void pbl_busy_wait_us | ( | uint32_t | us | ) |
Spin for at least a number of microseconds.
Counts CPU cycles at the CPU clock read on entry; changing the CPU clock meanwhile skews it. Interrupts and preemption are not masked and add to the delay. Privileged only.
| us | Microseconds to wait. |
| void pbl_sleep_ms | ( | uint32_t | ms | ) |
Sleep for at least a number of milliseconds.
Rounds up to whole ticks and adds one for the partial tick in progress, so the thread may sleep up to a tick longer than asked, plus scheduling latency.
| ms | Milliseconds to sleep; 0 yields instead. |
| void pbl_sleep_us | ( | uint32_t | us | ) |
Sleep for at least a number of microseconds.
Like pbl_sleep_ms(): the resolution is one tick, so any non-zero value sleeps at least a tick.
| us | Microseconds to sleep; 0 yields instead. |