Periodic and one-shot timers for scheduling tasks.
More...
Periodic and one-shot timers for scheduling tasks.
This module declares types and functions for creating and managing timers, which are created as periodic timers. A "callback" function is called when the timer matures, and the timer can be configured as a one-shot timer or otherwise cancelled by calling sys_timer_finalize() in the callback function.
The core that the timer callback runs on is determined by the platform's implementation, but on the Pico platform, the timer callback should be implemented to run on the same core that the timer was started on (at the moment this isn't the case, but it will be in the future).
◆ SYS_TIMER_CTX_SIZE
#define SYS_TIMER_CTX_SIZE 32 |
Size of the timer context buffer.
This defines the size of the context buffer used for timer operations. It should be large enough to hold any platform-specific timer context.
Definition at line 39 of file timer.h.
◆ sys_timer_t
Timer context structure.
Contains the state and configuration for timer operations.
◆ sys_timer_finalize()
Cancels and finalizes a timer.
- Parameters
-
timer | The timer context to finalize. |
- Returns
- true on success, false on failure. This function stops the timer and cleans up any resources associated with it. After calling this function, the timer context becomes invalid and should not be used again. To use a timer with the same configuration, initialize a new timer using sys_timer_init().
- Examples:
- clock/main.c.
◆ sys_timer_init()
Initializes a new timer context.
- Parameters
-
interval_ms | The periodic interval for the timer in milliseconds. |
userdata | Optional user data to pass to the callback. |
callback | The function to call on each timer event. |
- Returns
- A new sys_timer_t instance initialized with the specified parameters.
This function returns a sys_timer_t instance for the specified timer configuration, ready for use in starting and stopping the timer. Use sys_timer_start() to start the timer and sys_timer_finalize() to stop and clean up the timer. If you want to define a one-shot timer, call sys_timer_finalize() in your callback function.
- Examples:
- clock/main.c.
◆ sys_timer_start()
Starts a timer.
- Parameters
-
timer | The timer context to start. |
- Returns
- true on success, false on failure.
This function starts the timer with the specified interval and callback. If the timer is invalid, or cannot be started, the function returns false.
- Examples:
- clock/main.c.
◆ sys_timer_valid()
Checks if a timer is valid, configured and running.
- Parameters
-
timer | The timer context to validate. |
- Returns
- true if the timer is valid and is enabled.