Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions reference/micropython/machine/Timer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Timer:
mode: int = PERIODIC,
period: int | None = None,
callback: Callable[[Timer], None] | None = None,
hard: bool | None = None,
):
"""
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
Expand All @@ -70,6 +71,7 @@ class Timer:
mode: int = PERIODIC,
freq: int | None = None,
callback: Callable[[Timer], None] | None = None,
hard: bool | None = None,
):
"""
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
Expand All @@ -88,6 +90,7 @@ class Timer:
mode: int = PERIODIC,
tick_hz: int | None = None,
callback: Callable[[Timer], None] | None = None,
hard: bool | None = None,
):
"""
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
Expand All @@ -104,6 +107,7 @@ class Timer:
mode: int = PERIODIC,
period: int | None = None,
callback: Callable[[Timer], None] | None = None,
hard: bool | None = None,
) -> None: ...
@overload
def init(
Expand All @@ -112,6 +116,7 @@ class Timer:
mode: int = PERIODIC,
freq: int | None = None,
callback: Callable[[Timer], None] | None = None,
hard: bool | None = None,
) -> None: ...
@overload
def init(
Expand All @@ -120,6 +125,7 @@ class Timer:
mode: int = PERIODIC,
tick_hz: int | None = None,
callback: Callable[[Timer], None] | None = None,
hard: bool | None = None,
) -> None:
"""
Initialise the timer. Example::
Expand Down Expand Up @@ -157,6 +163,19 @@ class Timer:
The ``callback`` argument shall be specified. Otherwise an exception
will occur upon timer expiration:
``TypeError: 'NoneType' object isn't callable``

- ``hard`` can be one of:

- ``True`` - The callback will be executed in hard interrupt
context, which minimises delay and jitter but is subject to the
limitations described in :ref:`isr_rules` including being unable
to allocate on the heap.
- ``False`` - The callback will be scheduled as a soft interrupt,
allowing it to allocate but possibly also introducing
garbage-collection delays and jitter.

The default value of this option is port-specific for historical
reasons.
"""
...

Expand Down
21 changes: 21 additions & 0 deletions tests/quality_tests/feat_machine/check_machine/check_Timer_hard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Test Timer with hard parameter (added in MicroPython v1.27+)

from machine import Timer


def mycallback(t):
pass


# Timer with hard interrupt context
tim = Timer(-1)
tim.init(mode=Timer.PERIODIC, freq=1000, callback=mycallback, hard=True)

# Timer with soft interrupt context
tim.init(mode=Timer.PERIODIC, period=100, callback=mycallback, hard=False)

# Timer with hard parameter using freq
tim.init(freq=500, callback=mycallback, hard=True)

# Constructor with hard parameter
tim2 = Timer(-1, mode=Timer.ONE_SHOT, period=1000, callback=mycallback, hard=False)
Loading