ESP32 Virtual Timers??

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
jdts
Posts: 18
Joined: Mon Dec 23, 2019 2:55 pm

ESP32 Virtual Timers??

Post by jdts » Sun May 17, 2020 4:00 am

Does MP actually support virtual timers on ESP32? The documentation sure claims so, but the example given to demonstrate it doesn't even run correctly, and:

Code: Select all

>>> Timer(-1)==Timer(-1)
True

patrickw
Posts: 12
Joined: Sat Feb 01, 2020 7:51 pm
Contact:

Re: ESP32 Virtual Timers??

Post by patrickw » Sun May 17, 2020 5:58 am

What board and firmware are you running?

This is my setup IDF4 nightly build:

Code: Select all

>>> import os
>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.12.0',
version='v1.12-452-g801f7dca7 on 2020-05-14', machine='TinyPICO with ESP32-PICO-D4')
This ran fine on my board.

Code: Select all

from machine import Timer

tim = Timer(-1)
tim.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
I don't understand the point of Timer(-1)==Timer(-1) but it is also True on my board.

jdts
Posts: 18
Joined: Mon Dec 23, 2019 2:55 pm

Re: ESP32 Virtual Timers??

Post by jdts » Sun May 17, 2020 7:05 pm

Code: Select all

>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.12.0', version='v1.12-35-g10709846f on 2019-12-29', machine='ESP32 module with ESP32')
Yes you can run one timer callback, but the example has two calls with a virtual timer. You never see the "1", since the 2nd overwrites the first, as it would for a fixed hardware timer:

Code: Select all

=== 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))
>>> 2
2
2
2
2
2
2
2
2
2
...
It appears there are only 4 timers on the ESP32, and -1 is the same as 3:

Code: Select all

>>> Timer(-1) == Timer(0)
False
>>> Timer(-1) == Timer(3)
True

nevercast
Posts: 6
Joined: Tue Oct 22, 2019 11:37 pm

Re: ESP32 Virtual Timers??

Post by nevercast » Sun Jul 19, 2020 11:12 pm

At the time of writing this reply, the ESP32 does not have support for Virtual Timers. The docs are unfortunately incorrect in this regard. Perhaps an oversight when they were originally copied from either the WiPy1 or ESP8266. I am not sure which.

I've attempted to fix the docs here: https://github.com/micropython/micropython/pull/6258

Hopefully in the future the ESP32 port will use the FreeRTOS Software Timers.

shaoziyang
Posts: 337
Joined: Sun Apr 17, 2016 1:55 pm

Re: ESP32 Virtual Timers??

Post by shaoziyang » Mon Jul 20, 2020 12:46 am

You may use Timer(-1), Timer(-2)... for different virtual timer.

User avatar
mattyt
Posts: 301
Joined: Mon Jan 23, 2017 6:39 am

Re: ESP32 Virtual Timers??

Post by mattyt » Mon Jul 20, 2020 4:17 am

shaoziyang wrote:
Mon Jul 20, 2020 12:46 am
You may use Timer(-1), Timer(-2)... for different virtual timer.
Currently that's not correct on the ESP32; virtual timers are not implemented.

See Josh's PR #6258 for more details. In short there are four hardware timers and using -1 (and -2) actually maps to one of those timers due to a defect.

shaoziyang
Posts: 337
Joined: Sun Apr 17, 2016 1:55 pm

Re: ESP32 Virtual Timers??

Post by shaoziyang » Mon Jul 20, 2020 12:58 pm

mattyt wrote:
Mon Jul 20, 2020 4:17 am
shaoziyang wrote:
Mon Jul 20, 2020 12:46 am
You may use Timer(-1), Timer(-2)... for different virtual timer.
Currently that's not correct on the ESP32; virtual timers are not implemented.

See Josh's PR #6258 for more details. In short there are four hardware timers and using -1 (and -2) actually maps to one of those timers due to a defect.
Thanks for the tip

shaoziyang
Posts: 337
Joined: Sun Apr 17, 2016 1:55 pm

Re: ESP32 Virtual Timers??

Post by shaoziyang » Mon Jul 20, 2020 12:59 pm

mattyt wrote:
Mon Jul 20, 2020 4:17 am
shaoziyang wrote:
Mon Jul 20, 2020 12:46 am
You may use Timer(-1), Timer(-2)... for different virtual timer.
Currently that's not correct on the ESP32; virtual timers are not implemented.

See Josh's PR #6258 for more details. In short there are four hardware timers and using -1 (and -2) actually maps to one of those timers due to a defect.
Thanks for the tip

Post Reply