Skip to content

Circuitpython nickzoic 1046 nrf rtc #1534

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

Merged
merged 12 commits into from
Mar 28, 2019

Conversation

nickzoic
Copy link

This is an implementation of #1046 , with a couple of things missing:

  • I was having trouble with the weak references to common_hal_rtc_get_time and common_hal_rtc_set_time which I've got a workaround for but not really an explanation for
  • There's no calibration for this RTC. It could be calibrated by scaling the RTC counter values slightly but there's no facility for tuning the RTC clock itself.

@dhalbert
Copy link
Collaborator

I have been unable to get -flto to work on the nRF build, which is really strange (see #1396). I'm not sure if that might be related to the weak references issue, but possibly.

@nickzoic
Copy link
Author

I'm very puzzled about the whole thing. I'm not, at this point, sure if it is a flaw in my understanding of __attribute__((weak)) or something genuinely weird going on, maybe in combination with inheriting the __attribute__((noreturn)) from the raise?

I got as far as working out that if I removed the weak declarations, OR changed the error messages raised by the weak declarations to be different, then the problem goes away. 8a88a6f uses this workaround so at least I could make some progress on this issue, but I'm not real happy about it.

@nickzoic
Copy link
Author

(oh, and because I changed that error message it's upset the translations, which is what Travis is complaining about. I'm seeking a better way)

@tannewt tannewt self-requested a review February 13, 2019 18:33
tannewt
tannewt previously approved these changes Feb 14, 2019
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great! Thanks! Please update your translations and it should be good to go.

Adafruit CircuitPython 4.0.0-beta.2-31-ga48d69501 on 2019-02-13; Adafruit Feather nRF52840 Express with nRF52840
>>> import rtc
>>> r = rtc.RTC()
>>> r.datetime
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=29, tm_wday=5, tm_yday=1, tm_isdst=-1)
>>> import time
>>> time.time()
946684843
>>> time.localtime()
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=57, tm_wday=5, tm_yday=1, tm_isdst=-1)
>>> r.datetime = time.struct_time((2019, 2, 14, 10, 20, 0, 3, 0, -1))
>>> time.localtime()
struct_time(tm_year=2019, tm_mon=2, tm_mday=14, tm_hour=10, tm_min=20, tm_sec=2, tm_wday=3, tm_yday=45, tm_isdst=-1)
>>> time.localtime()
struct_time(tm_year=2019, tm_mon=2, tm_mday=14, tm_hour=10, tm_min=20, tm_sec=10, tm_wday=3, tm_yday=45, tm_isdst=-1)
>>> time.localtime()
struct_time(tm_year=2019, tm_mon=2, tm_mday=14, tm_hour=10, tm_min=20, tm_sec=12, tm_wday=3, tm_yday=45, tm_isdst=-1)
>>>

@ghost
Copy link

ghost commented Feb 16, 2019

Possibly this could be combined with deepsleep, which also needs an RTC timer as timeout and to advance the systick so time.monotonic() returns a reasonable result after sleep. This however would require a lower divide value (1024). My implementation of deepsleep is here: https://github.com/bboser/iotpython/blob/master/ports/nrf/common-hal/microcontroller/__init__.c.

The other possible issue is that apparently the softdevices (BT?) also use RTC timers. Somewhere I read that RTC0 is used by the softdevice but I cannot locate the reference.

@tannewt
Copy link
Member

tannewt commented Feb 20, 2019

@nickzoic Are you blocked by anything on this?

@nickzoic
Copy link
Author

Hi Scott! I got sidetracked trying to work out the weak reference thing. See comments at end of #1046.
Perhaps Dan's changes to make system will avoid this problem? It is very curious that it is a problem with -O2 set and not -O1, and very much repeatable.

@nickzoic nickzoic force-pushed the circuitpython-nickzoic-1046-nrf-rtc branch from a48d695 to f04294f Compare March 2, 2019 11:21
@nickzoic
Copy link
Author

nickzoic commented Mar 2, 2019

OK so I've rebased it and sadly, no change. I've just changed one of the messages for now and provided translations as best I can approximate for the new message.

@tannewt
Copy link
Member

tannewt commented Mar 8, 2019

I think we should get this in once the translations are merged. It should be good to go otherwise.

@nickzoic
Copy link
Author

nickzoic commented Mar 9, 2019 via email

@tannewt
Copy link
Member

tannewt commented Mar 14, 2019

@nickzoic please take a look when you are back. There are files that need to be merged.

