Getting Started with Embedded Rust on Teensy 4.0: A Surprising Journey from Arduino and MicroPython

· msj's blog


The Teensy 4.0 is a powerful microcontroller board that has gained popularity among hobbyists and professionals alike for its speed and versatility. It features an ARM Cortex-M7 processor running at 600 MHz, powered by the NXP iMXRT1062 chip, making it one of the fastest microcontrollers available today. While many users are familiar with programming the Teensy using the Arduino IDE or MicroPython, there’s a growing interest in using Rust for embedded systems development. Rust offers unique advantages, such as memory safety and concurrency, making it an appealing choice for embedded programming. In this blog post, we’ll explore how to get started with Embedded Rust on the Teensy 4.0 and highlight some surprising differences you might encounter when transitioning from Arduino and MicroPython.

Setting Up Your Environment #

The Embedded Rust Book #

Before diving into coding, it's essential to familiarize yourself with the concepts of embedded Rust development. A great resource for this is The Embedded Rust Book. This book covers everything from setting up your environment to writing your first embedded application, and it will guide you through the intricacies of working with Rust in an embedded context.

What is teensy4-rs and BSP? #

The teensy4-rs project is a collection of Rust libraries and tools designed specifically for the Teensy 4.0 microcontroller. It provides a hardware abstraction layer (HAL) that allows developers to interact with the microcontroller's peripherals in a safe and efficient manner.

A Board Support Package (BSP) is a set of software components that provide support for a specific hardware platform. In the case of teensy4-rs, the BSP includes drivers and abstractions for the Teensy 4.0's GPIO, timers, and other peripherals, making it easier to write embedded applications in Rust.

Creating a New Project with teensy4-rs-template #

To streamline the process of starting a new project, you can use the teensy4-rs-template. This template provides a pre-configured project structure that includes the necessary dependencies and setup for working with the Teensy 4.0.

To create a new project, run the following command:

1cargo generate --git https://github.com/imxrt-rs/teensy4-rs-template.git --name my_project

Follow the prompts to set up your project. This will create a new directory with the necessary files and structure for your Teensy 4.0 project.

Writing Your First Program #

Let’s write a simple program that blinks an LED on the Teensy 4.0. Open the src/main.rs file in your newly created project and replace its contents with the following code:

 1#![no_std]
 2#![no_main]
 3
 4use teensy4_bsp as bsp;
 5use teensy4_panic as _;
 6
 7use bsp::board;
 8use bsp::hal::timer::Blocking;
 9
10#[bsp::rt::entry]
11fn main() -> ! {
12    let board::Resources {
13        pit,
14        pins, mut gpio2, ..
15    } = board::t40(board::instances());
16    let led = board::led(&mut gpio2, pins.p13);
17
18    let mut delay = Blocking::<_, { board::PERCLK_FREQUENCY }>::from_pit(pit.0);
19    loop {
20        led.toggle();
21        delay.block_ms(1000);
22    }
23}

Building and Creating a HEX File #

To build your program, run:

1cargo build --release

This will compile your code and create a binary file in the target/thumbv7em-none-eabihf/release directory. To create a HEX file suitable for uploading to the Teensy 4.0, run:

1cargo objcopy --release -- -O ihex target/thumbv7em-none-eabihf/release/my_project.hex

Uploading Your Program #

To upload the program to your Teensy 4.0, you can use the teensy_loader_cli tool, which you can install via your package manager or download from the PJRC website. Once installed, you can upload your program with:

1teensy_loader_cli --mcu=TEENSY40 -w -v target/thumbv7em-none-eabihf/release/my_project.hex

Surprising Differences from Arduino and MicroPython #

Transitioning from Arduino and MicroPython to Rust can be a bit surprising. Here are some key differences you might encounter: