With set_time() you can set it (and itâs using RTC because you can test it by shortening your crystal with a pincet to see that the clock stops) but you may call it only once otherwise itâs logical that the clock resets after each reboot. Thatâs the reason that I put it in comment. I call it once, put in command, recompile and program again. But in that case time() always starts at twelve o clock. It is not using the RTC value anymore.
I also see that the RTC Init function is never called automatically.
I was searching on the internet how they make the couping between the RTC and the system time(). It should work as you call the RTC api, but it isnât. I remember in embedded Linux systems that there is something as a separate HW and SW clock.
Remember the ticket about the hiccups. If I want to use a STLinkV2 clone, the HW reset pin is not working and programming during sleep is not possible. It works with the original programmer because it uses HW reset.
I use \r because I want to see the clock overwritten in the console.
In TARGET_STM mbed_overrides.c there is a function mbed_sdk_init() that contains RTC init between
#if DEVICE_RTC
...
#endif /* DEVICE_RTC */
Which is supposed to be called.
In rtc_api.c there is a function rtc_init() that is doing less or more the same.
I think something is wrong in the mbed_sdk_init(), because it only works only after calling rtc_init().
Wow, this is silly, the same LSE clock init code is duplicated between mbed_overrides.c, rtc_api.c, and lp_ticker.c for STM. Whichever of those gets executed first is the one which actually configures the LSE oscillator. Those should really be combined.
Update: Turns out that the code is not completely duplicated between those three places, not really. The RTC clock init code is duplicated between mbed_overrides and rtc_api, but the overrides version does not do LSE init for some reason?
Regarding your issue, I think this is a legitimate case of undefined behavior in Mbed. I had actually assumed, and written in wiki pages, that the RTC is not activated until user code calls rtc_init(). So, the example you provided in your first post would be correct behavior.
However, the Mbed example code implies different behavior, and also it seems like STM32 got confused and basically set up the RTC clock to always initialize on boot.
Thinking about this from a little further back, I think the biggest source of weirdness is that, if gettimeofday() is called and the RTC is not initialized, it sets the time to 0 instead of just initializing the RTC. It would be much more logical for it to just call rtc_init() and use whatever time value the RTC is now providing.
So, I think that to fix this, we should do the following:
Update gettimeofday() to call rtc_init() if the RTC is not enabled
Update TARGET_STM/mbed_overrides.c to not initialize the RTC clock
Update rtc_init() and mbed_overrides.c to remove duplicated code
And regarding why your fix actually works⊠Iâm a bit confused. Because ultimately, calling time() before rtc_init() should always result in the RTC time starting from 0 at boot, because the mbed_rtc_time.cpp code is doing rtc_write(0). Are you sure that adding that clock enable line is causing it to not start from 0 at boot?
Why you would not do the initialization automatically? It seems to be more logical that you initialize as soon as the RTC is used and available to that you always get the right time value if you use time(NULL).
We donât want the RTC to auto init at boot because that starts the 32kHz oscillator on some targets, and people might not want the 32kHz oscillator to start when itâs not used in code.
With the change in my PR, the RTC doesnât start at boot, but it does start the first time you get the time (e.g. with time()), And it wonât reset to 0 this time with the set_time() call removed. So I claim it should do what you want.
Regarding to your question it seems that _rtc_isenabled() returns true as soon as __HAL_RCC_RTCAPB_CLK_ENABLE(); is executed. Thatâs the reason that with my fix in the mbed_sdk_init() the call to set_time(0); is never done anymore.
I will not dive in all the STM32 details but in CubeIDE I have seen that there are 3 inits:
Iâm now realizing that I was creating my tickets in the wrong repo.
It was in the âdeadâ original repo like also others are still doing:
Thatâs very confusing. If you create a new project with mbed-tools configure, the wrong repo is checked out by default. It means that Iâm constantly interacting with outdated mbed code and maybe facing already solved issues. And I will not be the firstâŠ
I donât know if it is possible to use the right repo by default to not confuse other users?
Oh well thatâs because the Mbed CLI stuff is from ARM, so it still uses the official (and mostly dead) repo. Jan and I are developing the Mbed CE fork which has its own build system and infrastructure. Youâre welcome to create tickets on the official repo, but the likelihood of anyone doing anything about them at this point is⊠not great.
Lol, I already migrated my tickets to the CE and will try the new doc. I will start from a fresh âboard-bringupâ project and transfer all the knowledge of the previous one to the new one. I will also make a pull request for the added HSE stuff on the MCU_STM32U575xG.