CMSIS-RTOS2
Version 2.2.0
Real-Time Operating System: API and RTX Reference Implementation
|
Create and control timer and timer callback functions. More...
Data Structures | |
struct | osTimerAttr_t |
Attributes structure for timer. More... | |
Typedefs | |
typedef void * | osTimerId_t |
typedef void(* | osTimerFunc_t )(void *argument) |
Timer callback function. More... | |
Enumerations | |
enum | osTimerType_t { osTimerOnce = 0, osTimerPeriodic = 1 } |
Timer type. More... | |
Functions | |
osTimerId_t | osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) |
Create and Initialize a timer. More... | |
const char * | osTimerGetName (osTimerId_t timer_id) |
Get name of a timer. More... | |
osStatus_t | osTimerStart (osTimerId_t timer_id, uint32_t ticks) |
Start or restart a timer. More... | |
osStatus_t | osTimerStop (osTimerId_t timer_id) |
Stop a timer. More... | |
uint32_t | osTimerIsRunning (osTimerId_t timer_id) |
Check if a timer is running. More... | |
osStatus_t | osTimerDelete (osTimerId_t timer_id) |
Delete a timer. More... | |
In addition to the Generic Wait Functions CMSIS-RTOS also supports virtual timer objects. These timer objects can trigger the execution of a function (not threads). When a timer expires, a callback function is executed to run associated code with the timer. Each timer can be configured as a one-shot or a periodic timer. A periodic timer repeats its operation until it is deleted or stopped. All timers can be started, restarted, or stopped.
The figure below shows the behavior of a periodic timer. For one-shot timers, the timer stops after execution of the callback function.
The following steps are required to use a software timer:
struct osTimerAttr_t |
Specifies the following attributes for the osTimerNew function.
Data Fields | ||
---|---|---|
const char * | name |
name of the timer Pointer to a constant string with a human readable name (displayed during debugging) of the timer object. Default: NULL no name specified. |
uint32_t | attr_bits |
attribute bits Reserved for future use (must be set to '0' for future compatibility). |
void * | cb_mem |
memory for control block Pointer to a memory for the timer control block object. Refer to Static Object Memory for more information. Default: NULL to use Automatic Dynamic Allocation for the timer control block. |
uint32_t | cb_size |
size of provided memory for control block The size (in bytes) of memory block passed with cb_mem. For RTX, the minimum value is defined with osRtxTimerCbSize (higher values are permitted). Default: 0 as the default is no memory provided with cb_mem. |
Timer ID identifies the timer.
Instances of this type hold a reference to a timer object.
Returned by:
void(* osTimerFunc_t)(void *argument) |
The timer callback function is called every time the timer elapses.
The callback might be executed either in a dedicated timer thread or in interrupt context. Thus it is recommended to only use ISR callable functions from the timer callback.
[in] | argument | The argument provided to osTimerNew. |
enum osTimerType_t |
The osTimerType_t specifies the a repeating (periodic) or one-shot timer for the function osTimerNew.
Enumerator | |
---|---|
osTimerOnce |
One-shot timer. The timer is not automatically restarted once it has elapsed. It can be restarted manually using osTimerStart as needed. |
osTimerPeriodic |
Repeating timer. The timer repeats automatically and triggers the callback continuously while running, see osTimerStart and osTimerStop. |
osTimerId_t osTimerNew | ( | osTimerFunc_t | func, |
osTimerType_t | type, | ||
void * | argument, | ||
const osTimerAttr_t * | attr | ||
) |
[in] | func | function pointer to callback function. |
[in] | type | osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. |
[in] | argument | argument to the timer callback function. |
[in] | attr | timer attributes; NULL: default values. |
The function osTimerNew creates an one-shot or periodic timer and associates it with a callback function with argument. The timer is in stopped state until it is started with osTimerStart. The function can be safely called before the RTOS is started (call to osKernelStart), but not before it is initialized (call to osKernelInitialize).
The function osTimerNew returns the pointer to the timer object identifier or NULL in case of an error.
Code Example
*const char * osTimerGetName | ( | osTimerId_t | timer_id | ) |
[in] | timer_id | timer ID obtained by osTimerNew. |
The function osTimerGetName returns the pointer to the name string of the timer identified by parameter timer_id or NULL in case of an error.
osStatus_t osTimerStart | ( | osTimerId_t | timer_id, |
uint32_t | ticks | ||
) |
[in] | timer_id | timer ID obtained by osTimerNew. |
[in] | ticks | time ticks value of the timer. |
The function osTimerStart starts or restarts a timer specified by the parameter timer_id. The parameter ticks specifies the value of the timer in time ticks.
Possible osStatus_t return values:
Code Example
osStatus_t osTimerStop | ( | osTimerId_t | timer_id | ) |
[in] | timer_id | timer ID obtained by osTimerNew. |
The function osTimerStop stops a timer specified by the parameter timer_id.
Possible osStatus_t return values:
Code Example
uint32_t osTimerIsRunning | ( | osTimerId_t | timer_id | ) |
[in] | timer_id | timer ID obtained by osTimerNew. |
The function osTimerIsRunning checks whether a timer specified by parameter timer_id is running. It returns 1 if the timer is running and 0 if the timer is stopped or an error occurred.
osStatus_t osTimerDelete | ( | osTimerId_t | timer_id | ) |
[in] | timer_id | timer ID obtained by osTimerNew. |
The function osTimerDelete deletes the timer specified by parameter timer_id.
Possible osStatus_t return values:
Code Example