-
Notifications
You must be signed in to change notification settings - Fork 202
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
RTIC v2.0.0 support #765
Comments
Hi @kyp44 and welcome! Ideally the implementation would live in this crate. By all means, feel free to open up a PR with a RTIC v2 monotonic implementation. I've actually been meaning to do it too, just haven't found the time yet - I'd be happy to assist you with that. By the way, if you're looking for async support on more peripherals: I've been working on adding async APIs to the HAL. The PR is pretty much complete at this point, it just hasn't gotten merged yet. Having some extra hands testing it out would absolutely help with getting it over the finish line. |
@jbeaurivage I appreciate the response! While I have never used RTIC v1, my impression is that the I do need async delays for my application, but looking at the async methods you've implemented for So I think I still would like to try to implement the global I'll fork and start working on this right away, and I'm happy to have you help or at least have another set of eyes on my implementation. I'm also happy to help you test your async HAL extensions, though I only have a PyGamer in terms of ATSAMD hardware so I may be limited in what I can test. |
No problem! Yeah having a global monotonic is entirely optional on v2. |
@kyp44 By the way, do reach out on Matrix if you haven't already: https://matrix.to/#/#atsamd-rs:matrix.org |
Most |
Status update: here I have got an RTC-based monotonic pretty much fully working, but am still working on ironing out some async behaviors that may or may not even be the monotonic misbehaving. Currently, the monotonic is implemented directly in the HAL but @jbeaurivage and I have been discussing on Matrix, and it may be more appropriate to submit it to the |
Timer-based implementation proposal: rtic-rs/rtic#994 |
UpdateWhile I am still putting them through their paces a bit: I have completed RTIC v2 monotonics using the RTC. @jbeaurivage and I have had some discussion about whether these monotnonics belong as part of this HAL (as the RTIC v1 monotonic does), or part of the rtic-monotonics crate. Note that as currently implemented they are part of the HAL, but I think more discussion is warranted on this as there are issues/concerns with either option. What to include?There are two monotonics:
Should both be included to give the end user a choice, or just one for simplicity (probably the RTC clock sourceIn order to use the montonics, the RTC clock source need to be setup. There two clock frequencies available: 1.024 kHz or 32.768 kHz, and two sources: internal or external. So this a total of four possible configurations. The way I see it, there are two ways to ensure the clock is setup:
Where do we belong?Whether residing in the HAL, or in rtic-monotonics, there are issues:
What are thoughts on this topic? Remaining tasks:
|
Setting up clocks is just a bunch of instructions to write in the right registers at the end of the day; I don't think duplicating such little amount of code in |
@maximeborges You are correct, and actually, in this case, setting the clock is only a single line of code. I also forgot to mention above that the RTC clock frequency is actually needed at compile time anyway (currently accomplished by having different macros to setup the monotonic), so simply just passing in an Given your insights here, I agree that it makes sense for the monotonics to live in I'm going to finish the tasks above, clean things up, and then submit a PR to @maximeborges It looks like you only added support in |
Maybe adding the implementation to the HAL for now would be easier, because it already handles all the differences between all supported chips and it would be easier to implement for all of them by reusing a lot of the timer stuff that is already present in the HAL. It would also be easier to experiment here until we settle on a set of useful implementations. |
@kyp44 A disadvantage compared to what?
What do you mean by that? Do you mean overflows can be missed if the interrupt is not processed for more than a second or does anything else break? I don't know if a timer that can not set intervals greater than a second would be very useful and even 32 seconds for the 1kHz variant wouldn't be sufficient for many use cases. All in all I think |
So the Regarding the robustness issues, I first implemented the RTC monotonic using I very much would like to resolve this
I've been thinking about purchasing a debugger to be able to troubleshoot this, but haven't yet. I have no problem keeping the monotonics in the HAL temporarily for experimentation / troubleshooting / enhancement and moving to EDIT: All ATSAMD chips have at least two compares in |
Feel free to ignore this idea, but for the time being, the RTC monotonic impl could live in its own crate, owned by We'd just have to be very deliberate about the fact that this new crate would be a temporary solution, because temporary solutions have a nasty tendency to become the de facto permanent solution if not looked after. |
I like the idea in principle, however all the ATSAMD variant infrastructure is in the HAL, not the workspace, so we wouldn't be able to take advantage of that. Really though, I don't think duplicating that (either in a new crate in the EDIT: I also like this because it keeps it isolated, making it easier to move later IMO. For purposes of awareness, testing, and troubleshooting, it certainly makes more sense than just living as a branch in my fork like it does currently. EDIT 2: Can anyone recommend debugger hardware that a) works well on ATSAMD chips and b) plays nicely with Rust on Linux? |
Ok, now I get it. I think I looked at the wrong code while reviewing and got confused.
Just to be sure: You mean the other way round?
I don't know why this might be either, but it should be fixed before it is being published. Your test might be unrealistic, but for now we don't know if it won't happen under less stress just after more time. If it happens after 15min under stress it might happen under normal circumstances after weeks or months which would be very bad in production and extremely hard to debug. The problems you seemed to be having look like further testing is needed to confirm/prevent your findings. Does anyone have experience with the SAMD RTC? I have never really used the RTC interrupts so I can only speculate... Regarding
This will probably become a permanent temporary solution... If it lives in the HAL it can easily be stripped later while bumping the minor version. The fact that it won't be available in the HAL forever can be documented, to reduce surprises in the future. |
You are correct, good catch! Added correcting edit note in the original comment. Yeah, the issue about the counter being one tick behind in the interrupt handler is odd, and I did have a look at the errata before, as you have, and did not find anything about it. The behavior seems to be consistent though. It's possible that I'm not understanding how the counter syncing works (it's supposed to be automatic on the SAMx51x chips) so perhaps that's the issue. Independent verification of this behavior would be good. I agree that the freezing issue should be addressed. It actually used to freeze sooner under that stress test (in about 90 seconds for the 1 kHz) clock. This I was able to troubleshoot successfully by moving some code around. In particular, I suspect the current task stalling issue is something similarly subtle that manifests only when the timing is just right. Note that the stall time (e.g. the 15 minutes I gave for the 1 kHz clock) is not consistent, the numbers above are just averages. Just like the prior problem, this could very well be caused by the need to have to wait for the clock to catch up due to the above quirk that you pointed out. Based on the following, it seems clear that it is related to the extra interrupts needed for half-period counting:
Lastly, the It sounds like there is some disagreement about where to put the monotonics, even on a temporary basis. I keep waffling myself, but maybe the HAL would be better to make it usable for people, but with the documentation clearly stating that it will later be moved to |
Status updateIn preparation for temporary publication in the HAL, I merged both monotonics into a new branch with a clean commit history, refactored a bit, and documented everything. I also broke the stress-test PyGamer program out into a separate project, since it does not really have anything to do with the PyGamer game I am working on (i.e. the project it was a branch in before). Remaining tasks:
|
Hi there! Thanks for reaching out in rtic-rs/rtic#995 :) Exciting with monotonics / more RTIC v2 support in atsamd-hal. We discussed this endeavor in RTIC's most recent weekly meeting, but tl;dr is that we're happy to help out with alpha/regular release(s) of How does this align with the other PR for atsamd21? |
@AfoHT The other PR is for a timer-based monotonic, whereas this uses the RTC peripheral. For sure though they would share some general ATSAMD infrastructure in If there is a mechanism to mark the RTC monotonic as experimental in |
Slightly related, I've been working on an implemenation of embassy-time-driver using the @kyp44 I've come across the same issues that you are having with the counter lagging the compare interrupt and always stepping in 4. I have found that enabling the RTC prescaler to at least div4 |
@sjroe Glad to hear this! I'm not sure how embassy time drivers compare with RTIC monotonics, but it would be great to be able to collaborate on this and maybe even have some code re-use if possible. My next experiment was actually going to be to play around with the prescaler and see how that affects the counter jumping, though you just provided the answer! I'll confirm these results as well. I'm still testing things and dealing with a few other strange issues, but the way I'm currently solving the compare lag issue is to simply wait for the counter to change first in the interrupt handler before doing anything else. So far, this seems to suffice to ensure that the counter is at least the compare value (which is critical for RTIC in particular) before any other logic is done. In the past I had a more complicated solution, but simply always waiting is much more simple and straightforward. What chip are you using to test with? You probably already read that I am using the PyGamer, which features the SAMD51J. I'd also invite you to stop over at the ATSAMD Matrix channel for live discussion. My time to work on this stuff is a bit limited lately, but I typically have that tab open when working on it. |
Status Update@sjroe I observed the same behavior when I enabled the prescalar, which was the same in both I have finally gotten both monotonics working robustly, with even the stress test using the 32 kHz clock and the @AfoHT I would like to at least get these compiling for all chips while it still resides in the HAL, but I am only able to test them on the |
Sounds good to me, I think enabling others to test it out and report issues, just as you suggested in the "alternative approach", is the way to go. To compare, to my knowledge we have not tested every single stm32, every single nrf etc. 👍 |
Want to solicit opinions from both HAL and RTIC folks. Though I cannot test it, I am adding SAMD11/21 support to these RTC monotonics, which is requiring only some pretty minor alternate compilation paths so far. However, I discovered that the RTC source clock is set in a completely different way for these chips. In particular, on the SAMx5x chips it is simply set to one of four values in a OSC32KCTRL register. However, on the SAMD11/21 chips one must setup a normal peripheral clock using the generic clock controller, which is more complicated. This led me to do some research, and I noticed that:
Currently these SAMD RTC monotonics do set up the RTC clock source, assuming the SAMx5x way of doing that. So I am asking if it makes more sense to likewise have these SAMD RTC monotonics not set the RTC clock sources, perhaps mentioning in their documentation that it needs to be configured ahead of time. This avoids re-inventing that wheel, especially in the more complicated case of the SAMD11/21 chips. EDIT: I would actually pose the same question about the ensuring that the RTC clock is enabled via the power manager (SAMD11/21) or the main clock (SAMx5x), noting that, on reset, the RTC clock is already enabled for all chips. As an example of when this can be problematic, requiring an TLDR: Should it be the RTC monotonics' responsibility to set and/or enable the RTC clock source? |
@kyp44, I haven't looked at
I would argue no. Generally speaking, I don't think libraries should set up clocks. That should be the user's responsibility. The obvious exception is if that library's explicit purpose is to set up clocks, but I don't think this case fits. The purpose of the monotonic implementation isn't to set up the clock, it's to implement a feature for RTIC. Instead, I think the implementation should require proof that you've set up a clock correctly. I think that's probably easy with |
@bradleyharden I agree that simply requiring proof that the clock has been configured would be ideal. Looks like this is easy with v2 but that no corresponding proof type exists in v1 for the SAMx5x chips. A proof type does exist for the SAMD11/21 chips though (for which there is a only a single clock API). I chalk this situation up to the fact that the RTC source is just another generic clock on the SAMD11/21 but is a bit of a special animal on the SAMx5x. Additionally it seems like, with v1 (on SAMx5x) you actually can only set the RTC clock to be the 1 kHz internal clock or the 32 kHz external clock. So for example, there is no way to select the internal 32 kHz clock. For these reasons, if we want to require proof, I would recommend requiring v2 on SAMx5x platforms. In both cases there is also the issue that the clock APIs make the RTC clock frequency available only at run time, whereas the monotonics need it at compile time, so the end user would have to specify this anyway when setting up the monotonic. The last issue is there was some uneasiness about having |
Status updateI am confident that the montonics are now ready for prime time. The robustness issues have all been resolved, and I refactored things so that the RTC mode and the monotonic type (basic or half-period counting) are decoupled. This enables using half-period counting in mode 0 for SAMx5x chips that have two compares in this mode, which it now does. Everything has been documented. I am ready to migrate them to Another approach might be to temporarily release/merge the montonics into the HAL so they can start being used, as we were initially planning to do, and then move them to I also tested the HAL Lastly, currently RFCRequesting comments for:
|
Great work! While I don't have complete answers to your questions, I do have some thoughts that may be useful.
That is definitely tricky. I think it would make sense for rtic-monotonics to require proof of a correct clock setup, although that means that it now depends on atsamd-hal. I don't think rtic-monotonics has historically depended on a HAL directly; maybe we could check with their maintainers to see if it's something they wish to support. One thing to watch out for is that our hal is still being heavily iterated on, and we publish breaking changes fairly regularly. While not ideal, this can somewhat be worked around.
Absolutely, I don't see a downside to that.
The way I see rtic-monotonics, and I'm happy to be corrected if I'm wrong, is that it provides an easy to use and opinionated way of getting a global timer working. There's something to be said about limiting the options and providing a sane set of defaults; users who have more specialized requirements can dive deeper in the specifics of the different peripherals. |
@jbeaurivage Thanks! Regarding the release issue, it is definitely the case that Regarding the RTC abstraction crate, I'm going to move forward with this as well as work on fixing I just posted a comment here in the hopes of getting some feeback on the above issue was well as limiting monotonic options. |
Hi, I'm new here. I'm working on a game targeting the PyGamer, which has a nice BSP here. Being a game, using async Rust is extremely valuable for me, and information on using async on SAMD processors seems to be very scare. It doesn't seem like Embassy supports SAMD processors at all, but RTIC v2 now uses async for software tasks, whereas v1 did not.
The HAL crate here does support RTIC (via the
rtic
feature), but it only seems to support RTIC v1 by implementing the oldMonotonic
trait. Things have changed with v2 and now there is a differentMonotonic
trait that has to be implemented, which involves some async functions.The current paradigm for RTIC v2 is to select the time driver from the
rtic_monotonics
crate, in which the type that implementsMonotonic
is created using macros instead of passing it to theapp
RTIC macro as before. Looking at the code for the supported microcontrollers (noting that SAMD is not among them), it seems likeMonotonic
is most easily implemented indirectly by instead implementing the simplerTimerQueueBackend
trait, which does not involve any async methods.I think I have enough embedded experience to implement an RTC-based driver for RTIC v2. Given that the v1 driver was implemented in this HAL instead of within the RTIC project, I wanted to solicit opinions on whether a new v2 driver should also just live with the HAL here (including relevant macros), or by adding SAMD support to the RTIC project? Thoughts?
NOTE: I am aware that I can already use RTIC v2 on SAMD chips using the
SysTick
driver that ships with RTIC, but I want a more efficient driver that does not generate frequent, periodic interrupts.The text was updated successfully, but these errors were encountered: