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:
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.
- 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:
- 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:
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.
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:
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.
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:
- Open main.c in the μVision editor. Find the USER CODE includes and add the cmsis_os2.h header.
#include "RTE_Components.h"
#ifdef RTE_Compiler_EventRecorder
#include "EventRecorder.h"
#endif
#include "cmsis_os2.h"
extern void app_main (void* arg);
- Find the following code sequence near main():
- Add the following code:
uint32_t HAL_GetTick (void) {
static uint32_t ticks = 0U;
uint32_t i;
if (osKernelGetState () == osKernelRunning) {
return ((uint32_t)osKernelGetTickCount ());
}
for (i = (SystemCoreClock >> 14U); i > 0U; i--) { __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); }
return ++ticks; }
- Repeat for user code 2 sequence inside main():
- Replace with:
#ifdef RTE_Compiler_EventRecorder
EventRecorderInitialize(EventRecordAll, 1);
#endif
SystemCoreClockUpdate();
osKernelInitialize();
osThreadNew(app_main, NULL, NULL);
osKernelStart();
- Create a new source file in your project called app_main.c and add the following content:
#include "cmsis_os2.h"
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.