Skip to content

Commit 0b1a6be

Browse files
projectgusdpgeorge
authored andcommitted
docs/library/machine.Timer: Explain the id parameter in more detail.
As noted in discussion on PR micropython#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 <[email protected]>
1 parent 5ea9a26 commit 0b1a6be

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

docs/library/machine.Timer.rst

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,47 @@
44
class Timer -- control hardware timers
55
======================================
66

7-
Hardware timers deal with timing of periods and events. Timers are perhaps
8-
the most flexible and heterogeneous kind of hardware in MCUs and SoCs,
9-
differently greatly from a model to a model. MicroPython's Timer class
10-
defines a baseline operation of executing a callback with a given period
11-
(or once after some delay), and allow specific boards to define more
12-
non-standard behaviour (which thus won't be portable to other boards).
7+
Timer class provides the ability to trigger a Python callback function after an
8+
expiry time, or periodically at a regular interval.
139

14-
See discussion of :ref:`important constraints <machine_callbacks>` on
15-
Timer callbacks.
16-
17-
.. note::
18-
19-
Memory can't be allocated inside irq handlers (an interrupt) and so
20-
exceptions raised within a handler don't give much information. See
21-
:func:`micropython.alloc_emergency_exception_buf` for how to get around
22-
this limitation, which applies to all callbacks of Timers created with
23-
``hard=True``.
10+
The available features and restrictions of Timer objects vary depending on the
11+
MicroPython board and port.
2412

2513
If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>`
2614
instead of this class.
2715

16+
Timer Types
17+
-----------
18+
19+
There are two types of Timer in MicroPython, but not all ports support both:
20+
21+
- Virtual timers. These are managed in software, and are generally more
22+
flexible. Multiple virtual timers can be constructed and active at once. The
23+
``id`` of a virtual timer is ``-1``. Not all ports support virtual timers, but
24+
it's recommended to use them when available.
25+
- Hardware timers. Hardware timers have integer ``id`` values starting at ``0``.
26+
The number of available ``id`` values is determined by the hardware. Hardware
27+
timers may be more accurate for very fine sub-millisecond timing (especially
28+
when ``hard=True`` is supported and set, see :ref:`isr_rules`.) Most
29+
microcontroller ports support hardware timers, except Zephyr and RP2 which
30+
only support virtual timers.
31+
2832
Constructors
2933
------------
3034

3135
.. class:: Timer(id, /, ...)
3236

33-
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
34-
virtual timer (if supported by a board).
37+
Construct a new Timer object with the given ``id``.
38+
39+
On ports which support virtual timers the ``id`` parameter is optional - the
40+
default value is ``-1`` which constructs a virtual timer.
41+
42+
On ports which support hardware timers, setting the ``id`` parameter to a
43+
non-negative integer determines which timer to use.
44+
3545
``id`` shall not be passed as a keyword argument.
3646

37-
See ``init`` for parameters of initialisation.
47+
Any additional parameters are handled the same as :func:`Timer.init()`.
3848

3949
Methods
4050
-------
@@ -73,22 +83,22 @@ Methods
7383

7484
- ``callback`` - The callable to call upon expiration of the timer period.
7585
The callback must take one argument, which is passed the Timer object.
86+
7687
The ``callback`` argument shall be specified. Otherwise an exception
7788
will occur upon timer expiration:
7889
``TypeError: 'NoneType' object isn't callable``
7990

8091
- ``hard`` can be one of:
8192

82-
- ``True`` - The callback will be executed in hard interrupt
83-
context, which minimises delay and jitter but is subject to the
84-
limitations described in :ref:`isr_rules` including being unable
85-
to allocate on the heap.
93+
- ``True`` - The callback will be executed in hard interrupt context,
94+
which minimises delay and jitter but is subject to the limitations
95+
described in :ref:`isr_rules`. Not all ports support hard interrupts,
96+
see the port documentation for more information.
8697
- ``False`` - The callback will be scheduled as a soft interrupt,
8798
allowing it to allocate but possibly also introducing
8899
garbage-collection delays and jitter.
89100

90-
The default value of this option is port-specific for historical
91-
reasons.
101+
The default value of this parameter is port-specific for historical reasons.
92102

93103
.. method:: Timer.deinit()
94104

docs/library/machine.TimerWiPy.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ defines a baseline operation of executing a callback with a given period
1818
(or once after some delay), and allow specific boards to define more
1919
non-standard behaviour (which thus won't be portable to other boards).
2020

21-
See discussion of :ref:`important constraints <machine_callbacks>` on
22-
Timer callbacks.
23-
24-
.. note::
25-
26-
Memory can't be allocated inside irq handlers (an interrupt) and so
27-
exceptions raised within a handler don't give much information. See
28-
:func:`micropython.alloc_emergency_exception_buf` for how to get around this
29-
limitation.
30-
3121
Constructors
3222
------------
3323

@@ -134,6 +124,9 @@ Methods
134124
``TimerWiPy.ONE_SHOT``. In the case that mode is ``TimerWiPy.PWM`` then trigger must be equal to
135125
``TimerWiPy.MATCH``.
136126

127+
Note that callback handlers are hard interrupts, and the constraints described in :ref:`isr_rules`
128+
apply when they are executed.
129+
137130
Returns a callback object.
138131

139132
.. method:: timerchannel.freq([value])

docs/library/machine.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ and unrestricted access to and control of hardware blocks on a system
1111
malfunction, lockups, crashes of your board, and in extreme cases, hardware
1212
damage.
1313

14-
.. _machine_callbacks:
15-
16-
A note of callbacks used by functions and class methods of :mod:`machine` module:
17-
all these callbacks should be considered as executing in an interrupt context.
18-
This is true for both physical devices with IDs >= 0 and "virtual" devices
19-
with negative IDs like -1 (these "virtual" devices are still thin shims on
20-
top of real hardware and real hardware interrupts). See :ref:`isr_rules`.
21-
2214
Memory access
2315
-------------
2416

0 commit comments

Comments
 (0)