Skip to content

A minimal embedded Rust application for the STM32F769 microcontroller demonstrating basic peripheral access and RTT (Real-Time Transfer) logging.

Notifications You must be signed in to change notification settings

abitofhelp/stm32_helloworld_rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

STM32F769 Hello World in Rust

A minimal embedded Rust application for the STM32F769 microcontroller demonstrating basic peripheral access and RTT (Real-Time Transfer) logging.

Features

  • Reads and displays device ID and revision ID from the DBGMCU peripheral
  • 5-second countdown demonstrating SysTick timer with 1-second intervals
  • Proper error handling without unwrap()
  • Power-efficient idle state using wfi() (Wait For Interrupt)
  • Uses defmt for efficient logging over RTT
  • Demonstrates proper embedded Rust project structure and best practices

Hardware Requirements

  • Microcontroller: STM32F769NIHx (or compatible STM32F7 series)
  • Debug Probe: ST-Link V2/V3 or compatible
  • Connection: USB cable for debug probe

Software Requirements

  • Rust toolchain (stable)
  • thumbv7em-none-eabi target
  • probe-rs tools for flashing and debugging

Installation

Install Rust and required tools:

# Install Rust (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Add ARM Cortex-M target
rustup target add thumbv7em-none-eabi

# Install probe-rs
cargo install probe-rs-tools --locked

Project Structure

stm32_helloworld_rs/
├── .cargo/
│   └── config.toml          # Cargo configuration with runner settings
├── .vscode/
│   └── launch.json          # VS Code debugger configuration
├── src/
│   └── main.rs              # Main application code
├── memory.x                 # Linker memory layout
├── Cargo.toml              # Project dependencies
└── README.md               # This file

Building

Build the project:

cargo build

For release builds with optimizations:

cargo build --release

Running

From Command Line

Flash and run with RTT output:

cargo run

This will:

  1. Build the project
  2. Flash it to the STM32F769
  3. Start RTT logging in the terminal

Press Ctrl+C to stop.

From VS Code

  1. Open the project in VS Code
  2. Open the "Run and Debug" panel (Ctrl+Shift+D / Cmd+Shift+D)
  3. Select "probe-rs STM32F769" from the dropdown
  4. Click the green play button or press F5

RTT output will appear in the Debug Console tab.

Expected Output

Hello, World!
device_id: 1105
revisions_id: 4097
5
4
3
2
1
Countdown complete! Program finished successfully.
  • device_id: 1105 corresponds to 0x0451 (STM32F769)
  • revisions_id: 4097 corresponds to 0x1001 (chip revision)
  • The countdown demonstrates 1-second timer intervals
  • After completion, the program enters a low-power idle state

Troubleshooting

Probe Not Found

macOS: If you encounter "Probe not found" errors:

  • Close any other applications using the ST-Link (STM32CubeProgrammer, etc.)
  • The PROBE_RS_USB_BACKEND=rusb environment variable is already configured in .cargo/config.toml

Linux: You may need to configure udev rules:

# Create udev rules for ST-Link
sudo tee /etc/udev/rules.d/70-st-link.rules > /dev/null <<'EOF'
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", MODE:="0666"
EOF

# Reload udev rules
sudo udevadm control --reload-rules
sudo udevadm trigger

No RTT Output in VS Code

Ensure that:

  1. The Debug Console tab is selected (not Terminal)
  2. RTT is enabled in .vscode/launch.json (already configured)
  3. The program is running (not paused at a breakpoint)

Build Fails

If you see linker errors:

  • Verify thumbv7em-none-eabi target is installed: rustup target list --installed
  • Clean and rebuild: cargo clean && cargo build

Understanding the Code

The application demonstrates fundamental embedded Rust concepts:

  1. #![no_std] and #![no_main]: Required attributes for bare-metal embedded applications
  2. Proper Error Handling: Uses match instead of unwrap() for safer peripheral initialization
  3. Peripheral Access: Uses the stm32f7xx-hal HAL crate for hardware access
  4. SysTick Timer: Configures the ARM Cortex-M SysTick timer for precise 1-second delays
  5. RTT Logging: Uses defmt and defmt-rtt for efficient debug output
  6. Power Management: Enters low-power idle using wfi() after completing the countdown
  7. Register Efficiency: Demonstrates reading hardware registers once and extracting multiple fields

Key Dependencies

  • cortex-m: Low-level access to Cortex-M peripherals
  • cortex-m-rt: Runtime support and startup code
  • stm32f7xx-hal: Hardware Abstraction Layer for STM32F7
  • defmt: Efficient logging framework for embedded
  • defmt-rtt: RTT transport for defmt
  • panic-probe: Panic handler that reports panics over defmt

License

Copyright (c) 2025 Michael Gardner, A Bit of Help, Inc.

This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.

Resources

Contributing

Contributions are welcome! This project serves as a starting template for STM32F7 embedded Rust development.

About

A minimal embedded Rust application for the STM32F769 microcontroller demonstrating basic peripheral access and RTT (Real-Time Transfer) logging.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published