Keil RTX5 is a feature-rich real-time operating system (RTOS). CMSIS and the CMSIS-RTOS2 API makes it easy to work with.

When setting up the project’s Run-Time Environment, ensure you add the appropriate system initialization code (C Startup).

Once this is done, the RTX5 initialization code is typically the same. It involves setting up the SysTick timer with the SystemCoreClockUpdate() function, then initializing and starting the RTOS.

Create main()

Return to the CMSIS view.

Within the Source Files group, a main.c is automatically created. Click on the file to open it in the text editor.

Delete any auto-generated code and replace it with the following:

    

        
        
            #include "RTE_Components.h"
#include  CMSIS_device_header
#include "cmsis_os2.h"

void app_main(void *);

int __attribute__((noreturn)) main (void) {
    SystemCoreClockUpdate();                    // initialize clocks etc

	osKernelInitialize();                       // initialize RTOS

	osThreadNew(app_main, NULL, NULL);          // Create application main thread

	if (osKernelGetState() == osKernelReady)    // If all OK...
		osKernelStart();                        // Start thread execution

	while(1);                                   // If you get to here, something has gone wrong!
}
        
    

Understanding the Code

The function osKernelStart() is designed to never return.

If your code reaches the infinite while() loop, something has gone wrong - most likely with the platform initialization code.

All threads should follow a prototype like this:

    

        
        
            void thread(void *);
        
    

The argument for this function is provided as the second parameter of the osThreadNew() function. Use NULL if no argument to pass.

In the example above, app_main is used as the main application thread, but this naming is arbitrary. From here, you will spawn all other threads in the RTOS.

Tip Tip: Naming the main application thread is flexible. Choose a name that clearly reflects its function.
Back
Next