Docu: Multiple machine.Timer() instances on ESP8266
There is sadly only a little docu on the timer usage on ESP8266. It shows this example:
from machine import Timer
tim = Timer(-1)
tim.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t:print(2))
As it uses the same timer only "2" is printed every 2 seconds, the one_shot "1" is never printed. I tried two instances of Timer(-1) but still only the last one was active. I just tried Timer(-2) and it worked, with Timer(1) and Timer(2) it also worked:
tim1 = Timer(1)
tim2 = Timer(2)
tim1.init(period=2500, mode=Timer.ONE_SHOT, callback=lambda t: print("once"))
tim2.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: print("periodic"))
sleep(3.5)
tim2.deinit()
# expected printout:
# periodic
# periodic
# once
# periodic
There is one thing I'm not sure about I'd like to understand before starting a pull request on the docu:
- Is only one virtual (RTOS -based) timer possible on the ESP8266? machine docu mentions Timer(-1) creates a virtual timer - which sounds good as it doesn't affect hardware.
- If I create a timer with another ID, will it interfere with PWM or sleep() or time()?
By the way, great project, love it:)
ESP32C6 Timers: Only Timer(0) and Timer(2) are functional for ESP32-C6 v1.24
Port, board and/or hardware
esp32-c6, generic board
MicroPython version
Micropython 1.24 custom compile only adding graphics library that does not use timer
Reproduction
not (likely) a build issue (I did not test on stock release firmware)
Expected behaviour
examples from Micropython documentation on Timer class:
WORKS CORRECTLY:
from machine import Timer
tim0 = Timer(0)
tim0.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(0))
FAILS:
from machine import Timer
tim1 = Timer(1)
tim1.init(period=2000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
WORKS CORRECTLY:
from machine import Timer
tim2 = Timer(2)
tim2.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(2))
FAILS:
from machine import Timer
tim3 = Timer(3)
tim3.init(period=2000, mode=Timer.ONE_SHOT, callback=lambda t:print(3))
Observed behaviour
code for Timer0 returns a '0'
code for Timer1 returns nothing
code for Timer2 returns a '2'
code for Timer3 returns nothing
Additional Information
The fix is to make the timer code for the ESP32-C6 behave like the ESP32-C3:
it appears the fix should be in the esp32 port file machine_timer.c line 79 which should be changed from:
#if CONFIG_IDF_TARGET_ESP32C3
to
#if CONFIG_IDF_TARGET_ESP32C3 or CONFIG_IDF_TARGET_ESP32C6 (or something like that because I do not know the syntax)
Code of Conduct
Yes, I agree