Page 1 of 2

machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Wed Jan 20, 2021 9:01 pm
by kbrenner
Upon setting the datetime with machine.RTC().datetime(), the hours, minutes, and day of the week appear to be reading incorrect values:
machine.RTC().datetime().PNG
machine.RTC().datetime().PNG (7 KiB) Viewed 79135 times
Does anyone know why this happens?

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Wed Jan 20, 2021 9:07 pm
by dhylands
I know rshell has a bunch of code to deal with the various RTC's:
https://github.com/dhylands/rshell/blob ... in.py#L951

It looks like the arguments to datetime are expected to be in the following order:
(year, month, day, weekday, hour, minute, second, subseconds)

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Wed Jan 20, 2021 10:10 pm
by kbrenner
dhylands wrote:
Wed Jan 20, 2021 9:07 pm
I know rshell has a bunch of code to deal with the various RTC's:
https://github.com/dhylands/rshell/blob ... in.py#L951

It looks like the arguments to datetime are expected to be in the following order:
(year, month, day, weekday, hour, minute, second, subseconds)
Got it, thanks. There appears to be a discrepancy between utime.localtime() and machine.RTC().datetime() then:
utime.localtime().PNG
utime.localtime().PNG (53.49 KiB) Viewed 79118 times

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Thu Jan 21, 2021 10:31 am
by pythoncoder
kbrenner wrote:
Wed Jan 20, 2021 10:10 pm
...
Got it, thanks. There appears to be a discrepancy between utime.localtime() and machine.RTC().datetime()...
There is. utime.localtime matches CPython's time module. Unfortunately RTC.datetime() - which isn't even documented - adopts a different convention which is based on STM hardware and the pyb module. The nearest you'll get to docs may be found here.

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Tue Feb 16, 2021 12:39 am
by kbrenner
I have started to notice that the call to the function:

utime.localtime() is now reporting the following value:

>>> utime.localtime()
(2015, 1, 1, 0, 44, 11, 3, 1)

I am not sure why it reset itself to the first day of the year in 2015. Do you have any idea why this is happening?

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Thu Feb 18, 2021 2:27 am
by kbrenner
Through trial and error, I've found that removing power to the pyboard and then plugging it back into the computer via microUSB is what is causing the time to reset. Simply hitting CTRL-D does not cause that to happen. Do either of you have any idea why this is occurring?

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Thu Feb 18, 2021 5:45 am
by dhylands
When you remove power then the the RTC will reset.

You need to provide power to VBAT (typically using a coin cell or simialr) if you want the RTC to keep ticking while power is removed.

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Thu Feb 18, 2021 3:50 pm
by kbrenner
Got it. So is there any way to get the actual datetime then in between power cycles (outside of providing VBAT power with a coin cell or equivalent)? Due to other constraints, I don't have the ability to include an additional coin cell just to power VBAT. I could add code to the boot.py which would rewrite a hardcoded time to the RTC on boot up, but that value is constantly changing as time passes so there would be no way of knowing what to write there.

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Thu Feb 18, 2021 4:29 pm
by dhylands
Yeah - there is no RTC that I'm aware that can keep time when not powered. This is also why external RTC devices (like: https://www.adafruit.com/product/4282) require a battery.

I have rshell synchronize the time on the device with the time on the PC whenever it connects. If you have a network connection you can query an NTP server to get the current time, but otherwise there really isn't anything you can do.

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Posted: Thu Feb 18, 2021 6:08 pm
by kbrenner
That makes sense, thanks!