Initializing RTC in Python without erasing date and time?

Great project, thanks for all of your valuable hard work!
Is there a way to initialize the RTC (for an H7P) from Python in a way that doesn’t erase the existing values in the date and time registers? I’m referring to this line in Python: myRTC = pyb.RTC(). I’m asking because I do a lot of work with the RTC in custom firmware, but then I sometimes want to jump to a Python script to do some image processing, and I want to preserve date-time across those jumps. I’m considering jumping to Python, then reading the low-level date-time registers before initialzing, then initializing the pyb.RTC, then re-insert the date-time back into the registers, but I fear that process will lose a couple of seconds when I do that. Thanks!

…I have found workarounds for my particular application, so if this is a narrow isolated issue then I will understand your need to fry bigger fish. I’m still thrilled and appreciative of everything this project can do! Thanks!

Hi, the time doesn’t reset when the RTC is initialized, unless you use rtc.datetime again to set/reset or disconnect the power/battery.

Hi and thanks for your suggestion.
Additional details: I had good success within Python scripts by using myRTC=pyb.RTC() to create an instance of RTC, and then myRTC.datetime to set and get the date and time. That worked great. But then I began using the RTC within custom firmware before launching a script. That also worked great by using HAL_Init(&hrct) to create an instance, and then using HAL_GetTime(&hrct…) and HAL_SetTime(&hrct…). That all worked great as long as I remained inside of the custom firmware. And with the battery connected throughout, of course (I provide power to both the main power rail and VBat). The problem hit when I initialized the RTC and set time-date using the HAL commands in the firmware, and then jumped to a script where I wanted to make additional reads of date and time. To do that easily from Python I needed to create a Python instance of the RTC using pyb.RTC(). Then used yy,MM,dd,ww,hh,mm,ss,uu = myRTC.datetime() to read it (which is exactly what I used successfully when I initialized and used the RTC from within Python). But when I used it to read date and time returned as zeros, because, I’m assuming, pyb.RTC wiped it clean. After it was wiped clean I could then set and get new RTC values with rtc.datetime, but the date-times I set with HAL commands in firmware were lost. Again, with power applied through the entire process.

The RTC driver in micropython assumes that nothing else is using the RTC, I’m not sure what your custom fw code is doing and how it affects micropython’s driver, and whether or not you have to make changes to the rtc driver to work with your code, you’ll have to figure that out on your own. Here’s the RTC driver: micropython/rtc.c at 269b8fa593227578f632e1929aa309d0dd8f67fb · openmv/micropython · GitHub

I can take it from here, thanks!!