Skip to content
Boris Lovosevic edited this page Aug 6, 2019 · 3 revisions

machine Module

Class Timer


This class includes support for using K210 hardware timer peripheral

K210 system has three TIMER modules, each with 4 channels, with the following characteristics:

  • 32-bit counter width
  • Configurable as up or down counter
  • Independent clocks
  • Configurable polarity for each interrupt
  • Configurable individual or combined interrupt output flags
  • Each timer has a read/write consistent register
  • Timer reload output, switches whenever the timer counter is reloaded
  • Output PWM mode , 0 % -100% duty cycle


Features

Several timer operational modes are available:

Mode Description
ONE_SHOT When timer is started, it runs for defined time and stops.
If given, the callback function is executed when the defined time elapses.
PERIODIC Timer runs repeatedly until explicitly stopped.
When defined time elapses the callback function is executed, if given
CHRONO Used for measuring the elapsed time with µs precision.
Can be started, paused, resumed and stopped
  • Up to 12 hardware timers can be used.
  • In CHRONO mode timers uses 1 MHz clock as timer base frequency to achieve 1 µs resolution.
  • In other modes timers uses hw timer clock as timer base frequency, resulution of 1 µs is available.
  • Each timer keeps track of number of timer events and number of executed callbacks

PWM and Timer clases uses the same K210 timers.
If some channel is used for Timer it cannot be used for PWM and vice versa.

Warning:

Due to MicroPython callback latency, some callbacks may not be executed if the timer period is very short.
The number of events and executed callbacks can be checked using tm.events() method.



Create the Timer instance object

tm = machine.Timer(timer_no)

timer_no argument is the timer number to be used for the timer.
Range: 0 ~ 11

Methods

tm.init(period, mode, callback, pin)

Argument Description
period Timer period in µs, only used for PERIODIC and ONE_SHOT timers
Default: 1000000 µs
mode Timer mode of operation.
Use constants:
machine.Timer.ONE_SHOT
machine.Timer.PERIODIC
machine.Timer.CHRONO
Default: PERIODIC
callback The Python callback function to be executed on timer event
Default: None
pin GPIO pin to be used as debug output.
If used, the gpio level will toggle on each timer event.
Default: -1 (not used)

All arguments must be given as kw arguments (arg=value)

The timer of the type CHRONO is not started after timer.init().

tm.deinit()

Stop and deinitialize the timer, free all used resources.

tm.value()

Returns the current timer counter value in µs
Most useful for CHRONO mode timers, as it returns the actual elapsed time.

tm.pause()

Pause the timer.

tm.stop()

Stop the timer. Alias for tm.pause()
Only use for timer in CHRONO mode.

tm.resume()

Resume the previously paused timer.

tm.start()

Resume the previously paused/stopped timer.
Same function as tm.resume(), but resets all timer counters.

tm.reshoot()

Start the ONE_SHOT timer again.
Only use for timer in ONE_SHOT mode.

tm.timernum()

Returns the hw timer number this timer uses.

tm.events()

Returns the number of timer events and number of executed callbacks.
The tuple is returned: (num_events, num_cb).
If no callbacks were missed, num_events = num_cb.

tm.isrunning()

Returns True if the timmer is currently running, False if not.

tm.period([period])

Get or set the timer period.
Executed without argument, returns the current timer's period.
Executed with period argument, changes the timer's period to the new value.

tm.callback([cb_func])

Disable or change the timer's callback function.
Executed without argument or with cb_func=None, disables timer's callback.
Executed with cb_func argument, changes the timer's calback function to the new one.
Only use for timers in ONE_SHOT or PERIODIC modes.


Examples