Berkner Tech

Intro to STM32 Lesson 3: USART

Time Required: 20 Minutes ⏱️

This tutorial goes through the steps for establishing a RS232 serial connection with an STM32 MCU for receiving and sending text over the USART or UART (Universal Synchronous Asynchronous Receiver Transmitter). Serial communication is the process of sequentially sending data one bit at a time over a single wire. It is one of the most simple yet effective methods of connecting your MCU with sensors, other processors, or, in this case, your computer.

Prerequisites Before Starting

    1. Everything from Lesson 2: GPIO
    2. STM32 Board
      1. Tutorial uses STM32F756ZG Nucleo 144
    3. USB Cable

Create a New Project with STM32XCubeMX

    1. Open STM32CubeMX
    2. Select New Project or Menu > File > New Project
    3. Under Board Selector select the series and type of your STM32 board, for this tutorial the NUCLEO-FZ576ZG STM32F7 Series Nucleo144 type will be used
    4. Select Start Project and Yes for initializing all peripherals with their default mode including the MPU

  1.  

Clock Configuration

The default settings under Clock Configuration (see below) should be sufficient for the purpose of this application.

Pinout Configuration

STM32 Nucleo boards include an ST-LINK onboard USB programmer and debugger with a few extra features including a USART.

Many of the STM32 Software Defaults do not match the STM32 hardware defaults. Using your STM32’s datasheet and User Guide will be key to determining the correct pinout.

    1. Open Pinout & Configuration > Connectivity in STM32CubeMX and select USART3

    2. Under Mode select Asynchronous

    3. Take note of the settings under Basic Parameters at the bottom as we’ll need these for our terminal later

    4. Now we need to select the USART RX and TX pins, if you type USART3_RX into the search bar, you’ll see both PB11 and PD9 start blinking. Which one is the correct choice? The answer is… it depends. Most MCUs will have multiple pins connected to each USART so serial data can be read from multiple locations by the same on-board hardware. In this case, we want to connect USART3 to the on-board ST-LINK so that it can be connected to the computer. A quick search for USART3 points us to the following section in the Nucleo-144 User Manual. It can be seen from this table that PD9 is the correct selection for USART3 RX and PD8 is the correct selection for USART3 TX. Click on PD9 and select USART3_RX . Click on PD8 and select USART3_TX .

    5. Switch to the Project Manager tab, name the project, and select STM32CubeIDE as the Toolchain / IDE.
    6. Now generate your code! If you need help doing this check out Lesson 1: GPIO

  1.  

  1.  

  1.  

  1.  

  1.  

Code Configuration

    1. Open STM32CubeIDE
    2. Open <YOUR_PROJECT_NAME>/Src/main.c in Project Explorer
    3. Add the following snippet between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */
      uint8_t Test[] = "STM32 USART Lesson Complete!rn"; // String to send over USART
      HAL_UART_Transmit(&huart3,Test,sizeof(Test),10); // Send in normal mode
      HAL_Delay(2000); // Wait 2 seconds before next transmit

    4. Open a serial console emulator of your choice such as PuTTY or TeraTerm. Ensure the baud rate and data bit length is the same as the settings selected above (1152000 Baud, 8 Bit Data). Select the COM Port the STM32 is connected to and open the connection.
    5. In STM32CubeIDE click Run to build and load the project onto the STM32 MCU
    6. STM32 USART Lesson Complete! should now be visible within your serial console

Congratulations! You can now communicate with your STM32MCU! Next we’ll dive into reading analog values with an Analog Digital Converter (ADC) and how to speed up this process with a Direct Memory Access (DMA) Controller in Lesson 4: ADC & DMA