Loading...
Searching...
No Matches
CMSIS-RTOS2

Tools > Operating System > FreeRTOS (SMP) + CMSIS-RTOS2: ARM's standard RTOS API, on FreeRTOS.

Tools > Operating System > FreeRTOS (SMP) + CMSIS-RTOS2: ARM's standard RTOS API, on FreeRTOS.

CMSIS-RTOS2 is the RTOS API of ARM's Cortex-M software standard (CMSIS), used by Keil MDK, STM32Cube and others, so code written for it on other Cortex-M boards runs here unchanged. Choosing FreeRTOS (SMP) + CMSIS-RTOS2 in Tools > Operating System builds the sketch with FreeRTOS, as FreeRTOS describes, plus ARM's CMSIS-RTOS2 layer for FreeRTOS (CMSIS-FreeRTOS; ADR 010). A sketch includes cmsis_os2.h. The functions are described in the CMSIS-RTOS2 API reference. Without the menu option, including cmsis_os2.h stops the build with a message naming the menu option.

#include <cmsis_os2.h>
void blink(void*) {
for (;;) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); osDelay(500); }
}
void setup() {
osThreadNew(blink, nullptr, nullptr); // default attributes: osPriorityNormal, 1 KB stack
}
void loop() {}
@ OUTPUT
Definition Common.h:40
void digitalWrite(pin_size_t pinNumber, PinStatus status)
Sets an output pin HIGH (3.3 V) or LOW (0 V).
PinStatus digitalRead(pin_size_t pinNumber)
Reads the level on a pin.
void pinMode(pin_size_t pinNumber, PinMode pinMode)
Sets a pin as an input or an output.
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void setup(void)
Written by the sketch: runs once after reset, on core 0.
#define LED_BUILTIN
On-board LED.
Definition pins_arduino.h:63

Differences from CMSIS-RTOS2 on other systems:

  • The kernel is already running when setup() starts. osKernelInitialize() and osKernelStart() are not needed; they return osError.
  • setup()/loop() and setup1()/loop1() are threads at osPriorityNormal (see Multicore), the default for osThreadNew().
  • osPriority_t p is FreeRTOS priority p - 1: osPriorityIdle (1) is the idle priority 0, osPriorityNormal (24) is 23, osPriorityISR (56) is 55, the FreeRTOS timer task's priority.
  • stack_size in osThreadAttr_t is in bytes (FreeRTOS's xTaskCreate() takes 4-byte words); 0 or no attributes gives 1 KB.
  • A tick is 1 ms: osDelay(), timeouts and osKernelGetTickCount() count milliseconds; osKernelGetTickFreq() is 1000.
  • osKernelGetSysTimerFreq() is the CPU clock (Tools > CPU Speed). osKernelGetSysTimerCount() counts CPU clock cycles on core 0, but on core 1 it only moves on once per tick, because the tick timer runs on core 0.
  • affinity_mask in osThreadAttr_t and osThreadSetAffinityMask() pin a thread to cores (osThreadProcessor(0), osThreadProcessor(1), or both ORed together); 0 means either core.
  • FreeRTOS functions can be used in the same sketch: an osThreadId_t is a FreeRTOS TaskHandle_t.
  • Errors stop the board, with the same hook functions as FreeRTOS.

Functions provided:

Group Functions
Kernel osKernelInitialize(), osKernelGetInfo(), osKernelGetState(), osKernelStart(), osKernelLock(), osKernelUnlock(), osKernelRestoreLock(), osKernelGetTickCount(), osKernelGetTickFreq(), osKernelGetSysTimerCount(), osKernelGetSysTimerFreq()
Threads osThreadNew(), osThreadGetName(), osThreadGetId(), osThreadGetState(), osThreadGetStackSpace(), osThreadSetPriority(), osThreadGetPriority(), osThreadYield(), osThreadSuspend(), osThreadResume(), osThreadExit(), osThreadTerminate(), osThreadGetCount(), osThreadEnumerate(), osThreadSetAffinityMask(), osThreadGetAffinityMask()
Thread flags osThreadFlagsSet(), osThreadFlagsClear(), osThreadFlagsGet(), osThreadFlagsWait()
Waiting osDelay(), osDelayUntil()
Timers osTimerNew(), osTimerGetName(), osTimerStart(), osTimerStop(), osTimerIsRunning(), osTimerDelete()
Event flags osEventFlagsNew(), osEventFlagsSet(), osEventFlagsClear(), osEventFlagsGet(), osEventFlagsWait(), osEventFlagsDelete()
Mutexes osMutexNew(), osMutexAcquire(), osMutexRelease(), osMutexGetOwner(), osMutexDelete()
Semaphores osSemaphoreNew(), osSemaphoreAcquire(), osSemaphoreRelease(), osSemaphoreGetCount(), osSemaphoreDelete()
Memory pools osMemoryPoolNew(), osMemoryPoolGetName(), osMemoryPoolAlloc(), osMemoryPoolFree(), osMemoryPoolGetCapacity(), osMemoryPoolGetBlockSize(), osMemoryPoolGetCount(), osMemoryPoolGetSpace(), osMemoryPoolDelete()
Message queues osMessageQueueNew(), osMessageQueuePut(), osMessageQueueGet(), osMessageQueueGetCapacity(), osMessageQueueGetMsgSize(), osMessageQueueGetCount(), osMessageQueueGetSpace(), osMessageQueueReset(), osMessageQueueDelete()

Not provided by the FreeRTOS layer (the build fails with "undefined reference" if a sketch uses one): osKernelSuspend(), osKernelResume(), osThreadGetStackSize(), osThreadJoin(), osThreadDetach(), osEventFlagsGetName(), osMutexGetName(), osSemaphoreGetName(), osMessageQueueGetName(), and the CMSIS-RTOS2 2.3 safety features: osKernelProtect(), osKernelDestroyClass(), osThreadGetClass(), osThreadGetZone(), osThreadSuspendClass(), osThreadResumeClass(), osThreadTerminateZone(), osThreadFeedWatchdog(), osThreadProtectPrivileged() and osFaultResume(). The attributes osThreadJoinable and osMutexRobust make osThreadNew() and osMutexNew() return NULL.