|
Zephyr Project API 4.4.99
A Scalable Open Source RTOS
|
System Clock APIs. More...
Topics | |
| System Timer Low-power Companion Interface | |
| System timer low-power companion interface. | |
Data Structures | |
| struct | k_timeout_t |
| Kernel timeout type. More... | |
| struct | k_timepoint_t |
| Kernel timepoint type. More... | |
Macros | |
| #define | K_NO_WAIT Z_TIMEOUT_NO_WAIT |
| Generate null timeout delay. | |
| #define | K_NSEC(t) |
| Generate timeout delay from nanoseconds. | |
| #define | K_USEC(t) |
| Generate timeout delay from microseconds. | |
| #define | K_CYC(t) |
| Generate timeout delay from cycles. | |
| #define | K_TICKS(t) |
| Generate timeout delay from system ticks. | |
| #define | K_MSEC(ms) |
| Generate timeout delay from milliseconds. | |
| #define | K_SECONDS(s) |
| Generate timeout delay from seconds. | |
| #define | K_MINUTES(m) |
| Generate timeout delay from minutes. | |
| #define | K_HOURS(h) |
| Generate timeout delay from hours. | |
| #define | K_FOREVER Z_FOREVER |
| Generate infinite timeout delay. | |
| #define | K_TIMEOUT_SUM(timeout1, timeout2) |
| Add two k_timeout_t values together. | |
| #define | K_TICKS_FOREVER ((k_ticks_t)(-1)) |
| #define | K_TIMEOUT_EQ(a, b) |
| Compare timeouts for equality. | |
| #define | NSEC_PER_USEC 1000U |
| number of nanoseconds per microsecond | |
| #define | NSEC_PER_MSEC 1000000U |
| number of nanoseconds per millisecond | |
| #define | USEC_PER_MSEC 1000U |
| number of microseconds per millisecond | |
| #define | MSEC_PER_SEC 1000U |
| number of milliseconds per second | |
| #define | SEC_PER_MIN 60U |
| number of seconds per minute | |
| #define | SEC_PER_HOUR 3600U |
| number of seconds per hour | |
| #define | SEC_PER_DAY 86400U |
| number of seconds per day | |
| #define | MIN_PER_HOUR 60U |
| number of minutes per hour | |
| #define | HOUR_PER_DAY 24U |
| number of hours per day | |
| #define | USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC)) |
| number of microseconds per second | |
| #define | NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC)) |
| number of nanoseconds per second | |
| #define | SYS_CLOCK_HW_CYCLES_TO_NS_AVG(X, NCYCLES) |
| SYS_CLOCK_HW_CYCLES_TO_NS_AVG converts CPU clock cycles to nanoseconds and calculates the average cycle time. | |
| #define | SYS_CLOCK_REALTIME 1 |
| The real-time clock (i.e. | |
| #define | SYS_CLOCK_MONOTONIC 4 |
| The monotonic clock. | |
| #define | SYS_TIMER_ABSTIME 4 |
| The flag used for specifying absolute timeouts. | |
Typedefs | |
| typedef uint32_t | k_ticks_t |
| Tick precision used in timeout APIs. | |
Functions | |
| k_spinlock_key_t | sys_clock_lock (void) |
| Lock the system clock. | |
| void | sys_clock_unlock (k_spinlock_key_t key) |
| Unlock the system clock. | |
| void | sys_clock_set_timeout (uint32_t ticks, bool idle) |
| Set system clock timeout. | |
| void | sys_clock_idle_exit (void) |
| Timer idle exit notification. | |
| void | sys_clock_announce_locked (uint32_t ticks, k_spinlock_key_t key) |
| Announce time progress to the kernel. | |
| static void | sys_clock_announce (uint32_t ticks) |
| Announce time progress to the kernel (legacy wrapper). | |
| uint32_t | sys_clock_elapsed (void) |
| Ticks elapsed since last sys_clock_announce() call. | |
| void | sys_clock_disable (void) |
| Disable system timer. | |
| void | sys_clock_no_timeout (void) |
| Notify the timer driver that no timeout is pending. | |
| void | sys_clock_idle_enter (uint32_t ticks) |
| Notify the timer driver that the CPU is entering low-power idle. | |
| uint32_t | sys_clock_cycle_get_32 (void) |
| Hardware cycle counter. | |
| uint64_t | sys_clock_cycle_get_64 (void) |
| 64 bit hardware cycle counter | |
| int64_t | k_uptime_ticks (void) |
| Get system uptime, in system ticks. | |
| static int64_t | k_uptime_get (void) |
| Get system uptime. | |
| static uint32_t | k_uptime_get_32 (void) |
| Get system uptime (32-bit version). | |
| static uint32_t | k_uptime_seconds (void) |
| Get system uptime in seconds. | |
| static int64_t | k_uptime_delta (int64_t *reftime) |
| Get elapsed time, and update the referenced time. | |
| static uint32_t | k_cycle_get_32 (void) |
| Read the hardware clock. | |
| static uint64_t | k_cycle_get_64 (void) |
| Read the 64-bit hardware clock. | |
| uint32_t | sys_clock_tick_get_32 (void) |
| Return the lower part of the current system tick count. | |
| int64_t | sys_clock_tick_get (void) |
| Return the current system tick count. | |
| k_timepoint_t | sys_timepoint_calc (k_timeout_t timeout) |
| Calculate a timepoint value. | |
| k_timeout_t | sys_timepoint_timeout (k_timepoint_t timepoint) |
| Remaining time to given timepoint. | |
| static int | sys_timepoint_cmp (k_timepoint_t a, k_timepoint_t b) |
| Compare two timepoint values. | |
| static bool | sys_timepoint_expired (k_timepoint_t timepoint) |
| Indicates if timepoint is expired. | |
| void | sys_clock_getrtoffset (struct timespec *tp) |
| Get the offset SYS_CLOCK_REALTIME with respect to SYS_CLOCK_MONOTONIC. | |
| int | sys_clock_gettime (int clock_id, struct timespec *tp) |
| Get the current time from the specified clock. | |
| int | sys_clock_settime (int clock_id, const struct timespec *tp) |
| Set the current time for the specified clock. | |
| int | sys_clock_nanosleep (int clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp) |
| Sleep for the specified amount of time with respect to the specified clock. | |
System Clock APIs.
| #define HOUR_PER_DAY 24U |
#include <clock.h>
number of hours per day
| #define K_CYC | ( | t | ) |
#include <kernel.h>
Generate timeout delay from cycles.
This macro generates a timeout delay that instructs a kernel API to wait up to t cycles to perform the requested operation.
| t | Duration in cycles. |
| #define K_FOREVER Z_FOREVER |
#include <kernel.h>
Generate infinite timeout delay.
This macro generates a timeout delay that instructs a kernel API to wait as long as necessary to perform the requested operation.
| #define K_HOURS | ( | h | ) |
| #define K_MINUTES | ( | m | ) |
| #define K_MSEC | ( | ms | ) |
#include <kernel.h>
Generate timeout delay from milliseconds.
This macro generates a timeout delay that instructs a kernel API to wait up to ms milliseconds to perform the requested operation.
| ms | Duration in milliseconds. |
| #define K_NO_WAIT Z_TIMEOUT_NO_WAIT |
#include <kernel.h>
Generate null timeout delay.
This macro generates a timeout delay that instructs a kernel API not to wait if the requested operation cannot be performed immediately.
| #define K_NSEC | ( | t | ) |
#include <kernel.h>
Generate timeout delay from nanoseconds.
This macro generates a timeout delay that instructs a kernel API to wait up to t nanoseconds to perform the requested operation. Note that timer precision is limited to the tick rate, not the requested value.
| t | Duration in nanoseconds. |
| #define K_SECONDS | ( | s | ) |
#include <kernel.h>
Generate timeout delay from seconds.
This macro generates a timeout delay that instructs a kernel API to wait up to s seconds to perform the requested operation.
| s | Duration in seconds. |
| #define K_TICKS | ( | t | ) |
#include <kernel.h>
Generate timeout delay from system ticks.
This macro generates a timeout delay that instructs a kernel API to wait up to t ticks to perform the requested operation.
| t | Duration in system ticks. |
| #define K_TIMEOUT_EQ | ( | a, | |
| b ) |
#include <clock.h>
Compare timeouts for equality.
The k_timeout_t object is an opaque struct that should not be inspected by application code. This macro exists so that users can test timeout objects for equality with known constants (e.g. K_NO_WAIT and K_FOREVER) when implementing their own APIs in terms of Zephyr timeout constants.
| #define K_TIMEOUT_SUM | ( | timeout1, | |
| timeout2 ) |
#include <kernel.h>
Add two k_timeout_t values together.
This macro adds two k_timeout_t values together. If only one value is an absolute timeout, the result will be an absolute timeout. If both are relative timeouts, the result will be a relative timeout. If the calculation overflows, underflows or if both values are absolute timeouts, K_FOREVER is returned.
| timeout1 | First k_timeout_t value |
| timeout2 | Second k_timeout_t value |
| #define K_USEC | ( | t | ) |
#include <kernel.h>
Generate timeout delay from microseconds.
This macro generates a timeout delay that instructs a kernel API to wait up to t microseconds to perform the requested operation. Note that timer precision is limited to the tick rate, not the requested value.
| t | Duration in microseconds. |
| #define MIN_PER_HOUR 60U |
#include <clock.h>
number of minutes per hour
| #define MSEC_PER_SEC 1000U |
#include <clock.h>
number of milliseconds per second
| #define NSEC_PER_MSEC 1000000U |
#include <clock.h>
number of nanoseconds per millisecond
| #define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC)) |
#include <clock.h>
number of nanoseconds per second
| #define NSEC_PER_USEC 1000U |
#include <clock.h>
number of nanoseconds per microsecond
| #define SEC_PER_DAY 86400U |
#include <clock.h>
number of seconds per day
| #define SEC_PER_HOUR 3600U |
#include <clock.h>
number of seconds per hour
| #define SEC_PER_MIN 60U |
#include <clock.h>
number of seconds per minute
| #define SYS_CLOCK_HW_CYCLES_TO_NS_AVG | ( | X, | |
| NCYCLES ) |
#include <clock.h>
SYS_CLOCK_HW_CYCLES_TO_NS_AVG converts CPU clock cycles to nanoseconds and calculates the average cycle time.
| #define SYS_CLOCK_MONOTONIC 4 |
#include <clock.h>
The monotonic clock.
This steady clock is used to measure time since the system booted. Time from this clock is always monotonically increasing.
| #define SYS_CLOCK_REALTIME 1 |
#include <clock.h>
The real-time clock (i.e.
"wall clock")
This clock is used to measure time since the epoch (1970-01-01 00:00:00 UTC).
It is not a steady clock; i.e. it may be adjusted for a number of reasons from initialization of a hardware real-time-clock, to network-time synchronization, to manual adjustment from the application.
| #define SYS_TIMER_ABSTIME 4 |
#include <clock.h>
The flag used for specifying absolute timeouts.
This flag may be passed to sys_clock_nanosleep to indicate the requested timeout is an absolute time with respect to the specified clock.
| #define USEC_PER_MSEC 1000U |
#include <clock.h>
number of microseconds per millisecond
| #define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC)) |
#include <clock.h>
number of microseconds per second
#include <clock.h>
Tick precision used in timeout APIs.
This type defines the word size of the timeout values used in k_timeout_t objects, and thus defines an upper bound on maximum timeout length (or equivalently minimum tick duration). Note that this does not affect the size of the system uptime counter, which is always a 64 bit count of ticks.
|
inlinestatic |
#include <kernel.h>
Read the hardware clock.
This routine returns the current time, as measured by the system's hardware clock.
|
inlinestatic |
#include <kernel.h>
Read the 64-bit hardware clock.
This routine returns the current time in 64-bits, as measured by the system's hardware clock, if available.
CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER. When that option is not enabled this routine asserts and returns 0.
#include <kernel.h>
Get elapsed time, and update the referenced time.
This routine computes the elapsed time between the current system uptime and an earlier reference time, in milliseconds.
| reftime | Pointer to a reference time, which is updated to the current uptime upon return. |
|
inlinestatic |
#include <kernel.h>
Get system uptime.
This routine returns the elapsed time since the system booted, in milliseconds.
CONFIG_SYS_CLOCK_TICKS_PER_SECconfig option.
|
inlinestatic |
#include <kernel.h>
Get system uptime (32-bit version).
This routine returns the lower 32 bits of the system uptime in milliseconds.
Because correct conversion requires full precision of the system clock there is no benefit to using this over k_uptime_get() unless you know the application will never run long enough for the system clock to approach 2^32 ticks. Calls to this function may involve interrupt blocking and 64-bit math.
CONFIG_SYS_CLOCK_TICKS_PER_SECconfig option
|
inlinestatic |
#include <kernel.h>
Get system uptime in seconds.
This routine returns the elapsed time since the system booted, in seconds.
| int64_t k_uptime_ticks | ( | void | ) |
#include <kernel.h>
Get system uptime, in system ticks.
This routine returns the elapsed time since the system booted, in ticks (c.f.
CONFIG_SYS_CLOCK_TICKS_PER_SEC
), which is the fundamental unit of resolution of kernel timekeeping.
|
inlinestatic |
#include <system_timer.h>
Announce time progress to the kernel (legacy wrapper).
Convenience wrapper around sys_clock_announce_locked that acquires the system clock lock internally. New drivers should prefer sys_clock_lock() + sys_clock_announce_locked() to protect driver state and tick accounting under a single lock.
| ticks | Elapsed time, in ticks |
| void sys_clock_announce_locked | ( | uint32_t | ticks, |
| k_spinlock_key_t | key ) |
#include <system_timer.h>
Announce time progress to the kernel.
Informs the kernel that the specified number of ticks have elapsed since the last call to sys_clock_announce() (or system startup for the first call). The timer driver is expected to deliver these announcements as close as practical (subject to hardware and latency limitations) to tick boundaries.
The caller must already hold the system clock lock obtained via sys_clock_lock(). The key is consumed: the lock is released when this function returns.
This is the preferred interface for timer ISRs that need to update driver-internal state (e.g. cycle counter baseline) atomically with the kernel tick accounting. See sys_clock_lock() for example usage.
| ticks | Elapsed time, in ticks |
| key | Lock key obtained from sys_clock_lock(). |
| uint32_t sys_clock_cycle_get_32 | ( | void | ) |
#include <system_timer.h>
Hardware cycle counter.
Timer drivers are generally responsible for the system cycle counter as well as the tick announcements. This function is generally called out of the architecture layer (
| uint64_t sys_clock_cycle_get_64 | ( | void | ) |
#include <system_timer.h>
64 bit hardware cycle counter
As for sys_clock_cycle_get_32(), but with a 64 bit return value. Not all hardware has 64 bit counters. This function need be implemented only if CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER is set.
| void sys_clock_disable | ( | void | ) |
#include <system_timer.h>
Disable system timer.
CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORTcan be used to check if the system timer has the capability of being disabled.
| uint32_t sys_clock_elapsed | ( | void | ) |
#include <system_timer.h>
Ticks elapsed since last sys_clock_announce() call.
Queries the clock driver for the current time elapsed since the last call to sys_clock_announce() was made. The kernel will call this with appropriate locking, the driver needs only provide an instantaneous answer.
| void sys_clock_getrtoffset | ( | struct timespec * | tp | ) |
#include <clock.h>
Get the offset SYS_CLOCK_REALTIME with respect to SYS_CLOCK_MONOTONIC.
The "wall clock" (i.e. SYS_CLOCK_REALTIME) depends on a base time that is set by the system. The base time may be updated for a number of reasons, such as initialization of a hardware real-time-clock (RTC), network time protocol (NTP) synchronization, or manual adjustment by the application.
This function retrieves the current time offset, as a timespec object, for SYS_CLOCK_REALTIME, with respect to SYS_CLOCK_MONOTONIC, and writes it to the provided memory location pointed-to by tp.
| tp | Pointer to memory where time will be written. |
| int sys_clock_gettime | ( | int | clock_id, |
| struct timespec * | tp ) |
#include <clock.h>
Get the current time from the specified clock.
| clock_id | The clock from which to query time. |
| tp | Pointer to memory where time will be written. |
| 0 | on success. |
| -EINVAL | when an invalid clock_id is specified. |
| void sys_clock_idle_enter | ( | uint32_t | ticks | ) |
#include <system_timer.h>
Notify the timer driver that the CPU is entering low-power idle.
Called from the power-management idle path when the CPU is about to sleep. A driver that hands off to a low-power wakeup timer, or otherwise reconfigures itself for sleep, does so here. sys_clock_idle_exit() undoes it on the way out. A driver with no low-power handling does not need to implement this hook.
The hook is optional. Without it, sys_clock_set_timeout() is called with its deprecated idle argument set to true, which keeps a driver keying its low-power handling on that argument working. It goes with the argument, by which time a platform using the power management facility is expected to have implemented sys_clock_idle_enter() itself.
| ticks | Ticks until the next expected wakeup, or SYS_CLOCK_IDLE_FOREVER when nothing needs to wake the CPU and the uptime accounting is allowed to drift, in which case the driver may stop its time base. Only the calling CPU is going idle: a driver whose time base is shared between CPUs must ensure only the last CPU going idle stops the clock. |
| void sys_clock_idle_exit | ( | void | ) |
#include <system_timer.h>
Timer idle exit notification.
This notifies the timer driver that the system is exiting the idle and allows it to do whatever bookkeeping is needed to restore timer operation and compute elapsed ticks.
| k_spinlock_key_t sys_clock_lock | ( | void | ) |
#include <system_timer.h>
Lock the system clock.
Acquires the kernel timer lock that protects tick accounting and the timeout queue. Timer drivers should call this at the start of their ISR and pass the returned key to sys_clock_announce_locked() which consumes it. The lock is released when sys_clock_announce_locked() returns.
The driver-provided functions sys_clock_set_timeout() and sys_clock_elapsed() are always called by the kernel with this lock already held.
Example usage from a timer ISR:
| int sys_clock_nanosleep | ( | int | clock_id, |
| int | flags, | ||
| const struct timespec * | rqtp, | ||
| struct timespec * | rmtp ) |
#include <clock.h>
Sleep for the specified amount of time with respect to the specified clock.
This function will cause the calling thread to sleep either
The accepted values for clock_id include
If rmtp is not NULL, and the thread is awoken prior to the time specified by rqtp, then any remaining time will be written to rmtp. If the thread has slept for at least the time specified by rqtp, then rmtp will be set to zero.
| clock_id | The clock to by which to sleep. |
| flags | Flags to modify the behavior of the sleep operation. |
| rqtp | Pointer to the requested time to sleep. |
| rmtp | Pointer to memory into which to copy the remaining time, if any. |
| 0 | on success. |
| -EINVAL | when an invalid clock_id, when rqtp contains nanoseconds outside of the range [0, 999999999], or when rqtp contains a negative value. |
| void sys_clock_no_timeout | ( | void | ) |
#include <system_timer.h>
Notify the timer driver that no timeout is pending.
Called by the kernel in place of sys_clock_set_timeout() when the timeout queue is empty and
CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE
is enabled. No tick announcement is forthcoming and the system does not care about precise uptime keeping, so the driver may do something to save resources: mask the wakeup, program the longest interval the hardware can hold, or both. Normal operation resumes at the next sys_clock_set_timeout().
The CPU keeps running, so sys_clock_cycle_get_32() and sys_clock_cycle_get_64() must keep counting: a thread can still call k_cycle_get_32() or k_busy_wait(). A driver whose cycle counter is driven by the timer it would stop can therefore only mask the interrupt. One whose cycle counter lives in a different clock or power domain may stop more. Stopping the time base belongs in sys_clock_idle_enter().
Unlike sys_clock_disable(), this is not a teardown.
The hook is optional. Without it, sys_clock_set_timeout() is asked for the longest wait it can express, UINT32_MAX ticks. That is numerically what K_TICKS_FOREVER was here, so a driver that has not migrated and still keys on that value stops its clock as it always did.
#include <system_timer.h>
Set system clock timeout.
Informs the system clock driver that the next needed call to sys_clock_announce() will not be until the specified number of ticks from the current time have elapsed. Note that spurious calls to sys_clock_announce() are allowed (i.e. it's legal to announce every tick and implement this function as a noop), the requirement is that one tick announcement should occur within one tick BEFORE the specified expiration (that is, passing ticks==1 means "announce the next tick", this convention was chosen to match legacy usage). Similarly a ticks value of zero is legal: it simply indicates the kernel would like the next tick announcement as soon as possible.
No tick count carries a meaning of its own. The driver arms what it is asked for, clamping to what its hardware can hold and announcing the ticks that did elapse.
The two conditions a driver used to infer from the tick value now have their own entry points: sys_clock_no_timeout() for "no timeout is pending" and sys_clock_idle_enter() for "the CPU is going to sleep".
A final note about SMP: note that the call to sys_clock_set_timeout() is made on any CPU, and reflects the next timeout desired globally. The resulting calls(s) to sys_clock_announce() must be properly serialized by the driver such that a given tick is announced exactly once across the system. The kernel does not (cannot, really) attempt to serialize things by "assigning" timeouts to specific CPUs.
| ticks | Timeout in tick units |
| idle | Deprecated, and always false when the kernel calls this function: idle entry is notified through sys_clock_idle_enter(), whose fallback passes true here. Scheduled for removal in a future release; new code must ignore it. |
| int sys_clock_settime | ( | int | clock_id, |
| const struct timespec * | tp ) |
#include <clock.h>
Set the current time for the specified clock.
| clock_id | The clock for which the time should be set. |
| tp | Pointer to memory specifying the desired time. |
| 0 | on success. |
| -EINVAL | when an invalid clock_id is specified or when tp contains nanoseconds outside of the range [0, 999999999]. |
| int64_t sys_clock_tick_get | ( | void | ) |
| uint32_t sys_clock_tick_get_32 | ( | void | ) |
#include <clock.h>
Return the lower part of the current system tick count.
| void sys_clock_unlock | ( | k_spinlock_key_t | key | ) |
#include <system_timer.h>
Unlock the system clock.
Releases the kernel timer lock previously acquired with sys_clock_lock(). Provided for drivers with special needs; most drivers should use sys_clock_announce_locked() which handles unlocking automatically.
| key | Lock key returned by sys_clock_lock(). |
| k_timepoint_t sys_timepoint_calc | ( | k_timeout_t | timeout | ) |
#include <clock.h>
Calculate a timepoint value.
Returns a timepoint corresponding to the expiration (relative to an unlocked "now"!) of a timeout object. When used correctly, this should be called once, synchronously with the user passing a new timeout value. It should not be used iteratively to adjust a timeout (see sys_timepoint_timeout() for that purpose).
|
inlinestatic |
#include <clock.h>
Compare two timepoint values.
This function is used to compare two timepoint values.
| a | Timepoint to compare |
| b | Timepoint to compare against. |
|
inlinestatic |
#include <clock.h>
Indicates if timepoint is expired.
| timepoint | Timepoint to evaluate |
| true | if the timepoint is in the past, false otherwise |
| k_timeout_t sys_timepoint_timeout | ( | k_timepoint_t | timepoint | ) |
#include <clock.h>
Remaining time to given timepoint.
Returns the timeout interval between current time and provided timepoint. If the timepoint is now in the past or if it was created with K_NO_WAIT then K_NO_WAIT is returned. If it was created with K_FOREVER then K_FOREVER is returned.
| timepoint | Timepoint for which a timeout value is wanted. |