machine.Timer.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. .. currentmodule:: machine
  2. .. _machine.Timer:
  3. class Timer -- control hardware timers
  4. ======================================
  5. Hardware timers deal with timing of periods and events. Timers are perhaps
  6. the most flexible and heterogeneous kind of hardware in MCUs and SoCs,
  7. differently greatly from a model to a model. MicroPython's Timer class
  8. defines a baseline operation of executing a callback with a given period
  9. (or once after some delay), and allow specific boards to define more
  10. non-standard behavior (which thus won't be portable to other boards).
  11. See discussion of :ref:`important constraints <machine_callbacks>` on
  12. Timer callbacks.
  13. .. note::
  14. Memory can't be allocated inside irq handlers (an interrupt) and so
  15. exceptions raised within a handler don't give much information. See
  16. :func:`micropython.alloc_emergency_exception_buf` for how to get around this
  17. limitation.
  18. Constructors
  19. ------------
  20. .. class:: Timer(id, ...)
  21. Construct a new timer object of the given id. Id of -1 constructs a
  22. virtual timer (if supported by a board).
  23. Methods
  24. -------
  25. .. only:: port_wipy
  26. .. method:: Timer.init(mode, \*, width=16)
  27. Initialise the timer. Example::
  28. tim.init(Timer.PERIODIC) # periodic 16-bit timer
  29. tim.init(Timer.ONE_SHOT, width=32) # one shot 32-bit timer
  30. Keyword arguments:
  31. - ``mode`` can be one of:
  32. - ``Timer.ONE_SHOT`` - The timer runs once until the configured
  33. period of the channel expires.
  34. - ``Timer.PERIODIC`` - The timer runs periodically at the configured
  35. frequency of the channel.
  36. - ``Timer.PWM`` - Output a PWM signal on a pin.
  37. - ``width`` must be either 16 or 32 (bits). For really low frequencies < 5Hz
  38. (or large periods), 32-bit timers should be used. 32-bit mode is only available
  39. for ``ONE_SHOT`` AND ``PERIODIC`` modes.
  40. .. method:: Timer.deinit()
  41. Deinitialises the timer. Stops the timer, and disables the timer peripheral.
  42. .. only:: port_wipy
  43. .. method:: Timer.channel(channel, \**, freq, period, polarity=Timer.POSITIVE, duty_cycle=0)
  44. If only a channel identifier passed, then a previously initialized channel
  45. object is returned (or ``None`` if there is no previous channel).
  46. Otherwise, a TimerChannel object is initialized and returned.
  47. The operating mode is is the one configured to the Timer object that was used to
  48. create the channel.
  49. - ``channel`` if the width of the timer is 16-bit, then must be either ``TIMER.A``, ``TIMER.B``.
  50. If the width is 32-bit then it **must be** ``TIMER.A | TIMER.B``.
  51. Keyword only arguments:
  52. - ``freq`` sets the frequency in Hz.
  53. - ``period`` sets the period in microseconds.
  54. .. note::
  55. Either ``freq`` or ``period`` must be given, never both.
  56. - ``polarity`` this is applicable for ``PWM``, and defines the polarity of the duty cycle
  57. - ``duty_cycle`` only applicable to ``PWM``. It's a percentage (0.00-100.00). Since the WiPy
  58. doesn't support floating point numbers the duty cycle must be specified in the range 0-10000,
  59. where 10000 would represent 100.00, 5050 represents 50.50, and so on.
  60. .. note::
  61. When the channel is in PWM mode, the corresponding pin is assigned automatically, therefore
  62. there's no need to assign the alternate function of the pin via the ``Pin`` class. The pins which
  63. support PWM functionality are the following:
  64. - ``GP24`` on Timer 0 channel A.
  65. - ``GP25`` on Timer 1 channel A.
  66. - ``GP9`` on Timer 2 channel B.
  67. - ``GP10`` on Timer 3 channel A.
  68. - ``GP11`` on Timer 3 channel B.
  69. .. only:: port_wipy
  70. class TimerChannel --- setup a channel for a timer
  71. ==================================================
  72. Timer channels are used to generate/capture a signal using a timer.
  73. TimerChannel objects are created using the Timer.channel() method.
  74. Methods
  75. -------
  76. .. method:: timerchannel.irq(\*, trigger, priority=1, handler=None)
  77. The behavior of this callback is heavily dependent on the operating
  78. mode of the timer channel:
  79. - If mode is ``Timer.PERIODIC`` the callback is executed periodically
  80. with the configured frequency or period.
  81. - If mode is ``Timer.ONE_SHOT`` the callback is executed once when
  82. the configured timer expires.
  83. - If mode is ``Timer.PWM`` the callback is executed when reaching the duty
  84. cycle value.
  85. The accepted params are:
  86. - ``priority`` level of the interrupt. Can take values in the range 1-7.
  87. Higher values represent higher priorities.
  88. - ``handler`` is an optional function to be called when the interrupt is triggered.
  89. - ``trigger`` must be ``Timer.TIMEOUT`` when the operating mode is either ``Timer.PERIODIC`` or
  90. ``Timer.ONE_SHOT``. In the case that mode is ``Timer.PWM`` then trigger must be equal to
  91. ``Timer.MATCH``.
  92. Returns a callback object.
  93. .. only:: port_wipy
  94. .. method:: timerchannel.freq([value])
  95. Get or set the timer channel frequency (in Hz).
  96. .. method:: timerchannel.period([value])
  97. Get or set the timer channel period (in microseconds).
  98. .. method:: timerchannel.duty_cycle([value])
  99. Get or set the duty cycle of the PWM signal. It's a percentage (0.00-100.00). Since the WiPy
  100. doesn't support floating point numbers the duty cycle must be specified in the range 0-10000,
  101. where 10000 would represent 100.00, 5050 represents 50.50, and so on.
  102. Constants
  103. ---------
  104. .. data:: Timer.ONE_SHOT
  105. .. data:: Timer.PERIODIC
  106. Timer operating mode.