-
Notifications
You must be signed in to change notification settings - Fork 65
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
MicroPython CANPico: Is the received CAN messages delayed by the GC? #16
Comments
Frame are received by interrupt handler, and the garbage collector is run with interrupts enabled, so they will continue to be received during GC. There is a very large (128 frame) receive FIFO filled by the ISR, so the recv() call can be locked out for some time (several milliseconds) without frames being dropped. The XIP flash of the RP2040 does cause interrupts to be locked out for some time. You can read more about the priority inversion effects of the XIP in my blog post here: https://kentindell.github.io/2021/03/05/pico-priority-inversion/ The ISR runs from RAM to minimize the effects of XIP flash. And the MCP2517FD / MCP2518FD CAN controller itself is configured to use a large receive FIFO so even if the ISR is delayed by XIP of lower priority code, frames shouldn't be dropped. Finally, hardware CAN ID filters can be used to discard unwanted CAN frames so that they don't fill up the buffers. |
Hello @kentindell I took a time to read that docs and others to be sure that I will not say something wrong, but maybe still can happen! And want to buy some
I get this
Thank you in advance! |
Yes, it's possible that GC delays the Python code. Also the time taken to print to the REPL console is large. But the drivers created a very large receive buffer so that if the GC time is not long then frames should not be dropped. There are also status indicators that can be included in the FIFO to indicate if frames have been dropped (and how many). The IRQ pin from the CAN controller is necessary for the drivers to operate. If you want to debug using a logic analyzer then you can use the trigger pin included for this purpose. The SOF pin is not necessary: the drivers do not use the pin. |
Hello @kentindell Understood! That I understand very well about the large However, let me please suggest you a work around that maybe can works, but just you can tell me if is possible to implement on the If it's not too much to ask, I'd appreciate it if you could answer my question number 2 above. Thank you very much! |
I see what you're trying to do now. Yes, GC is way too slow. You could insert a callback into the current ISR so that it will call into your code with the parameters of the received CAN frame. It could possibly even run on the second CPU core, while the ISR ran to completion (perhaps discarding the frame if the callback returned a certain value). You could code this callback either in Python or in C. If you want to use Python then it has to be carefully coded not use dynamic memory allocation, of course, along the lines you outlined. It could even use the Python threading support to start a worker thread on the second CPU core. It would require a custom firmware build but if the idea works out we could add the support in to the main firmware. |
Hi @kentindell This feature will be amazing for the real-time execution task via CAN :) Maybe less than My knowledge about C is very basic and I never builded by myself a MicroPython for the RPico - I always get the ready binary from MicroPython website. But I want to learn how to build because my intention is to apply the CANPico MicroPython patch to the original MicroPython. Anyway, unfortunately I have no knowledge to do modifications on the CANPico firmware to check if that idea really works.
I think this idea is excellent, because that can help to take out If you really think to implement this feature, my suggestion is it to be configurable by a Python Let me know if you would like to open a new issue with subject "Feature request..." for this new feature - I can do that :) Thank you very much for your attention! This is a great project! |
Hello.
Congratulations for the great project supporting CANBUS on Rp2040 using MicroPython!
I would like to use MicroPython on the rp2040/RPico (CANPico) for realtime communication using CANPico firmware, and I would like to know what is the behaviour if CAN received message when Garbage Collector (GC) is running. Will the received/read message delayed by the GC, or the CAN will interrupt the GC to handle the CAN message?
Thank you very much!
The text was updated successfully, but these errors were encountered: