Timer objects

Timer objects provide mechanisms for task synchronization relative to specific time points or intervals. Timers operate similarly to events; they transition to a signaled state whenever a specified duration elapses and return to a non-signaled state when reset.

Timer creation and deletion

The osCreateTimer function initializes a timer object. A timer may be created during system initialization, from an interrupt service routine, or by a running task. An optional name may be assigned during creation, allowing other tasks to obtain a handle to the timer via the osOpenTimer function. Both creation and opening functions return a handle assigned by the system, which serves to uniquely identify the object. All subsequent operations on the timer require this handle. When a timer is no longer required, the handle should be released using osCloseHandle. The timer object is deleted by the system only after all tasks that opened it have closed their handles. Detailed information is available in the System Objects Management section.

If timer functionality is not required, the OS_USE_TIMER constant can be set to 0 to reduce the final code size.

Using a timer object

Upon creation, a timer is unconfigured and resides in the signaled state. To activate the timer, the osSetTimer function must be invoked. This function defines the interval duration for a single cycle. During this cycle, the timer transitions to the non-signaled state; once the interval expires, the timer returns to the signaled state.

Cycles may be repeated for a specific number of iterations as defined by the PassCount parameter in the osSetTimer function. After each cycle, an internal pass counter is incremented, and the timer must be reset to initiate the next cycle. When the specified pass count is reached, the timer stops and remains in the signaled state until osSetTimer is called again. If PassCount is set to 0, the timer operates as a periodic timer that runs indefinitely, starting a new cycle each time it is reset.

Timers can be reset explicitly via osResetTimer or automatically, depending on the reset mode selected during object creation.

For manual-reset timers, a new cycle must be initiated by calling osResetTimer. In the case of auto-reset timers, a new cycle begins immediately upon the conclusion of the previous one. If multiple tasks are waiting on an auto-reset timer, only one task will be released at the end of each cycle.

If osResetTimer is called while a cycle is still in progress, the current cycle is restarted from the beginning. The pass counter only increments upon the successful completion of a full cycle. A cycle may be restarted multiple times; however, the reset function will fail if called on a stopped timer.

To terminate a timer immediately, use the osCancelTimer function. Stopping a timer transitions its state to signaled and releases all tasks currently waiting on it.

SpaceShadow documentation