STM32CubeMX

Table Of Contents

- STM32CubeMX documentation
- CubeMX User Manual
  - 1 STM32Cube overview
  - 2 Getting started with STM32CubeMX
  - 3 Installing and running STM32CubeMX
  - 4 STM32CubeMX user interface
  - 5 STM32CubeMX tools
  - 6 STM32CubeMX C Code generation overview
  - 7 Code generation for dual-core MCUs (STM32H7 dual-core product lines only)
  - 8 Code generation with TrustZone enabled (STM32L5 series only)
  - 9 Device tree generation (STM32MPUs only)
  - 10 Support of additional software components using CMSIS-Pack standard
  - 11 Tutorial 1: From pinout to project C code generation using an MCU of the STM32F4 series
  - 12 Tutorial 2 - Example of FatFs on an SD card using STM32429I-EVAL evaluation board
  - 13 Tutorial 3 - Using the Power Consumption Calculator to optimize the embedded application consumption and more
  - 14 Tutorial 4 - Example of UART communications with an STM32L053xx Nucleo board
  - 15 Tutorial 5: Exporting current project configuration to a compatible MCU
  - 16 Tutorial 6 - Adding embedded software packs to user projects
  - 17 Tutorial 7 - Using the X-Cube-BLE1 software pack
  - 18 Creating LPBAM projects
  - Appendix A STM32CubeMX pin assignment rules
  - Appendix B STM32CubeMX C code generation design choices and limitations
  - Appendix C STM32 microcontrollers power consumption parameters
  - Appendix D STM32Cube embedded software packages
  - Revision history
- Release Notes
  - Release details
  - Release fixes
  - Known limitations
    - Miscellaneous
    - 2.1. When selecting microcontrollers in the STM32C0 series
    - 2.2. When selecting microcontrollers in the STM32G0 series
    - 2.3. When selecting microcontrollers in the STM32G4 series
    - 2.4. When selecting microcontrollers in the STM32H5 series
    - 2.5. When selecting microcontrollers in the STM32H7 series
    - 2.6. When selecting dual-core microcontrollers in the STM32H7 series
    - 2.7. When selecting microcontrollers in the STM32L5 series
    - 3.4. How to improve the wake-up from Stop sequences without relying on the global SystemClock_Config()
    - 3.3. List pinout-compatible microcontrollers
    - 3.2. DDR binaries for the STM32MP1 boards
    - 3.1. Command line authentication required
    - 2.21. Symbolic links on Ubuntu
    - 2.20. User manual
    - 2.19. DDR test suite
    - 2.18. STM32CubeIDE toolchain
    - 2.13. When selecting microcontrollers in the STM32WL series
    - 2.11. When selecting microcontrollers in the STM32WB series
    - 2.17. When using additional software packs
    - 2.16. When using the Example Selector
    - 2.15. When using the Memory Management Tool
    - 2.14. When selecting microprocessors in the STM32MP1 series
    - 2.11.1. When selecting microcontrollers in the STM32WB0 product lines
    - 2.12. When selecting microcontrollers in the STM32WBA series
    - 2.13.1. When selecting microcontrollers in the STM32WL33 product lines
    - 2.10. When selecting microcontrollers in the STM32U5 series
    - 2.9. When selecting microcontrollers in the STM32U3 series
    - 2.8. When selecting microcontrollers in the STM32N6 series
- Running STM32CubeMX in command-line mode
- FAQ
- Community

# 13 Tutorial 3 - Using the Power Consumption Calculator to optimize the embedded application consumption and more

** /* USER CODE END 2 */**

> /* Infinite loop */

> /* USER CODE BEGIN WHILE */ while (1)

13 Tutorial 3 - Using the Power Consumption Calculator to optimize the embedded application consumption and more

13.1 Tutorial overview

This tutorial focuses on STM32CubeMX Power Consumption Calculator (Power Consumption Calculator) feature and its benefits to evaluate the impacts of power-saving techniques on a given application sequence.

The key considerations to reduce a given application power consumption are:

• Reducing the operating voltage

• Reducing the time spent in energy consuming modes

It is up to the developer to select a configuration that gives the best compromise between low-power consumption and performance.

• Maximizing the time spent in non-active and low-power modes

• Using the optimal clock configuration

The core should always operate at relatively good speed, since reducing the operating frequency can increase energy consumption if the microcontroller has to remain for a long time in an active operating mode to perform a given operation.

• Enabling only the peripherals relevant for the current application state and clock-gating the others

• When relevant, using the peripherals with low-power features (e.g. waking up the microcontroller with the I2C)

• Minimizing the number of state transitions

• Optimizing memory accesses during code execution

– Prefer code execution from RAM to flash memory

– When relevant, consider aligning CPU frequency with flash memory operating frequency for zero wait states.

The following tutorial shows how the STM32CubeMX Power Consumption Calculator feature can help to tune an application to minimize its power consumption and extend the battery life.

Note: The Power Consumption Calculator does not account for I/O dynamic current consumption and external board components that can also affect current consumption. For this purpose, an “additional consumption” field is provided for the user to specify such consumption value.

13.2 Application example description

The application is designed using the NUCLEO-L476RG board, based on an STM32L476RGTx device, and supplied by a 2.4 V battery.

The main purpose of this application is to perform ADC measurements and transfer the conversion results over UART. It uses:

- Multiple low-power modes: Low-power run, Low-power sleep, Sleep, Stop and Standby • Multiple peripherals: USART, DMA, Timer, COMP, DAC and RTC

  > – The RTC is used to run a calendar and to wake up the CPU from Standby when a specified time has elapsed.

  > – The DMA transfers ADC measurements from ADC to memory

  > – The USART is used in conjunction with the DMA to send/receive data via the virtual COM port and to wake up the CPU from Stop mode.

  > The process to optimize such complex application is to start describing first a functional only sequence then to introduce, on a step by step basis, the low-power features provided by the STM32L476RG microcontroller.

  > 13.3 Using the Power Consumption Calculator

  > 13.3.1 Creating a power sequence

  > Follow the steps below to create the sequence (see Figure 651):

  > 1. Launch STM32CubeMX.

  > 2. Click new project and select the Nucleo-L476RG board from the Board tab.

  > 3. Click the Power Consumption Calculator tab to select the Power Consumption Calculator view. A first sequence is then created as a reference.

  > 4. Adapt it to minimize the overall current consumption. To do this:

  > a) Select 2.4 V VDD power supply. This value can be adjusted on a step by step basis (see Figure 652).

  > b) Select the Li-MnO2 (CR2032) battery. This step is optional. The battery type can be changed later on (see Figure 652).

  > Figure 651. Power Consumption Calculation example

  > Figure 652. VDD and battery selection menu

  > 5. Enable the Transition checker to ensure the sequence is valid (see Figure 652). This option allows verifying that the sequence respects the allowed transitions implemented within the STM32L476RG.

  > 6. Click the Add button to add steps that match the sequence described in Figure 652.

  > – By default the steps last 1 ms each, except for the wake-up transitions preset using the transition times specified in the product datasheet (see Figure 653).

  > – Some peripherals for which consumption is unavailable or negligible are highlighted with * (see Figure 653).

  > 7. Click the Save button to save the sequence as SequenceOne.

  > The application consumption profile is generated. It shows that the overall sequence consumes an average of 2.01 mA for 9 ms, and that the battery lifetime is only four days (see Figure 654).

  > Figure 654. sequence results before optimization

  > 13.3.2 Optimizing application power consumption

  > Let us now take actions to optimize the overall consumption and the battery lifetime. These actions are performed on steps 1, 4, 5, 6, 7, 8, and 10.

  > The next figures show on the left the original step, and on the right the step updated with optimization actions.

  > Step 1 (Run) - Findings

  > All peripherals are enabled although the application requires only the RTC.

  > • Actions

  > – Lower the operating frequency

  > – Enable only the RTC peripheral

  > – To reduce the average current consumption, reduce the time spent in this mode

  > • Result

  > The current is reduced from 9.05 to 2.16 mA (see Figure 655).

  > Figure 655. Step 1 optimization

  > Step 4 (Run, RTC) • Action

  > Reduce the time spent in this mode to 0.1 ms

  > Step 5 (Run, ADC, DMA, RTC)

  > • Actions

  > – Change to Low-power run mode

  > – Lower the operating frequency

  > • Results

  > The current consumption is reduced from 6.17 mA to 271 µA (see Figure 656).

  > Figure 656. Step 5 optimization

  > Step 6 (Sleep, DMA, ADC, RTC)

  > • Actions

  > – Switch to Lower-power sleep mode (BAM mode)

  > – Reduce the operating frequency to 2 MHz

  > • Results

  > The current consumption is reduced from 703 µA to 93 µA (see Figure 657).

  > Figure 657. Step 6 optimization

  > Step 7 (Run, DMA, RTC, USART)

  > • Actions

  > – Switch to Low-power run mode

  > – Use the power efficient LPUART peripheral

  > – Reduce the operating frequency to 1 MHz using the interpolation feature

  > • Results

  > The current consumption is reduced from 1.92 mA to 42 µA (see Figure 658).

  > Figure 658. Step 7 optimization

  > |image588|

  > Step 8 (Stop 0, USART) • Actions

  > – Switch to Stop1 low-power mode

  > – Use the power-efficient LPUART peripheral

  > > • Results

  > The current consumption is reduced (see Figure 659).

  > Figure 659. Step 8 optimization

  > Step 10 (RTC, USART) • Actions

  > – Use the power-efficient LPUART peripheral

  > – Reduce the operating frequency to 1 MHz

  > > • Results

  > The current consumption is reduced from 1.89 mA to 234 µA (see Figure 660).

  > The example given in Figure 661 shows an average current consumption reduction of 155 µA.

  > See Figure 661 for the overall results: 7 ms duration, about two months battery life, and an average current consumption of 165.25 µA.

  > Use the compare button to compare the current results to the original ones saved as SequenceOne.pcs.

  > Figure 661. Power sequence results after optimizations

