mirror of
https://github.com/micropython/micropython.git
synced 2025-12-15 17:30:14 +01:00
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c5_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_p4) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
As noted in discussion on PR #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 <angus@redyak.com.au>
114 lines
3.9 KiB
ReStructuredText
114 lines
3.9 KiB
ReStructuredText
.. currentmodule:: machine
|
|
.. _machine.Timer:
|
|
|
|
class Timer -- control hardware timers
|
|
======================================
|
|
|
|
Timer class provides the ability to trigger a Python callback function after an
|
|
expiry time, or periodically at a regular interval.
|
|
|
|
The available features and restrictions of Timer objects vary depending on the
|
|
MicroPython board and port.
|
|
|
|
If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>`
|
|
instead of this class.
|
|
|
|
Timer Types
|
|
-----------
|
|
|
|
There are two types of Timer in MicroPython, but not all ports support both:
|
|
|
|
- Virtual timers. These are managed in software, and are generally more
|
|
flexible. Multiple virtual timers can be constructed and active at once. The
|
|
``id`` of a virtual timer is ``-1``. Not all ports support virtual timers, but
|
|
it's recommended to use them when available.
|
|
- Hardware timers. Hardware timers have integer ``id`` values starting at ``0``.
|
|
The number of available ``id`` values is determined by the hardware. Hardware
|
|
timers may be more accurate for very fine sub-millisecond timing (especially
|
|
when ``hard=True`` is supported and set, see :ref:`isr_rules`.) Most
|
|
microcontroller ports support hardware timers, except Zephyr and RP2 which
|
|
only support virtual timers.
|
|
|
|
Constructors
|
|
------------
|
|
|
|
.. class:: Timer(id, /, ...)
|
|
|
|
Construct a new Timer object with the given ``id``.
|
|
|
|
On ports which support virtual timers the ``id`` parameter is optional - the
|
|
default value is ``-1`` which constructs a virtual timer.
|
|
|
|
On ports which support hardware timers, setting the ``id`` parameter to a
|
|
non-negative integer determines which timer to use.
|
|
|
|
``id`` shall not be passed as a keyword argument.
|
|
|
|
Any additional parameters are handled the same as :func:`Timer.init()`.
|
|
|
|
Methods
|
|
-------
|
|
|
|
.. method:: Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None, hard=True)
|
|
|
|
Initialise the timer. Example::
|
|
|
|
def mycallback(t):
|
|
pass
|
|
|
|
# periodic at 1kHz
|
|
tim.init(mode=Timer.PERIODIC, freq=1000, callback=mycallback)
|
|
|
|
# periodic with 100ms period
|
|
tim.init(period=100, callback=mycallback)
|
|
|
|
# one shot firing after 1000ms
|
|
tim.init(mode=Timer.ONE_SHOT, period=1000, callback=mycallback)
|
|
|
|
Keyword arguments:
|
|
|
|
- ``mode`` can be one of:
|
|
|
|
- ``Timer.ONE_SHOT`` - The timer runs once until the configured
|
|
period of the channel expires.
|
|
- ``Timer.PERIODIC`` - The timer runs periodically at the configured
|
|
frequency of the channel.
|
|
|
|
- ``freq`` - The timer frequency, in units of Hz. The upper bound of
|
|
the frequency is dependent on the port. When both the ``freq`` and
|
|
``period`` arguments are given, ``freq`` has a higher priority and
|
|
``period`` is ignored.
|
|
|
|
- ``period`` - The timer period, in milliseconds.
|
|
|
|
- ``callback`` - The callable to call upon expiration of the timer period.
|
|
The callback must take one argument, which is passed the Timer object.
|
|
|
|
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`. Not all ports support hard interrupts,
|
|
see the port documentation for more information.
|
|
- ``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 parameter is port-specific for historical reasons.
|
|
|
|
.. method:: Timer.deinit()
|
|
|
|
Deinitialises the timer. Stops the timer, and disables the timer peripheral.
|
|
|
|
Constants
|
|
---------
|
|
|
|
.. data:: Timer.ONE_SHOT
|
|
Timer.PERIODIC
|
|
|
|
Timer operating mode.
|