Note

This is the documentation for the latest development branch and may refer to features that are not available in released versions. If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

Timer Usage Tutorial#

What is Timer?#

machine.Timer is used to execute a callback function after a timeout. The K230 provides hardware timers and a software timer, suitable for one-shot delays, periodic sampling, status reporting, timeout control, and other scenarios.

Two concepts of Timer need to be distinguished:

  • The timer source is determined by the number: 0 to 5 are hardware timers, -1 is the software timer.

  • The callback execution mode is determined by hard: hard=True executes in interrupt context, hard=False executes in normal Python context.

These two choices are independent of each other. A hardware timer paired with hard=False is still timed by hardware, but the Python callback will be executed later.

K230 Timer Features#

Feature

Description

Number of hardware timers

6, numbered 0 to 5

Software timer

1, numbered -1, does not occupy hardware timer resources

Minimum period

5 ms

Supported modes

One-shot mode (Timer.ONE_SHOT), periodic mode (Timer.PERIODIC)

Callback mode

Interrupt context (hard=True) or normal Python context (hard=False)

Parameter Description#

Parameter

Type

Description

index

Integer

-1 for software timer; 0 to 5 for hardware timers.

period

Integer (ms)

Timer period, minimum 5 ms.

freq

Integer (Hz)

Timer frequency. When set, takes priority over period; the actual period is computed as 1000 // freq milliseconds.

mode

Constant

Timer.ONE_SHOT (single shot) or Timer.PERIODIC (periodic).

callback

Function

Called when the timer expires; receives a Timer argument.

hard

Boolean

Keyword-only argument, defaults to True. Determines the Python execution context for the callback.

Callback Modes#

hard=False: Normal Python Context#

Expiry events are first handled by the timer interrupt, then the Python callback is scheduled to run in the normal Python context.

  • You can use print() and execute regular Python code that allocates memory.

  • Callback execution time is affected by system scheduling and Python load, so it is not hard real-time.

  • For the same timer, only one pending task is kept while the callback is awaiting execution; if the timer expires too quickly, multiple events may be coalesced or lost.

This is the recommended choice for most Python applications.

hard=True: Interrupt Context#

This is the default. The Python callback is invoked directly by the timer interrupt, giving lower latency but with strict limitations:

  • The callback must be very short.

  • Do not call print(), sleep(), blocking I/O, network, or filesystem APIs.

  • Do not create Python objects or perform operations that may allocate memory.

Violating these restrictions may cause exceptions, lost callbacks, or system instability. When complex processing is needed, a hard=True callback may only set a pre-prepared state that the main loop handles; in most cases using hard=False directly is more appropriate.

Mode Details#

One-Shot Mode Timer.ONE_SHOT#

tim.init(period=100, mode=Timer.ONE_SHOT, callback=func, hard=False)
  • The timer fires only once.

  • It stops automatically after expiring.

Periodic Mode Timer.PERIODIC#

tim.init(freq=2, mode=Timer.PERIODIC, callback=func, hard=False)
  • Fires continuously at the configured period until reinitialized or deinit() is called.

  • freq=2 corresponds to approximately 500 ms period.

Resources and Lifecycle#

  • Each hardware timer number can only be used by one Timer.

  • Timer(-1) has only one software timer instance; repeated creation will return the same object.

  • Calling init() again on a running timer will replace the existing configuration.

  • deinit() will stop the timer and release resources. The released object cannot call init() again; a new Timer needs to be created.

Application Scenario Examples#

  • Periodic sampling: reading sensor data

  • Timeout control: handling task execution timeouts

  • System heartbeat: periodically updating status or outputting logs

  • Low-latency simple interrupt operations: use hard=True, and keep callbacks short without allocating memory

Tip

For the complete parameter description of the Timer module, please refer to the K230 Timer API Documentation

Comments list
Comments
Log in