2016-06-07 17:46:27 -04:00
|
|
|
.. currentmodule:: machine
|
2017-04-18 01:27:37 -04:00
|
|
|
.. _machine.Timer:
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-01-28 03:55:48 -05:00
|
|
|
class Timer -- control hardware timers
|
2015-10-14 06:32:01 -04:00
|
|
|
======================================
|
|
|
|
|
2017-01-28 03:55:48 -05:00
|
|
|
Hardware timers deal with timing of periods and events. Timers are perhaps
|
|
|
|
the most flexible and heterogeneous kind of hardware in MCUs and SoCs,
|
|
|
|
differently greatly from a model to a model. MicroPython's Timer class
|
|
|
|
defines a baseline operation of executing a callback with a given period
|
|
|
|
(or once after some delay), and allow specific boards to define more
|
2021-04-30 02:53:36 -04:00
|
|
|
non-standard behaviour (which thus won't be portable to other boards).
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-01-28 04:08:25 -05:00
|
|
|
See discussion of :ref:`important constraints <machine_callbacks>` on
|
|
|
|
Timer callbacks.
|
|
|
|
|
2015-10-14 06:32:01 -04:00
|
|
|
.. note::
|
|
|
|
|
2015-10-20 10:24:25 -04:00
|
|
|
Memory can't be allocated inside irq handlers (an interrupt) and so
|
|
|
|
exceptions raised within a handler don't give much information. See
|
2015-10-14 06:32:01 -04:00
|
|
|
:func:`micropython.alloc_emergency_exception_buf` for how to get around this
|
|
|
|
limitation.
|
|
|
|
|
2018-07-29 10:19:41 -04:00
|
|
|
If you are using a WiPy board please refer to :ref:`machine.TimerWiPy <machine.TimerWiPy>`
|
|
|
|
instead of this class.
|
|
|
|
|
2015-10-14 06:32:01 -04:00
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2020-08-31 11:12:38 -04:00
|
|
|
.. class:: Timer(id, /, ...)
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2020-08-31 11:12:38 -04:00
|
|
|
Construct a new timer object of the given ``id``. ``id`` of -1 constructs a
|
2017-01-28 03:55:48 -05:00
|
|
|
virtual timer (if supported by a board).
|
2020-08-31 11:12:38 -04:00
|
|
|
``id`` shall not be passed as a keyword argument.
|
|
|
|
|
2020-10-09 17:31:00 -04:00
|
|
|
See ``init`` for parameters of initialisation.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2022-10-23 13:47:14 -04:00
|
|
|
.. method:: Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2018-07-29 10:19:41 -04:00
|
|
|
Initialise the timer. Example::
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2020-08-31 11:58:51 -04:00
|
|
|
def mycallback(t):
|
|
|
|
pass
|
|
|
|
|
2022-10-23 13:47:14 -04:00
|
|
|
# periodic at 1kHz
|
|
|
|
tim.init(mode=Timer.PERIODIC, freq=1000, callback=mycallback)
|
|
|
|
|
2020-08-31 11:58:51 -04:00
|
|
|
# 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)
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2018-07-29 10:19:41 -04:00
|
|
|
Keyword arguments:
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2018-07-29 10:19:41 -04:00
|
|
|
- ``mode`` can be one of:
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2018-07-29 10:19:41 -04:00
|
|
|
- ``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.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2022-10-23 13:47:14 -04:00
|
|
|
- ``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.
|
|
|
|
|
2020-08-31 11:58:51 -04:00
|
|
|
- ``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``
|
|
|
|
|
2016-06-07 18:33:49 -04:00
|
|
|
.. method:: Timer.deinit()
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-01-28 03:55:48 -05:00
|
|
|
Deinitialises the timer. Stops the timer, and disables the timer peripheral.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-02-14 07:46:04 -05:00
|
|
|
Constants
|
|
|
|
---------
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-02-14 07:46:04 -05:00
|
|
|
.. data:: Timer.ONE_SHOT
|
2018-07-29 10:19:41 -04:00
|
|
|
Timer.PERIODIC
|
2016-02-14 07:46:04 -05:00
|
|
|
|
2017-01-28 03:55:48 -05:00
|
|
|
Timer operating mode.
|