Skip to content

long27032002/STM32F4_SPL_Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

STM32F4 Project Template with Standard Peripheral Library (SPL)

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.


πŸ“‹ Table of Contents


✨ Features

  • βœ… 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 .gitignore for embedded projects
  • βœ… Linker script and startup code included
  • βœ… Support for multiple flash tools (OpenOCD, ST-Link, J-Link)

πŸ“ Project Structure

.
β”œβ”€β”€ 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


πŸ”§ Prerequisites

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

πŸ“₯ Tool Installation Guide

Windows

1. ARM GCC Toolchain

  • 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"

2. GNU Make

  • Download from: ezwinports
  • Extract to: C:\Program Files\make
  • Add to PATH: C:\Program Files\make\bin

3. OpenOCD (for flashing)

4. ST-Link Utilities (alternative flashing)

5. Verify Installation

Open Command Prompt and run:

arm-none-eabi-gcc --version
make --version
openocd --version

Linux (Ubuntu/Debian)

# 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

macOS

# 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

πŸš€ Getting Started

1. Clone or Download the Template

# Using Git
git clone https://github.com/long27032002/stm32f4-spl-template.git
cd stm32f4-spl-template

# Or download ZIP and extract

2. Configure the Project

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

3. Write Your Code

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

πŸ› οΈ Build & Flash Commands

Build Commands

Command Description
make Build the project (generate .elf, .hex, .bin)
make clean Clean all build artifacts
make all Same as make (default target)

Flash Commands

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 β€”

Complete Workflow Example

# Clean previous builds
make clean

# Build the project
make

# Flash to microcontroller (choose one)
make flash
# OR
make flash-stflash
# OR
make flash-st

πŸ“ VS Code Integration

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

πŸ” Troubleshooting

Common Issues

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 clean then make again.

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 (sudo or udev rules)

Undefined reference to RCC/GPIO functions

Make sure SPL source files are included in C_SOURCES in the Makefile.

Permission Issues on Linux

# 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 trigger

πŸ‘¨β€πŸ’» Author

Nguyα»…n NhΖ° HαΊ£i Long β€” GitHub

πŸ“§ Email: [email protected]

⭐ Acknowledgments


For issues, questions, or contributions, please open an issue on GitHub.

About

My template for STM32F4 microcontroller development using the Standard Peripheral Library (SPL)

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages