STM32Cube  Version 2.0
Create Projects for STM32F2 Series with STM32Cube HAL and STM32CubeMX
 All Files Pages
Add CMSIS-RTOS RTX

This section explains how to add the CMSIS-RTOS RTX real-time operating system.

Setup in STM32CubeMX

In STM32CubeMX ensure that the System Tick Interrupt is set to the lowest priority (which is the highest number).

Configuration tab

  • Under System open NVIC Configuration. Set the following:
    • Group priority using at least 1-bit for pre-emption
    • SysTick and PendSV: lowest pre-emption priority (highest possible value)
    • SVC: one pre-emption priority higher than SysTick/PendSVC
  • Click OK to close the NVIC Configuration dialog.
NVIC.png

Setup in MDK

In MDK, the steps to add the CMSIS-RTOS RTX real-time operating system to a project that is configured with STM32CubeMX are:

Step 1: Add CMSIS-RTOS RTX

  • Open the Manage Run-Time Environment window and add CMSIS:RTOS (API):Keil RTX.
rte_bsp_rtx.png

Step 2: Configure CMSIS-RTOS RTX parameters

  • Open the configuration file RTX_Conf_CM.c from the project window, component group CMSIS.
  • Optionally you may change to the Configuration Wizard tab.
  • Change the following settings:
    • Main Thread stack size [bytes] 1024
    • RTOS Kernel Timer input clock frequency [Hz] set to core clock frequency
  • Save the file.

Step 3: Add code for CMSIS-RTOS RTX in 'main.c'

  • Open the source file main.c from the project window, source group STM32CubeMX:Common Sources and add code lines in the sections USER CODE BEGIN / USER CODE END as shown below:
    :
    /* USER CODE BEGIN Includes */
    #include "RTE_Components.h"
    #ifdef RTE_Compiler_EventRecorder
    #include "EventRecorder.h"
    #endif
    #include "cmsis_os2.h"
    extern void app_main (void* arg);
    /* USER CODE END Includes */
    ...
    /* USER CODE BEGIN 0 */
    /* Override default HAL_GetTick function */
    uint32_t HAL_GetTick (void) {
    static uint32_t ticks = 0U;
    uint32_t i;
    if (osKernelGetState () == osKernelRunning) {
    return ((uint32_t)osKernelGetTickCount ());
    }
    /* If Kernel is not running wait approximately 1 ms then increment and return auxiliary tick counter value */
    for (i = (SystemCoreClock >> 14U); i > 0U; i--) {
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    }
    return ++ticks;
    }
    /* Override default HAL_InitTick function */
    HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
    UNUSED(TickPriority);
    return HAL_OK;
    }
    /* USER CODE END 0 */
    ...
    int main(void)
    {
    :
    /* USER CODE BEGIN 2 */
    #ifdef RTE_Compiler_EventRecorder
    EventRecorderInitialize(EventRecordAll, 1);
    #endif
    SystemCoreClockUpdate();
    osKernelInitialize(); // Initialize CMSIS-RTOS
    osThreadNew(app_main, NULL, NULL); // Create application main thread
    osKernelStart();
    /* USER CODE END 2 */
    :

Step 5: Build Project in MDK

  • Use Project - Build Target to generate the application code.