|
4 | 4 | class Timer -- control hardware timers |
5 | 5 | ====================================== |
6 | 6 |
|
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. |
13 | 9 |
|
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. |
24 | 12 |
|
25 | 13 | If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>` |
26 | 14 | instead of this class. |
27 | 15 |
|
| 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 | + |
28 | 32 | Constructors |
29 | 33 | ------------ |
30 | 34 |
|
31 | 35 | .. class:: Timer(id, /, ...) |
32 | 36 |
|
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 | + |
35 | 45 | ``id`` shall not be passed as a keyword argument. |
36 | 46 |
|
37 | | - See ``init`` for parameters of initialisation. |
| 47 | + Any additional parameters are handled the same as :func:`Timer.init()`. |
38 | 48 |
|
39 | 49 | Methods |
40 | 50 | ------- |
@@ -73,22 +83,22 @@ Methods |
73 | 83 |
|
74 | 84 | - ``callback`` - The callable to call upon expiration of the timer period. |
75 | 85 | The callback must take one argument, which is passed the Timer object. |
| 86 | + |
76 | 87 | The ``callback`` argument shall be specified. Otherwise an exception |
77 | 88 | will occur upon timer expiration: |
78 | 89 | ``TypeError: 'NoneType' object isn't callable`` |
79 | 90 |
|
80 | 91 | - ``hard`` can be one of: |
81 | 92 |
|
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. |
86 | 97 | - ``False`` - The callback will be scheduled as a soft interrupt, |
87 | 98 | allowing it to allocate but possibly also introducing |
88 | 99 | garbage-collection delays and jitter. |
89 | 100 |
|
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. |
92 | 102 |
|
93 | 103 | .. method:: Timer.deinit() |
94 | 104 |
|
|
0 commit comments