Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 13

Embedded Rust Project Setup for ARM Cortex-M

Recommended resources

Welcome back! In our previous lesson, we established the "why" of embedded Rust, exploring the no_std environment and understanding that it allows Rust to run directly on hardware without an operating system. Now, it's time to move from theory to practice and tackle the "how."

This lesson will guide you through setting up your first real embedded Rust project. Your goal is to master Embedded Rust, and that journey begins with a solid project foundation. We will use a community-standard project template specifically designed for ARM Cortex-M microcontrollers, the workhorse of the embedded world. By the end of this lesson, you will have a complete, buildable project structure, ready for the hardware interaction we'll cover next.

The Embedded Tooling Ecosystem

Before we create the project, we need to install a few essential tools. Unlike a standard application where cargo handles almost everything, embedded development requires a bit more setup to bridge the gap between your computer and the target microcontroller.

The Embedded Rust Book provides an excellent overview of the necessary tools.

The Embedded Rust Book

This section explains why we need several different tools for cross-compilation and interacting with remote hardware.

Please read the introduction and the first two subsections, cargo-generate and cargo-binutils. Don't worry about GDB or OpenOCD for now; we'll introduce them when we need them. Focus on understanding the role of cargo-generate for project templating and cargo-binutils for inspecting our compiled code.

For this lesson, we will focus on two key installations:

  1. A cross-compilation target: To tell Rust how to build code for an ARM processor instead of your computer's processor.
  2. cargo-generate: The tool we'll use to create a project from a template.

Step 1: Installing the Tools

Let's get your development environment ready. The instructions are straightforward commands you'll run in your terminal.

Add the ARM Cortex-M Target

First, we need to tell rustup to download the necessary components for cross-compiling to a common ARM architecture. We'll use the thumbv7em-none-eabihf target, which is suitable for many popular Cortex-M4F and M7F microcontrollers (the 'hf' stands for "hardware floating-point").

Run the following command in your terminal:

rustup target add thumbv7em-none-eabihf

This command downloads a version of the core library (and other built-in components) pre-compiled for this specific ARM architecture.

Install cargo-generate

Next, install cargo-generate. This is a Cargo subcommand that fetches a project template from a Git repository and sets it up for you.

Run this command:

cargo install cargo-generate

With these two commands, your system is now equipped to create a standard embedded Rust project.


Step 2: Generating Your First Embedded Project

Instead of creating a project from scratch and configuring everything manually, we will use the cortex-m-quickstart template. This is a huge time-saver and ensures your project is set up according to best practices.

The process is explained clearly in the following article.

Embedded Rust: The Cortex-M QuickStart Template

This resource introduces the cortex-m-quickstart template and shows you exactly how to use it.

First, read the section "Introduction to the Cortex-M QuickStart Template" to understand what the template provides. Then, follow the instructions in the next section, "Creating a Project...", to generate your own.

As described in the article, execute this command in your terminal:

cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart

You will be prompted for a project name. Let's call it blinky-app. After the command finishes, navigate into the new directory: cd blinky-app.

You now have a complete, structured embedded project!


Step 3: Exploring the Project Structure

The template has created several important files and configurations. Let's examine the most critical ones to understand what's happening under the hood. The Embedded Rust Book provides detailed explanations.

The Embedded Rust Book

This guide walks through the files and code generated by the template, explaining the purpose of each part.

In the "Getting Started" chapter, first read the subsection "Creating a non standard Rust program". This analyzes the src/main.rs file, explaining the no_std, no_main, and entry attributes. Next, read the subsection "Cross compiling". This will show you the .cargo/config file, which sets the default compilation target. Finally, in the next chapter, "Hardware," read the subsection "Configuring". This explains the purpose of the memory.x file and how it defines the memory layout for the microcontroller.

