CMSIS-RTOS RTX  Version 4.51
CMSIS-RTOS RTX: Real-Time Operating System for Cortex-M processor-based devices
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Using CMSIS-RTOS RTX

This section explains how to create a simple application with the CMSIS-RTOS RTX kernel.

Template Project

The directory Templates contains CMSIS-RTOS RTX templates for every supported compiler and every supported Cortex-M processor.

File or Directory Content
main.c Template for a CMSIS-RTOS RTX main thread function.
RTX_Conf_CM.c Configuration File for CMSIS-RTOS RTX. Refer to Configuration of CMSIS-RTOS RTX for more information.
.\CM0 Template project files for Cortex-M0 and Cortex-M1 processor.
.\CM3 Template project files for Cortex-M3 and Cortex-M4 without FPU processor.
.\CM4 Template project files for Cortex-M4 with FPU processor.
.\CMx\ARM Template project files for ARMCC Compiler.
.\CMx\G++ Template project files for CodeSoucery G++ Compiler.
.\CMx\GCC Template project files for GCC Compiler.
.\CMx\IAR Template project files for IAR Compiler.

Each template project file shows the basic configuration and the usage of the CMSIS-RTOS RTX Library and Configuration File. You may directly load this project templates with Keil MDK or IAR EW-ARM.

UV4_Template.png
CMSIS-RTOS RTX Template loaded with Keil MDK-ARM.

The file main.c is the very minimal valid CMSIS-RTOS file and contains simply just a 'main' function that is executed as a thread.

Example Project .\Examples\RTX_ex1

The example project provided in .\Examples\RTX_ex1 shows the usage of:

#include "cmsis_os.h"
/* Forward reference */
void threadX (void const *argument);
/* Thread IDs */
osThreadId main_id;
osThreadId threadX_id;
/* Thread definitions */
osThreadDef(threadX, osPriorityNormal, 1, 0);
/*----------------------------------------------------------------------------
Thread X
*---------------------------------------------------------------------------*/
void threadX (void const *argument) {
for (;;) {
/* Wait for completion of do-this */
osSignalWait(0x0004, osWaitForever); /* do-that */
/* Pause for 20 ms until signaling event to main thread */
osDelay(20);
/* Indicate to main thread completion of do-that */
osSignalSet(main_id, 0x0004);
}
}
/*----------------------------------------------------------------------------
Main Thread
*---------------------------------------------------------------------------*/
int main (void) {
/* Get main thread ID */
main_id = osThreadGetId();
/* Create thread X */
threadX_id = osThreadCreate(osThread(threadX), NULL);
for (;;) { /* do-this */
/* Indicate to thread X completion of do-this */
osSignalSet(threadX_id, 0x0004);
/* Wait for completion of do-that */
/* Wait now for 50 ms */
osDelay(50);
}
}

Example Project .\Examples\RTX_ex2

The example project provided in .\Examples\RTX_ex2 shows in addition to the features demonstrated in RTX_ex1 the usage of:

  • osKernelInitialize which stops thread switching of the CMSIS-RTOS RTX to run an initialization sequence.
  • osKernelStart which starts thread switching and resumes the operation of the CMSIS-RTOS RTX thread scheduler.
  • osThreadYield which provides a method for co-operative thread switching.
#include "cmsis_os.h"
volatile uint16_t counter; /* counter for main thread */
volatile uint16_t counter1; /* counter for thread 1 */
volatile uint16_t counter2; /* counter for thread 2 */
volatile uint16_t counter3; /* counter for thread 3 */
/* Thread IDs */
osThreadId thread1_id;
osThreadId thread2_id;
osThreadId thread3_id;
/* Forward reference */
void job1 (void const *argument);
void job2 (void const *argument);
void job3 (void const *argument);
/* Thread definitions */
/*----------------------------------------------------------------------------
Thread 1: 'job1'
*---------------------------------------------------------------------------*/
void job1 (void const *argument) { /* higher priority to preempt job2 */
while (1) { /* endless loop */
counter1++; /* increment counter 1 */
osDelay(10); /* wait for timeout: 10ms */
}
}
/*----------------------------------------------------------------------------
Thread 2: 'job2'
*---------------------------------------------------------------------------*/
void job2 (void const *argument) {
while (1) { /* endless loop */
counter2++; /* increment counter 2 */
if (counter2 == 0) { /* signal overflow of counter 2 */
osSignalSet(thread3_id, 0x0001);/* to thread 3 */
}
}
}
/*----------------------------------------------------------------------------
Thread 3: 'job3'
*---------------------------------------------------------------------------*/
void job3 (void const *argument) {
while (1) { /* endless loop */
osSignalWait(0x0001, osWaitForever); /* wait for signal event */
counter3++; /* process overflow from counter 2 */
}
}
/*----------------------------------------------------------------------------
Main Thread
*---------------------------------------------------------------------------*/
int main (void) { /* program execution starts here */
/* Set higher priority of main thread to preempt job2 */
thread1_id = osThreadCreate(osThread(job1),NULL); /* create thread1 */
thread2_id = osThreadCreate(osThread(job2),NULL); /* create thread2 */
thread3_id = osThreadCreate(osThread(job3),NULL); /* create thread3 */
}