Raise exception from timer interrupt routine Micropython RP2040 #9229
Replies: 4 comments 7 replies
-
Just to clarify, what do you expect to happen with the raised exception? Interrupt the main thread?
|
Beta Was this translation helpful? Give feedback.
-
Just for interest I tried that. For some reason I could not put the raise into the lambda function. But in separate small function it worked.
|
Beta Was this translation helpful? Give feedback.
-
Here, the except code runs, as the print statement is executed. But the loop does not stop, since a is not modified. Putting all into a class does not help either. Hmm... |
Beta Was this translation helpful? Give feedback.
-
Sorry! I said the wrong thing in my first reply. The timer runs its callback via the scheduler (it's a soft interrupt in the context of https://docs.micropython.org/en/latest/reference/isr_rules.html ). Scheduled tasks have a built-in catch-all exception handler which just prints the exception out directly. If you want to signal from an interrupt handler, then you should do so with a global variable. from machine import Timer
import time
timeout = False
def r(t):
global timeout
timeout = True
timer = Timer(-1, period=3000, mode=Timer.ONE_SHOT, callback=r)
while True:
if timeout:
print("Timeout")
break
# otherwise keep working In general though it's likely easier to implement timeouts by measuring time taken.. import time
t_start = time.ticks_ms()
while True:
if time.ticks_diff(time.ticks_ms(), t_start) > 3000:
print("Timeout")
break
# otherwise keep working |
Beta Was this translation helpful? Give feedback.
-
Is it possible to raise an exception from a timer interrupt?
Having tried variations on this code there is likely something I just don't get! Thanks
Beta Was this translation helpful? Give feedback.
All reactions