Timer 1 & Timer 3 issues: Timer 1 never triggers | Timer 3 crashes 100% of time #14235
-
Hi, Trying to run the python code below on an ESP32-C3 results in a strange output/crash. from machine import Timer
__version__ = '20240403'
def timer0_callback(timer):
print('t0')
def timer1_callback(timer):
print('t1')
def timer2_callback(timer):
print('t2')
def timer3_callback(timer):
print('t3')
def main():
timer0 = Timer(0)
timer0.init(period=1000, mode=Timer.PERIODIC, callback=timer0_callback)
timer1 = Timer(1)
timer1.init(period=200, mode=Timer.PERIODIC, callback=timer1_callback)
timer2 = Timer(2)
timer2.init(period=50, mode=Timer.PERIODIC, callback=timer2_callback)
# timer3 = Timer(3)
# timer3.init(period=300, mode=Timer.PERIODIC, callback=timer3_callback)
while True:
pass
if __name__ == '__main__':
main() Outputs/behaviour:
Dump (with Timer 3 uncommented): MPY: soft reboot Core 0 register dump: Stack memory: ELF file SHA256: 03a427956fd9ca12 Rebooting... Using the following HW/SW:
Any ideas are highly appreciated, Tom |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Tight busy loops are critical in that they do not allow other tasks to run. Try for the wait loop:
You have to import idle in at the start of your script as well. Instead of idle, you can as well use a short sleep_ms(), like sleep_ms(1). |
Beta Was this translation helpful? Give feedback.
-
Hi Robert, Thank you for your great help. Seems I made a beginner mistake (assuming ESP32 covers all ESP32 subtypes and IDE not complaining on init of Timers 1 and 3, even aware of C3 variant and not checking the espressif datasheet). Have a good time Tom |
Beta Was this translation helpful? Give feedback.
-
The ESP32 C6 has the same behavior as the C3. Only Timer(0) and Timer(2) are allowed. Things get fun with the use of Timer(4), which overwrites Timer(0) and so on. |
Beta Was this translation helpful? Give feedback.
-
@dmoibm That behavior was fixed with commit 1360584 , PR #16439 in December. Using a recent preview only Timers 0 and 1 are allowed for ESP32C6. |
Beta Was this translation helpful? Give feedback.
Reading the C3 manual and looking into the ESP port code reveals:
Thus, on C3, you can use timer(0) and timer(2). Anything else is odd and should be prevented by the code. Maybe it's better on a C3 to just allow timer 0 and 1 and set the group always to 0.