You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository contains a simple UART (Universal Asynchronous Receiver/Transmitter) driver for the STM32F4 microcontroller. The project demonstrates basic UART receive (RX) and transmit (TX) functionality and uses UART input to control an onboard LED.
📁 Project Files
File
Description
uart.c
UART driver implementation
uart.h
UART driver API/header
main.c
Main program using UART and GPIO logic
🔌 Hardware and Pin Configuration
STM32F4 Pins Used
Peripheral
Pin
Function
Description
UART2
PA2
TX (AF7)
Transmits UART data
UART2
PA3
RX (AF7)
Receives UART data
GPIOA
PA5
Output
Controls the onboard user LED
⚙️ Configuration Details
UART2 Pin Setup
PA2: Configured as alternate function AF7 for UART2 TX
PA3: Configured as alternate function AF7 for UART2 RX
Alternate functions are set in UART2_RXTX_INIT() in uart.c
LED Pin Setup
PA5: Configured as output
Receiving character '1' over UART turns the LED ON
Any other character turns the LED OFF
UART Parameters
Parameter
Value
Baud Rate
115200
Data Bits
8
Stop Bits
1
Parity
None
Flow Control
None
🧠 How the Code Works
1. Initialization
UART2 is initialized with the above parameters
GPIOA clock is enabled
PA2, PA3 set as alternate function for UART
PA5 is set as output for LED control
2. Main Loop Operation
Continuously waits for a character from UART2
If received character is '1', sets PA5 HIGH (LED ON)
Otherwise, sets PA5 LOW (LED OFF)
▶️ Using the Example
Requirements
Hardware: STM32F4 Discovery board (or compatible)
Software: STM32CubeIDE, Keil uVision, or any ARM GCC toolchain
Serial Terminal: PuTTY, Tera Term, Minicom, etc.
Serial Terminal Settings
Setting
Value
Baud Rate
115200
Data Bits
8
Stop Bits
1
Parity
None
Flow Ctrl
None
🔁 Flashing & Testing Instructions
Copy uart.c, uart.h, and main.c into your STM32 project
Compile and upload the code to your STM32F4 board
Connect UART2 pins:
PA2 (TX) → Connect to RX of USB-to-Serial adapter
PA3 (RX) → Connect to TX of USB-to-Serial adapter
Open your serial terminal and apply the settings above
Send:
'1' → LED ON
Any other key → LED OFF
📌 Code Structure Highlights
Proper initialization of GPIO and UART pin modes
Baud rate calculations based on 16 MHz system clock
(⚠️ Adjust if your board uses a different clock)
Low-level register operations are commented for clarity
About
This project demonstrates basic UART communication on an STM32F4 microcontroller. Using UART2 (PA2/PA3), the code receives characters from a serial terminal and controls the onboard LED (PA5) — the LED turns ON when '1' is received, and OFF for any other character. The project includes simple driver code for UART initialization, data tx and rx opt