14 Tutorial 4 - Example of UART communications with

an STM32L053xx Nucleo board

This tutorial aims at demonstrating how to use STM32CubeMX to create a UART serial communication application for a NUCLEO-L053R8 board.

A Windows PC is required for the example. The ST-Link USB connector is used both for serial data communications, and firmware downloading and debugging on the MCU. A Type-A to mini-B USB cable must be connected between the board and the computer. The USART2 peripheral uses PA2 and PA3 pins, which are wired to the ST-Link connector. In addition, USART2 is selected to communicate with the PC via the ST-Link Virtual COM Port. A serial communication client, such as Tera Term, needs to be installed on the PC to display the messages received from the board over the virtual communication Port.

14.1 Tutorial overview

Tutorial 4 will take you through the following steps:

1. Selection of the NUCLEO-L053R8 board from the New Project menu.

2. Selection of the required features (debug, USART, timer) from the Pinout view:

peripheral operating modes as well as assignment of relevant signals on pins.

3. Configuration of the MCU clock tree from the Clock Configuration view.

4. Configuration of the peripheral parameters from the Configuration view

5. Configuration of the project settings in the Project Manager menu and generation of the project (initialization code only).

6. Project update with the user application code corresponding to the UART communication example.

7. Compilation, and execution of the project on the board.

8. Configuration of Tera Term software as serial communication client on the PC.

9. The results are displayed on the PC.

14.2 Creating a new STM32CubeMX project and

selecting the Nucleo board

To do this, follow the sequence below:

1. Select File > New project from the main menu bar. This opens the New Project window.

2. Go to the Board selector tab and filter on STM32L0 series.

3. Select NUCLEO-L053R8 and click OK to load the board within the STM32CubeMX user interface (see Figure 662).

Figure 662. Selecting NUCLEO_L053R8 board

14.3 Selecting the features from the Pinout view

1. Select Debug Serial Wire under SYS (see Figure 663).

Figure 663. Selecting debug pins

2. Select Internal Clock as clock source under TIM2 peripheral (see Figure 664).

Figure 664. Selecting TIM2 clock source

> 3. Select the Asynchronous mode for the USART2 peripheral (see Figure 665).

Figure 665. Selecting asynchronous mode for USART2

> 4. Check that the signals are properly assigned on pins (see Figure 666):

– SYS_SWDIO on PA13

– TCK on PA14

– USART_TX on PA2

– USART_RX on PA3

14.4 Configuring the MCU clock tree from the Clock Configuration view

1. Go to the Clock Configuration tab and leave the configuration untouched, in order to use the MSI as input clock and an HCLK of 2.097 MHz (see Figure 667).

Figure 667. Configuring the MCU clock tree

14.5 Configuring the peripheral parameters from the Configuration view

1. From the Configuration tab, click USART2 to open the peripheral Parameter Settings window and set the baud rate to 9600. Make sure the Data direction is set to “Receive and Transmit” (see Figure 668).

2. Click OK to apply the changes and close the window.

Figure 668. Configuring USART2 parameters

3. Click TIM2 and change the prescaler to 16000, the Word Length to 8 bits and the Counter Period to 1000 (see Figure 669).

4. Enable TIM2 global interrupt from the NVIC Settings tab (see Figure 670).

Figure 670. Enabling TIM2 interrupt

14.6 Configuring the project settings and generating the project

1. In the Project Settings menu, specify project name and destination folder, and select the EWARM IDE toolchain (see Figure 671).

If the firmware package version is not already available on the user PC, a progress window opens to show the download progress.

2. In the Code Generator tab, configure the code to generate as shown in Figure 672, and click OK to generate it.

14.7 Updating the project with the user application code

Add the user code as follows:

/* USER CODE BEGIN 0 */

#include “stdio.h”

#include “string.h”

/* Buffer used for transmission and number of transmissions */ char aTxBuffer[1024]; int nbtime=1;

/* USER CODE END 0 */

Within the main function, start the timer event generation function as follows:

/* USER CODE BEGIN 2 */

** /* Start Timer event generation */**

** HAL_TIM_Base_Start_IT(&htim2);**

** /* USER CODE END 2 */**

/* USER CODE BEGIN 4 */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){ sprintf(aTxBuffer,”STM32CubeMX rocks %d times \t”, ++nbtime);

HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 5000); }

/* USER CODE END 4 */

14.8 Compiling and running the project

1. Compile the project within your favorite IDE.

2. Download it to the board.

3. Run the program.

14.9 Configuring Tera Term software as serial communication

client on the PC

1. On the computer, check the virtual communication port used by ST Microelectronics from the Device Manager window (see Figure 673).

|image603|

2. To configure Tera Term to listen to the relevant virtual communication port, adjust the parameters to match the USART2 parameter configuration on the MCU (see Figure 674).

Figure 674. Setting Tera Term port parameters

|image604|

3. The Tera Term window displays a message coming from the board at a period of a few seconds (see Figure 675).

Figure 675. Setting Tera Term port parameters

|image605|

Tutorial 5: Exporting current project configuration to a compatible MCU

15 Tutorial 5: Exporting current project configuration to

a compatible MCU

When List pinout compatible MCUs is selected from the Pinout menu, STM32CubeMX retrieves the list of the MCUs which are compatible with the current project configuration, and offers to export the current configuration to the newly selected compatible MCU.

This tutorial shows how to display the list of compatible MCUs and export your current project configuration to a compatible MCU:

1. Load an existing project, or create and save a new project:

Figure 676. Existing or new project pinout

|image606|

2. Go to the Pinout menu and select List Pinout Compatible MCUs. The Pinout compatible window pops up (see Figure 677 and Figure 678).

If needed, modify the search criteria and the filter options and restart the search process by clicking the Search button.

The color shading and the Comments column indicate the level of matching:

– Exact match: the MCU is fully compatible with the current project (see Figure 678 for an example).

– Partial match with hardware compatibility: the hardware compatibility can be ensured but some pin names could not be preserved. Hover the mouse over the desired MCU to display an explanatory tooltip (see Figure 677 for an example).

Tutorial 5: Exporting current project configuration to a compatible MCU

– Partial match without hardware compatibility: not all signals can be assigned to the exact same pin location and a remapping will be required. Hover the mouse over the desired MCU to display an explanatory tooltip (see Figure 678 for an example).

Figure 677. List of pinout compatible MCUs - Partial match with hardware compatibility

|image607|

Tutorial 5: Exporting current project configuration to a compatible MCU

3. Select an MCU to import the current configuration to, and click OK, Import:

Figure 679. Selecting a compatible MCU and importing the configuration

|image608|

The configuration is now available for the selected MCU:

Figure 680. Configuration imported to the selected compatible MCU

|image609|

Tutorial 5: Exporting current project configuration to a compatible MCU

4. To see the list of compatible MCUs, select Outputs under the Window menu.

To load the current configuration to another compatible MCU, double-click the list of compatible MCUs.

5. To remove some constraints on the search criteria, several solutions are possible:

– Select the Ignore Pinning Status checkbox to ignore pin status (locked pins).

– Select the Ignore Power Pins checkbox to not take into account the power pins.

– Select the Ignore System Pins to not take into account the system pins. Hover the mouse over the checkbox to display a tooltip that lists the system pins available on the current MCU.