A ready-to-use template for STM32F4 microcontroller development using the Standard Peripheral Library (SPL). Includes a fully configured Makefile, proper folder structure, and all necessary files to start coding immediately.
- Features
- Project Structure
- Prerequisites
- Tool Installation Guide
- Getting Started
- Build & Flash Commands
- VS Code Integration
- Troubleshooting
- Author
- β Full STM32F4xx Standard Peripheral Library included
- β GNU Make build system (no IDE lock-in)
- β ARM GCC toolchain support
- β OpenOCD/ST-Link flashing support
- β VS Code configuration files included
- β
Proper
.gitignorefor embedded projects - β Linker script and startup code included
- β Support for multiple flash tools (OpenOCD, ST-Link, J-Link)
.
βββ Makefile # Main build configuration
βββ stm32_hello.ld # Linker script
βββ system_stm32f4xx.c # System initialization
βββ .gitignore # Git ignore rules
βββ README.md # This file
β
βββ src/ # Source files
β βββ main.c # Main program
β
βββ inc/ # Header files
β βββ main.h
β βββ CMSIS/ # CMSIS headers
β βββ STM32F4xx/ # STM32F4xx CMSIS files
β βββ STM32F4xx_StdPeriph_Driver/ # SPL headers
β βββ inc/
β
βββ startup/ # Startup code
βββ startup_stm32f4xx.s # Assembly startup file
| Tool | Purpose | Minimum Version |
|---|---|---|
| ARM GCC Toolchain | Compiler | 10.3.1 |
| GNU Make | Build automation | 4.2 |
| OpenOCD (optional) | Flashing/Debugging | 0.11.0 |
| ST-Link Tools (optional) | Flashing | 1.7.0 |
| Git (optional) | Version control | 2.30.0 |
- Download from: Arm GNU Toolchain Downloads
- Choose:
arm-gnu-toolchain-xx.x.rel1-mingw-w64-i686-arm-none-eabi.exe - Install to:
C:\Program Files (x86)\Arm GNU Toolchain\ - During installation, check "Add path to environment variable"
- Download from: ezwinports
- Extract to:
C:\Program Files\make - Add to PATH:
C:\Program Files\make\bin
- Download from: OpenOCD for Windows
- Extract to:
C:\OpenOCD - Add to PATH:
C:\OpenOCD\bin
- Download from: ST website
- Install normally
Open Command Prompt and run:
arm-none-eabi-gcc --version
make --version
openocd --version# Update package list
sudo apt update
# Install ARM GCC toolchain
sudo apt install gcc-arm-none-eabi
# Install GNU Make
sudo apt install make
# Install OpenOCD
sudo apt install openocd
# Install ST-Link tools
sudo apt install stlink-tools
# Install Git (optional)
sudo apt install git
# Verify installation
arm-none-eabi-gcc --version
make --version
openocd --version# Install Homebrew (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install ARM GCC toolchain
brew install gcc-arm-embedded
# Install GNU Make (usually pre-installed)
brew install make
# Install OpenOCD
brew install openocd
# Install ST-Link tools
brew install stlink
# Verify installation
arm-none-eabi-gcc --version
make --version
openocd --version# Using Git
git clone https://github.com/long27032002/stm32f4-spl-template.git
cd stm32f4-spl-template
# Or download ZIP and extractEdit the Makefile to match your specific needs:
# Change target name (optional)
TARGET = your_project_name
# Change MCU if using different STM32F4 variant
# For STM32F407VG: already configured
# For other variants, update:
# - LDSCRIPT (linker script)
# - Startup file in startup/
# - C_DEFS (change STM32F407xx to your MCU)Edit src/main.c to add your application code. The template includes a simple LED blinking example:
#include "main.h"
int main(void) {
// Enable GPIO clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// Configure LED pin (PD12 - green LED on STM32F4-Discovery)
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStruct);
while (1) {
GPIO_ToggleBits(GPIOD, GPIO_Pin_12);
for (int i = 0; i < 1000000; i++); // Simple delay
}
}| Command | Description |
|---|---|
make |
Build the project (generate .elf, .hex, .bin) |
make clean |
Clean all build artifacts |
make all |
Same as make (default target) |
| Command | Description | Required Tool |
|---|---|---|
make flash |
Flash using OpenOCD (ST-Link v2) | OpenOCD |
make flash-st |
Flash using ST-Link CLI | STM32_Programmer_CLI |
make flash-stflash |
Flash using st-flash tools | st-flash |
make flash-jlink |
Flash using J-Link | J-Link tools |
make rebuild-flash |
Clean, rebuild, and flash | (depends on method) |
make help |
Show all available commands | β |
# Clean previous builds
make clean
# Build the project
make
# Flash to microcontroller (choose one)
make flash
# OR
make flash-stflash
# OR
make flash-stThe template includes VS Code configuration files for a better development experience.
Open the project:
code .Install recommended extensions:
- C/C++ (Microsoft)
- Cortex-Debug
- Makefile Tools
Build:
- Terminal:
make - VS Code Task:
Ctrl+Shift+B
Debug:
- Press
F5(configured for OpenOCD + ST-Link)
arm-none-eabi-gcc: command not found
Toolchain not installed or not in PATH. Reinstall or manually add to PATH.
make: *** No rule to make target
Check if all source files exist in correct paths. Run
make cleanthenmakeagain.
OpenOCD: command not found
Install OpenOCD or use a different flash method (
make flash-stflash).
Flash fails with can't find device
- Check ST-Link connection
- Install drivers (Windows only)
- Check permissions on Linux (
sudoor udev rules)
Undefined reference to RCC/GPIO functions
Make sure SPL source files are included in
C_SOURCESin the Makefile.
# Add user to dialout group
sudo usermod -a -G dialout $USER
# Create udev rule for ST-Link
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3748", MODE="0666"' \
| sudo tee /etc/udev/rules.d/49-stlink.rules
# Reload udev
sudo udevadm control --reload-rules
sudo udevadm triggerNguyα» n NhΖ° HαΊ£i Long β GitHub
π§ Email: [email protected]
- STMicroelectronics for the Standard Peripheral Library
- ARM for the GCC toolchain
- OpenOCD community
- All open-source contributors
For issues, questions, or contributions, please open an issue on GitHub.