docs/library/machine.Timer: Explain the id parameter in more detail.
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c5_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_p4) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled

As noted in discussion on PR #18263, the id parameter is optional on ports
that support virtual timers.

Add some more general explanation of hardware vs virtual timers, and remove
redundant documentation about timer callbacks in favour of the isr_rules
page.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton
2025-11-05 11:22:18 +11:00
committed by Damien George
parent 5ea9a2662d
commit 0b1a6bebae
3 changed files with 38 additions and 43 deletions

View File

@@ -4,37 +4,47 @@
class Timer -- control hardware timers class Timer -- control hardware timers
====================================== ======================================
Hardware timers deal with timing of periods and events. Timers are perhaps Timer class provides the ability to trigger a Python callback function after an
the most flexible and heterogeneous kind of hardware in MCUs and SoCs, expiry time, or periodically at a regular interval.
differently greatly from a model to a model. MicroPython's Timer class
defines a baseline operation of executing a callback with a given period
(or once after some delay), and allow specific boards to define more
non-standard behaviour (which thus won't be portable to other boards).
See discussion of :ref:`important constraints <machine_callbacks>` on The available features and restrictions of Timer objects vary depending on the
Timer callbacks. MicroPython board and port.
.. note::
Memory can't be allocated inside irq handlers (an interrupt) and so
exceptions raised within a handler don't give much information. See
:func:`micropython.alloc_emergency_exception_buf` for how to get around
this limitation, which applies to all callbacks of Timers created with
``hard=True``.
If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>` If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>`
instead of this class. instead of this class.
Timer Types
-----------
There are two types of Timer in MicroPython, but not all ports support both:
- Virtual timers. These are managed in software, and are generally more
flexible. Multiple virtual timers can be constructed and active at once. The
``id`` of a virtual timer is ``-1``. Not all ports support virtual timers, but
it's recommended to use them when available.
- Hardware timers. Hardware timers have integer ``id`` values starting at ``0``.
The number of available ``id`` values is determined by the hardware. Hardware
timers may be more accurate for very fine sub-millisecond timing (especially
when ``hard=True`` is supported and set, see :ref:`isr_rules`.) Most
microcontroller ports support hardware timers, except Zephyr and RP2 which
only support virtual timers.
Constructors Constructors
------------ ------------
.. class:: Timer(id, /, ...) .. class:: Timer(id, /, ...)
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a Construct a new Timer object with the given ``id``.
virtual timer (if supported by a board).
On ports which support virtual timers the ``id`` parameter is optional - the
default value is ``-1`` which constructs a virtual timer.
On ports which support hardware timers, setting the ``id`` parameter to a
non-negative integer determines which timer to use.
``id`` shall not be passed as a keyword argument. ``id`` shall not be passed as a keyword argument.
See ``init`` for parameters of initialisation. Any additional parameters are handled the same as :func:`Timer.init()`.
Methods Methods
------- -------
@@ -73,22 +83,22 @@ Methods
- ``callback`` - The callable to call upon expiration of the timer period. - ``callback`` - The callable to call upon expiration of the timer period.
The callback must take one argument, which is passed the Timer object. The callback must take one argument, which is passed the Timer object.
The ``callback`` argument shall be specified. Otherwise an exception The ``callback`` argument shall be specified. Otherwise an exception
will occur upon timer expiration: will occur upon timer expiration:
``TypeError: 'NoneType' object isn't callable`` ``TypeError: 'NoneType' object isn't callable``
- ``hard`` can be one of: - ``hard`` can be one of:
- ``True`` - The callback will be executed in hard interrupt - ``True`` - The callback will be executed in hard interrupt context,
context, which minimises delay and jitter but is subject to the which minimises delay and jitter but is subject to the limitations
limitations described in :ref:`isr_rules` including being unable described in :ref:`isr_rules`. Not all ports support hard interrupts,
to allocate on the heap. see the port documentation for more information.
- ``False`` - The callback will be scheduled as a soft interrupt, - ``False`` - The callback will be scheduled as a soft interrupt,
allowing it to allocate but possibly also introducing allowing it to allocate but possibly also introducing
garbage-collection delays and jitter. garbage-collection delays and jitter.
The default value of this option is port-specific for historical The default value of this parameter is port-specific for historical reasons.
reasons.
.. method:: Timer.deinit() .. method:: Timer.deinit()

View File

@@ -18,16 +18,6 @@ defines a baseline operation of executing a callback with a given period
(or once after some delay), and allow specific boards to define more (or once after some delay), and allow specific boards to define more
non-standard behaviour (which thus won't be portable to other boards). non-standard behaviour (which thus won't be portable to other boards).
See discussion of :ref:`important constraints <machine_callbacks>` on
Timer callbacks.
.. note::
Memory can't be allocated inside irq handlers (an interrupt) and so
exceptions raised within a handler don't give much information. See
:func:`micropython.alloc_emergency_exception_buf` for how to get around this
limitation.
Constructors Constructors
------------ ------------
@@ -134,6 +124,9 @@ Methods
``TimerWiPy.ONE_SHOT``. In the case that mode is ``TimerWiPy.PWM`` then trigger must be equal to ``TimerWiPy.ONE_SHOT``. In the case that mode is ``TimerWiPy.PWM`` then trigger must be equal to
``TimerWiPy.MATCH``. ``TimerWiPy.MATCH``.
Note that callback handlers are hard interrupts, and the constraints described in :ref:`isr_rules`
apply when they are executed.
Returns a callback object. Returns a callback object.
.. method:: timerchannel.freq([value]) .. method:: timerchannel.freq([value])

View File

@@ -11,14 +11,6 @@ and unrestricted access to and control of hardware blocks on a system
malfunction, lockups, crashes of your board, and in extreme cases, hardware malfunction, lockups, crashes of your board, and in extreme cases, hardware
damage. damage.
.. _machine_callbacks:
A note of callbacks used by functions and class methods of :mod:`machine` module:
all these callbacks should be considered as executing in an interrupt context.
This is true for both physical devices with IDs >= 0 and "virtual" devices
with negative IDs like -1 (these "virtual" devices are still thin shims on
top of real hardware and real hardware interrupts). See :ref:`isr_rules`.
Memory access Memory access
------------- -------------