STM32Cube  Version 2.0
Create Projects for STM32L5 Series with STM32Cube HAL and STM32CubeMX
 All Files Functions Variables Macros Pages
Use STM32CubeMX in MDK Projects

Create New Project with STM32Cube Framework

The following steps explain how to create a new MDK project and how to select as STM32 device.

  • Open µVision.
  • From the menu, select Project - New µVision project. Choose any name and location.
  • Select your target device. If you have many device family packs installed, the search field helps you to find a device quickly:
    uv_select_device.png
  • The Run-Time Environment Manager opens.
  • [Optional] Select CMSIS:RTOS2:Keil RTX5 from the list of available software components. Select the “Source” variant.
  • [Optional] Switch the component bundle for the component class Device to 'STM32CubeMX' by selecting it from the drop down list in the 'Variant' column.
  • Select Device:STM32Cube Framework:STM32CubeMX to trigger the integration of the Cube HAL into your project:
    uv_rte_sel_cubemx.png
  • Press 'Resolve' to satisfy all missing dependencies. This ensures that all required components will be added to your project.
  • Pressing OK closes the dialog.
  • A dialog opens asking to launch STM32CubeMX. Click on “Start STM32CubeMX” and wait until the STM32CubeMX GUI is opened:
    open_generator.png

Configure I/O and Clock

  • The STM32CubeMX window shows your selected device:
    CubeMX_new.png
  • On the Pinout & Configuration tab, use the Pinout view to configure all necessary I/O pins to the required functions.
  • On the Clock Configuration tab, adjust the clock to the desired settings.
  • Use Help [F1] to get access to the STM32CubeMX user manual.

Generate Code

  • Use the Generate Code button to run the STM32CubeMX code generation:
    generate_code.png
  • Close the dialog that confirms the end of the generation.
  • Click OK to close the Manage Run-Time Environment window and confirm to import changes:
    uv_import.png

In case you need to change the configuration of your device, use the button in the Manage Run-Time Environment window to launch STM32CubeMX:

MDK_RTE_Run_gen.png

Project Overview

The C code generated by STM32CubeMX covers the initialization of the MCU peripherals using the STM32Cube firmware libraries. STM32CubeMX creates a source group in your project called STM32CubeMX:Common Sources. This group contains three files that contain user-dedicated sections allowing to insert user-defined C code:

MDK_project_files.png

main.c is used to:

  • Resetting the MCU to a known state by calling the HAL_init() function that resets all peripherals, initializes the Flash memory interface and the SysTick.
  • Configuring and initializing the system clock.
  • Configuring and initializing the GPIOs that are not used by peripherals.
  • Defining and calling, for each configured peripheral, a peripheral initialization function that defines a handle structure that will be passed to the corresponding peripheral HAL init function which in turn will call the peripheral HAL MSP initialization function.

stm32l4xx_it.c contains the generated interrupt handlers. The column “Generate IRQ Handler” in STM32CubeMX NVIC settings allows controlling whether the interrupt handler function call shall be generated or not.

stm32l4xx_it.h is the associated header file.

[Optional] Add and Configure Keil RTX5

The following steps are a recommendation on how to integrate Keil RTX5 in a project that is configured with STM32CubeMX.

  • In STM32CubeMX, open the (1) Pinout & Configuration tab, click on (2) System Core and then click on (3) NVIC. Switch to the (4) Code generation tab and under (5) Generate IRQ handler disable:
    • System service call via SWI instruction
    • Pendable request for system service
    • Time base: System tick timer
      CubeMX_NVIC_RTX5.png
  • Open main.c in the μVision editor. Find the USER CODE includes and add the cmsis_os2.h header.
    /* 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 */
  • Find the following code sequence near main():
    /* USER CODE BEGIN 0 */
    /* USER CODE END 0 */
  • Add the following code:
    /* USER CODE BEGIN 0 */
    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; }
    /* USER CODE END 0 */
  • Repeat for user code 2 sequence inside main():
    /* USER CODE BEGIN 2 */
    /* USER CODE END 2 */
  • Replace with:
    /* 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 */
  • Create a new source file in your project called app_main.c and add the following content:
    #include "cmsis_os2.h" // ::CMSIS:RTOS2
    void app_main (void const* arg) {
    while(1) {
    ...
    }
    }

STM32CubeMX generated code and HAL initialization is now isolated in the main.c module. Feel free to extend app_main.c with your user code instead.