Skip to content

drivers: timer: nrf_grtc_timer: add last_count initialization #91432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

adamkondraciuk
Copy link
Contributor

@adamkondraciuk adamkondraciuk commented Jun 11, 2025

The GRTC counter is not cleared at startup, therefore the last_count variable needs to be initialized accordingly. This change:

@github-actions github-actions bot added area: Timer Timer platform: nRF Nordic nRFx size: XS A PR changing only a single line of code labels Jun 11, 2025
nordic-krch
nordic-krch previously approved these changes Jun 11, 2025
nika-nordic
nika-nordic previously approved these changes Jun 11, 2025
@@ -485,6 +485,7 @@ static int sys_clock_driver_init(void)
}
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */

last_count = counter();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be wrapped in Kconfig-equivalent of NRFX_GRTC_CLEAR_AT_INIT ? or this feature is not configurable via Kconfig ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another PR for that: #89664

kl-cruz
kl-cruz previously approved these changes Jun 12, 2025
@adamkondraciuk adamkondraciuk dismissed stale reviews from kl-cruz, nika-nordic, and nordic-krch via ad3f2da June 12, 2025 11:03
@adamkondraciuk adamkondraciuk force-pushed the NRFX-7834-update-last-count-at-grtc-init branch 2 times, most recently from ad3f2da to 86c221f Compare June 12, 2025 16:16
nordic-krch
nordic-krch previously approved these changes Jun 13, 2025
Comment on lines 494 to 495
last_count = sys_clock_tick_get() * CYC_PER_TICK;
grtc_start_value = last_count;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a potential race between here and sys_clock_timeout_handler for last_count?
If that is not the case and sys_clock_announce is not expected to be called at this point, could this code be simplified and skip sys_clock_tick_get logic to just call counter()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely better to use counter() here to initialize both these variables than to involve sys_clock_tick_get() (calling this function from the system timer driver doesn't actually seem to be appropriate).

The GRTC counter is not cleared at startup, therefore the
`last_count` variable needs to be initialized accordingly.
This change:
- Prevents overflow of the `sys_clock_announce()` int32_t parameter
- Ensures the correct uptime value, which should be reset during
  initialization

Signed-off-by: Adam Kondraciuk <adam.kondraciuk@nordicsemi.no>
Update GRTC tests to reflect uptime reset at startup.

Signed-off-by: Adam Kondraciuk <adam.kondraciuk@nordicsemi.no>
@adamkondraciuk adamkondraciuk force-pushed the NRFX-7834-update-last-count-at-grtc-init branch from 86c221f to 6248aac Compare June 30, 2025 08:38
Copy link

@nordic-krch nordic-krch added the bug The issue is a bug, or the PR is fixing a bug label Jun 30, 2025
@dkalowsk dkalowsk added this to the v4.2.0 milestone Jun 30, 2025
@danieldegrasse danieldegrasse merged commit 017dca3 into zephyrproject-rtos:main Jun 30, 2025
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Timer Timer bug The issue is a bug, or the PR is fixing a bug platform: nRF Nordic nRFx size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nRF54L: uptime does not reset across reboots
8 participants