px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
7.6 How to get started with STM32CubeMX

1. Introduction

STM32CubeMX is a free cross-platform graphical tool to generate code for STM32 microcontrollers. It's the easiest way to generate test / demo code for a new STM32 microcontroller and understand it's complex clock system. It's also handy to check pin function assignments before commiting to PCB layout.

STm32CubeMX is now integrated into STM32CudeIDE, but my personal preference is to use it as a standalone tool.

2. Download and Install

HERE is the download page.

2.1 Install required packages

Open the Embedded Software Packages Manager dialog:

Help > Manage embedded software packages Alt-U

Note that by default the "STM32Cube MCU Packages" tab is selected, but you can also select the "ARM" or "STMicroelectronics" tab to respectively install CMSIS and ST middleware libraries.

Expand the STM32L0 series by clicking on the black triangle and tick the latest "STM32Cube MCU Package for STM32L0 Series". Press the "Install Now" button" to download and install the support package.

3. Create a new project

Open the New Project dialog:

File > New Project Ctrl-N

Note that by default the "MCU/MPU Selector" tab is selected, but you can also select the "Board Selector" or "Cross Selector" tab.

In Part Number Search type in "stm32l072rb" and double-click on "STM32L072RBTx" (the LQFP64 chip package).

You will now be presented with a Pinout view of the microcontroller pins.

Save your new project.

File > Save Project As .. Ctrl-A

Be sure to save your project early and often as the software does crash sometimes.

4. Assign a function to each pin

Left click on a pin and a menu will appear that list all of the possible functions that can be assigned to that pin. Left click on the desired function and it will be assigned to that pin.

You can also remove an assignment by left clicking on the pin and left clicking on "Reset_State".

To give a new label or change a label, right click on the pin and left click on "Enter User Label"

It's slightly annoying, but if you change a pin assignment (e.g. from "GPIO_Output" to "GPIO_Input") it will lose it's label and you will have to type it in again.

The Alternate functions for each pin are listed in tables 17 - 22 starting on p.53 of the STM32L072RB Datasheet:

From this table it's easy to spot for example that USART2_TX can be assign to PA2 by selecting AF4 (Alternate Function 4).

The PX-HER0 board schematic makes it easy. Each possible pin function is listed in the schematic symbol of the STM32L072 IC and the assigned function is underlined in red.

System Core > GPIO

Expand the System Core group by left clicking on it and activate the GPIO window by left clicking on it. Each pin's attributes can be changed in the "GPIO Mode and Configuration" window.

Click on the column heading "User Label" to sort the pins by asigned name or "Pin Name" to return back to default sorting.

Select a pin in the list by clicking on it and the Configuration window will appear. If the pin is configured as an output, the initial level can be changed from "Low" to "High". The GPIO mode can be changed from "Output Push Pull" to "Output Open Drain" or the internal pull-up or pull-down resistor can be enabled. The maximum output speed can be changed from "Low" to "Medium", "High" or "Very High". It is also an opportunity to change the user label.

5. Assign a peripheral function

You will notice that when you configure PA2 as USART2_TX it turns yellow and not green. This indicates that the peripheral has not been enabled yet.

Connectivity > USART2

Expand the Connectivity group by left clicking on it and activate the USART2 window by left clicking on it. Change the Mode to Asynchronous to enable the peripheral. PA2 and PA3 will turn green to indicate that the peripheral is enabled.

By default the Parameter Settings tab is selected. The Baud Rate, Word Length, Parity and Stop Bits can be changed if desired.

The NVIC Settings tab allows the USART2 interrupt to be enabled.

The GPIO Settings tab allows for example to enable the pull-up resistor on PA3 (USART2_RX).

You may need to scroll the window down to see the PA3 Configuration dialog.

System Core > GPIO

If you return to the GPIO window you will see that a USART tab has appeared. It provides an alternative way to enable the pull-up resistor of PA3. You can also change the pin from "Alternate Function Push Pull" to "Alternate Function Open Drain". Very versatile!

5. Clock configuration

Timers > RTC

Configure PC14 and PC15 as OSC32 pins to use an external 32.768 kHz crystal as a clock source. Expand the Timers group and select the RTC window. Activate the Clock Source and Calender.

Select the Clock Configuration tab. You will notice that the LSE (Low Speed External) oscillator source is the 32.768 kHz crystal (±20 ppm accuracy). The LSE is selected as the clock source for the RTC in the RTC Source Mux symbol.

The 16 MHz HSI RC (High Speed Internal RC Oscillator; ±2% accuracy) is enabled and selected as the clock source for the PLL in the PLL Source Mux symbol. The PLL (Phase Locked Loop) is configured to multiply the input clock frequency by 4 and divide it by 2.

The 32 MHz PLLCLK is selected as the clock source for SYSCLK (System Clock) in the System Clock Mux symbol. The SYSCLK is then distributed to all of the different peripherals.

You will notice that the USBCLK is grayed out, because the USB peripheral has not been enabled yet. It can be supplied from the PLL or a separate 48 MHz RC oscillator. The accuracy of the clock can be improved by using the USB SOF (Start Of Frame) packet that is received every 1 ms.

6. Project Manager

Select the Project Manager tab.

6.1 Project

The Project tab will be selected by default. The Toolchain / IDE can be changed to STM32CubeIDE. The location of the complete STM32Cube library is displayed (grayed out) at the bottom.

6.2 Code Generator

The Code Generator will only copy the neccesary library files into the project. Tick "Generate peripheral initialization as a pair of '.c/.h' files per peripheral" and "Set all free pins as analog (to optimize the power consumption)".

6.3 Advanced Settings

In Advanced Settings the tool can be configured to general HAL (Hardware Abstraction Layer) or LL (Low Level) code for each peripheral.

I use the tool to generate lean LL code for the RCC (Reset and Clock Control) peripheral and use it to enable the clocks for the PX-HER0 board in px_board.h : STM32L072RB PX-HER0 Board.

7. Generate Code

Click on the "Generate Code" button. After the tool has finished, you can open the folder to view the files or open the project in STM32CubeIDE. Great!

8. Help

Help > Docs & Resources Alt-D

The relevant documents for the selected microcontroller is neatly collected in the "Docs & Resources" window. Once downloaded it's just a few mouse clicks away.

9. Example

A complete STM32CubeMX HAL project has been configured for the PX-HER0 board and can be found HERE.