Skip to content

Commit a08d84f

Browse files
CopilotJosverl
andauthored
Add hard parameter to machine.Timer signatures (#851)
* Initial plan * Add hard parameter to machine.Timer signatures Co-authored-by: Josverl <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Josverl <[email protected]> Co-authored-by: Jos Verlinde <[email protected]>
1 parent 2cd28c2 commit a08d84f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

reference/micropython/machine/Timer.pyi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Timer:
5252
mode: int = PERIODIC,
5353
period: int | None = None,
5454
callback: Callable[[Timer], None] | None = None,
55+
hard: bool | None = None,
5556
):
5657
"""
5758
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
@@ -70,6 +71,7 @@ class Timer:
7071
mode: int = PERIODIC,
7172
freq: int | None = None,
7273
callback: Callable[[Timer], None] | None = None,
74+
hard: bool | None = None,
7375
):
7476
"""
7577
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
@@ -88,6 +90,7 @@ class Timer:
8890
mode: int = PERIODIC,
8991
tick_hz: int | None = None,
9092
callback: Callable[[Timer], None] | None = None,
93+
hard: bool | None = None,
9194
):
9295
"""
9396
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
@@ -104,6 +107,7 @@ class Timer:
104107
mode: int = PERIODIC,
105108
period: int | None = None,
106109
callback: Callable[[Timer], None] | None = None,
110+
hard: bool | None = None,
107111
) -> None: ...
108112
@overload
109113
def init(
@@ -112,6 +116,7 @@ class Timer:
112116
mode: int = PERIODIC,
113117
freq: int | None = None,
114118
callback: Callable[[Timer], None] | None = None,
119+
hard: bool | None = None,
115120
) -> None: ...
116121
@overload
117122
def init(
@@ -120,6 +125,7 @@ class Timer:
120125
mode: int = PERIODIC,
121126
tick_hz: int | None = None,
122127
callback: Callable[[Timer], None] | None = None,
128+
hard: bool | None = None,
123129
) -> None:
124130
"""
125131
Initialise the timer. Example::
@@ -157,6 +163,19 @@ class Timer:
157163
The ``callback`` argument shall be specified. Otherwise an exception
158164
will occur upon timer expiration:
159165
``TypeError: 'NoneType' object isn't callable``
166+
167+
- ``hard`` can be one of:
168+
169+
- ``True`` - The callback will be executed in hard interrupt
170+
context, which minimises delay and jitter but is subject to the
171+
limitations described in :ref:`isr_rules` including being unable
172+
to allocate on the heap.
173+
- ``False`` - The callback will be scheduled as a soft interrupt,
174+
allowing it to allocate but possibly also introducing
175+
garbage-collection delays and jitter.
176+
177+
The default value of this option is port-specific for historical
178+
reasons.
160179
"""
161180
...
162181

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Test Timer with hard parameter (added in MicroPython v1.27+)
2+
3+
from machine import Timer
4+
5+
6+
def mycallback(t):
7+
pass
8+
9+
10+
# Timer with hard interrupt context
11+
tim = Timer(-1)
12+
tim.init(mode=Timer.PERIODIC, freq=1000, callback=mycallback, hard=True)
13+
14+
# Timer with soft interrupt context
15+
tim.init(mode=Timer.PERIODIC, period=100, callback=mycallback, hard=False)
16+
17+
# Timer with hard parameter using freq
18+
tim.init(freq=500, callback=mycallback, hard=True)
19+
20+
# Constructor with hard parameter
21+
tim2 = Timer(-1, mode=Timer.ONE_SHOT, period=1000, callback=mycallback, hard=False)

0 commit comments

Comments
 (0)