embedded rust on iot devices

52
Embedded Rust On IoT Devices Lars Gregori Hybris Labs

Upload: lars-gregori

Post on 16-Apr-2017

158 views

Category:

Technology


5 download

TRANSCRIPT

Embedded Rust On IoT DevicesLars GregoriHybris Labs

Hybris Labs Prototypes

https://labs.hybris.com/prototype/

Motivation

Functional IoT (http://fpiot.metasepi.org/):

Imagine IoT devices are:

• connected to the internet

• developed in a short time

• storing personal data

• secure

• more intelligence

• inexpensive

Can C design IoT devices like that? No, can't.

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Rust Overview

rust-lang.org

1.9.0

rust-lang.org

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

Rust OverviewRust FAQ

Rust FAQ

• safe, concurrent, practical system language

• “We do not intend to be 100% static, 100% safe, 100% reflective, or too dogmatic in any other sense.”

• Mozilla Servo

Rust FAQ

• multi-paradigm

• OO

• no garbage collection

• strong influences from the world of functional programming

• built on LLVM

Rust FAQ

• Cross-Platform

• Android, iOS, …

• Conditional compilation #[cfg(target_os="macos")]

Rust OverviewControl & Safety

Control & Safety

C/C++

Safety

Control

Haskell

Go

Java

Python

Control & Safety

C/C++

Safety

Control

Haskell

Go

Java

Rust

Python

Rust OverviewRust Concepts

Rust Concepts

• ownership, the key concept

• borrowing, and their associated feature ‘references’

• lifetimes, an advanced concept of borrowing

‘fighting with the borrow checker’

https://doc.rust-lang.org/book/

Rust Concepts: Ownership

let v = vec![1, 2, 3];

let v2 = v;

// …

println!("v[0] is: {}", v[0]);

Rust Concepts: Ownership

let v = vec![1, 2, 3];

let v2 = v;

// …

println!("v[0] is: {}", v[0]); ERRORuse of moved value: `v`

Rust Concepts: Ownership

let v = vec![1, 2, 3];

let v2 = v;

take_something(v2);

println!("v[0] is: {}", v[0]); ERRORuse of moved value: `v`

Rust Concepts: Ownership

let v = 1;

let v2 = v;

println!("v is: {}", v);

Rust Concepts: Ownership

let v = 1;

let v2 = v;

println!("v is: {}", v); OK

Copy Type

Rust Concepts: Ownership

fn foo(v1: Vec<i32>, v2: Vec<i32>) -> (Vec<i32>, Vec<i32>, i32) {

// do stuff with v1 and v2

(v1, v2, 42) // hand back ownership, and the result of our function

}

let v1 = vec![1, 2, 3];

let v2 = vec![1, 2, 3];

let (v1, v2, answer) = foo(v1, v2);

Rust Concepts: References and Borrowing

fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {

// do stuff with v1 and v2

42 // return the answer

}

let v1 = vec![1, 2, 3];

let v2 = vec![1, 2, 3];

let answer = foo(&v1, &v2);

// we can use v1 and v2 here!

Rust Concepts: References and Borrowing

fn foo(v: &Vec<i32>) {

v.push(5);

}

let v = vec![];

foo(&v);

Rust Concepts: References and Borrowing

fn foo(v: &Vec<i32>) {

v.push(5);

}

let v = vec![];

foo(&v);

ERRORcannot borrow immutable borrowed content `*v` as mutable

Rust Concepts: References and Borrowing

The Rules for borrowing:

1. Any borrow must last for a scope no greater than that of the owner.

2. Only one of these kinds of borrows, but not both at the same time:

• one or more references (&T) to a resource

• exactly one mutable reference (&mut T)

Rust Concepts: References and Borrowing

let mut x = 5;

let y = &mut x;

*y += 1;

println!("{}", x); Error

RUN

cannot borrow `x` as immutable because it is also borrowed as mutable

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Embedded Rustzinc.rs

zinc.rs

Zinc is an experimental attempt to write an ARM stack that would be similar to CMSIS or mbed in capabilities but would show rust’s best safety features applied to embedded development.

Zinc is mostly assembly-free and completely C-free at the moment.

STM32 Nucleo-F411RE

STM32 Nucleo-F411RE

▶︎Embedded Rust

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Cross-Compiling

Rustup

https://www.rustup.rs/

rustup is an installer for the systems programming language Rust

> rustup install nightly-2016-09-17

> rustup override set nightly-2016-09-17

GNU ARM Embedded Toolchain

https://launchpad.net/gcc-arm-embedded/+download

arm-none-eabi-gcc

arm-none-eabi-gdb

eabi = Embedded Application Binary Interface (>)

Building

cargobuild--target=thumbv7em-none-eabi--featuresmcu_stm32f4

▶︎Cross-Compiling

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

MCU DebuggingDebugging Rust

zinc.rs Issue #336

Failing to build example. #336

src/hal/layout_common.ld

comment out *(.debug_gdb_scripts)

Debugging Rust

>openocd…-cgdb_port3333

>arm-none-eabi-gdb

(gdb)targetremote:3333

(gdb)file./target/thumbv7em-none-eabi/debug/blink_stm32f4

(gdb)breakmain

(gdb)continue

(gdb)next

Debugging Rust

(gdb) set delay = 1000

(gdb) list

(gdb) disassemble

▶︎MCU Debugging

What we’ve seen

• Rust Overview ✔

• Embedded Rust ✔

• Cross Compiling ✔

• MCU Debugging ✔

• Hello World

Thank you!

@choas

labs.hybris.com