-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
ports/esp32/machine_timer.c: Solves #4078, ESP32 timer reinitilization error #4811
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
ports/esp32/machine_timer.c: Solves #4078, ESP32 timer reinitilization error #4811
Conversation
…nitilization error
Thanks for the patch, but I'm not sure it's the right way to solve that issue of error 261. Active timers are put on a linked list (on the MicroPython side) and if you create a timer twice then it'll be placed twice on this list. I think it would be better to check for an existing timer on the list when creating one, and reuse it if found. |
Oh yes, didn't actually think of that! But now when checking the code, I don't believe that's the case. The init function calls machine_timer_disable which removes the existing timer from the list, before changing parameters and reenabling it. https://github.com/micropython/micropython/blob/master/ports/esp32/machine_timer.c#L179 |
But that doesn't stop there being multiple instances of Timer all pointing to the same underlying hardware resource, because these multiple Timer instances are distinct pointers to the MicroPython heap. |
Okay, I see. I can implement a reference counting solution. Just as a sanity check before I start, do you think the following would be a suitable solution:
Btw. I still believe the flags in the patch are necessary though, to solve the problem that occurs in programs such as this, which also raises OSError 261: timer0 = Timer(0)
timer0.init(period=1000, mode=Timer.PERIODIC, callback=my_callback)
timer0.deinit()
timer0.init(period=1000, mode=Timer.PERIODIC, callback=my_callback) |
I don't think it needs that. There are only 4 timers in total so just keeping them in the link list forever after creation is fine (until soft reset, via
That particular code runs fine for me, no error. |
The patch solves the problem where multiple Timer objects (e.g. multiple Timer(0) instances) could initialize multiple handles to the same internal timer. The list of timers is now maintained not for "active" timers (where init is called), but for all timers created. The timers are only removed from the list of timers on soft-reset (machine_timer_deinit_all).
Okay, check the latest commit. You are right, the flags are not necessary. The example raising OSError 261 above only appeared when re-executing the code after a soft-reboot, however, it works now without the flags. |
Thank you! Merged in de76f73 |
Solves the problem where multiple calls to
machine.Timer.init()
causes anOSError 261
on ESP32 boards.The fix is explained here: espressif/arduino-esp32#1869 (comment)
Solves issue #4078.