Skip to content

Commit 5f46f1b

Browse files
committed
docs: Document the cross-port Timer hard= option.
Update the main machine.Timer specification, and any references to hard/soft interrupts in port-specific documentation. There is a separate copy of the machine.Timer documentation for the pyboard, so update that too to keep everything consistent. Signed-off-by: Chris Webb <chris@arachsys.com>
1 parent c028484 commit 5f46f1b

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

docs/esp32/quickref.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ with a timer ID of 0, 0 and 1, or from 0 to 3 (inclusive)::
287287
The period is in milliseconds. When using UART.IRQ_RXIDLE, timer 0 is needed for
288288
the IRQ_RXIDLE mechanism and must not be used otherwise.
289289

290+
Timer callbacks are scheduled as soft interrupts on this port; hard
291+
callbacks are not implemented. Specifying ``hard=True`` will raise
292+
a ValueError.
293+
290294
Virtual timers are not currently supported on this port.
291295

292296
.. _Pins_and_GPIO:

docs/esp8266/quickref.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ with timer ID of -1::
108108

109109
The period is in milliseconds.
110110

111+
By default, timer callbacks are scheduled as soft interrupts on this port.
112+
Specify ``hard=True`` to run them in hard interrupt context instead.
113+
111114
Pins and GPIO
112115
-------------
113116

docs/library/machine.Timer.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ Timer callbacks.
1818

1919
Memory can't be allocated inside irq handlers (an interrupt) and so
2020
exceptions raised within a handler don't give much information. See
21-
:func:`micropython.alloc_emergency_exception_buf` for how to get around this
22-
limitation.
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``.
2324

2425
If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>`
2526
instead of this class.
@@ -38,7 +39,7 @@ Constructors
3839
Methods
3940
-------
4041

41-
.. method:: Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)
42+
.. method:: Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None, hard=True)
4243

4344
Initialise the timer. Example::
4445

@@ -76,6 +77,19 @@ Methods
7677
will occur upon timer expiration:
7778
``TypeError: 'NoneType' object isn't callable``
7879

80+
- ``hard`` can be one of:
81+
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.
86+
- ``False`` - The callback will be scheduled as a soft interrupt,
87+
allowing it to allocate but possibly also introducing
88+
garbage-collection delays and jitter.
89+
90+
The default value of this option is port-specific for historical
91+
reasons.
92+
7993
.. method:: Timer.deinit()
8094

8195
Deinitialises the timer. Stops the timer, and disables the timer peripheral.

docs/library/pyb.Timer.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Constructors
6262
Methods
6363
-------
6464

65-
.. method:: Timer.init(*, freq, prescaler, period, mode=Timer.UP, div=1, callback=None, deadtime=0, brk=Timer.BRK_OFF)
65+
.. method:: Timer.init(*, freq, prescaler, period, mode=Timer.UP, div=1, callback=None, deadtime=0, brk=Timer.BRK_OFF, hard=True)
6666

6767
Initialise the timer. Initialisation must be either by frequency (in Hz)
6868
or by prescaler and period::
@@ -115,6 +115,18 @@ Methods
115115
``mode=Pin.ALT, alt=Pin.AFn_TIMx``. The pin's GPIO input features are
116116
available in alt mode - ``pull=`` , ``value()`` and ``irq()``.
117117

118+
- ``hard`` can be one of:
119+
120+
- ``True`` - The callback will be executed in hard interrupt
121+
context, which minimises delay and jitter but is subject to the
122+
limitations described in :ref:`isr_rules` including being unable
123+
to allocate on the heap.
124+
- ``False`` - The callback will be scheduled as a soft interrupt,
125+
allowing it to allocate but possibly also introducing
126+
garbage-collection delays and jitter.
127+
128+
The default value of this option is True.
129+
118130
You must either specify freq or both of period and prescaler.
119131

120132
.. method:: Timer.deinit()

0 commit comments

Comments
 (0)