💻

STM32 Programming Basics and Setup

Aug 2, 2024

STM32 Programming Tutorial Notes

Introduction

  • Tutorial for STM32 programming
  • Led by Greidi
  • Goals:
    • Control an LED
    • Handle an interrupt
    • Send data from STM32 to PC

Prerequisites

  • Install STM32 CubeIDE

Setting Up STM32 CubeIDE

  • Configure workspace without spaces in project names (use underscores)
  • Use Create New STM32 Project option in CubeIDE
  • Select STM32 Nucleo board (specifically F401RE)
  • Configure project settings (keep defaults)

Project Structure

  • Multiple projects can reside in a single workspace
  • Use MCU/MPU Selector for custom boards
  • STM32 provides HAL libraries for peripherals

Interface Overview

  • GUI shows the STM32 chip configuration, including:
    • Oscillators
    • UART interface
    • Debug lines
    • Configured peripherals
  • Recommended to download Nucleo schematic from STM32 website for reference

GPIO Configuration

  • LED connected to PA5
  • Switch connected to PC13 (configured as external interrupt)
  • Pull-up resistors are used for switches

Interrupts

  • Configure external interrupts in CubeIDE
  • Options include: rising edge, falling edge, dual edge
  • Falling edge trigger is recommended for a button press
  • Use debounce capacitors to prevent multiple triggers

Writing Code for LED Control

  • Write code in the main.c file under user code sections
  • Use HAL_Delay for timing
  • Example of blinking an LED:
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);  
    HAL_Delay(1000);  
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);  
    HAL_Delay(1000);  
    

UART Communication

  • Configure UART in CubeIDE (USART)
  • Use HAL_UART_Transmit to send data to PC
  • Example message:
    char *message = "Hello World!";  
    HAL_UART_Transmit(&huart2, (uint8_t*)message, strlen(message), timeout);  
    
  • Configuration: baud rate, word length, parity, etc.

Debugging

  • Use software debuggers like PuTTY for UART communication
  • Employ breakpoints in CubeIDE for debugging the code execution
  • Monitor the UART output for data sent to PC

Conclusion

  • Successfully configured and programmed STM32 to control an LED and send data to a PC
  • Thanks to Greidi for the tutorial and viewers for their support.