CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE lets the kernel skip the wakeups that
keep uptime accurate while no timeout is pending. No in-tree board
selects it and no test enables it, so the drivers implementing it have
never run. Several stop their time base outright, which freezes
k_cycle_get_32() under a running thread and can hang k_busy_wait(), and
some of those cannot start it again at all.
Mark the option experimental until those drivers have been audited.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Track the time spent in deep doze / sleep states on ITE EC SoCs
(IT8xxx2, IT51xxx) by capturing the 32.768 kHz free-run timer timestamp
before entering deep sleep and accumulating elapsed cycles upon waking.
Add ite_ec_clock_get_sleep_ticks() to convert total deep-sleep cycles
to kernel ticks.
Signed-off-by: Keith Short <keithshort@google.com>
Assisted-by: Antigravity:gemini-1.5-pro
Rename ite_ec_timer_block_idle() to ite_it8xxx2_timer_block_idle() to
reflect that this DMA clock synchronization idle check is specific to
the IT8XXX2 SoC family. Remove the unnecessary header guard around its
prototype in soc_timer.h.
Signed-off-by: Keith Short <keithshort@google.com>
Assisted-by: Antigravity:gemini-1.5-pro
The MCUX LPTMR system-timer driver previously defaulted to enabled
whenever the /chosen/zephyr,system-timer node was compatible with
nxp,lptmr. On SoCs whose devicetree points zephyr,system-timer at an
LPTMR instance (e.g. frdm_mcxw71/mcxw716c), this forced every build,
including non-power-management builds, to select the 32.768 kHz LPTMR
and disable the Cortex-M SysTick. Tests that rely on the high-resolution
SysTick clock (tests/kernel/timer/timer_api) then failed because the
1024 tick/s LPTMR does not provide the required timing resolution.
Restore the explicit per-SoC policy: the generic driver no longer
defaults MCUX_LPTMR_TIMER on, and each SoC opts in on its own terms.
MCXW7xx, KE1xF and MCXC enable the LPTMR only when power management is
enabled (default y if PM); i.MX95 and i.MX952 M7 enable it only when the
LPTMR is not claimed by the counter driver (default y if
!COUNTER_MCUX_LPTMR). This keeps SysTick as the system timer for plain
builds while preserving the low-power LPTMR configuration for PM builds.
Update the 4.5 migration guide accordingly.
Fixes#115491
Signed-off-by: Holt Sun <holt.sun@nxp.com>
When CONFIG_SYSTEM_TIMER_LPM_COMPANION_COUNTER is used to keep time across
a low-power state, sys_clock_idle_exit() reconstructs the elapsed time
and announces it to the kernel. Two intermediate values were computed
in 32 bits, which overflows once a single low-power sleep exceeds 2^32
core cycles (~134 s at 32 MHz):
- dcycles (the unannounced cycle delta) was uint32_t. Truncation
makes the announced tick count far too small, so k_sleep() never
completes and the MCU appears to never wake from STOP.
- announced_cycles += dticks * CYC_PER_TICK evaluated the product in
32 bits before adding to the 64-bit accumulator. Truncation under-counts
announced_cycles by a multiple of 2^32, so each subsequent sleep's
dcycles is too large and kernel time runs progressively fast.
Widen dcycles to 64 bits and cast the multiply to cycle_t so both
operations are performed in 64 bits. Only affects builds with a 64-bit
cycle counter (CONFIG_CORTEX_M_SYSTICK_64BIT_CYCLE_COUNTER) using the
counter companion; verified on STM32L151 (32 MHz core, LSE RTC companion)
with 5- and 9-minute STOP intervals.
Signed-off-by: Markus Grafen <markus.grafen@medicassist.de>
Assisted-by: Claude:claude-opus-4.8
MIN_DELAY is the floor of the SysTick LOAD register: the closest-in
timeout the driver will program. It must exceed the longest the SysTick
interrupt can stay masked, otherwise the 24-bit counter can wrap more than
once between elapsed() reads and lose a LOAD's worth of cycles. That is a
wall-clock property, but the default was a fixed cycle count:
MAX(1024, CYC_PER_TICK/16)
A fixed cycle count is a different wall-clock time on every clock. On a
32 kHz SysTick the 1024-cycle floor is ~31 ms (31 ticks), so the driver
could never schedule a timeout shorter than 31 ticks and every such board
had to override zephyr,min-timeout-cycles by hand.
Compute it instead as a fixed wall-clock budget scaled to the actual
frequency (min_delay = SYSTICK_MIN_DELAY_US * HW_CYCLES_PER_SEC), resolved
at init and refreshed on a runtime frequency change, so it also works when
the frequency is only known at runtime. At ~10 us this matches the old
1024 cycles near 100 MHz and scales sensibly elsewhere. The tick rate is
not involved; if the budget exceeds a tick on some clock, sub-tick timeouts
are simply unavailable there. A dts override still takes precedence.
Floor the result at two cycles: LOAD is programmed as (cycles - 1) and a
LOAD of zero stops the counter, so the smallest usable reload is two cycles
(LOAD 1). On a slow clock such as a 32 kHz SysTick the 10 us budget rounds
below that, so the hardware floor is what applies and the board still needs
no override.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
The default MCUX_OS_TIMER_MIN_DELAY of 1000 cycles is too large
relative to CYC_PER_TICK (100) on platforms clocked at 1 MHz with
SYS_CLOCK_TICKS_PER_SEC=10000. Because MIN_DELAY exceeds CYC_PER_TICK
by 10x, nearly every sys_clock_set_timeout() call adds an extra tick,
inflating short delays and causing the kernel timer_behavior test
(timer_ramp) to fail for low delay values.
Reduce the default to 50 cycles (50 us at 1 MHz), which provides
sufficient hardware setup margin for the match register without
interfering with normal tick scheduling.
Verified on frdm_mcxa344 and frdm_mcxa156 hardware:
- tests/kernel/timer/timer_behavior: all cases pass
Signed-off-by: Neil Chen <cheng.chen_1@nxp.com>
The val_out snapshot exported by elapsed() was written after the
wrap-realignment step, so callers received last_load instead of 0
whenever sample C happened to read VAL == 0.
sys_clock_set_timeout() feeds that snapshot into the drift
compensation as val1 and compares it against a later raw read of
SysTick->VAL as val2. Mixing a realigned operand with a raw one makes
val1 - val2 evaluate to a whole counter period that never elapsed, so
cycle_count jumps forward by up to one full timer period on every
reprogram that lands on a zero VAL sample.
The realignment of 0 to last_load is an internal convention of
elapsed(): it exists only so the wrap-detection below can assume
COUNTFLAG and wrapping occur on the same cycle. It has no meaning for
a caller that measures a window against its own raw register read.
Move the export to immediately after the three register reads so
val_out carries the raw sample, which is what the code did before
elapsed() grew the out-parameter. The out-parameter still removes the
gap between the window elapsed() accounts for and the window the
drift compensation measures.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Header file include/zephyr/sys_clock.h is deprecated and will be removed
someday. Update the whole file tree to include zephyr/sys/clock.h
straight instead of zephyr/sys_clock.h.
This change was made running the sed shell command below:
$ sed -i 's/zephyr\/sys_clock\.h/zephyr\/sys\/clock\.h/' \
`grep -rsl "zephyr/sys_clock\.h" drivers/timer`
Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
Refactor elapsed() to thread the SysTick->VAL snapshot out via an
optional pointer parameter instead of requiring an independent,
racy re-read or duplicating the wrap-detection logic.
An earlier fix for a VAL re-read race in timeout calculation unrolled
elapsed() inside sys_clock_set_timeout(). That unrolling duplicated
the complex wrap-detection logic across two places in the driver,
creating a maintenance hazard.
Resolve this by refactoring elapsed() to accept a uint32_t *val_out
parameter. When provided, it exports the exact val2 snapshot used
to calculate elapsed cycles. In sys_clock_set_timeout(), calling
elapsed(&val1) safely captures this snapshot to seed the drift
calculation, eliminating the timing gap with perfect precision and
no code duplication.
Signed-off-by: Surya Prakash T <suryat@aerlync.com>
Allow the CH32V QingKe systick timer driver to be selected on the CH32H41X
series. The systick register layout (CTLR/SR/CNT/CMP) is identical to the
existing QingKe parts, so no driver code change is needed; only the Kconfig
dependency has to be extended.
Signed-off-by: Liu Changjie <liucj1228@outlook.com>
Instead of manually defining a variable for the compatibles' names, use
the ones that get autogenerated upon build.
Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
Instead of manually defining a variable for the compatible's name, use
the one that gets autogenerated upon build.
Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
The tick timer fires only on an exact TTMR.TP == TTCR[27:0] match, but
sys_clock_set_timeout() wrote the computed compare value blindly. A
deadline that lands at or behind the count by the time TTMR is written
- for instance a timeout armed just before its tick boundary, or an
arm delayed by interrupt latency - is silently missed until the 28-bit
count wraps, 13.4 seconds at the 20 MHz QEMU clock. The one-tick arms
in the ISR (tickful) and at driver init carry the same, rarer,
exposure.
Route all compare writes through a catch-up: after writing the target,
re-read the count and, while the target is not strictly in the future,
push it MIN_DELAY ahead of the fresh count. A post-write count read
still behind the target proves the match is armed; the exit check
accepts any strictly-future target rather than demanding MIN_DELAY of
remaining headroom, which a count that advances between the write and
the re-read could never satisfy (that stricter form livelocks under
QEMU icount, where the count moves every instruction).
Found by the kernel.timer.timeout_churn near-boundary sweep, which
steps zero-duration timer arms across the tick phase: an arm landing
within the final microseconds of a tick fired 13430 ms late.
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
The kernel re-evaluates its earliest timeout on every add and abort; a
timeslice reset is one of each, so a syscall-heavy thread produces a
sustained stream of sys_clock_set_timeout() calls, most of them landing
on the same tick boundary as the previous one. For a RELOAD backend
every reprogram restarts the hardware counter, so such a stream keeps
any period from ever completing and can postpone the announce for the
stream's duration; it also multiplies the reprogram windows in which
emulated timers misbehave (QEMU's SysTick holds VAL at zero around a
reprogram, and its cancelled-period callbacks race the vCPU).
Two guards in timer_core_arm(), both cheap and backend-agnostic:
- Deadline dedup: cache the armed tick-aligned deadline and program the
hardware only when it moves. The announce invalidates the cache, as
do the drivers whose sys_clock_unused() stops or reprograms the
hardware behind the core's back (cortex_m_systick, cmsdk_apb).
- Catch-up latch: when the reload is floored at TIMER_CORE_MIN_DELAY
(deadline due, or the un-announced span about to overrun
TIMER_CORE_CYCLES_MAX), leave an already in-flight floored reload
alone instead of restarting it, so the fire that resolves the
catch-up gets through even under a rearm stream faster than the
floor. Cleared by the announce.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>