@tannewt tannewt modified the milestones: 4.0.0 - Bluetooth, 4.x Mar 14, 2019
(works if MP_WEAK common_hal_rtc_get_time is removed)
the __weak linking works fine so long as these functions are not identical.
I have not yet worked out why.
The mysterious MP_WEAK linking bug still exists, thus the new message for 'set'.
@nickzoic nickzoic force-pushed the circuitpython-nickzoic-1046-nrf-rtc branch from c297266 to a06ce33 Compare March 27, 2019 23:06
@nickzoic
Copy link
Author

OK @tannewt trying yet again to get this to pass CI checks ...

@dhalbert
Copy link
Collaborator

@nickzoic Just saw this issue in micropython: micropython#4590. Is it relevant to your impl?

@nickzoic
Copy link
Author

@dhalbert you mean for the problems with the Ethernet wing like #1500, right?
(I think there was another one too, or maybe that was a discussion on Discord)
I've never been able to successfully replicate those so haven't got anywhere tracking them down.

The Wiznet5K implementation for circuitpython doesn't use LWIP but it might be worth looking at the error handling in wiznet5k_socket_send() and see if there's situations there where we should retry rather than erroring out.

@dhalbert
Copy link
Collaborator

RIght - I didn't know how much of your code was common with the MPy code, if any.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked one old comment and looks good otherwise. Will merge after Travis gives the ok. Thanks for your persistence on this!

@tannewt tannewt self-assigned this Mar 28, 2019
@nickzoic
Copy link
Author

nickzoic commented Mar 28, 2019 via email

@tannewt
Copy link
Member

tannewt commented Mar 28, 2019

Ya, I looked into having a smarter merge but didn't see any great options.

@tannewt tannewt merged commit a30bbe6 into adafruit:master Mar 28, 2019
@tannewt
Copy link
Member

tannewt commented Apr 1, 2019

@nickzoic Want to take a look at why this broke BLE? Totally ok if you don't have the time.

@ghost
Copy link

ghost commented Apr 1, 2019 via email

@nickzoic
Copy link
Author

nickzoic commented Apr 1, 2019 via email

nickzoic added a commit to nickzoic/micropython that referenced this pull request Apr 2, 2019
(to avoid interference with Bluetooth Softdevice. See
adafruit#1534 (comment)
with thanks to @bboser for pointing it out)
@nickzoic
Copy link
Author

nickzoic commented Apr 2, 2019

OK so I've changed it across to RTC2 and it works from the point of view of the RTC, has anyone got some code which demonstrates it not working with BLE which would work with BLE stuff I might have lying around?
(@bboser @tannewt @dhalbert)

@dhalbert
Copy link
Collaborator

dhalbert commented Apr 2, 2019

See the example in #1726. Try that. If that doesn't cause an immediate error, then doing something like https://github.com/adafruit/Adafruit_CircuitPython_BLE/blob/master/examples/ble_uart_echo_test.py with the Adafruit Bluefruit LE Connect app on the other end would be a simple test.

@dhalbert
Copy link
Collaborator

dhalbert commented Apr 2, 2019

Also, take out the MP_WEAK attributes on the functions. That should not be necessary. If they ever were, they are not needed now.

@nickzoic
Copy link
Author

nickzoic commented Apr 2, 2019

OK that UART example works with nickzoic/micropython@48f056647f which now uses RTC2. Thanks for the pointer, I hadn't tried using BLE for anything yet.

I'll rebase onto master and make a new PR very soon

nickzoic added a commit to nickzoic/micropython that referenced this pull request Apr 2, 2019
(to avoid interference with Bluetooth Softdevice. See
adafruit#1534 (comment)
with thanks to @bboser for pointing it out)
@dhalbert
Copy link
Collaborator

dhalbert commented Apr 2, 2019

Yes, we don't compile the source files for modules that are turned off. But rtc.RTC was the only one that ever used MP_WEAK. I'm not sure why it was done that way to begin with. If you grep for MP_WEAK, you'll find very few uses. You don't need the NotImplemented versions. But a use or two here or there may need to check #if CIRCUITPY_RTC.

Or maybe I am all wrong about this, but looking at the uses of RTC, it does not seem to need fallback routines.

@nickzoic
Copy link
Author

nickzoic commented Apr 2, 2019

Yep, I've removed them altogether since both atmel-samd and nrf ports have RTC anyway.

Thanks for your patience :-) #1736 should hopefully work out better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants