← index #7871PR #18263
Related · medium · value 2.539
QUERY · ISSUE

ESP32C3 timer issue

openby bdambrosioopened 2021-10-02updated 2026-03-24
port-esp32

When using the micropython ble repl example (works on regular esp32), I receive the following error:
dupterm: Exception in write() method, deactivating: Traceback (most recent call last):
File "bluetooth_repl.py", line 74, in write
File "bluetooth_repl.py", line 29, in schedule_in
OSError: (-258, 'ESP_ERR_INVALID_ARG')

this traces back to:
_timer.init(mode=machine.Timer.ONE_SHOT, period=delay_ms, callback=_wrap)

I have tried creating timers from repl for the ESP32C3, and always get the error above on calling init, regardless of the subset of arguments provided or the values used. For reference:

from machine import Timer
t1=Timer(-1)
t1
Timer(3fccd9c0; alarm_en=1073741823, auto_reload=1070351008, counter_en=1070350504)
t1.init(mode=Timer.ONE_SHOT, period=10, callback=p)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: (-258, 'ESP_ERR_INVALID_ARG')

I realize C3 support is a work in progress, and am delighted overall, tnx! Perhaps the timers aren't fully supported yet on this device?

tnx

CANDIDATE · PULL REQUEST

esp32/machine_timer.*: Adding support for Timer(-1) soft/virtual timers.

openby LucienMPopened 2025-10-13updated 2026-01-28
enhancementport-esp32

Summary

Soft timers using Timer(-1) has been missing, but appeared to work due to type checking not being right on the ESP32 platforms.
The timers are limited to system hardware timers that are, at most, limited to 4. However there are times when more timers would be very useful so I added soft timers.

See the discussion here: https://github.com/orgs/micropython/discussions/18056

before 1.26 AFAICS the Timer( id, ... ) would take the id and wouldn't check what format the id was assuming it was an integer from 0..MAX in the type expected as GROUP:INDEX. So when specifying -1 this would erroneously select a hardware timer, but the user of "machine.Timer" would have expected, and assumed almost limitless amount of, soft/virtual timers. ESP32 is limited to a max of 4 timers, but depends on the hardware. There would be collision between timers if you allocated more.

Starting in 1.26 there is a check for the value being 0..MAX, and so -1 is now correctly rejected.

I add this functionality for soft timers, the -1 flag, to the ESP32 library.

Starting in 1.27 it seems the Timer(nn,...,hard=...) flag is also now supported.

Testing

I tested on Spotpear ESP32 board for which I am running LVGL + MicroPython 1.26 and wrote this extension for.
See: https://github.com/Spotpear-Scratch/board_firmware on the v1.26 branch

I tested manually thru console with the following set of conditions:

  1. creating timer
  2. creating timer, then initializing it
  3. creating timer, initialize to period, then de-initializing it
  4. creating timer, initialize to period, then checking its value/printing
  5. creating timer, initialize to period, with repeating
  6. creating timer, initialize to frequency
  7. creating timers with invalid timing
  8. creating timer, try to access value

Trade-offs and Alternatives

This is an expansion to existing machine.Timer so code size will increase by only a small amount (few functions, and a few additional if statements plus memory structure change for timer), it expands the number of timers available from the less than 4 hardware timers, adding an additional soft/virtual set up to as many as the user has memory from albeit at a lower resolution.

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