Skip to content

drivers: rtc: STM32 rtc scalers #89313

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

greavesinator85
Copy link

@greavesinator85 greavesinator85 commented Apr 30, 2025

Allow RTC prescalers to be configurable via dts.

Prescalers can now be adjusted to give more accurate RTC readings, up to 30.5us on LSI/LSE

Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

I have some doubts about the motivation behind this change and if it is actually required.
Please clarify and see comments to make the usage of these new properties fully clear to user.

Also, this compatible is STM32 generic so please remove STM32U5 from the title and provide a more detailled description about the usefulness of this change

@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch 3 times, most recently from 5e3f372 to 2db263e Compare May 1, 2025 14:16
Allow RTC prescalers to be configurable via dts

Signed-off-by: Jake Greaves <[email protected]>
@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch from 2db263e to d09764a Compare May 1, 2025 14:19
@greavesinator85 greavesinator85 requested a review from erwango May 1, 2025 14:21
@greavesinator85 greavesinator85 changed the title drivers: rtc: STM32U5XX rtc scalers drivers: rtc: STM32 rtc scalers May 1, 2025
@greavesinator85
Copy link
Author

I have some doubts about the motivation behind this change and if it is actually required. Please clarify and see comments to make the usage of these new properties fully clear to user.

Also, this compatible is STM32 generic so please remove STM32U5 from the title and provide a more detailled description about the usefulness of this change

Ready for re-review. This was needed to make the most use out of the RTC sub second register, giving our RTC timestamping accuracy of up to 30.5us

Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

Looks good for this part.

However (and it seems you're bound to hit all the corner cases in our drivers):

  • There's already a prescaler part of st,stm32-rtc binding inherited from rtc.yaml. No impact from what I could see but worse to be cross-checked
  • This binding also applies to drivers/counter/counter_ll_stm32_rtc.c driver, which implements these prescalers in a different way and we're not supposed to make properties specific to a driver ("compatible is supposed to describe hardware"). So you'd need to align also counter rtc driver to make use of those (which is a more consequent clean up I'm affraid)

@greavesinator85
Copy link
Author

Looks good for this part.

However (and it seems you're bound to hit all the corner cases in our drivers):

  • There's already a prescaler part of st,stm32-rtc binding inherited from rtc.yaml. No impact from what I could see but worse to be cross-checked
  • This binding also applies to drivers/counter/counter_ll_stm32_rtc.c driver, which implements these prescalers in a different way and we're not supposed to make properties specific to a driver ("compatible is supposed to describe hardware"). So you'd need to align also counter rtc driver to make use of those (which is a more consequent clean up I'm affraid)

I cross checked the existing prescaler and I also don't see a conflict.

I've refactored the rtc counter driver to include the new prescaler override options

@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch 2 times, most recently from 85ccb70 to 57d6954 Compare May 30, 2025 14:10
@github-actions github-actions bot added the area: Samples Samples label May 30, 2025
@github-actions github-actions bot requested review from kartben and nashif May 30, 2025 14:11
@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch from 57d6954 to 5388d4d Compare May 30, 2025 14:11
@greavesinator85 greavesinator85 requested a review from erwango May 30, 2025 14:12
@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch 2 times, most recently from cce5cbf to 9a45464 Compare June 2, 2025 10:11
Copy link

sonarqubecloud bot commented Jun 2, 2025

Copy link
Collaborator

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

@greavesinator85, could you move all changes related to coding style into a dedicated commit?
Commit "drivers: counter: STM32 rtc counter" header line should be rephrased IMHO, to something like drivers: counter: stm32: config RTC prescaler from DT.

defined(CONFIG_SOC_SERIES_STM32L0X) || defined(CONFIG_SOC_SERIES_STM32L1X) || \
defined(CONFIG_SOC_SERIES_STM32L5X) || defined(CONFIG_SOC_SERIES_STM32H7X) || \
defined(CONFIG_SOC_SERIES_STM32H5X) || defined(CONFIG_SOC_SERIES_STM32WLX)
#define RTC_EXTI_LINE LL_EXTI_LINE_17
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unrelated change? I would suggest to discard it.

Ditto for changes on tick_t type definition and added space char to #endif directive line.

Copy link
Author

Choose a reason for hiding this comment

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

Removed

static int rtc_stm32_start(const struct device *dev)
{
#if defined(CONFIG_SOC_SERIES_STM32WBAX) || defined(CONFIG_SOC_SERIES_STM32U5X)
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
const struct rtc_stm32_config *cfg = dev->config;

/* Enable RTC bus clock */
if (clock_control_on(clk, (clock_control_subsys_t) &cfg->pclken[0]) != 0) {
if (clock_control_on(clk, (clock_control_subsys_t)&cfg->pclken[0]) != 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would be in favor of such a change. However I think it deserved a specific commit.

Copy link
Author

Choose a reason for hiding this comment

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

Removed formatting changes from this PR

when HSE is selected as the source clock.
RTC HSE prescaler value. Optional, if not set defaulted in code to:
- not used if source clock is LSE/LSI
- value calculated based on HSE clock frequency if RTC source clock is HSE
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would suggest another phrasing:

      RTC HSE prescaler value. Applies only when RTC source clock is HSE.
      Optional, defaulting to the value calculated based on HSE clock frequency.

Copy link
Author

Choose a reason for hiding this comment

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

Updated to reflect this

@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch 2 times, most recently from 9284f8e to fe2a86e Compare July 3, 2025 12:05
Handle RTC prescaler options inside RTC counter driver

Signed-off-by: Jake Greaves <[email protected]>
@greavesinator85 greavesinator85 force-pushed the stm32u5-config-rtc-scalers-via-dts branch from fe2a86e to 8bf4e52 Compare July 3, 2025 12:06
@mathieuchopstm mathieuchopstm added this to the v4.3.0 milestone Jul 3, 2025
Copy link

sonarqubecloud bot commented Jul 3, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants