RFC: Add rtc module#745
Conversation
|
Thank you @notro! I'm pretty busy this weekend but will get back to you tomorrow evening or Monday. |
tannewt
left a comment
There was a problem hiding this comment.
Overall, this is very exciting! I'm excited to see native time keeping. Thank you for contributing this! I have a few suggestions before we merge though.
Please also make a PR to https://github.com/adafruit/asf4 with the ASF4 patch that you linked to. Once we merge it their, the submodule in this repo can be updated and make Travis happier.
There was a problem hiding this comment.
It might be on by default or done by ASF4.
There was a problem hiding this comment.
Please make this a property. See this for an example: https://github.com/adafruit/circuitpython/blob/master/shared-bindings/digitalio/DigitalInOut.c#L223
Other drivers have this as a property as well: https://github.com/adafruit/Adafruit_CircuitPython_DS3231/blob/master/adafruit_ds3231.py#L111
There was a problem hiding this comment.
This should be in MP_STATE_VM() so that its considered a root point by the garbage collector. https://github.com/adafruit/circuitpython/blob/master/py/mpstate.h#L171 Without that, we risk cleaning up the time source object before the VM is done.
There was a problem hiding this comment.
You'll want an rtc_reset below as well that sets the time source back to the built-in. Otherwise, you'll refer to an old object when a soft reload happens.
|
How should the other ports that use the time module deal with this rtc module?
|
|
Thanks for the work on this! I'd suggest A variant of 2. that stubs it out in each port. That way its easy to see where to implement it. (Hopefully we'll have it in all that support it eventually.) Also, you should be able to update this with the newer submodule for asf4 which includes the calendar changes. Thanks! |
Add an rtc module that provides a singleton RTC class with - a datetime property to set and get time if the board supports it. - a calbration property to adjust the clock. There's also an rtc.set_time_source() method to override this RTC object using pure python. The time module gets 3 methods: - time.time() - time.localtime() - time.mktime() The rtc timesource is used to provide time to the time module. lib/timeutils is used for time conversions and thus only supports dates after 2000.
Support the rtc module by using hal_calendar.
|
Version 2 Changes:
|
|
Thank you so much! I made a couple small tweaks and am merging. Feel free to follow up with any changes you'd still like to make. |
|
I see that the switch to using the external oscillator broke various boards and has been reverted. I'm getting really nice numbers, but then suddenly something happens that throws it out of wack and 3-4 secs is lost. I'm using these scripts:
Explanation: Isn't time.monotonic() in seconds? I wonder why that clock only does 20 secs in 60 seconds of wall time. Adafruit CircuitPlayground Express I tried this board as well and it is really off, +5 seconds per minute. I'm currently using this to see if I can calibrate it to something that can be usable: class RTC(object):
def __init__(self):
self._calibration = 0
self.base = time.mktime(rtc.RTC().datetime)
@property
def datetime(self):
t = rtc.RTC().datetime
if (abs(self._calibration) > 127):
secs = time.mktime(t)
elapsed = secs - self.base
adjust = float(elapsed) * self._calibration / 10**6
t = time.localtime(secs + int(adjust))
return t
@datetime.setter
def datetime(self, t):
rtc.RTC().datetime = t
self.base = time.mktime(t)
@property
def calibration(self):
return self._calibration
@calibration.setter
def calibration(self, val):
self._calibration = val
if abs(val) < 128:
rtc.RTC().calibration = val
else:
rtc.RTC().calibration = 0
r = RTC()
rtc.set_time_source(r)Current run: I should probably look into calibrating the clock source itself. |
|
@notro Yup, we had to follow up with a couple fixes. No big deal though. Email me at scott@adafruit.com and I'd be happy to get you more boards to test with if you like. We have an existing bug for supporting external crystals in 3.x and this should be part of it. Thanks again for the contribution! |
This PR adds an
rtcmodule and hooks it up to thetimemodule. It also adds rtc support to ports/atmel-samd.I haven't looked into how it affects/breaks other ports.
This is the ASF4 change: 0001-Add-hal_calendar.patch
I used start.atmel.com to find the right incantations.
Comments are welcome, so I can get this into shape for inclusion.
Testing
I was suprised that there was no unittest module in CP, I used this one: unittest.py
test_rtc.py
test_time.py
code.py
Problem
The rtc clock is speeding ~0.5 second each day so I need to look into that.
This work started out in #671.
Edit: Update test_rtc.py to use datetime property