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

### On this page

- an STM32L053xx Nucleo board
  - 14.1 Tutorial overview
  - selecting the Nucleo board
    - Figure 662. Selecting NUCLEO_L053R8 board
  - 14.3 Selecting the features from the Pinout view
    - Figure 664. Selecting TIM2 clock source
    - Figure 665. Selecting asynchronous mode for USART2
  - 14.4 Configuring the MCU clock tree from the Clock Configuration view
  - 14.5 Configuring the peripheral parameters from the Configuration view
    - Figure 670. Enabling TIM2 interrupt
  - 14.6 Configuring the project settings and generating the project
  - 14.7 Updating the project with the user application code
  - 14.8 Compiling and running the project
  - client on the PC

# 14 Tutorial 4 - Example of UART communications with an STM32L053xx Nucleo board

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

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

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

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