← index #8383PR #6258
Related · high · value 0.185
QUERY · ISSUE

Docu: Multiple machine.Timer() instances on ESP8266

openby manuel-91opened 2022-03-04updated 2022-10-19
port-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:

  1. 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.
  2. If I create a timer with another ID, will it interfere with PWM or sleep() or time()?

By the way, great project, love it:)

CANDIDATE · PULL REQUEST

docs/esp32: Fix machine.Timer quickref

closedby nevercastopened 2020-07-19updated 2020-07-24
docsport-esp32

ESP32 docs talks about Virtual Timers or Software Timers. This feature is NOT implemented on the ESP32. What's worse is that using timer id -1 fails silently as hardware timer 3 because negative one 0b1111_1111 is treated as 0b11 when masked into a group and index for the hardware configuration.

I've fixed the docs with demonstration of using the hardware timers instead. I have also tested these on my ESP32 boards, they work as expected. Happy for someone to review and verify if they feel compelled.

This issue was also mentioned in the forums here: https://forum.micropython.org/viewtopic.php?f=18&t=8365&sid=721b0f0ca8f2657ae634ab830185392b

CC @mattytrentini because Matt identified the issue I was having on Slack and related it to this forum post.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied