Library 04- USART for STM32F4

USARTA lot of times when you work on some project, you want to display data on computer. This can be done with USART peripheral on MCU. With USART you can connect more than just computer, you can connect with GSM modules, GPRS, bluetooth and so much more. Our discovery board supports up to 8 USART channels. In this tutorial we will use USART1 to show principle how to use USART in our project. But first, we have to initialize our pins and peripheral.

Pins pack 1 Pins pack 2 Pins pack 3
U(S)ARTx TX RX TX RX TX RX APB
USART1 PA9 PA10 PB6 PB7 2
USART2 PA2 PA3 PD5 PD6 1
USART3 PB10 PB11 PC10 PC11 PD8 PD9 1
UART4 PA0 PA1 PC10 PC11 1
UART5 PC12 PD2 1
USART6 PC6 PC7 PG14 PG9 2
UART7 PE8 PE7 PF7 PF6 1
UART8 PE1 PE0 1

USART1 uses pins

  • PA9 for transmitting data
  • PA10 for receiving data

so they must have enabled clock, set for alternating function and set them this alternating function.

Then, when you initialized pins, we have to initialize USART peripheral too

Because our USART uses interrupt for incoming data, we have to initialize NVIC (Nested Vector Interrupt Controller). Everytime when data will come, our main program will stop executing and our interrupt routine will jump in.

Library

Features

  • Operate with up to 8 U(S)ARTs in UART mode
  • Selectable pins for USART
  • Receive interrupt handler

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx USART
  • TM
    • defines.h
    • attributes.h
    • TM GPIO

Functions and enumerations

Hardware

Your computer probably has not RS232 connector, but it has USB for sure. You need USB to serial converter. I prefer FTDI which is very stable and support 3V3 levels. You can buy converter on ebay for about 5$. Connect converter’s TX with board’s RX and converter’s RX with board’s TX. To display data, you need some terminal, I use one from Bray. Open program and set:

  • Select your COM port
  • baudrate to 9600
  • Data bits to 8
  • Parity to none
  • Stop bits to 1

Example

Project available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!