Skip to content
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

STM32 RTC sub seconds support #61580

Merged
merged 6 commits into from Sep 26, 2023

Conversation

niedzwiecki-dawid
Copy link
Member

Implement support for handling the sub second registers.

Copy link
Contributor

@gautierg-st gautierg-st left a comment

Choose a reason for hiding this comment

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

Some comments on new magic values that could be replaced. Otherwise LGTM

drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
@niedzwiecki-dawid
Copy link
Member Author

I haven't payed attention to that earlier, but the clock source was checked incorrectly. I've fixed that in the new, first commit.

drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
gautierg-st
gautierg-st previously approved these changes Aug 24, 2023
Copy link
Contributor

@gautierg-st gautierg-st left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks

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.

Please add some (at least minimal) way to get this compiled in CI

@niedzwiecki-dawid
Copy link
Member Author

Sorry for the delay, I'm working on the comments now.

@niedzwiecki-dawid
Copy link
Member Author

Please add some (at least minimal) way to get this compiled in CI

Do you have any suggestions how to do it?

@erwango
Copy link
Member

erwango commented Sep 13, 2023

Here is the log:

*** Booting Zephyr OS build zephyr-v3.4.0-2458-gcfb5f828b708 ***
Running TESTSUITE counter_basic
===================================================================
START - test_all_channels
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing rtc@40002800
PASS - test_all_channels in 4.022 seconds
===================================================================
START - test_cancelled_alarm_does_not_expire
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing rtc@40002800
PASS - test_cancelled_alarm_does_not_expire in 7.891 seconds
===================================================================
START - test_late_alarm
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Skipped for rtc@40002800
PASS - test_late_alarm in 0.813 seconds
===================================================================
START - test_late_alarm_error
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Skipped for rtc@40002800
PASS - test_late_alarm_error in 0.813 seconds
===================================================================
START - test_multiple_alarms
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Skipped for counter
Skipped for counter
Skipped for rtc@40002800
PASS - test_multiple_alarms in 1.083 seconds
===================================================================
START - test_set_top_value_with_alarm
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Skipped for rtc@40002800
PASS - test_set_top_value_with_alarm in 1.576 seconds
===================================================================
START - test_short_relative_alarm
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing rtc@40002800
Assertion failed at WEST_TOPDIR/zephyr/tests/drivers/counter/counter_basic_api/src/test_counter.c:801: test_short_relative_alarm_instance: (i + 1 not equal to cnt)
rtc@40002800: Expected 6 callbacks, got 5
FAIL - test_short_relative_alarm in 0.753 seconds
===================================================================
START - test_single_shot_alarm_notop
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing rtc@40002800
Assertion failed at WEST_TOPDIR/zephyr/tests/drivers/counter/counter_basic_api/src/test_counter.c:403: test_single_shot_alarm_instance: (0 not equal to err)
rtc@40002800: Counter set alarm failed (err: -16)
FAIL - test_single_shot_alarm_notop in 1.221 seconds
===================================================================
START - test_single_shot_alarm_top
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Skipped for rtc@40002800
PASS - test_single_shot_alarm_top in 1.303 seconds
===================================================================
TESTSUITE counter_basic failed.
Running TESTSUITE counter_no_callback
===================================================================
START - test_set_top_value_without_alarm
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Testing counter
Skipped for rtc@40002800
PASS - test_set_top_value_without_alarm in 0.848 seconds
===================================================================
TESTSUITE counter_no_callback succeeded
------ TESTSUITE SUMMARY START ------
SUITE FAIL -  77.78% [counter_basic]: pass = 7, fail = 2, skip = 0, total = 9 duration = 19.475 seconds
- PASS - [counter_basic.test_all_channels] duration = 4.022 seconds
- PASS - [counter_basic.test_cancelled_alarm_does_not_expire] duration = 7.891 seconds
- PASS - [counter_basic.test_late_alarm] duration = 0.813 seconds
- PASS - [counter_basic.test_late_alarm_error] duration = 0.813 seconds
- PASS - [counter_basic.test_multiple_alarms] duration = 1.083 seconds
- PASS - [counter_basic.test_set_top_value_with_alarm] duration = 1.576 seconds
- FAIL - [counter_basic.test_short_relative_alarm] duration = 0.753 seconds
- FAIL - [counter_basic.test_single_shot_alarm_notop] duration = 1.221 seconds
- PASS - [counter_basic.test_single_shot_alarm_top] duration = 1.303 seconds
SUITE PASS - 100.00% [counter_no_callback]: pass = 1, fail = 0, skip = 0, total = 1 duration = 0.848 seconds
- PASS - [counter_no_callback.test_set_top_value_without_alarm] duration = 0.848 seconds
------ TESTSUITE SUMMARY END ------
===================================================================
RunID: c68cf89ac73c546729e0323ac92016ff
PROJECT EXECUTION FAILED