Let's summarize the key components you've just read about:

  • src/main.rs: This is your main application file. You'll notice it looks different from a standard Rust program:

    • #![no_std]: Declares that we are not linking the standard library.
    • #![no_main]: Declares that we are not using the standard main function entry point.
    • use cortex_m_rt::entry;: Imports the entry attribute macro from the cortex-m-rt crate.
    • #[entry]: This attribute marks the main function as the program's entry point for the cortex-m-rt runtime.
    • fn main() -> !: The function signature indicates that main is a divergent functionβ€”it never returns. In an embedded system, your main program loop should run forever.
  • Cargo.toml: Here you'll find the dependencies:

    • cortex-m: Provides low-level access to ARM Cortex-M processor features.
    • cortex-m-rt: Provides a runtime environment for Cortex-M devices, handling startup and exception vectors.
    • panic-halt: A simple crate that provides a panic handler that just puts the processor into an infinite loop if the program panics. This is essential in a no_std environment.
  • .cargo/config.toml: This file contains a crucial piece of configuration:

    [build]
    # Pick ONE of these compilation targets
    # ...
    target = "thumbv7m-none-eabi" 
    
The template defaults to a Cortex-M3 target. Let's update it to the target we installed. Open `.cargo/config.toml`, comment out the `thumbv7m-none-eabi` line, and uncomment the `thumbv7em-none-eabihf` line:
```toml
[build]
# Pick ONE of these compilation targets
# target = "thumbv6m-none-eabi"      # Cortex-M0 and Cortex-M0+
# target = "thumbv7m-none-eabi"      # Cortex-M3
# target = "thumbv7em-none-eabi"     # Cortex-M4 and Cortex-M7 (no FPU)
target = "thumbv7em-none-eabihf"   # Cortex-M4F and Cortex-M7F (with FPU)
```
This tells Cargo to always use this target by default, so you don't have to type `--target thumbv7em-none-eabihf` for every command.
  • memory.x: This is a linker script. It defines the memory map of your target microcontrollerβ€”how much Flash and RAM it has, and where they are located in memory. The template provides default values, which you would change to match your specific hardware.
    MEMORY
    {
      /* NOTE 1 K = 1 KiBi = 1024 bytes */
      FLASH : ORIGIN = 0x08000000, LENGTH = 256K
      RAM   : ORIGIN = 0x20000000, LENGTH = 40K
    }
    

Step 4: Building the Project

With the project generated and configured, the final step is to compile it. Thanks to the config.toml file, this is as simple as running the standard build command.

In your project directory (blinky-app), run:

cargo build

Cargo will compile all the crates and produce an executable file. You will find the output in target/thumbv7em-none-eabihf/debug/blinky-app. This is not a program you can run on your computer; it's an ELF file specifically compiled for an ARM Cortex-M processor.


The Big Picture: Abstraction Layers

You've successfully created and built an embedded project! The structure provided by the template handles the low-level "scaffolding" required to get Rust code running on bare metal. This scaffolding fits into a larger ecosystem of crates that work together.

This diagram shows the typical layers of abstraction in an embedded Rust project. Your application code sits at the top, relying on layers of crates that abstract away the hardware details.

This diagram shows the typical layers of abstraction in an embedded Rust project. Your application code sits at the top, relying on layers of crates that abstract away the hardware details.

The project we've just set up provides the very bottom layers:

  • The cortex-m crate provides access to the CPU core itself.
  • The cortex-m-rt crate acts as the Architecture Support Crate, handling the runtime startup.

Our current main function doesn't do anything. To control hardware like LEDs, timers, or communication interfaces (the Peripherals), we need to use a Hardware Abstraction Layer (HAL). The HAL is the next layer up in the diagram, and it will be the focus of our next lesson.


Conclusion

In this lesson, you have moved from theory to practice, building a bridge from the no_std concept to a tangible project. You have accomplished the essential first step in any embedded development workflow.

Here are the key takeaways:

  • Embedded Rust development requires specific tools like cargo-generate and ARM cross-compilation targets for rustup.
  • Project templates like cortex-m-quickstart are the standard way to begin, as they provide a pre-configured, best-practice structure.
  • An embedded project is configured with key files: .cargo/config.toml sets the default target, and memory.x defines the hardware's memory layout.
  • The cortex-m-rt crate is fundamental for handling the program's startup and entry point on a bare-metal system.

You now have a complete, buildable foundation. In the next lesson, we will build upon this by introducing a Hardware Abstraction Layer (HAL) crate, which will finally allow us to interact with the outside world by controlling a GPIO pin to blink an LED.