-
Notifications
You must be signed in to change notification settings - Fork 2k
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
cpu/esp32: allow external 32 kHz crystal for the RTC hardware timer #13061
Conversation
52c17b0
to
7a3a48c
Compare
@@ -1,6 +1,7 @@ | |||
PSEUDOMODULES += esp32_wrover_kit_camera | |||
|
|||
USEMODULE += boards_common_esp32 | |||
USEMODULE += esp_rtc_timer_32k |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we always want to build the RTC?
I'd rather put
ifneq (,$(filter periph_rtc,$(USEMODULE)))
USEMODULE += esp_rtc_timer_32k
endif
in the board's Makefile.dep
. Or maybe add a new FEATURES_PROVIDED
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esp_rtc_timer_32
is just a pseudomodule to control the compilation in cpu/esp32/periph/rtc.c
and doesn't depend on periph_rtc
. RTC is only compiled if module periph_rtc
is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see - but then it would probably be better semantics to set
FEATURES_PROVIDED += esp_rtc_timer_32k
in Makefile.features
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, if I am right, we would need an additional FEATURES_OPTIONAL
in MCU's Makefile.include
to use the esp32_rtc_timer_32k
module when the feature is provided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and then check FEATURES_USED
to select the the proper implementation.
Maybe a bit more convoluted, but certainly the 'cleaner' solution.
On
RTC still works as before. Since this automatically falls back to the internal oscillator, is there any reason to not always use I also get a slightly smaller binary with this compared to the default behavior. |
Yes.
The problem is that the accuracy of the RTC hardware timer with the internal 150 kHz RC oscillator is much worse than the accuracy of the emulated RTC timer. So if you don't have an external 32.768 kHz crystal connected, it is better to use the default configuration with the emulated RTC. Otherwise the system time would drift quite fast (up to 3 seconds per minute). Therefore, the user has to enable the |
I committed a small change that avoids that module |
8edc752
to
d024771
Compare
ifneq (,$(filter periph_rtc,$(USEMODULE))) | ||
FEATURES_OPTIONAL += esp_rtc_timer_32k | ||
endif | ||
|
||
ifneq (,$(filter esp_rtc_timer_32k,$(FEATURES_USED))) | ||
USEMODULE += esp_rtc_timer_32k | ||
endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit surprised this works, since FEATURES_USED only gets set by Makefile.features
, but I guess that's just the magic of make
s dynamic variables.
I compile-tested esp32-wrover-kit
and there the esp_rtc_timer_32k
path gets build, as opposed to esp32-wroom-32
- nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as intended - please squash.
d024771
to
1e71606
Compare
@benpicco Thanks for reviewing, testing and merging. |
Contribution description
This PR changes the RTC low-level driver to allow the use of the RTC hardware timer with an external 32.768 kHz crystal.
The RTC hardware timer of ESP32 can be clocked with either an external 32.768 kHz crystal or an internal adjustable 150 kHz RC oscillator. If the the external 32.768 kHz crystal is not available (as on most boards), the internal 150 kHz RC oscillator is used automatically. However, since this internal 150 kHz RC oscillator is not very accurate, the RTC low-level driver uses by default the PLL-controlled 64-bit microsecond system timer to emulate the RTC timer.
To allow the use of the RTC hardware timer for boards with an external 32 kHz crystal, the pseudomodules
esp_rtc_timer
andesp_rtc_timer_32k
have been defined to control which timer is used for RTC as following:default
Use the emulated RTC timer. The RTC hardware timer with the internal RC 150 kHz oscillator is only used in deep sleep mode and during a reset.
esp_rtc_timer
Use always the RTC hardware timer with the internal 150 kHz RC oscillator.
esp_rtc_timer_32k
Use the RTC hardware timer with the external 32.768 kHz crystal. If the external 32.768 kHz crystal is not available, the RTC hardware timer is used with the internal 150 kHz RC oscillator.
Furthermore, module
esp_rtc_timer_32k
is enabled by default for boardesp32-wrover-kit
by this PR.This PR requires the bootloader images and the automatic XTAL clock detection from PR #13059.
Testing procedure
Use a board with and a board without external 32.768 kHz crystal. Flash and test both boards with
and
All tests should work as expected. Module
esp_rtc_timer_32
depends on moduleesp_rtc_timer
so that all cases are covered.Use a terminal program, reset the boards and check for following output during startup.
SLOW
is the clock used for RTC timer:esp_rtc_timer_32k
and the board with 32.768 kHz crystal:esp_rtc_timer_32k
and the board w/o 32.768 kHz crystal:esp_rtc_timer_32k
and any board:Issues/PRs references
Depends on PR #13059