Sarkitshala

STM32 Setup — Step-by-step (English)

This guide is for beginners: covering hardware, toolchain, CubeMX/IDE, example code, and debugging tips.

1. What is STM32?

STM32 is an ARM Cortex-M based microcontroller family by STMicroelectronics. It offers a wide range of cores (M0/M0+/M3/M4/M7/M33) and peripherals — GPIO, UART, I²C, SPI, ADC, timers, DMA, USB, CAN, etc.

2. Required Hardware

  • STM32 development board (e.g., Nucleo / Discovery / Blue Pill / custom board)
  • ST-Link/V2 or onboard ST-Link debugger (Nucleo/Discovery boards have it built-in)
  • USB cable, breadboard, jumper wires, LED + resistor

3. Required Software

  1. STM32CubeIDE — ST's official IDE that integrates CubeMX configuration tool. (Recommended for beginners)
  2. STM32CubeMX — Pinout/clock generator (standalone CubeMX is useful if using CLI/VSCode)
  3. GNU Arm Embedded Toolchain (arm-none-eabi-gcc) — compiler
  4. OpenOCD / ST-Link Utility — flashing/debugging (for custom setups)
  5. VS Code + PlatformIO — if you prefer a lightweight editor

4. Basic Steps to Create a Project (CubeMX / CubeIDE)

  1. Select Board/MCU: Choose a Nucleo board or enter MCU part number (e.g., STM32F103C8).
  2. Configure Pinout: Map on-chip peripherals to pins — e.g., PA5 as GPIO output for LED.
  3. Configure Clock: Set system clock (HSE/HSI/LSE) and adjust PLL settings.
  4. Middleware/Peripherals: Enable UART, I²C, SPI, ADC, etc., and set parameters (baud rate, mode, prescalers).
  5. Project Settings & Code Generation: Choose toolchain (STM32CubeIDE, Makefile, SW4STM32) and click "Generate Code".
  6. Build & Flash: Click build, then use ST-Link to flash & debug.

5. Simple Blink Example (HAL)

In a CubeMX-generated project, your main.c may contain code like this — toggling PA5 pin:

/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/* USER CODE END 2 */

while (1)
{
  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  HAL_Delay(500); // 500 ms
}

This uses HAL API: HAL_GPIO_TogglePin and HAL_Delay. CubeMX generates the GPIO initialization boilerplate for you.

6. HAL vs LL vs Bare-Metal

  • HAL: High-level, portable, easy to use — best for beginners.
  • LL (Low Layer): Lightweight, faster, hardware-close — good for optimization.
  • Bare-metal / Register: Full control, but error-prone and time-consuming.

7. VS Code / PlatformIO Workflow

Using PlatformIO in VS Code allows rapid project management. If generating code from CubeMX, make sure to check Makefile/CMake integration — PlatformIO supports multiple boards & frameworks.

8. Debugging Tips

  • Check ST-Link connection (drivers must be installed)
  • Verify reset/BOOT0 pin state if flashing fails
  • Use SWD pins (PA13/PA14 on many MCUs) for debugging
  • Read OpenOCD logs to understand error causes

9. Common Troubleshooting

  1. "No target device found" — check power, USB cable, drivers, board jumpers
  2. Wrong clock configuration — if MCU hangs, verify HSE/PLL settings
  3. Compilation errors after code generation — could be a toolchain/version mismatch

10. Useful Resources

  • ST official: STM32CubeIDE & STM32CubeMX documentation
  • STM32 step-by-step wiki & community forums
  • STM32 example projects & HAL/LL user manuals