STM32CubeMX Tutorial Series: Overview

From Waveshare Wiki
Jump to: navigation, search

Although STM32CubeMX offers a user interface and generates a C code compliant with STM32 MCU design and firmware solutions, it is recommended to refer to the product technical documentation for details on actual implementation of microcontroller peripherals and firmware.

In this part, we use a STM32 development board, Open103Z, with STM32F103ZETx MCU. This board features a external crystal as a High Speed Clock, and the PF6, PF7, PF8, PF9 pins are connected to LEDs.

Create a New Project

Open the STM32cubeMX software and click New Project.

Stm32-cubemx-tutorial-series-1.png

Select the MCU of your development board in the MCU Filters list.

Stm32-cubemx-tutorial-series-2.png

In this part, we select the STM32F103ZETx MCU as an example. Now a new project was created.

RCC Configuration

RCC (Reset and clock control) Configuration. Select Crystal/Ceranic Resonator as High Speed Clock (HSE). In this part, we select the STM32F103ZETx MCU as an example.

Stm32-cubemx-tutorial-series-3.png

GPIO Configuration

Zoom in on the chip view and click the pin to set the GPIO mode. Because PF6, PF7, PF8 and PF9 are connected to LEDs, we set them to GPIO_Output. The yellow pins are currently in use by other function and the green pins are already setup by user.

Stm32-cubemx-tutorial-series-4.png

Clock Configuration

Thanks to STM32CubeMX, the clock Configuration is initiative and easy. Click the "Clock Configuration" tab and you can se each peripheral clock at a glance. The HCLK of this chip is 72MHz, so we enter 72 for the HCLK and the frequency value for buses or peripheral clocks will be updated. (If RCC High Speed Clock is disabled, you cannot enter 72 as the max MHz here)

Stm32-cubemx-tutorial-series-5.png

Configuration View

Stm32-cubemx-tutorial-series-6.png

STM32CubeMX Configuration window gives an overview of all the software configurable components. They are:

  • Multimedia: Media, LCD
  • Control: Timer
  • Analog: DAC、ADC
  • Connectivity: UART, SPI, I2C, USB, ETH
  • System: DMA, GPIO, NVIC, RCC, Watchdog
  • middlewares: FreeRTOS, FATFS, LwIP, USB

In this project, we wont't use DMA so ignore it. Click GPIO in the Configuration pane to open the GPIO configuration window that allows configuring the GPIO pin settings.

Stm32-cubemx-tutorial-series-7.png
  • GPIO Pin level: Low
  • GPIO mode: Output Push Pull
  • Maximum output speed: Low
  • User Label: LEDn

User Label changes the default name into a user defined name. The Chip view (Pinout tab) is updated accordingly.

Stm32-cubemx-tutorial-series-8.png

Power Consumption Calculator

For an ever-growing number of embedded systems applications, power consumption is a major concern. To help minimizing it, STM32CubeMX offers the Power Consumption Calculator (PCC) tab. Skip. STM32CubeMX offers the Power Consumption Calculator to help minimizing power consumption. Skip this tap.

Stm32-cubemx-tutorial-series-9.png

Generate Project Report

Reports can be generated at any time during the configuration. Click Project > Generate Reports to generate .pdf and .txt reports. The reports summarize all the settings and MCU configuration performed for the project. Project Settings window prompts at the first time generate a report.

You can open the Project Settings window by selecting Project > Settings from the STM32CubeMX menu bar.

Stm32-cubemx-tutorial-series-10.png

Enter the project name and specify the Project Location. Select your most commonly used IDE (e.g. MDK-ARM V5) and keep the Heap and Stack settings as default. Click the Code Generator tab and check "Generated peripheral initialization as a pair of '.c/.h' files per IP"

Stm32-cubemx-tutorial-series-11.png

Generate Project Code

You can get the source code by selecting Project > Generate Code. After the code generated, click Open Project button to open the project in the IDE you specified.

Stm32-cubemx-tutorial-series-12.png

Here the MDK was specified as the IDE.

Stm32-cubemx-tutorial-series-13.png

Build the project and we will analyse the C code.

C code Analysis

Open gpio.c file and we find the function MX_GPIO_Init(). This initiates the LED pins. It is defined in gpio.c

We will find the GPIO operation functions on the stm32f1xx_hal_gpio.h file. Add these to the main block:

HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(100);    // 100ms
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
HAL_Delay(100);    // 100ms
HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
HAL_Delay(100);    // 100ms
HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
HAL_Delay(100);    // 100ms

Rebuild the project and download it to the development board. Target configurations are often required. Click Options for Target and select Debug tap, then choose the Debugger accordingly. (Here is ST-Link)

Stm32-cubemx-tutorial-series-14.png

Select Settings > Flash Download and check "Reset and Run"

Stm32-cubemx-tutorial-series-15.png

After program downloaded, LEDs will blink one by one.

Last, the STM32CubeMX tool offers a firmware library. Users can call the functions to accelerate development. And your code may well be consistent with the full range of STM32 MCU code. The STM32CubeMx also provides a graphical view for the GPIO, peripherals, clock and so on, fast generating C code for initialization.

Then following lessons explain how to operating peripherals with the Open746I-C Development board. It may help you to get started with STM32CubeMx.