STM32Cube  Version 2.0
Create Projects for STM32F0 Series with STM32Cube HAL and STM32CubeMX
 All Files Pages
Use STM32CubeMX in MDK Projects

Create New Project with STM32Cube Framework

Create MDK project and select a STM32 device

  • Open µVision.
  • From the menu select Project - New µVision project. Choose any name and location.
  • Select the device on your target hardware. If you have many Device Family Packs installed, the search field helps you to find a device quickly.
  • The Run-Time Environment Manager will show up.
  • Optionally: Select CMSIS::RTOS2::Keil RTX5 from the list of available software components. Select the “Source” variant.
  • 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.
  • Press 'Resolve' to satisfy all missing dependencies. This will make sure all components required will be included in your project.
  • Close the dialog by pressing “OK”.
  • This will automatically open a dialog to launch STM32CubeMX. Click on “Start STM32CubeMX” and wait until the STM32CubeMX GUI is fully loaded:
open_generator.png

Configure I/O and Clock

  • Verify that the device that shows up in the opened STM32CubeMX window is the one you chose for your project.
CubeMX_new.png
  • Use the PinOut view to configure all necessary I/O pins to the required functions. This is an example of setting pin PB13 to output mode:
CubeMX_GPIO_Output.png
  • Switch to the Clock Configuration tab and adjust the clock to the desired settings. HCLK is the setting for the clock speed of the corea and should be configured first:
CubeMX_ClockConfiguration.png

Generate Code

  • From the menu select Project – Generate Code.
  • 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.
CubeMX_generate_code.png

You can re-open the CubeMX project linked to the MDK project at anytime and make changes as required. The RTE window provides a button to launch CubeMX:

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 has created 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 in charge of:

  • 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.

Add and Configure RTX5

(Optional)

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

  • In STMCubeMX open the Configuration Tab, click on the NVIC configuration button and remove the checkmarks in "Generate IRQ handler" for SVC, PendSV and SysTick Interrupts:
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) {
    ...
    }
    }

CubeMX 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.