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

../../../../_images/image8350.webp

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.

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

  2. Configuration of the peripheral parameters from the Configuration view

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

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

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

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

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

../../../../_images/image8360.webp

14.3 Selecting the features from the Pinout view

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

Figure 663. Selecting debug pins

../../../../_images/image8370.webp
  1. Select Internal Clock as clock source under TIM2 peripheral (see Figure 664).

Figure 664. Selecting TIM2 clock source

../../../../_images/image838.webp
  1. Select the Asynchronous mode for the USART2 peripheral (see Figure 665).

Figure 665. Selecting asynchronous mode for USART2

../../../../_images/image8390.webp
  1. 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

../../../../_images/image1276.webp

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

../../../../_images/image8410.webp

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

../../../../_images/image8420.webp
  1. Click TIM2 and change the prescaler to 16000, the Word Length to 8 bits and the Counter Period to 1000 (see Figure 669).

../../../../_images/image1280.webp
  1. Enable TIM2 global interrupt from the NVIC Settings tab (see Figure 670).

Figure 670. Enabling TIM2 interrupt

../../../../_images/image8440.webp

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

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

../../../../_images/image1285.webp

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

../../../../_images/image1287.webp
  1. 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

../../../../_images/image8480.webp
  1. The Tera Term window displays a message coming from the board at a period of a few seconds (see Figure 675).