@niedzwiecki-dawid
Copy link
Member Author

Thanks for the logs. How do you run the tests? It's executed on the nucleo_f429zi board, right? I've ordered the board to do it on my side and make sure everything is working fine. I should be able to reproduce the tests in a few days.

@niedzwiecki-dawid
Copy link
Member Author

I've managed to reproduce the tests on the nucleo_f429zi board. Working on the fix.

@niedzwiecki-dawid
Copy link
Member Author

niedzwiecki-dawid commented Sep 20, 2023

I've debugged the problem.

The test failures occur when 1 tick alarm is programmed. For example test_short_relative_alarm test checks the 1 tick alarm 100 times. The alarm is not triggered in ~20% cases.

I've noticed that the alarm doesn't occur when it is programmed one tick before deadline. It is a case when one tick occurs during the calculation of register values in the set_alarm function. IMO it still should work, but I started reading the reference manual one more time and I found the sentence Each change of the RTC_CR register is taken into account after 1 to 2 RTCCLK clock cycles due to clock synchronization..

I think it means 1 tick alarm won't work for RTC with subseconds and an additional check should be added that the relative minimum tick value is 2.

@erwango Please let me know what you think.

@erwango
Copy link
Member

erwango commented Sep 20, 2023

@erwango Please let me know what you think.

It seems there's a good amount of drivers dealing with such cases already. See STM32 timer based counter driver for instance.
Would you be able to apply the same in current case ?

@niedzwiecki-dawid
Copy link
Member Author

@erwango Please let me know what you think.

It seems there's a good amount of drivers dealing with such cases already. See STM32 timer based counter driver for instance. Would you be able to apply the same in current case ?

I had to use a bit different detection method. It works fine and passes all testes.

The config values have been hardcoded as magic values. Introduce
universal calculation based on the DTS entries.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
Synchronize reading two separate registers. In some edge cases the read
registers could point different dates.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
@erwango
Copy link
Member

erwango commented Sep 21, 2023

I had to use a bit different detection method. It works fine and passes all testes.

Thanks I'll have a look once I have free time.

Just a side note, when updating a PR, please don't rebase at the same time. This prevents to easily see the diff using the compare button provided by github and we have to review the whole change again.
If rebase is really required, please proceed in 2 steps:
rebase and force push then update PR and force push (or in the order that better suit you).

If everyone would do the same, it would save a lot of time to reviewers.

@niedzwiecki-dawid
Copy link
Member Author

Just a side note, when updating a PR, please don't rebase at the same time. This prevents to easily see the diff using the [compare](https://github.com/zephyrproject-rtos/zephyr/compare/6b87b0597c6b94fb6b4b25474f5e4c473c1ffa6d..6ce4d587861db359b7e36337b55e7f4669f70c69) button provided by github and we have to review the whole change again. If rebase is really required, please proceed in 2 steps: rebase and force push then update PR and force push (or in the order that better suit you).

If everyone would do the same, it would save a lot of time to reviewers.

Thanks for the tip. I just noticed how it works.

@niedzwiecki-dawid
Copy link
Member Author

Sorry for the noise. I run the auto-formatter

drivers/counter/counter_ll_stm32_rtc.c Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Outdated Show resolved Hide resolved
drivers/counter/counter_ll_stm32_rtc.c Show resolved Hide resolved
Fix indentations of the API definition.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
RTC drivers uses only seconds, so transition to microseconds is
necessary.

Change way of calculation tick<->time to avoid unnecessary
conversations.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
Add support for using the sub second registers. It allows reading and
setting alarm with the sub second tick resolution.

The RTC module is configured to get as high frequency as possible, which
equals the source clock (RTCCLK) divided by 2. To get such frequency,
the asynchronous prescaler is set to 1.

According to RM, setting the asynchronous prescaler to a high value
minimize consumption, so the change increase the power consumption.

Use a config to enable the sub second support.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
Split the read function into 2 versions (date and no date) since they
don't have common code.

It improves readability.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
@niedzwiecki-dawid
Copy link
Member Author

Thanks for the comments. I should have done the requested changes by myself.

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.

LGTM, but I'd like @gautierg-st to approve as well.

Copy link
Contributor

@gautierg-st gautierg-st left a comment

Choose a reason for hiding this comment

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

LGTM

@carlescufi carlescufi merged commit 68292b0 into zephyrproject-rtos:main Sep 26, 2023
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants