CMSIS-RTOS2: CMSIS-RTOS C API v2

| CMSIS-RTOS2 Version 2.1.3 Real-Time Operating System: API and RTX Reference Implementation

- Main Page
- Usage and Description
- Reference

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
CMSIS-RTOS C API v2

Overview of all CMSIS-RTOS C API v2 functions that are implemented in the cmsis_os2.h header file.

# Common Design Concepts

All RTOS objects share a common design concept. The overall life-cycle of an object can be summarized as created -> in use -> destroyed.

Create Objects

An object is created by calling its `osXxxNew` function. The new function returns an identifier that can be used to operate with the new object. The actual state of an object is typically stored in an object specific control block. The memory layout (and size needed) for the control block is implementation specific. One should not make any specific assumptions about the control block. The control block layout might change and hence should be seen as an implementation internal detail.

In order to expose control about object specific options all `osXxxNew` functions provide an optional `attr` argument, which can be left as NULL by default. It takes a pointer to an object specific attribute structure, commonly containing the fields

- `name` to attach a human readable name to the object for identification,
- `attr_bits` to control object-specific options,
- `cb_mem` to provide memory for the control block manually, and
- `cb_size` to quantify the memory size provided for the control block.

The `name` attribute is only used for object identification, e.g. using RTOS-aware debugging. The attached string is not used for any other purposes internally.

The `cb_mem` and `cb_size` attributes can be used to provide memory for the control block manually instead of relying on the implementation internal memory allocation. One has to assure that the amount of memory pointed to by `cb_mem` is sufficient for the objects control block structure. If the size given as `cb_size` is not sufficient the `osXxxNew` function returns with an error, i.e. returning NULL. Furthermore providing control block memory manually is less portable. Thus one has to take care about implementation specific alignment and placement requirements for instance. Refer to Memory Management for further details.

Object Usage

After an object has been created successfully it can be used until it is destroyed. The actions defined for an object depends on its type. Commonly all the `osXxxDoSomething` access function require the reference to the object to work with as the first `xxx_id` parameter.

The access function can be assumed to apply some sort of sanity checking on the id parameter. So that it is assured one cannot accidentally call an access function with a NULL object reference. Furthermore the concrete object type is verified, i.e. one cannot call access functions of one object type with a reference to another object type.

All further parameter checks applied are either object and action specific or may even be implementation specific. Thus one should always check action function return values for `osErrorParameter` to assure the provided arguments were accepted.

As a rule of thumb only non-blocking access function can be used from Interrupt Service Routines (ISR). This incorporates `osXxxWait` functions (and similar) limited to be called with parameter `timeout` set to 0, i.e. usage of try-semantics.

Object Destruction

Objects that are not needed anymore can be destructed on demand to free the control block memory. Objects are not destructed implicitly. Thus one can assume an object id to be valid until `osXxxDelete` is called explicitly. The delete function finally frees the control block memory. In case of user provided control block memory, see above, the memory must be freed manually as well.

The only exception one has to take care of are Threads which do not have an explicit `osThreadDelete` function. Threads can either be `detached` or `joinable`. Detached threads are automatically destroyed on termination, i.e. call to osThreadTerminate or osThreadExit or return from thread function. On the other hand joinable threads are kept alive until one explicitly calls osThreadJoin.

# Function Reference

- Kernel Information and Control
  - osKernelGetInfo : Get RTOS Kernel Information.
  - osKernelGetState : Get the current RTOS Kernel state.
  - osKernelGetSysTimerCount : Get the RTOS kernel system timer count.
  - osKernelGetSysTimerFreq : Get the RTOS kernel system timer frequency.
  - osKernelInitialize : Initialize the RTOS Kernel.
  - osKernelLock : Lock the RTOS Kernel scheduler.
  - osKernelUnlock : Unlock the RTOS Kernel scheduler.
  - osKernelRestoreLock : Restore the RTOS Kernel scheduler lock state.
  - osKernelResume : Resume the RTOS Kernel scheduler.
  - osKernelStart : Start the RTOS Kernel scheduler.
  - osKernelSuspend : Suspend the RTOS Kernel scheduler.
  - osKernelGetTickCount : Get the RTOS kernel tick count.
  - osKernelGetTickFreq : Get the RTOS kernel tick frequency.
- Thread Management
  - osThreadDetach : Detach a thread (thread storage can be reclaimed when thread terminates).
  - osThreadEnumerate : Enumerate active threads.
  - osThreadExit : Terminate execution of current running thread.
  - osThreadGetCount : Get number of active threads.
  - osThreadGetId : Return the thread ID of the current running thread.
  - osThreadGetName : Get name of a thread.
  - osThreadGetPriority : Get current priority of a thread.
  - osThreadGetStackSize : Get stack size of a thread.
  - osThreadGetStackSpace : Get available stack space of a thread based on stack watermark recording during execution.
  - osThreadGetState : Get current thread state of a thread.
  - osThreadJoin : Wait for specified thread to terminate.
  - osThreadNew : Create a thread and add it to Active Threads.
  - osThreadResume : Resume execution of a thread.
  - osThreadSetPriority : Change priority of a thread.
  - osThreadSuspend : Suspend execution of a thread.
  - osThreadTerminate : Terminate execution of a thread.
  - osThreadYield : Pass control to next thread that is in state READY.
- Thread Flags
  - osThreadFlagsSet : Set the specified Thread Flags of a thread.
  - osThreadFlagsClear : Clear the specified Thread Flags of current running thread.
  - osThreadFlagsGet : Get the current Thread Flags of current running thread.
  - osThreadFlagsWait : Wait for one or more Thread Flags of the current running thread to become signaled.
- Event Flags
  - osEventFlagsGetName : Get name of an Event Flags object.
  - osEventFlagsNew : Create and Initialize an Event Flags object.
  - osEventFlagsDelete : Delete an Event Flags object.
  - osEventFlagsSet : Set the specified Event Flags.
  - osEventFlagsClear : Clear the specified Event Flags.
  - osEventFlagsGet : Get the current Event Flags.
  - osEventFlagsWait : Wait for one or more Event Flags to become signaled.
- Generic Wait Functions
  - osDelay : Wait for Timeout (Time Delay).
  - osDelayUntil : Wait until specified time.
- Timer Management
  - osTimerDelete : Delete a timer.
  - osTimerGetName : Get name of a timer.
  - osTimerIsRunning : Check if a timer is running.
  - osTimerNew : Create and Initialize a timer.
  - osTimerStart : Start or restart a timer.
  - osTimerStop : Stop a timer.
- Mutex Management
  - osMutexAcquire : Acquire a Mutex or timeout if it is locked.
  - osMutexDelete : Delete a Mutex object.
  - osMutexGetName : Get name of a Mutex object.
  - osMutexGetOwner : Get Thread which owns a Mutex object.
  - osMutexNew : Create and Initialize a Mutex object.
  - osMutexRelease : Release a Mutex that was acquired by osMutexAcquire.
- Semaphores
  - osSemaphoreAcquire : Acquire a Semaphore token or timeout if no tokens are available.
  - osSemaphoreDelete : Delete a Semaphore object.
  - osSemaphoreGetCount : Get current Semaphore token count.
  - osSemaphoreGetName : Get name of a Semaphore object.
  - osSemaphoreNew : Create and Initialize a Semaphore object.
  - osSemaphoreRelease : Release a Semaphore token up to the initial maximum count.
- Memory Pool
  - osMemoryPoolAlloc : Allocate a memory block from a Memory Pool.
  - osMemoryPoolDelete : Delete a Memory Pool object.
  - osMemoryPoolFree : Return an allocated memory block back to a Memory Pool.
  - osMemoryPoolGetBlockSize : Get memory block size in a Memory Pool.
  - osMemoryPoolGetCapacity : Get maximum number of memory blocks in a Memory Pool.
  - osMemoryPoolGetCount : Get number of memory blocks used in a Memory Pool.
  - osMemoryPoolGetName : Get name of a Memory Pool object.
  - osMemoryPoolGetSpace : Get number of memory blocks available in a Memory Pool.
  - osMemoryPoolNew : Create and Initialize a Memory Pool object.
- Message Queue
  - osMessageQueueDelete : Delete a Message Queue object.
  - osMessageQueueGet : Get a Message from a Queue or timeout if Queue is empty.
  - osMessageQueueGetCapacity : Get maximum number of messages in a Message Queue.
  - osMessageQueueGetCount : Get number of queued messages in a Message Queue.
  - osMessageQueueGetMsgSize : Get maximum message size in a Message Queue.
  - osMessageQueueGetName : Get name of a Message Queue object.
  - osMessageQueueGetSpace : Get number of available slots for messages in a Message Queue.
  - osMessageQueueNew : Create and Initialize a Message Queue object.
  - osMessageQueuePut : Put a Message into a Queue or timeout if Queue is full.
  - osMessageQueueReset : Reset a Message Queue to initial empty state.

The following CMSIS-RTOS C API v2 functions can be called from threads and Interrupt Service Routines (ISR):

- osKernelGetInfo, osKernelGetState, osKernelGetTickCount, osKernelGetTickFreq, osKernelGetSysTimerCount, osKernelGetSysTimerFreq
- osThreadGetId, osThreadFlagsSet
- osEventFlagsSet, osEventFlagsClear, osEventFlagsGet, osEventFlagsWait
- osSemaphoreAcquire, osSemaphoreRelease, osSemaphoreGetCount
- osMemoryPoolAlloc, osMemoryPoolFree, osMemoryPoolGetCapacity, osMemoryPoolGetBlockSize, osMemoryPoolGetCount, osMemoryPoolGetSpace
- osMessageQueuePut, osMessageQueueGet, osMessageQueueGetCapacity, osMessageQueueGetMsgSize, osMessageQueueGetCount, osMessageQueueGetSpace